2

I want to train a binary classifier using Keras and my training data is of shape (2000,2,128) and labels of shape (2000,) as Numpy arrays.

The idea is to train such that embeddings together in a single array means they are either same or different, labelled using 0 or 1 respectively.

The training data looks like: [[[0 1 2 ....128][129.....256]][[1 2 3 ...128][9 9 3 5...]].....] and the labels looks like [1 1 0 0 1 1 0 0..].

Here is the code:

import keras
from keras.layers import Input, Dense

from keras.models import Model

frst_input = Input(shape=(128,), name='frst_input')
scnd_input = Input(shape=(128,),name='scnd_input')
x = keras.layers.concatenate([frst_input, scnd_input])
x = Dense(128, activation='relu')(x)
x=(Dense(1, activation='softmax'))(x)
model=Model(inputs=[frst_input, scnd_input], outputs=[x])
model.compile(optimizer='rmsprop', loss='binary_crossentropy',
              loss_weights=[ 0.2],metrics=['accuracy'])

I am getting the following error while running this code:

ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[[ 0.07124118, -0.02316936, -0.12737238, ...,  0.15822273,
      0.00129827, -0.02457245],
    [ 0.15869428, -0.0570458 , -0.10459555, ...,  0.0968155 ,
      0.0183982 , -0.077924...

How can I resolve this issue? Is my code correct to train the classifier using two inputs to classify?

2
  • 1
    Pass a list of two arrays corresponding to two input layers when calling fit method. Commented Oct 17, 2018 at 15:30
  • 2
    Don't use softmax as the activation of last layer. Otherwise, since it has one unit, it would always output 1. Instead use sigmoid. Commented Oct 17, 2018 at 15:33

1 Answer 1

2

Well, you have two options here:

1) Reshape the training data to (2000, 128*2) and define only one input layer:

X_train = X_train.reshape(-1, 128*2)

inp = Input(shape=(128*2,))
x = Dense(128, activation='relu')(inp)
x = Dense(1, activation='sigmoid'))(x)
model=Model(inputs=[inp], outputs=[x])

2) Define two input layers, as you have already done, and pass a list of two input arrays when calling fit method:

# assuming X_train have a shape of `(2000, 2, 128)` as you suggested
model.fit([X_train[:,0], X_train[:,1]], y_train, ...)

Further, since you are doing binary classification here, you need to use sigmoid as the activation of last layer (i.e. using softmax in this case would always outputs 1, since softmax normalizes the outputs such that their sum equals to one).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.