2017-01-25 21 views
4

私はKerasライブラリを使ってPythonでニューラルネットワークを作成しています。私はトレーニングデータ(txtファイル)をロードし、ネットワークを開始し、ニューラルネットワークの重みを "適合"させました。私は出力テキストを生成するコードを書いています。ここでは、コードは次のようになります。ニューラルネットのKerasモデルload_weights

#!/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') 

私の問題は、次のとおりです。実行時に次のエラーが生成されます

model.load_weights(filename) 
NameError: name 'model' is not defined 

私は次のように追加したが、エラーが解消されない:

from keras.models import Sequential 
from keras.models import load_model 

どれでも助けに感謝します。あなたが最初のネットワークオブジェクトを作成する必要があり

答えて

9

は、それをコンパイルし、modelと呼ばれ、後にのみ例を作業model.load_weights(fname)

を呼び出す:

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()) 
+2

は完璧に動作する、VEYありがとうございました。しかし、私はkeras.layersからの変更が必要でした。dens、活性化:keras.layers.coreからimport Dense。 – Deadulus

関連する問題