如何在使用keras EarlyStopping时阻止TypeError:不支持的操作数类型 - :'NoneType'和'int'?

时间:2017-02-26 18:27:51

标签: python keras

这是我的代码:

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)])

我收到以下错误:

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

当我不使用EarlyStopping时,它不会产生错误。

任何人都有修复?

1 个答案:

答案 0 :(得分:3)

如果您考虑一下:您要求在培训期间不使用验证来监控验证损失。

使用

...

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false);
    ButterKnife.bind(this, view);
    return view;
}  

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
...

例如,如果您想进行验证。它将使用20%的数据作为验证集。您不会接受这些样本的培训,只需在每个时代结束时验证您的模型。

正如您在关于此代码的其他问题中所提到的:将上次激活更改为softmax以与model.fit(X_train, Y_train, nb_epoch=20, batch_size=20, validation_split=0.2, callbacks=[EarlyStopping(monitor='val_loss', patience=4)]) 一起使用。或根据您的需要将目标切换到categorical_crossentropy