Nils Mackay
Nils Mackay

Reputation: 429

How to input single variable at the last layer in keras?

I am trying to include a variable in the last step of a feed-forward NN in Keras. I seem to only be able to get it working when I include 2 columns, instead of only one. Here's my code example:

First I prepare my main input datasets:

import pandas as pd
from keras.models import Model
from keras.layers import Dense, Input, Concatenate
from keras.optimizers import Adam

iris = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
iris.loc[:, 'target'] = (iris.species == "setosa").map(int)
train_x = iris.drop(columns=['target', 'species'])
train_y = iris['target'].map(int)

Then I split off train_x into two separate dataframes, and enter them into the network at different locations:

feature_x = train_x.drop(columns='petal_width')
single_feature_x = train_x[['petal_width']]

input_x = Input(shape=feature_x.shape, name='feature_input')
single_input_x = Input(shape=single_feature_x.shape, name='single_input')

x = Dense(4, activation='relu')(input_x)

concat_feat = Concatenate(axis=-1, name='concat_fc')([x, single_input_x])

outputs = Dense(1, activation='sigmoid')(concat_feat)

model = Model(inputs=[input_x, single_input_x], outputs=outputs)
model.compile(loss='binary_crossentropy',
              optimizer=Adam(lr=0.001))

model.fit({'feature_input': feature_x,
           'single_input': single_feature_x},
          train_y,
          epochs=100,
          batch_size=512,
          verbose=1)

This throws the error:

ValueError: Shape must be rank 2 but is rank 3 for '{{node model_5/concat_fc/concat}} = ConcatV2[N=2, T=DT_FLOAT, Tidx=DT_INT32](model_5/dense_10/Relu, model_5/Cast_1, model_5/concat_fc/concat/axis)' with input shapes: [?,4], [?,1,1], [].

However, if I add this one line it runs just fine:

feature_x = train_x.drop(columns='petal_width')
single_feature_x = train_x[['petal_width']]
# Add a constant column so the shape becomes (?,2)
single_feature_x.loc[:, 'constant'] = 0

Why does this work with two columns but not with one?

Upvotes: 1

Views: 343

Answers (1)

Marco Cerliani
Marco Cerliani

Reputation: 22031

you simply have to specify correctly the input shape. In the case of 2D data, you need to pass only the feature dim. The sample dimension is not required. You simply need to correct the input into:

input_x = Input(shape=feature_x.shape[1], name='feature_input')
single_input_x = Input(shape=single_feature_x.shape[1], name='single_input')

here the running notebook

Upvotes: 1

Related Questions