Tanmay Bhatnagar
Tanmay Bhatnagar

Reputation: 2470

Error in last keras layer of neural network

Model :

model = Sequential()
act = 'relu'
model.add(Dense(430, input_shape=(3,)))
model.add(Activation(act))

model.add(Dense(256))
model.add(Activation(act))
model.add(Dropout(0.4))

model.add(Dense(148))
model.add(Activation(act))
model.add(Dropout(0.3))

model.add(Dense(1))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
#model.summary()

Error : Error when checking target: expected activation_4 to have shape (None, 1) but got array with shape (1715, 2)

The error is in the last layer of the neural network. The neural network is trying to classify whether 2 drugs are synergic or not. COMPLETE SOURCE CODE : https://github.com/tanmay-edgelord/Drug-Synergy-Models/blob/master/Drug%20Synergy%20NN%20Classifier.ipynb

Data: https://github.com/tanmay-edgelord/Drug-Synergy-Models/blob/master/train.csv

Upvotes: 2

Views: 54

Answers (1)

Miriam Farber
Miriam Farber

Reputation: 19634

In your code you have the following lines:

y_binary = to_categorical(Y[:])
y_train = y_binary[:split]
y_test = y_binary[split:]

to_categorical transforms the vector into a 1-hot vector. So since you have two classes, it transforms every number to a vector of length 2 (0 is transformed to [1,0] and 1 is transformed to [0,1]). So your last layer needs to be defined as follows:

model.add(Dense(2))
model.add(Activation('softmax'))

(note that I replaced the 1 with 2).

Upvotes: 2

Related Questions