keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
62.12k stars 19.49k forks source link

model.save and load giving different result #4875

Closed dipanjannag closed 3 years ago

dipanjannag commented 7 years ago

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_model
model = 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 if model and model2 are run under same session (same notebook session). If I serialize lstmweights 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?

Panache1 commented 7 years ago

I'm having a similar problem, but it has to do with setting stateful=True. If I do that, the prediction from the original model is different from the prediction of the saved and reloaded model.

`# DEPENDENCIES import numpy as np

from keras.models import Sequential from keras.layers.core import Dense, Activation from keras.layers.recurrent import LSTM

TRAINING AND VALIDATION FILES

xTrain = np.random.rand(200,10) yTrain = np.random.rand(200,1) xVal = np.random.rand(100,10) yVal = np.random.rand(100,1)

ADD 3RD DIMENSION TO DATA

xTrain = xTrain.reshape(len(xTrain), 1, xTrain.shape[1])
xVal = xVal.reshape(len(xVal), 1, xVal.shape[1])

CREATE MODEL

model = Sequential() model.add(LSTM(200, batch_input_shape=(10, 1, xTrain.shape[2])

, stateful=True # With this line, the reloaded model generates different predictions than the original model

    ))

model.add(Dense(yTrain.shape[1])) model.add(Activation("linear"))

model.compile(loss="mean_squared_error", optimizer="rmsprop") model.fit(xTrain, yTrain, batch_size=10, nb_epoch=2, verbose=0, shuffle='False', validation_data=(xVal, yVal))

PREDICT RESULTS ON VALIDATION DATA

yFit = model.predict(xVal, batch_size=10, verbose=1) print() print(yFit)

SAVE MODEL

model.save('my_model.h5') del model

RELOAD MODEL

from keras.models import load_model model = load_model('my_model.h5') yFit = model.predict(xVal, batch_size=10, verbose=1) print() print(yFit)

DO IT AGAIN

del model model = load_model('my_model.h5') yFit = model.predict(xVal, batch_size=10, verbose=1) print() print(yFit)`

fighting41love commented 7 years ago

Same problem. Is there sth wrong with the function "save model"?

bibhashthakur commented 7 years ago

I am having the exact same issue. Does anyone know exactly what the problem is?

FTAsr commented 7 years ago

Same issue using json format for saving a very simple model. I get different results on my test data before and after save/loading the model.

classifier = train(model, trainSet, devSet)

TEST BEFORE SAVING

test(model, classifier, testSet)

save model to json

model_json = classifier.to_json() with open('../data/local/SICK-Classifier', "w") as json_file: json_file.write(model_json)

load model fron json

json_file = open('../data/local/SICK-Classifier', 'r') loaded_model_json = json_file.read() json_file.close() classifier = model_from_json(loaded_model_json)

TEST AFTER SAVING

test(model, classifier, testSet)

HarshaVardhanP commented 7 years ago

I crosschecked all these functions - they seem to be working properly. model.save(), load_model(), model.save_weights() and model.load_weights()

kswersky commented 7 years ago

I ran into a similar issue. After saving my model, the weights were changed and my predictions became random.

For my own case, it came down to how I was mixing vanilla Tensorflow with Keras. It turns out that Keras implicitly runs tf.global_variables_initializer if you don't tell it that you will do so manually. This means that in trying to save my model, it was first re-initializing all of the weights.

The flag to prevent Keras from doing this is _MANUAL_VAR_INIT in the tensorflow backend. You can turn it on like this, before training your model:

from keras.backend import manual_variable_initialization manual_variable_initialization(True)

Hope this helps!

HarshaVardhanP commented 7 years ago

Hi @kswersky,

Thanks for your answer.

I am using Keras 2.0 with Tensorflow 1.0 setup. I am building model in Keras and using Tensorflow pipeline for training and testing. When you load the keras model, it might reinitialize the weights. I avoided tf.global_variables_initializer() and used load_weights('saved_model.h5'). Then model got the saved weights and I was able to reproduce correct results. I did not have to do the _manual_var_init step. (its a very good answer for just Keras)

May be I confused people who are just using Keras.

pras135 commented 7 years ago

