unable to train siamese with validation_data '<' not supported between instances of 'float' and 'list'

159 Views Asked by At

When i use keras model.fit such that i don't use "validation_data" and only x_train and y_train i don't get any error even if i use "validation_split" things work fine. Below is working piece of code

def siamese(x_train,y_train):

    W_init = tf.keras.initializers.he_normal(seed=100)
    b_init = tf.keras.initializers.he_normal(seed=50)

    input_shape = (24,939)
    left_input = Input(input_shape)
    right_input = Input(input_shape)

    encoder = Sequential()
    encoder.add(Conv1D(filters=6,kernel_size=4, padding='same', activation='relu',input_shape=input_shape,kernel_initializer=W_init, bias_initializer=b_init))
    encoder.add(BatchNormalization())
    encoder.add(Dropout(.1))
    encoder.add(MaxPool1D())
    encoder.add(Conv1D(filters=4,kernel_size=3, padding='same', activation='relu'))
    encoder.add(BatchNormalization())
    encoder.add(Dropout(.1))
    encoder.add(MaxPool1D())
    encoder.add(Conv1D(filters=3,kernel_size=2, padding='same', activation='relu'))
    encoder.add(BatchNormalization())
    encoder.add(Dropout(.1))
    encoder.add(MaxPool1D())
    encoder.add(Flatten())
    encoder.add(Dense(64,activation='relu'))
    encoder.add(Dropout(.3))

    encoded_l = encoder(left_input)
    encoded_r = encoder(right_input)
    distance = Lambda(euclidean_distance, output_shape=eucl_dist_output_shape)([encoded_l, encoded_r])
    adam = optimizers.Adam(lr=.001)
    earlyStopping = EarlyStopping(monitor='loss',min_delta=0,patience=3,verbose=1,restore_best_weights=False)
    callback_early_stop_reduceLROnPlateau=[earlyStopping]
    model = Model([left_input, right_input], distance)
    model.compile(loss=contrastive_loss, optimizer=adam,metrics=[accuracy])
    model.summary()
    history = model.fit([(x_train[:,:,:,0]).astype(np.float32),(x_train[:,:,:,1]).astype(np.float32)],y_train,validation_split = .15,batch_size=64,epochs=4,callbacks=callback_early_stop_reduceLROnPlateau)
    return model,history
model1,history1=siamese(xtrain_np_img1_img2,y_train_numpy)

Output::::

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_5 (InputLayer)            (None, 24, 939)      0                                            
__________________________________________________________________________________________________
input_6 (InputLayer)            (None, 24, 939)      0                                            
__________________________________________________________________________________________________
sequential_3 (Sequential)       (None, 64)           23337       input_5[0][0]                    
                                                                 input_6[0][0]                    
__________________________________________________________________________________________________
lambda_3 (Lambda)               (None, 1)            0           sequential_3[1][0]               
                                                                 sequential_3[2][0]               
==================================================================================================
Total params: 23,337
Trainable params: 23,311
Non-trainable params: 26
__________________________________________________________________________________________________
Train on 12653 samples, validate on 2233 samples
Epoch 1/4
12653/12653 [==============================] - 8s 668us/step - loss: 5.2016 - accuracy: 0.4152 - val_loss: 0.1739 - val_accuracy: 0.7323
Epoch 2/4
12653/12653 [==============================] - 7s 533us/step - loss: nan - accuracy: 0.4359 - val_loss: nan - val_accuracy: 1.0000
Epoch 3/4
12653/12653 [==============================] - 7s 539us/step - loss: nan - accuracy: 0.4117 - val_loss: nan - val_accuracy: 1.0000
Epoch 4/4
12653/12653 [==============================] - 7s 532us/step - loss: nan - accuracy: 0.4117 - val_loss: nan - val_accuracy: 1.0000
Epoch 00004: early stopping

Now i wanted to introduce "validation_data" and not use "validation_split"

So i tried first

def siamese(x_train,y_train,x_val,y_val):

    W_init = tf.keras.initializers.he_normal(seed=100)
    b_init = tf.keras.initializers.he_normal(seed=50)

    input_shape = (24,939)
    left_input = Input(input_shape)
    right_input = Input(input_shape)

    encoder = Sequential()
    encoder.add(Conv1D(filters=6,kernel_size=4, padding='same', activation='relu',input_shape=input_shape,kernel_initializer=W_init, bias_initializer=b_init))
    encoder.add(BatchNormalization())
    encoder.add(Dropout(.1))
    encoder.add(MaxPool1D())
    encoder.add(Conv1D(filters=4,kernel_size=3, padding='same', activation='relu'))
    encoder.add(BatchNormalization())
    encoder.add(Dropout(.1))
    encoder.add(MaxPool1D())
    encoder.add(Conv1D(filters=3,kernel_size=2, padding='same', activation='relu'))
    encoder.add(BatchNormalization())
    encoder.add(Dropout(.1))
    encoder.add(MaxPool1D())
    encoder.add(Flatten())
    encoder.add(Dense(64,activation='relu'))
    encoder.add(Dropout(.3))

    encoded_l = encoder(left_input)
    encoded_r = encoder(right_input)
    distance = Lambda(euclidean_distance, output_shape=eucl_dist_output_shape)([encoded_l, encoded_r])
    adam = optimizers.Adam(lr=.001)
    earlyStopping = EarlyStopping(monitor='loss',min_delta=0,patience=3,verbose=1,restore_best_weights=False)
    callback_early_stop_reduceLROnPlateau=[earlyStopping]
    model = Model([left_input, right_input], distance)
    model.compile(loss=contrastive_loss, optimizer=adam,metrics=[accuracy])
    model.summary()
    history = model.fit([(x_train[:,:,:,0]).astype(np.float32),(x_train[:,:,:,1]).astype(np.float32)],y_train,tuple([(x_val[:,:,:,0]).astype(np.float32),(x_val[:,:,:,1]).astype(np.float32)]),y_val,batch_size=128,epochs=4,callbacks=callback_early_stop_reduceLROnPlateau)
    return model,history
