mco
mco

Reputation: 1

tf.keras "history not defined"... but it appears to me to be defined

I am building a simple model in TensorFlow for an introductory course that I am taking. I cannot find the reason for the error "history not defined." I would be extremely grateful for any assistance in identifying the source of this error and fixing it.

Here is the code:

def train_model(model, scaled_train_images, train_labels):
    history = model.fit(scaled_train_images, train_labels, epochs=5, batch_size=256)

# Run function to train the model
train_model(model, scaled_train_images, train_labels)

Output:

Train on 60000 samples

Epoch 1/5
60000/60000 [===] - 56s 936us/sample - loss: 0.0623 - accuracy: 0.9809

Epoch 2/5
60000/60000 [===] - 56s 932us/sample - loss: 0.0538 - accuracy: 0.9838

Epoch 3/5
60000/60000 [===] - 56s 925us/sample - loss: 0.0475 - accuracy: 0.9858

Epoch 4/5
60000/60000 [===] - 55s 923us/sample - loss: 0.0422 - accuracy: 0.9869- los

Epoch 5/5
60000/60000 [===] - 55s 917us/sample - loss: 0.0380 - accuracy: 0.9883

#Load the model history into a pandas DataFrame

frame = pd.DataFrame(history.history)

---------------------------------------------------------------------------
NameError
Traceback (most recent call last)
<ipython-input-22-895ca3f31ddf> in <module>
    1 # Run this cell to load the model history into a pandas DataFrame
    2 
----> 3 frame = pd.DataFrame(history.history)

NameError: name 'history' is not defined

Upvotes: 0

Views: 1782

Answers (1)

Matteo Zanoni
Matteo Zanoni

Reputation: 4142

The history variable is only defined inside the train_model function and therefore is not accessible outside.

To fix this return it:

def train_model(model, scaled_train_images, train_labels):
    return model.fit(scaled_train_images, train_labels, epochs=5, batch_size=256)

history = train_model(model, scaled_train_images, train_labels)

frame = pd.DataFrame(history.history)

Upvotes: 1

Related Questions