Use model.model.save() instead of model.save()

lotempeledGong commented 7 years ago

I'm stuck with the same problem. The only solution for now is move to python 2.7 ?

@pras135 , if I do as you suggest I cannot perform model.predict_classes(x), AttributeError: 'Model' object has no attribute 'predict_classes'

kefth commented 7 years ago

Same issue here. When I load a saved model my predictions are random.

pras135 commented 7 years ago

@lotempeledGong

model_1 = model.model.save('abcd.h5') # save the model as abcd.h5 from keras.models import load_model model_1 = load_model('abcd.h5') # load the saved model y_score = model_1.predict_classes(data_to_predict) # supply data_to_predict

Hope it helps.

lotempeledGong commented 7 years ago

@pras135 What you suggested is in the same session, and it does work. Unfortunately I need this to work in separate sessions, and if you do the following:

(in first python session) model_1 = model.model.save('abcd.h5') # save the model as abcd.h5 (close python session) (open second python session) model_1 = load_model('abcd.h5') # load the saved model y_score = model_1.predict_classes(data_to_predict) # supply data_to_predict

I receive the following error: AttributeError: 'Model' object has no attribute 'predict_classes'

dipanjannag commented 7 years ago

@lotempeledGong That should not happen. Check if load_model means same as keras.models.load_model in your context. This should work just fine.

lotempeledGong commented 7 years ago

@deeiip thanks, but this still doesn't work for me. However this is not my main problem here. What I want eventually is to train a model, save it, close the python session and in a new python session load the trained model and obtain the same accuracy. Currently when I try to do this, the loaded model gives random predictions and it is as though it wasn't trained. By the way, in case this is a versions issue- I'm running with Keras 2.0.4 with Tensorflow 1.1.0 backend on Python 3.5.

BBarbosa commented 7 years ago

@lotempeledGong I'm facing exactly the same issue you refer here. However, I'm using Tensorflow 1.1 and TFlearn 0.3 on Windows10 with Python 3.5.2.

chenlihuang commented 7 years ago

@deeiip have you solved this problem? how to?

chenlihuang commented 7 years ago

