【问题标题】:TypeError: 'tuple' object is not callable?TypeError:“元组”对象不可调用?
【发布时间】:2021-09-30 11:56:29
【问题描述】:

在我成功训练我的 LSTM 模型以查看学习曲线的模式后,我正在尝试绘制模型准确度和模型损失学习曲线(看看它是过拟合还是欠拟合)。问题是错误“TypeError:'tuple' object is not callable”不断弹出。我是一个初学者,所以我会接受我能得到的任何建议。我正在使用 Python 3.8.8 和 Numpy 1.18.1。

LSTM 模型

model=Sequential()
model.add(LSTM(32, return_sequences=True, input_shape = (n_time_steps, n_features),
              kernel_regularizer = l2(0.000001), bias_regularizer = l2(0.000001), name='lstm_1'))
model.add(Flatten(name='flatten'))
model.add(Dense(64, activation='relu',kernel_regularizer = l2(0.000001), bias_regularizer = l2(0.000001), name='dense_1' ))
model.add(Dense(len(np.unique(y_train)), activation='softmax', 
                kernel_regularizer = l2(0.000001), bias_regularizer = l2(0.000001), name='output'))
model.summary()

# Compile the model
model.compile(loss='sparse_categorical_crossentropy', optimizer = Adam(), metrics=['accuracy'])

模型拟合:

history = model.fit(train_gen, epochs = 5, validation_data= test_gen, callbacks=callbacks)

学习曲线:

def plot_learningCurve(history, epochs):
  # Plot training & validation accuracy values
  epoch_range = range(1, epochs+1)
  plt.plot(epoch_range, history.history['accuracy'])
  plt.plot(epoch_range, (history.history['val_accuracy']))
  plt.title('Model accuracy')
  plt.ylabel('Accuracy')
  plt.xlabel('Epoch')
  plt.legend(['Train', 'Val'], loc='upper left')
  plt.show()

  # Plot training & validation loss values
  plt.plot(epoch_range, history.history['loss'])
  plt.plot(epoch_range, history.history['val_loss'])
  plt.title('Model loss')
  plt.ylabel('Loss')
  plt.xlabel('Epoch')
  plt.legend(['Train', 'Val'], loc='upper left')
  plt.show()

plot_learningCurve(history,5)

错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-66-f70f77d7b751> in <module>
----> 1 plot_learningCurve(history,5)

<ipython-input-65-ece94f9461ab> in plot_learningCurve(history, epochs)
      2   # Plot training & validation accuracy values
      3   epoch_range = range(1, epochs+1)
----> 4   plt.plot(epoch_range, history.history['accuracy'])
      5   plt.plot(epoch_range, (history.history['val_accuracy']))
      6   plt.title('Model accuracy')

TypeError: 'tuple' object is not callable

【问题讨论】:

    标签: python lstm recurrent-neural-network tf.keras keras-layer


    【解决方案1】:

    尝试使用回调函数来存储指标,然后绘制它们,如下所示https://stackoverflow.com/a/66780538/2269826

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2020-08-26
      • 1970-01-01
      • 2017-11-17
      • 2015-03-11
      • 1970-01-01
      • 2018-12-25
      相关资源
      最近更新 更多