Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I'm using the Keras library to create a neural network in python. I have loaded the training data (txt file), initiated the network and "fit" the weights of the neural network. I have then written code to generate the output text. Here is the code:

#!/usr/bin/env python

# load the network weights

filename = "weights-improvement-19-2.0810.hdf5"

model.load_weights(filename)

model.compile(loss='categorical_crossentropy', optimizer='adam')

My problem is: on execution the following error is produced:

 model.load_weights(filename)

 NameError: name 'model' is not defined

I have added the following but the error still persists:

from keras.models import Sequential

from keras.models import load_model

Any help would be appreciated.

1 Answer

0 votes
by (41.4k points)

After creating the network object called model and then compiling it, call  model.load_weights(filename):

Refer to this code:

from keras.models import Sequential

from keras.layers import Dense, Activation

def build_model():

    model = Sequential()

    model.add(Dense(output_dim=64, input_dim=100))

    model.add(Activation("relu"))

    model.add(Dense(output_dim=10))

    model.add(Activation("softmax"))

    model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])

    return model

model1 = build_model()

model1.save_weights('my_weights.model')

model2 = build_model()

model2.load_weights('my_weights.model')

# do stuff with model2 (e.g. predict())

Browse Categories

...