@kswersky l add the from keras.backend import manual_variable_initialization manual_variable_initialization(True) but the error came: tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value Variable [[Node: Variable/_24 = _SendT=DT_FLOAT, client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_8_Variable", _device="/job:localhost/replica:0/task:0/gpu:0"]] [[Node: Variable_1/_27 = _Recv[_start_time=0, client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_10_Variable_1", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

chenlihuang commented 7 years ago

@HarshaVardhanP how to avoided tf.global_variables_initializer() before load model?

HarshaVardhanP commented 7 years ago

@chenlihuang Now, I am using tf.global_variables_initializer(). It will init net variables. And use load_weights() to load the saved weights. This seems easier for me than using load_model().

chenlihuang commented 7 years ago

@HarshaVardhanP can you give some ideas for me on the keras level . l don't care the backend on TF. only easy uase keras layers. that's mean how can l solve the problem in this beacuse I didn't touch the TF .

Ssica commented 7 years ago

EDIT: i've noticed loading my model doesnt give different results so I guess I dont need the workaround.

Atvar commented 7 years ago

Unfortunately, I've run into the same issue that many others on here seem to have encountered -- I've trained what seems to be an extremely powerful text classifier (based on cross-validation, at least, with a healthy-sized dataset), but upon loading a saved model -- either using load_model or model.load_weights -- my model's performance is now completely worthless when tested in a new session. There's absolutely no way this is the same model that I trained. I've tried every suggestion offered here, but to no avail. Super disheartening to see what appeared to be such good results and have no ability to actually use the classifier in other sessions/settings. I hope this gets addressed soon. (P.S. I'm running with Keras 2.0.4 with Tensorflow 1.1.0 backend on Python 3.5.)

ChandrahasJR commented 7 years ago

@gokceneraslan @fchollet Many of us are facing this issue. Could you please take a look ?

Thanks!

kevinjos commented 7 years ago

I do not see any issue with model serialization using the save_model() and load_model() functions from the latest Tensorflow packaged Keras.

For example:

import tensorflow.contrib.keras as keras
m = train_keras_cnn_model() # Fill in the gaps with your model
model_fn = "test-keras-model-serialization.hdf5"
keras.models.save_model(m, model_fn)
m_load = keras.models.load_model(model_fn)
m_load_weights = m_load.get_weights()
m_weights = m.get_weights()
assert len(m_load_weights) == len(m_weights)
for i in range(len(m_weights)):
    assert np.array_equal(m_load_weights[i], m_weights[i])
print("Model weight serialization test passed"
ChandrahasJR commented 7 years ago

Hi @kevinjos

The issue is not with using the saved model in the same session. If i save a model from session 1 and load it in session 2 and use two exactly the same data to perform inference, the results are different.

To be more specific, the inference results from the session in which the model was built is much better compared to results from a different session using the same model.

How is tensorflow packaged keras different from vanilla keras itself ?

kevinjos commented 7 years ago

@Chandrahas1991 When I run a similar code as above with a fresh tf session I get the same results. Have you tried the suggestion offered by @kswersky to set a flag to prevent automatic variable initialization? Could the issue with serialization apply only to LSTM layers? Or more specifically stateful LSTM layers? Have you tried using only the Keras code packaged with TensorFlow?

import tensorflow.contrib.keras as keras
from tensorflow.contrib.keras import backend as K
m = train_keras_cnn_model() # Fill in the gaps with your model
model_fn = "test-keras-model-serialization.hdf5"
m_weights = m.get_weights()
keras.models.save_model(m, model_fn)
K.clear_session()
m_load = keras.models.load_model(model_fn)
m_load_weights = m_load.get_weights()
assert len(m_load_weights) == len(m_weights)
for i in range(len(m_weights)):
    assert np.array_equal(m_load_weights[i], m_weights[i])
print("Model weight serialization test passed"
ChandrahasJR commented 7 years ago

@kevinjos I got this error while working with CNN's and @kswersky's solution did not work for me.

I haven't tried with keras packed in tensorflow. I'll check it out. Thanks for the suggestion!

gokceneraslan commented 7 years ago

There are already tests in Keras to check if model saving/loading works. Unless you write a short and fully reproducible code (along with the data) and fully describe your working environment (keras/python version, backend), it's difficult to pinpoint the cause of all issues mentioned here.

I've just sent another PR to add more tests about problems described here. @deeiip can you check #7024 and see if it's similar to the code that you use to reproduce this? With my setup, (keras master branch, python 3.6, TF backend) I cannot reproduce any model save/load issues with either mlps or convlstms, even if I restart the session in between.

Some people mentioned reproducibility problems about stateful RNNs. As far as I know, RNN states are not saved via save_model(). Therefore, there must be differences when you compare predictions before and after saving the model, since states are reset. But keep in mind that this report is not about stateful RNNs.

amadupu commented 7 years ago

check the input data if is consistent for the same data across multiple executions.

Jamesmoly commented 7 years ago

I am having this same problem, but only when I use an embedding layer and keras API. Before closing the session the accuracy after running model.evaluate is >90%, after opening a new session and running a model.evaluate on the exact same data I get ~ 80% accuracy. I have tried saving the model using save,model() and by saving to json and saving and then also loading the weights. Both methods give the same results.

I use the same data with a sequential model without an embedding layer and API model without an embedding layer and both work as expected.

embedding_layer = Embedding(len(word_index) +1, 28, input_length = 28)

sequence_input = Input(shape=(max_seq_len,), dtype='int32')

embedded_sequences = embedding_layer(sequence_input)

x = Conv1D(128, 3, activation='relu')(embedded_sequences) x = MaxPooling1D(3)(x) x = Conv1D(128, 2, activation='relu')(x) x = MaxPooling1D(2)(x) x = Conv1D(128, 2, activation='relu')(x) x = MaxPooling1D(2)(x) # global max pooling x = Flatten()(x) x = Dense(12, activation='relu')(x) x = Dropout(0.2)(x) preds = Dense(46, activation='softmax')(x)

model = Model(sequence_input, preds)

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])

model.fit(x_train, y_train, validation_data=(x_val, y_val), epochs=1, batch_size=300)

I am using anaconda v3.5 and tf r1.1

FrancescoLR commented 7 years ago

Any news on that? Similar problem here. I am training a unet architecture CNN. The training gives good results and the evaluate function works fine loading the model. As I generate new predicitons they are completely random and uncorrelated to the ground truths.. Loading the model or the weights separately does not help. Keras 2.0.4 with Tensorflow 1.1.0 backend on Python 2.7.

gokceneraslan commented 7 years ago

It's difficult to help without a fully reproducible code.

jgustave commented 7 years ago

I ran in to the same issue (and am mixing Keras and Tensorflow) and I am already using manual_variable_initialization(True). I had to add the extra load_weights(..) call to get it to work.

    def __init__(self):
        self.session = tf.Session()
        K.set_session(self.session)
        K.manual_variable_initialization(True)

        self.model = load_model(FILENAME)
        self.model._make_predict_function()

        self.session.run(tf.global_variables_initializer())
        self.model.load_weights(FILENAME) #### Added this line
        self.default_graph = tf.get_default_graph()

        self.default_graph.finalize()
Dapid commented 7 years ago

For anyone experiencing this, can you check that the weights of the model are exactly the same right after training and after being reloaded? To machine precision.

ZheweiMedia commented 7 years ago

I have the same issue. In my case if I use Lambda layer then the prediction results after loading are not correct. If I remove the Lambda layer, then the results after loading are the same as training.

EDIT: I checked my code again, and I found the issue is because of using a undefined parameter in my lambda function. After I solved it, the results are consistent.

jinksmysteryman commented 7 years ago

Just switched to a GPU setup, and this issue came up. I haven't tried switching back to CPU only, but will let you know if it seems to be the issue. Additionally #4044 has been happening a lot, although I'm not sure if it's related. Tensorflow is only used to set the device (GPU) for each model.

Python 3.4.3, Keras 2.0.6, TensorFlow 1.2.0, TensorFlow-GPU 1.1.0

UPDATE: It seems to occur when training two models at the same time (i.e. train model1, test model1 with expected accuracy, train model2, test model1 with random predictions). I think this might be an issue with the load_model function, and I'm going to try using the instance method (load the model from json and then load it's weights) to see if the same error occurs.

