Woden
Woden

Reputation: 1386

How to use Grid Search when there are multiple inputs, including a matrix?

I am trying to tune the neuron model, but since the inputs are multiple and in different shapes, it seems impossible to stack it together.

There are two types of inputs when I make the model.

one of them is:

cats = [train.time_signature,train.key,train.mode_cat,train.loudness_cat,train.tempo_cat,train.duration_cat]

The shape of them are all (7678,)

another input is:

num_train

The shape of it is (7678,30), which is a matrix

When fitting the Keras model, it's okay to just concatenate them together as a training set:

final_model.fit([cats,num_train],train.genre_cat,batch_size=50,epochs=1000,verbose=1,validation_split=.1)

However, when I use the GridSearch, it doesn't allow me to the same input as I did in the model.

grid_result = grid.fit([cats,num_train],train.genre_cat)

It shows error :ValueError: Found input variables with inconsistent numbers of samples: [2, 7678], which means the num_train is not allowed because I got the other 30 samples on index 1.

Is there anything that I can deal with this problem? Thanks.

Reference:

  1. Grid Search for Keras with multiple inputs
  2. https://github.com/keras-team/keras/issues/2748

Upvotes: 1

Views: 399

Answers (1)

Marco Cerliani
Marco Cerliani

Reputation: 22031

I think that the trick is always the same: make one single input which is the concatenation of the multiple inputs.

In this particular case, we have N inputs of dim (n_sample,1) and one input of dim (n_sample,30). The new concatenated input will have dimensions (n_sample, n_stacked_columns).

we make the separation of the columns inside the model

n_sample = 100

x1 = np.random.uniform(0,1, (n_sample,1)) # (n_sample,1)
x2 = np.random.uniform(0,1, (n_sample,1)) # (n_sample,1)
x3 = np.random.uniform(0,1, (n_sample,1)) # (n_sample,1)
x = np.random.uniform(0,1, (n_sample,30)) # (n_sample,30)
X = np.column_stack([x1,x2,x3,x])  # (n_sample,1+1+1+30)
y = np.random.uniform(0,1, n_sample)


inp = Input((X.shape[-1],))

inp1 = Lambda(lambda x: tf.expand_dims(x[:,0],-1))(inp) # (None,1)
inp2 = Lambda(lambda x: tf.expand_dims(x[:,1],-1))(inp) # (None,1)
inp3 = Lambda(lambda x: tf.expand_dims(x[:,2],-1))(inp) # (None,1)
inp_matrix = Lambda(lambda x: x[:,3:])(inp) # (None,30)

d1 = Dense(8)(inp1)
d2 = Dense(8)(inp2)
d3 = Dense(8)(inp3)
d_matrix = Dense(8)(inp_matrix)

concat = Concatenate()([d1,d2,d3,d_matrix])
out = Dense(1)(concat)

model = Model(inp, out)
model.compile('adam','mse')

model.fit(X,y, epochs=3)

Upvotes: 1

Related Questions