Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent the TypeError: unsupported operand type(s) for -: 'NoneType' and 'int' when using keras EarlyStopping?

Tags:

python

keras

Here is my code:

from keras.callbacks import EarlyStopping

model = Sequential()
model.add(Dense(50, input_dim=33, init='uniform', activation='relu'))
for u in range(3): #how to efficiently add more layers
    model.add(Dense(33, init='uniform', activation='relu'))
model.add(Dense(122, init='uniform', activation='sigmoid'))

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

model.fit(X_train, Y_train, nb_epoch=20, batch_size=20, callbacks=[EarlyStopping(monitor='val_loss', patience=4)])

and I'm getting the following error:

TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

It doesn't produce that error when I don't use EarlyStopping.

Anyone have a fix?

like image 824
Ravaal Avatar asked Feb 26 '17 18:02

Ravaal


1 Answers

If you think about it : you ask to monitor a validation loss without using a validation during the training.

Use

model.fit(X_train, Y_train, nb_epoch=20, batch_size=20, validation_split=0.2, callbacks=[EarlyStopping(monitor='val_loss', patience=4)])

For example if you want to have a validation. It will use 20% of your data as validation set. You won't be training on those samples, just validating your model at the end of each epoch.

And as mentionned in your other question about this code : change the last activation to a softmax to use with categorical_crossentropy. Or switch the objective to binary_crossentropy depending on your needs.

like image 100
Nassim Ben Avatar answered Nov 27 '22 12:11

Nassim Ben