UPDATE 2: Instance methods didn't solve the issue.

UPDATE 3: I feel kinda silly. My code was the issue. 👎 Sorry for the spam.

diyathn commented 7 years ago

I'm training a LSTM RNN for description generation using Keras (Tensorflow Backend) with MSCOCO dataset. When training the model it had 92% accuracy with 0.79 loss. Further when the model was training I tested the description generation at each epoch and the model provided very good predictions with a meaningful description when it gives a random word.

However after training I loaded the model using model.load_weights(WEIGHTS) method in Keras and tried to create a description by giving a random word as I've done before. But now model is not providing a meaningful description and it just outputs random words which has no meaning at all.

I checked weight values and they are same too.

My model parameters are:

10 LSTM layers Learning rate: 0.04 Activation: Softmax Loss Function: Categorical Cross entropy Optimizer: rmsprop

My Tesorflow version: 1.2.1 Python: 3.5 Keras Version: 2.0.6

Does anyone have a solution for this ?

rrki commented 7 years ago

What I've tried here is:

md = config_a_model() # a pretty complicated convolutional NN. md.save('model.h5')

md1 = config_a_model() # same config as above md1.load_weights('model.h5') md1.save('another_model.h5')

model.h5 is 176M and another_model.h5 is only 29M.

Python 2.7.6 tensorflow-gpu (1.2.1) Keras 2.0.6

HarshaVardhanP commented 7 years ago

Did you try model.save_weights() ?? I am using model.save_weights(), model.load_weights(), model.load_from_json() which are working fine.

rrki commented 7 years ago

@HarshaVardhanP Will try.. What's the difference between save() and save_weights() ? Also I feel it's a recent issue for me since I upgraded Keras from 2.0.1 => 2.0.6 last weekend. I didn't have such issue before, as previously the prediction results are much better. Now the prediction results are horrible. That's why I tested above model save->load->save again steps.

