尝试训练神经网络时出错

时间:2021-03-10 03:51:12

标签: python tensorflow machine-learning keras neural-network

我正在训练一个带有 keras 的神经网络,其中包含 193 个分类和回归的数字输入和输出。

这是我的代码:

from keras.models import Sequential, Input, Model
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.callbacks import EarlyStopping

inputs = Input(shape=(X_train.shape[1],))
layer1 = Dense(193, activation='relu')(inputs)
dropout1 = Dropout(0.1)(layer1)
layer2 = Dense(128, activation='relu')(dropout1)
dropout2 = Dropout(0.25)(layer2)
layer3 = Dense(128, activation='relu')(dropout2)
dropout3 = Dropout(0.5)(layer3)
classifier = Dense(11, activation='softmax')(dropout3)
regression = Dense(1, activation='linear')(layer3)
model = Model(inputs=inputs, outputs=[classifier, regression])

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

early_stop = EarlyStopping(monitor='val_loss', min_delta=0, patience=100, verbose=1, mode='auto')

当我尝试拟合模型时:

history = model.fit(x=X_train, y=[y_train, y_train_aq], batch_size=256, epochs=100, validation_data=(X_val, [y_val, y_val_aq]), callbacks=[early_stop])

这是我遇到问题的地方。我认为这与我没有正确插入两个输出有关。 y_train 和 y_val 是带有分类输出(热编码)的 numpy 数组。 y_train_aq 和 y_val_aq 是带有回归输出的 numpy 数组。

尝试拟合模型时出现此错误:

Epoch 1/100
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-72-119a47d93b5d> in <module>()
----> 1 history = model.fit(x=X_train, y=[y_train, y_train_aq], batch_size=256, epochs=100, validation_data=(X_val, [y_val, y_val_aq]), callbacks=[early_stop])

2 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
    853       # In this case we have created variables on the first call, so we run the
    854       # defunned version which is guaranteed to never create variables.
--> 855       return self._stateless_fn(*args, **kwds)  # pylint: disable=not-callable
    856     elif self._stateful_fn is not None:
    857       # Release the lock early so that multiple threads can perform the call

TypeError: 'NoneType' object is not callable

有人能告诉我我做错了什么以及如何解决吗?谢谢!

0 个答案:

没有答案