Krush23
Krush23

Reputation: 741

How to solve " NameError: name 'model' is not defined " error?

While trying to predict the output I am facing the error NameError: name 'model' is not defined. How to solve this.

%%time
# Lstm
model = Sequential()
model.add(LSTM(data_dim, input_shape=(95,data_dim), activation='relu'))
model.add(Dense(data_dim))
model.compile(loss='mse', optimizer='adam')
model.fit(X_train, y_train, epochs=10, batch_size=96)
model.summary()

The above model trained well. While trying model.predict(X_test1), I am having the issue mentioned above.

Upvotes: 0

Views: 5881

Answers (1)

Richard Nemeth
Richard Nemeth

Reputation: 1864

The issue lies in the magic function %%time. In the latest version of IPython in Jupyter, running a cell with time magic function as a header runs the cell out of the global context. This is also true for %%timeit.

Practically it means that all the new variables defined in the %%time cell do not exist in the main context, including your model variable, which is why you receive the NameError exception, since the interpreter can not find a variable named model.

Removing the %%time line from your cell will do the trick.

Upvotes: 2

Related Questions