model1,history1=siamese(xtrain_np_img1_img2,y_train_numpy,xtest_np_img1_img2,y_test_numpy)

The error i got is TypeError: fit() got multiple values for argument 'batch_size'

So i tried another way since i was not able to troubleshoot above issue as

def siamese(x_train,y_train,x_val,y_val,batch_size,epochs,callbacks):

    W_init = tf.keras.initializers.he_normal(seed=100)
    b_init = tf.keras.initializers.he_normal(seed=50)

    input_shape = (24,939)
    left_input = Input(input_shape)
    right_input = Input(input_shape)

    encoder = Sequential()
    encoder.add(Conv1D(filters=6,kernel_size=4, padding='same', activation='relu',input_shape=input_shape,kernel_initializer=W_init, bias_initializer=b_init))
    encoder.add(BatchNormalization())
    encoder.add(Dropout(.1))
    encoder.add(MaxPool1D())
    encoder.add(Conv1D(filters=4,kernel_size=3, padding='same', activation='relu'))
    encoder.add(BatchNormalization())
    encoder.add(Dropout(.1))
    encoder.add(MaxPool1D())
    encoder.add(Conv1D(filters=3,kernel_size=2, padding='same', activation='relu'))
    encoder.add(BatchNormalization())
    encoder.add(Dropout(.1))
    encoder.add(MaxPool1D())
    encoder.add(Flatten())
    encoder.add(Dense(64,activation='relu'))
    encoder.add(Dropout(.3))

    encoded_l = encoder(left_input)
    encoded_r = encoder(right_input)
    distance = Lambda(euclidean_distance, output_shape=eucl_dist_output_shape)([encoded_l, encoded_r])
    adam = optimizers.Adam(lr=.001)
    earlyStopping = EarlyStopping(monitor='loss',min_delta=0,patience=3,verbose=1,restore_best_weights=False)
    callback_early_stop_reduceLROnPlateau=[earlyStopping]
    model = Model([left_input, right_input], distance)
    model.compile(loss=contrastive_loss, optimizer=adam,metrics=[accuracy])
    model.summary()
    history = model.fit([(x_train[:,:,:,0]).astype(np.float32),(x_train[:,:,:,1]).astype(np.float32)],y_train,tuple([(x_val[:,:,:,0]).astype(np.float32),(x_val[:,:,:,1]).astype(np.float32)]),y_val,batch_size,epochs,callbacks)
    return model,history
model1,history1=siamese(xtrain_np_img1_img2,y_train_numpy,xtest_np_img1_img2,y_test_numpy,64,4,callback_early_stop_reduceLROnPlateau)

Now this time error is

TypeError                                 Traceback (most recent call last)
<ipython-input-17-fd746aea477d> in <module>
----> 1 model1,history1=siamese(xtrain_np_img1_img2,y_train_numpy,xtest_np_img1_img2,y_test_numpy,64,4,callback_early_stop_reduceLROnPlateau)

<ipython-input-15-cebaa8a123ad> in siamese(x_train, y_train, x_val, y_val, batch_size, epochs, callbacks)
     36     model.summary()
---> 38     history = model.fit([(x_train[:,:,:,0]).astype(np.float32),(x_train[:,:,:,1]).astype(np.float32)],y_train,tuple([(x_val[:,:,:,0]).astype(np.float32),(x_val[:,:,:,1]).astype(np.float32)]),y_val,batch_size,epochs,callbacks)
     39     return model,history

~\AppData\Roaming\Python\Python37\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
   1179                 val_inputs = val_x + val_y + val_sample_weights
   1180 
-> 1181         elif validation_split and 0. < validation_split < 1.:
   1182             if any(K.is_tensor(t) for t in x):
   1183                 raise ValueError(

TypeError: '<' not supported between instances of 'float' and 'list'

I am pretty sure i am making some trivial mistake as i am learning machine learning.

The reason why i am trying this because i want to use a tool named "talos" and since i am working with siamese network which takes multiple input and for talos to work properly i can't use validation_split but validation_data https://autonomio.github.io/talos/#/Examples_Multiple_Inputs

The reason why i want to use talos is for query for another thread because my model is not performing well so i thought may be i should first try hyperparameter tuning.

0

There are 0 best solutions below