Ad

How To Solve " NameError: Name 'model' Is Not Defined " Error?

- 1 answer

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.

Ad

Answer

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.

Ad
source: stackoverflow.com
Ad