Keras save model issue

≯℡__Kan透↙ 提交于 2019-12-05 08:14:12

问题


This is a variational autoencoder network, I have to define a sampling method to generate latent z, I thinks it might be something wrong with this. This py file is doing training, the other py file is doing predicting online, so I need to save the keras model, there is nothing wrong with saving model, but when I load model from 'h5' file, it shows an error:

NameError: name 'latent_dim' is not defined

The following is code:

df_test = df[df['label']==cluster_num].iloc[:,:data_num.shape[1]]

data_scale_ = preprocessing.StandardScaler().fit(df_test.values)

data_num_ = data_scale.transform(df_test.values)

models_deep_learning_scaler.append(data_scale_)

batch_size = data_num_.shape[0]//10

original_dim = data_num_.shape[1]

latent_dim = data_num_.shape[1]*2

intermediate_dim = data_num_.shape[1]*10

nb_epoch = 1

epsilon_std = 0.001



x = Input(shape=(original_dim,))

init_drop = Dropout(0.2, input_shape=(original_dim,))(x)

h = Dense(intermediate_dim, activation='relu')(init_drop)

z_mean = Dense(latent_dim)(h)

z_log_var = Dense(latent_dim)(h)





def sampling(args):

    z_mean, z_log_var = args

    epsilon = K.random_normal(shape=(latent_dim,), mean=0.,

                              std=epsilon_std)

    return z_mean + K.exp(z_log_var / 2) * epsilon



# note that "output_shape" isn't necessary with the TensorFlow backend

z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])



# we instantiate these layers separately so as to reuse them later



decoder_h = Dense(intermediate_dim, activation='relu')

decoder_mean = Dense(original_dim, activation='linear')

h_decoded = decoder_h(z)

x_decoded_mean = decoder_mean(h_decoded)





def vae_loss(x, x_decoded_mean):

    xent_loss = original_dim * objectives.mae(x, x_decoded_mean)

    kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)

    return xent_loss + kl_loss



vae = Model(x, x_decoded_mean)

vae.compile(optimizer=Adam(lr=0.01), loss=vae_loss)



train_ratio = 0.95

train_num = int(data_num_.shape[0]*train_ratio)



x_train = data_num_[:train_num,:]

x_test = data_num_[train_num:,:]



vae.fit(x_train, x_train,

        shuffle=True,

        nb_epoch=nb_epoch,

        batch_size=batch_size,

        validation_data=(x_test, x_test))

vae.save('./models/deep_learning_'+str(cluster_num)+'.h5')

del vae

from keras.models import load_model
vae = load_model('./models/deep_learning_'+str(cluster_num)+'.h5')

It shows error: NameError: name 'latent_dim' is not defined


回答1:


For variational loss you are using many variable not known by Keras module. You need to pass them through custom_objects param of load_model function.

In your case:

vae.save('./vae_'+str(cluster_num)+'.h5')
vae.summary()

del vae

from keras.models import load_model
vae = load_model('./vae_'+str(cluster_num)+'.h5', custom_objects={'latent_dim': latent_dim, 'epsilon_std': epsilon_std, 'vae_loss': vae_loss})
vae.summary()



回答2:


If you load model (.h5) file in your new py file, you can use load_model('/.h5', compile = False). Because you do not need to any custom objects (i.e loss function or latent_dim, etc) in prediction step.



来源:https://stackoverflow.com/questions/41754247/keras-save-model-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!