Python Forum

Full Version: 'list' object not callable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi
i am new to python. iam working on lstm model. i want to plot graph of loss and accuracy. i know that model history is saved. i have used below code.
var1=history. history['loss']
plt.plot(var1)

but I got an error.

plt.plot(var1)
Traceback (most recent call last):
File "<ipython-input-124-f96da71a5192>", line 1, in <module>
plt.plot(var1)
TypeError: 'list' object is not callable

please help me in removing this error. thank you.
It looks like you expect plt to be an object that has a plot() method. But instead it's just a list. Possibly you are create plt incorrectly. Without seeing the code, we cannot guess.
(Nov-28-2020, 06:43 PM)bowlofred Wrote: [ -> ]It looks like you expect plt to be an object that has a plot() method. But instead it's just a list. Possibly you are create plt incorrectly. Without seeing the code, we cannot guess.

model = Sequential()
model.add(LSTM(lstm_units, input_shape=(num_timestamps,1)))
model.add(Dense(dense_units, activation='relu'))
model.add(Dense(dense_units, activation='relu'))
model.add(Dense(dense_units, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=tf.keras.losses.categorical_crossentropy, optimizer='adam', metrics=['accuracy'])
print('Model compiled successfully')

# --- Fit model
history=model.fit(train_X, train_Y ,epochs=epochs, batch_size=batch_size, verbose=1, validation_split=0.2)
print('Model fit successfully')
....
accuracy = model.evaluate(test_X, test_Y, batch_size=batch_size, verbose=1)
print('Evaluation of the model completed')
print(accuracy[1]*100, 'percent')

plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
Still not enough. Nowhere in that code is plt assigned. It's used on line 18, but it must be assigned somewhere before that. You would get a different error if that were all your code.
(Nov-28-2020, 09:46 PM)bowlofred Wrote: [ -> ]Still not enough. Nowhere in that code is plt assigned. It's used on line 18, but it must be assigned somewhere before that. You would get a different error if that were all your code.

import csv
import numpy as np
from pandas import read_csv
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
from tensorflow.keras.callbacks import TensorBoard
import matplotlib.pyplot as plt

model = Sequential()
model.add(LSTM(lstm_units, input_shape=(num_timestamps,1)))
model.add(Dense(dense_units, activation='relu'))
model.add(Dense(dense_units, activation='relu'))
model.add(Dense(dense_units, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=tf.keras.losses.categorical_crossentropy, optimizer='adam', metrics=['accuracy'])
print('Model compiled successfully')
 
# --- Fit model
history=model.fit(train_X, train_Y ,epochs=epochs, batch_size=batch_size, verbose=1, validation_split=0.2)
print('Model fit successfully')
....
accuracy = model.evaluate(test_X, test_Y, batch_size=batch_size, verbose=1)
print('Evaluation of the model completed')
print(accuracy[1]*100, 'percent')
 
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
Are these two separate pieces of code? Your first post (with the error) has lines in it that aren't found elsewhere. I asked to understand more of what was happening in the first code, but the other listings seem to be something else.

What's the relationship between the lines with the error and the rest of the code you've posted?

In your initial error, the problem is that instead of just being the import of matplotlib.pyplot, plt was probably reassigned to a list. Don't do that and the error shouldn't happen. The actual place it's happening isn't in any of the code that's been posted.