Keras保存模型问题

时间:2017-01-20 00:41:02

标签: save deep-learning keras

这是一个变分自动编码器网络,我必须定义一个生成潜在z的采样方法,我认为这可能是错误的。这个py文件正在进行训练,另一个py文件正在进行在线预测,所以我需要保存keras模型,保存模型没有任何问题,但是当我从' h5'文件,它显示错误:

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')

以下是代码:

NameError: name 'latent_dim' is not defined

显示错误: public final class Algorithm { public static <T extends Object & Comparable<? super T>> T max(List<? extends T> list, int begin, int end) { T maxElem = list.get(begin); for (++begin; begin < end; ++begin) if (maxElem.compareTo(list.get(begin)) < 0) maxElem = list.get(begin); return maxElem; } }

2 个答案:

答案 0 :(得分:2)

对于变分损失,您使用的是许多Keras模块未知的变量。您需要通过custom_objects函数的load_model参数传递它们。

在你的情况下:

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

答案 1 :(得分:0)

如果在新的py文件中加载模型(.h5)文件,则可以使用load_model('/。h5',compile = False)。 因为您不需要在预测步骤中使用任何自定义对象(即损失函数或latent_dim等)。