Errors in saving a Keras model structure

874 views
Skip to first unread message

Ming-Hua Chung

unread,
Jun 1, 2016, 6:24:38 PM6/1/16
to Keras-users
Hi, all,

While following the instruction in FAQ to save a model structure, I kept getting error massages regardless whether I save it in JSON or YAML. The error messages are the same on both. Does anyone have the same problem? If so, how do I get around it? Appreciate your help!

# save as JSON
vae_json = vae.to_json() # save as YAML vae_yaml = vae.to_yaml()
vae2 = model_from_json(vae_json)
vae2 = model_from_yaml(vae_yaml)

Here is the error messages:
>>> vae2 = model_from_yaml(vae_yaml)

Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/local/miniconda/lib/python2.7/site-packages/keras/models.py", line 27, in model_from_yaml
return layer_from_config(config, custom_objects=custom_objects)
File "/usr/local/miniconda/lib/python2.7/site-packages/keras/utils/layer_utils.py", line 35, in layer_from_config
return layer_class.from_config(config['config'])
File "/usr/local/miniconda/lib/python2.7/site-packages/keras/engine/topology.py", line 2216, in from_config
layer(input_tensors)
File "/usr/local/miniconda/lib/python2.7/site-packages/keras/engine/topology.py", line 485, in __call__
self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
File "/usr/local/miniconda/lib/python2.7/site-packages/keras/engine/topology.py", line 543, in add_inbound_node
Node.create_node(self, inbound_layers, node_indices, tensor_indices)
File "/usr/local/miniconda/lib/python2.7/site-packages/keras/engine/topology.py", line 153, in create_node
output_tensors = to_list(outbound_layer.call(input_tensors, mask=input_masks))
File "/usr/local/miniconda/lib/python2.7/site-packages/keras/layers/core.py", line 446, in call
return self.function(x, **arguments)
File "<input>", line 22, in sampling
NameError: global name 'batch_size' is not defined

Michael
Message has been deleted

Ming-Hua Chung

unread,
Jun 2, 2016, 9:57:06 AM6/2/16
to Keras-users
Hi, Constantin,

I have follows the autoencoder example in Keras blog to set up my model:
batch_size = 16
original_dim = 784
latent_dim = 2
intermediate_dim = 128
epsilon_std = 0.01
nb_epoch = 40

x = Input(batch_shape=(batch_size, original_dim))
h = Dense(intermediate_dim, activation='relu')(x)

And this is how my model eventually looks like:

>>> vae.summary()
____________________________________________________________________________________________________
Layer (type)                       Output Shape        Param #     Connected to
=======================================================================================
input_1 (InputLayer)               (16, 784)           0
____________________________________________________________________________________________________
dense_1 (Dense)                    (16, 128)           100480      input_1[0][0]
____________________________________________________________________________________________________
dense_2 (Dense)                    (16, 2)             258         dense_1[0][0]
____________________________________________________________________________________________________
dense_3 (Dense)                    (16, 2)             258         dense_1[0][0]
____________________________________________________________________________________________________
lambda_1 (Lambda)                  ((16, 2), 2)        0           dense_2[0][0]
                                                                                  dense_3[0][0] 
And this is how I train it:

vae.fit(x_train, x_train,
        shuffle=True,
        nb_epoch=nb_epoch,
        batch_size=batch_size,
        validation_data=(x_test, x_test))

I don't think I miss anything since the model coding part is basically a direct copy from Keras blog example. But thank you for your reply anyway.

Michael

On Thursday, June 2, 2016 at 3:01:53 AM UTC-5, Constantin Ei wrote:
Hi,
seems to me, that you simply have to define "batch_size". Your error message says, it is required but missing.

-- Constantin

Ming-Hua Chung

unread,
Jun 2, 2016, 10:33:26 AM6/2/16
to Keras-users
After further digging, I realize that the problem is probably caused by function from_config since I got the same error message when I try to load back my model configuration. Still no solution though.

>>> config = vae.get_config()
>>> vae = Model.from_config(config)


Traceback (most recent call last):
File "<input>", line 1, in <module>
  File "/usr/local/miniconda/lib/python2.7/site-packages/keras/engine/topology.py", line 2216, in from_config
layer(input_tensors)
File "/usr/local/miniconda/lib/python2.7/site-packages/keras/engine/topology.py", line 485, in __call__
self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
File "/usr/local/miniconda/lib/python2.7/site-packages/keras/engine/topology.py", line 543, in add_inbound_node
Node.create_node(self, inbound_layers, node_indices, tensor_indices)
File "/usr/local/miniconda/lib/python2.7/site-packages/keras/engine/topology.py", line 153, in create_node
output_tensors = to_list(outbound_layer.call(input_tensors, mask=input_masks))
File "/usr/local/miniconda/lib/python2.7/site-packages/keras/layers/core.py", line 446, in call
return self.function(x, **arguments)
File "<input>", line 22, in sampling
NameError: global name 'batch_size' is not defined
>>> batch_size
16

Ming-Hua Chung

unread,
Jun 2, 2016, 2:32:53 PM6/2/16
to Keras-users
Ok, so I solve the problem myself. In case anyone is interested, here is the solution.

Here is the codes of variational autoencoder from Keras Blog:
def sampling(args):
    z_mean, z_log_sigma = args
    epsilon = K.random_normal(shape=(batch_size, latent_dim),
                              mean=0., std=epsilon_std)
    return z_mean + K.exp(z_log_sigma) * epsilon
Once I changed to this (below), then I don't get the error messages anymore.
def sampling(args):
    z_mean, z_log_sigma = args
    epsilon = K.random_normal(shape=(16, 2),
                              mean=0., std=0.01)
    return z_mean + K.exp(z_log_sigma) * epsilon

So, it is not really from_config, but from the sampling function.

Michael

zgb...@gmail.com

unread,
Feb 26, 2017, 2:02:16 AM2/26/17
to Keras-users
That helps, but it still cannot solve the question. 

If we save the variational autoencoder model: 

autoencoder = vae.save('variationalAutoencoder.h5')

and then load it in another script: 

vae = load_model('variationalAutoencoder.h5')

The errors will occur, like
batch_size not difined, latent_dim not defined, invalid objects vae_loss

Anybody can solve it?
Reply all
Reply to author
Forward
0 new messages