未为变体自动编码器模型定义keras.backend

时间:2020-05-11 15:28:50

标签: keras keras-layer autoencoder

我创建了一个变体自动编码器模型。为了进行采样,我创建了以下方法:

from keras import backend as k
def sampling(args):
    z_mean , z_log_var=args
    batch=k.shape(z_mean)[0]
    dim=k.int_shape(z_mean)[1]

    epsilon=k.random_normal(shape=(batch,dim))
    return z_mean + k.exp(0.5 * z_log_var) * epsilon

这是模型架构:

def create_variationalModel(original_dim):
    input_shape=(original_dim,)
    intermidiate_dim=58
    batch_size=10
    latent_dim=3
    epochs=100

    inputs=Input(shape=input_shape,name="encoder_input")
    x= Dense(units=original_dim,activation="tanh")(inputs) 
    x= Dense(units=int(original_dim/2),activation="tanh")(x) 
    x1= Dense(units=int(original_dim/4),activation="tanh")(x) 
    x2= Dense(units=int(original_dim/8),activation="tanh")(x1) 
    x3= Dense(units=10,activation="tanh")(x2) 
    z_mean=Dense(latent_dim,name="z_mean")(x3)
    z_log_var=Dense(latent_dim,name="z_log_var")(x3)

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


    encoder=Model(inputs,[z_mean,z_log_var,z],name="encoder")
    encoder.summary()

    latent_inputs=Input(shape=(latent_dim,),name="z_sampling")
    x= Dense(units=10,activation="tanh")(latent_inputs) 
    x1=Dense(units=int(original_dim/8),activation="tanh")(x)
    x2=Dense(units=int(original_dim/4),activation="tanh")(x1)
    x3=Dense(units=int(original_dim/2),activation="tanh")(x2)
    x3=Dense(units=original_dim,activation="tanh")(x3)
    outputs=Dense(units=original_dim,activation="sigmoid")(x3)

    decoder=Model(latent_inputs,outputs,name="decoder")
    decoder.summary()


    outputs=decoder(encoder(inputs)[2])
    vae = Model(inputs,outputs,name="vae_mlp")


    reconstruction_loss=mse(inputs,outputs)
    reconstruction_loss*=original_dim


    kl_loss = 1 + z_log_var -k.square(z_mean) - k.exp(z_log_var)
    kl_loss=k.sum(kl_loss,axis=-1)
    kl_loss*=-0.5
    vae_loss=k.mean(reconstruction_loss+kl_loss)
    vae.add_loss(vae_loss)
    plot_model(vae,to_file='vae.png',show_shapes=True)
    vae.compile(optimizer=RMSprop(),loss="mean_squared_error",metrics=["mae"])
    return vae

训练模型并进行测试之后,我决定像这样保存它:

vae.save("./models/vae.h5")

但是当我尝试像这样加载模型时:

model = load_model("./models/vae.h5")

我有这个问题:

--------------------------------------------------- ---------------------------- NameError追溯(最近的呼叫 最后) 1#负载模型 ----> 2个模型= load_model(“ ./ models / vae.h5”) 3#总结模型。 4 model.summary() 5,其中open(“ ./ models / LabelEncoders_dic.pickle”,“ rb”)为f:

〜/ anaconda3 / envs / myenv / lib / python3.8 / site-packages / keras / engine / saving.py 在load_wrapper(* args,** kwargs)中 490 os.remove(tmp_filepath) 491返回res -> 492 return load_function(* args,** kwargs) 493 494返回load_wrapper

〜/ anaconda3 / envs / myenv / lib / python3.8 / site-packages / keras / engine / saving.py 在load_model(文件路径,custom_objects,编译) 582如果H5Dict.is_supported_type(filepath): 583,H5Dict(filepath,mode ='r')为h5dict: -> 584模型= _deserialize_model(h5dict,custom_objects,编译) 第585章(1) 586 def load_function(h5file):

〜/ anaconda3 / envs / myenv / lib / python3.8 / site-packages / keras / engine / saving.py 在_deserialize_model中(h5dict,custom_objects,编译) 272提高ValueError('在配置中找不到模型。') 273模型_配置= json.loads(模型_配置。解码('utf-8')) -> 274模型= model_from_config(model_config,custom_objects = custom_objects) 275 model_weights_group = h5dict ['model_weights'] 276

