Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

model.save and load giving different result #4875

Closed
@dipanjannag

Description

@dipanjannag

I am trying to save a simple LSTM model for text classification. The input of the model is padded vectorized sentences.

model = Sequential()model.add(LSTM(40, input_shape=(16, 32)))model.add(Dense(20))model.add(Dense(8, activation='softmax'))model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

For saving I'm using the following snippet:

for i in range(50):    from sklearn.cross_validation import train_test_split    data_train, data_test, labels_train, labels_test = train_test_split(feature_set, dummy_y, test_size=0.1, random_state=i)    accuracy = 0.0    try:        with open('/app/accuracy', 'r') as file:            for line in file:                accuracy = float(line)    except Exception:        print("error")    model.fit(data_train, labels_train, nb_epoch=50)    loss, acc = model.evaluate(feature_set, dummy_y)    if acc > accuracy:        model.save_weights("model.h5", overwrite=True)        model.save('my_model.h5', overwrite=True)        print("Saved model to disk.\nAccuracy:")        print(acc)        with open('/app/accuracy', 'w') as file:            file.write('%f' % acc)

But whenever I'm trying to load the same model

from keras.models import load_modelmodel = load_model('my_model.h5')

I'm getting random accuracy like an untrained model. same result even when trying to load weights separately.
If I set the weights

lstmweights=model.get_weights()model2.set_weights(lstmweights)

like above. It is working ifmodel andmodel2 are run under same session (same notebook session). If I serializelstmweights and try to load it from different place, again I'm getting result like untrained model. It seems saving only the weights are not enough. So why model.save is not working. Any known point?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp