变分自动编码器(keras)中的新错误



我曾经用这个代码来训练变分自动编码器(我在论坛上找到了这个代码,并根据我的需求进行了调整):

import pickle
from pylab import mpl,plt
#lecture des résultats
filename=r'XXX.pic'
data_file=open(filename,'rb')
X_sec = pickle.load(data_file)#[:,3000:]
data_file.close()
size=X_sec.shape[0]
prop=0.75
cut=int(size*prop)
X_train=X_sec[:cut]
X_test=X_sec[cut:]
std=X_train.std()
X_train /= std
X_test /= std
import keras
from keras import layers
from keras import backend as K
from keras.models import Model
import numpy as np
#encoding_dim = 12
sig_shape = (3600,)
batch_size = 128
latent_dim = 12
input_sig = keras.Input(shape=sig_shape)
x = layers.Dense(128, activation='relu')(input_sig)
x = layers.Dense(64, activation='relu')(x)
shape_before_flattening = K.int_shape(x)
x = layers.Dense(32, activation='relu')(x)
z_mean = layers.Dense(latent_dim)(x)
z_log_var = layers.Dense(latent_dim)(x)
encoder=Model(input_sig,[z_mean,z_log_var])

def sampling(args):
z_mean, z_log_var = args
epsilon = K.random_normal(shape=(K.shape(z_mean)[0], latent_dim),
mean=0., stddev=1.)
return z_mean + K.exp(z_log_var) * epsilon
z = layers.Lambda(sampling)([z_mean, z_log_var])
decoder_input = layers.Input(K.int_shape(z)[1:])
x = layers.Dense(np.prod(shape_before_flattening[1:]),activation='relu')(decoder_input)
x = layers.Reshape(shape_before_flattening[1:])(x)
x = layers.Dense(128, activation='relu')(x)
x = layers.Dense(3600, activation='linear')(x)
decoder = Model(decoder_input, x)
z_decoded = decoder(z)
class CustomVariationalLayer(keras.layers.Layer):
def vae_loss(self, x, z_decoded):
x = K.flatten(x)
z_decoded = K.flatten(z_decoded)
xent_loss = keras.metrics.mae(x, z_decoded)
kl_loss = -5e-4 * K.mean(
1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
return K.mean(xent_loss + kl_loss)
def call(self, inputs):
x = inputs[0]
z_decoded = inputs[1]
loss = self.vae_loss(x, z_decoded)
self.add_loss(loss, inputs=inputs)
return x
y = CustomVariationalLayer()([input_sig, z_decoded])
vae = Model(input_sig, y)
vae.compile(optimizer='rmsprop', loss=None)
vae.summary()
vae.fit(x=X_train, y=None,shuffle=True,epochs=100,batch_size=batch_size,validation_data=(X_test, None))

它过去工作很顺利,但我已经更新了我的库,现在我得到了这个错误:

文件"C: \Users\user\AppData\Local\conda\conda\envs\my_root\lib\site packages\tensorflow_core\python\framework\ops.py",第1619行,在_create_c_op中c_op=c_api.TF_FinishOperation(op_desc)

InvalidArgumentError:图中的节点名称重复:'lambda_1/random_normal/shape'

在处理上述异常的过程中,发生了另一个异常:

追踪(最近一次通话):

文件"I: \Documents\Nico\Python\finance\travail_amont\autoencoder_variationnel_bruit.py";,第74行,inz=层。Lambda(采样)([z_mean,z_log_var])

文件"C: \Users\user\AppData\Local\conda\conda\envs\my_root\lib\site packages\keras\backend\tensorflow_backend.py",第75行,在symbolic_fn_wrapper中return func(*args,**kwargs)

文件"C: \Users\user\AppData\Local\conda\conda\envs\my_root\lib\site packages\keras\engine\base_layer.py";,第506行,在调用output_shape=self.compute_output_shape(input_shape)中

文件"C: \Users\user\AppData\Local\conda\conda\envs\my_root\lib\site packages\keras\layers\core.py",第674行,compute_output_shapex=自身调用(xs)

文件"C: \Users\user\AppData\Local\conda\conda\envs\my_root\lib\site packages\keras\layers\core.py",716线,呼叫中return self.function(输入,**参数)

文件"I: \Documents\Nico\Python\finance\travail_amont\autoencoder_variationnel_bruit.py";,第71行,采样中平均值=0.,标准偏差=1.)

文件"C: \Users\user\AppData\Local\conda\conda\envs\my_root\lib\site packages\keras\backend\tensorflow_backend.py",第4329行,随机_正常shape,mean=mean,stddev=stddev,dtype=dtype,seed=seed)

文件"C: \Users\user\AppData\Local\conda\conda\envs\my_root\lib\site packages\tensorflow_core\python\keras\backend.py",第5602行,随机_正常shape,mean=mean,stddev=stddev,dtype=dtype,seed=seed)

文件"C: \Users\user\AppData\Local\conda\conda\envs\my_root\lib\site packages\tensorflow_core\python\ops\random_ops.py",第69行,随机_正常shape_tensor=tensor_util.shape_sensor(形状)

文件"C: \Users\user\AppData\Local\conda\conda\envs\my_root\lib\site packages\tensorflow_core\python\framework\tensor_util.py",第994行,在shape_tensor中return ops.convert_to_sensor(shape,dtype=dtype,name="shape")

文件"C: \Users\user\AppData\Local\conda\conda\envs\my_root\lib\site packages\tensorflow_core\python\framework\ops.py",行1314,在convert_to_sensor中ret=conversion_fc(值,dtype=dtype,name=name,as_ref=as_ref)

文件"C: \Users\user\AppData\Local\conda\conda\envs\my_root\lib\site packages\tensorflow_core\python\ops\array_ops.py",第1368行,在_autopacking_conversion_function中return _autopacking_helper(v,数据类型,名称或"packed")

文件"C: \Users\user\AppData\Local\conda\conda\envs\my_root\lib\site packages\tensorflow_core\python\ops\array_ops.py",第1304行,在_autopacking_helper中返回gen_array_ops.pack(elems_as_tensor,name=scope)

文件"C: \Users\user\AppData\Local\conda\conda\envs\my_root\lib\site packages\tensorflow_core\python\ops\gen_array_ops.py",5704行,包装中"包装";,values=values,axis=axis,name=name)

文件"C: \Users\user\AppData\Local\conda\conda\envs\my_root\lib\site packages\tensorflow_core\python\framework\op_def_library.py",第742行,在_apply_op_helper中attrs=attr_protos,op_def=op_def)

文件"C: \Users\user\AppData\Local\conda\conda\envs\my_root\lib\site packages\tensorflow_core\python\framework\func_graph.py",第595行,在_create_op_internal中计算机设备)

文件"C: \Users\user\AppData\Local\conda\conda\envs\my_root\lib\site packages\tensorflow_core\python\framework\ops.py",第3322行,在_create_op_internal中op_def=op_def)

文件"C: \Users\user\AppData\Local\conda\conda\envs\my_root\lib\site packages\tensorflow_core\python\framework\ops.py",第1786行,initcontrol_input_ops)

文件"C: \Users\user\AppData\Local\conda\conda\envs\my_root\lib\site packages\tensorflow_core\python\framework\ops.py",第1622行,在_create_c_op中提升值错误(str(e))

ValueError:图形中的节点名称重复:'lambda_1/random_normal/shape'

我不知道这个错误:"图中的节点名称重复;。有人知道线索吗?谢谢

如果使用的是tf 2.x,则按如下方式导入keras模块。

from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.kerasimport backend as K
from tensorflow.keras.models import Model

与此相关的更多信息,#36509,#130

相关内容

  • 没有找到相关文章

最新更新