〜/ anaconda3 / envs / myenv / lib / python3.8 / site-packages / keras / engine / saving.py 在model_from_config(config,custom_objects)中 625'Sequential.from_config(config)?') 626从..layers导入反序列化 -> 627返回反序列化(config,custom_objects = custom_objects) 628 629

〜/ anaconda3 / envs / myenv / lib / python3.8 / site-packages / keras / layers / init .py 在反序列化(config,custom_objects)中 163个globs ['Model'] = models.Model 164个globs ['Sequential'] =模型。Sequential -> 165 return deserialize_keras_object(config, 166 module_objects = globs, 167 custom_objects = custom_objects,

〜/ anaconda3 / envs / myenv / lib / python3.8 / site-packages / keras / utils / generic_utils.py 在deserialize_keras_object(identifier,module_objects, custom_objects,printable_module_name) 142 custom_objects = custom_objects或{} 143如果has_arg(cls.from_config,'custom_objects'): -> 144返回cls.from_config( 145 config ['config'], 146 custom_objects = dict(list(_GLOBAL_CUSTOM_OBJECTS.items())+

〜/ anaconda3 / envs / myenv / lib / python3.8 / site-packages / keras / engine / network.py in from_config(cls,config,custom_objects)1054#首先, 我们创建所有图层并排队处理1055个节点
对于config ['layers']中的layer_data: -> 1056 process_layer(layer_data)1057 1058#然后,我们按照层深度的顺序处理节点。

〜/ anaconda3 / envs / myenv / lib / python3.8 / site-packages / keras / engine / network.py 在..layers导入的process_layer(layer_data)1039中 反序列化为deserialize_layer 1040 -> 1041层=反序列化图层(layer_data,1042 custom_objects = custom_objects)1043
created_layers [layer_name] =图层

〜/ anaconda3 / envs / myenv / lib / python3.8 / site-packages / keras / layers / init .py 在反序列化(config,custom_objects)中 163个globs ['Model'] = models.Model 164个globs ['Sequential'] =模型。Sequential -> 165 return deserialize_keras_object(config, 166 module_objects = globs, 167 custom_objects = custom_objects,

〜/ anaconda3 / envs / myenv / lib / python3.8 / site-packages / keras / utils / generic_utils.py 在deserialize_keras_object(identifier,module_objects, custom_objects,printable_module_name) 142 custom_objects = custom_objects或{} 143如果has_arg(cls.from_config,'custom_objects'): -> 144返回cls.from_config( 145 config ['config'], 146 custom_objects = dict(list(_GLOBAL_CUSTOM_OBJECTS.items())+

〜/ anaconda3 / envs / myenv / lib / python3.8 / site-packages / keras / engine / network.py 在from_config(cls,config,custom_objects)中1073
node_data = node_data_list [node_index] 1074
尝试: -> 1075 process_node(layer,node_data)1076 1077#如果节点没有全部 入站层

〜/ anaconda3 / envs / myenv / lib / python3.8 / site-packages / keras / engine / network.py 在process_node(layer,node_data)1023#中并进行构建 该图层(如果需要)。如果输入张量为1024,则: -> 1025层(unpack_singleton(input_tensors),** kwargs)1026 1027 def process_layer(layer_data):

〜/ anaconda3 / envs / myenv / lib / python3.8 / site-packages / keras / backend / tensorflow_backend.py 在symbolic_fn_wrapper(* args,** kwargs)中 73如果_SYMBOLIC_SCOPE.value: 74与get_graph()。as_default(): ---> 75 return func(* args,** kwargs) 其他76个: 77 return func(* args,** kwargs)

〜/ anaconda3 / envs / myenv / lib / python3.8 / site-packages / keras / engine / base_layer.py 在通话中(自己,输入内容,**) 487#实际调用该图层, 488#收集输出,蒙版和形状。 -> 489输出= self.call(输入,** kwargs) (490)第490章 491

〜/ anaconda3 / envs / myenv / lib / python3.8 / site-packages / keras / layers / core.py 通话中(自己,输入,掩码) 714其他: 715 self._input_dtypes = K.dtype(输入) -> 716返回self.function(inputs,** arguments) 717 718 def compute_mask(self,input,mask = None):

〜/ anaconda3 / envs / myenv / lib / python3.8 / site-packages / keras / layers / core.py 在采样(参数)

NameError:名称“ k”未定义

K 来自keras导入后端的 k 。即使您添加此导入,我也有同样的错误。有人知道如何解决此问题吗?

0 个答案:

没有答案