-1

I am trying to plot the Model accuracy and Model loss learning curves after I have successfully trained my LSTM model to see the pattern of the learning curves (to see if its overfitting or underfitting) . The problem is the error "TypeError: 'tuple' object is not callable" keeps popping up. I'm a beginner when it comes to this so I'll take any advice I can get. I am using Python 3.8.8 and Numpy 1.18.1.

LSTM Model

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

Model Fitting:

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

Learning Curves:

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)

The error:

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

1 Answers1

0

try using a callback function to store the metrics and then plot them, like shown here https://stackoverflow.com/a/66780538/2269826

B.Kocis
  • 1,954
  • 20
  • 19