HarshaVardhanP commented 7 years ago

@rrki - I had this issue while loading Keras models into Tensorflow. model.save() didn't work for me for some reason. It's supposed to save architecture and weights. So, I saved/loaded architecture and weights separately which seems to be working fine for sometime.

rrki commented 7 years ago

@HarshaVardhanP I've tried save_weights() and also keras.models.save_model() They are basically all the same. I can't explain why the mode file sizes differ a lot. I tried to print out every layer's summary() and seems the network's architecture is there. But predict() always gives random results...

Dapid commented 7 years ago

@rrki we would need a specific example to find what is going wrong there, including a minimal model that reproduces the error, and some data to train it.

Otherwise, can you check if the weights of the model are the same, to machine precision, before and after reloading?

rrki commented 7 years ago

@Dapid Thank you for the reply. Correct me if I'm misusing something...

import numpy as np
from keras.callbacks import TensorBoard
from keras.layers import Conv1D, MaxPooling1D
from keras.layers.advanced_activations import PReLU
from keras.layers.core import Dense, Activation, Dropout, Flatten
from keras.layers.normalization import BatchNormalization
from keras.layers.wrappers import TimeDistributed
from keras.legacy.layers import Merge
from keras.models import save_model, load_model, Sequential

def config_model():
    pRelu = PReLU()
    md1 = Sequential(name='md1')
    md2 = Sequential(name='md2')
    md1.add(BatchNormalization(axis=1, input_shape=(10, 520)))
    md2.add(BatchNormalization(axis=1, input_shape=(10, 520)))
    md1.add(Conv1D(512, 1, padding='causal', activation='relu', name='conv1_f1_s1'))
    md2.add(Conv1D(512, 3, padding='causal', activation='relu', name='conv1_f3_s1'))
    md1.add(TimeDistributed(Dense(512, activation='relu')))
    md2.add(TimeDistributed(Dense(512, activation='relu')))
    merge_1 = Sequential(name='merge_1')
    merge_1.add(Merge([md1, md2], mode='concat'))
    merge_1.add(MaxPooling1D(name='mxp_merge'))
    merge_1.add(Dropout(0.2))
    merge_1.add(Dense(512))
    merge_1.add(pRelu)
    merge_1.add(Flatten())
    md3 = Sequential(name='md3')
    md3.add(BatchNormalization(axis=1, input_shape=(10, 520)))
    md3.add(Conv1D(512, 1, padding='causal', activation='relu', name='conv1_f1_s1_2'))
    md3.add(MaxPooling1D())
    md3.add(Dense(256))
    md3.add(Flatten())
    model = Sequential(name='final_model')
    model.add(Merge([merge_1, md3], mode='concat'))
    model.add(Dense(2))
    model.add(Activation('softmax', name='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam',
                  metrics=['accuracy'])
    return model

if __name__ == '__main__':
    X = np.random.rand(2000, 10, 520)
    Y = np.random.rand(2000, 2)
    model = config_model()
    tbCallBack = TensorBoard(
        log_dir='tb_logs', histogram_freq=4, write_graph=True,
        write_grads=True, write_images=True)
    model.fit([X]*3, Y, epochs=10, batch_size=100, callbacks=[tbCallBack])
    model.save('model.h5')

    model2 = config_model()
    model2.load_weights('model.h5')
    model2.save('model2.h5')

Result: -rw-r--r-- 1 root root 3762696 Aug 2 11:57 model2.h5 -rw-r--r-- 1 root root 23983824 Aug 2 11:57 model.h5

rrki commented 7 years ago

@Dapid I tried save_weights() and it looks correct from the model file sizes. (but why?) I guess the problem I'm facing is, I trained a huge model which took one week, but in previous code I did model.save(). Now what should be the right way to load this model from disk?

Dapid commented 7 years ago

@rrki your example doesn't work for me:

Using TensorFlow backend.
file.py:22: UserWarning: The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.
  merge_1.add(Merge([md1, md2], mode='concat'))
Traceback (most recent call last):
  File "/home/david/.virtualenv/py35/lib/python3.5/site-packages/tensorflow/python/framework/common_shapes.py", line 671, in _call_cpp_shape_fn_impl
    input_tensors_as_shapes, status)
  File "/usr/lib64/python3.5/contextlib.py", line 66, in __exit__
    next(self.gen)
  File "/home/david/.virtualenv/py35/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimensions must be equal, but are 5 and 512 for 'dense_3/add' (op: 'Add') with input shapes: [?,5,512], [1,512,1].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "file.py", line 45, in <module>
    model = config_model()
  File "file.py", line 25, in config_model
    merge_1.add(Dense(512))
  File "/home/david/.virtualenv/py35/lib/python3.5/site-packages/keras/models.py", line 469, in add
    output_tensor = layer(self.outputs[0])
  File "/home/david/.virtualenv/py35/lib/python3.5/site-packages/keras/engine/topology.py", line 596, in __call__
    output = self.call(inputs, **kwargs)
  File "/home/david/.virtualenv/py35/lib/python3.5/site-packages/keras/layers/core.py", line 840, in call
    output = K.bias_add(output, self.bias)
  File "/home/david/.virtualenv/py35/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 3479, in bias_add
    x += reshape(bias, (1, bias_shape[0], 1))
  File "/home/david/.virtualenv/py35/lib/python3.5/site-packages/tensorflow/python/ops/math_ops.py", line 838, in binary_op_wrapper
    return func(x, y, name=name)
  File "/home/david/.virtualenv/py35/lib/python3.5/site-packages/tensorflow/python/ops/gen_math_ops.py", line 67, in add
    result = _op_def_lib.apply_op("Add", x=x, y=y, name=name)
  File "/home/david/.virtualenv/py35/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "/home/david/.virtualenv/py35/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2508, in create_op
    set_shapes_for_outputs(ret)
  File "/home/david/.virtualenv/py35/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1873, in set_shapes_for_outputs
    shapes = shape_func(op)
  File "/home/david/.virtualenv/py35/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1823, in call_with_requiring
    return call_cpp_shape_fn(op, require_shape_fn=True)
  File "/home/david/.virtualenv/py35/lib/python3.5/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn
    debug_python_shape_fn, require_shape_fn)
  File "/home/david/.virtualenv/py35/lib/python3.5/site-packages/tensorflow/python/framework/common_shapes.py", line 676, in _call_cpp_shape_fn_impl
    raise ValueError(err.message)
ValueError: Dimensions must be equal, but are 5 and 512 for 'dense_3/add' (op: 'Add') with input shapes: [?,5,512], [1,512,1].
rrki commented 7 years ago

@Dapid What's your environment? I'm using Python 3.4.3 tensorflow-gpu (1.2.1) Keras (2.0.6) <- which follows instructions from ISSUE_TEMPLATE

How comes I didn't get any error except for those Merge layer warnings?

rrki commented 7 years ago
tensorboard

visualized via tensorboard...

Dapid commented 7 years ago

Ah, now it works, it must have been the image_data_format in the json.

I'll take a look. Thanks for the example.

TingDaoK commented 7 years ago

Anyone solved the problem? I found out when I used the statful RNN the code open('my_model_architecture.json','w').write(json_string) self.model.save_weights('my_model_weights.h5') self.model.save('/home/tdk/models/LSTM_3layers_model_weights_2_Callback_%f.h5'%score) temp = load_model('/home/tdk/models/LSTM_3layers_model_weights_2_Callback_%f.h5'%score) y_load = temp.predict(self.validation_data[0]) temp2 = model_from_json(open('my_model_architecture.json').read()) temp2.load_weights('my_model_weights.h5') y_load2 = temp2.predict(self.validation_data[0]) y_show = self.model.predict(self.validation_data[0]) y_show was different from y_load and y_load was same as y_load2. When I set the statful False. I can get the same y_show and y_load. However, when I open another python, and try to get the prediction, the prediction seems to be random as @lotempeledGong described. The other code is thatmodel = model_from_json(open('my_model_architecture.json').read()) model.load_weights('my_model_weights.h5') a = model.predict(test_data) np.savetxt("test_predict.txt",a) I don't know how to solve it, anyone has idea? or do I misuse something?