我有一个简单的序列模型,我正在尝试向该模型添加一个变体层,我的代码是:
encoder_inputs = Input(shape=(None,))
encoder_emb = Embedding(input_dim=vocab_in_size, output_dim=embedding_dim)
encoder_lstm =LSTM(units=units,return_state=True)
encoder_outputs, state_h, state_c = encoder_lstm(encoder_emb(encoder_inputs))
################## VAE ##################################
latent_dim =256
# output layer for mean and log variance
z_mu = Dense(2)(state_h) #remplacer h
z_log_var = Dense(2)(state_h)
def sampling(args):
batch = K.shape(z_mu)[0]
dim = K.int_shape(z_mu)[1]
z_mean, z_log_sigma = args
epsilon = K.random_normal(shape=(batch,dim),
mean=0., stddev=1.)
return z_mean + K.exp(z_log_sigma/2) * epsilon
# note that "output_shape" isn't necessary with the TensorFlow backend
# so you could write `Lambda(sampling)([z_mean, z_log_sigma])`
z = Lambda(sampling, output_shape=(2,))([z_mu, z_log_var])
decoder_inputs = Input(shape=(None,))
inp2=decoder_inputs
decoder_emb = Embedding(input_dim=vocab_out_size, output_dim=embedding_dim)(decoder_inputs)
hidden=Dense(units,activation='relu')(z)
state_1 = Dense(units)(hidden)
state_2 = Dense(units)(hidden)
decoder_lstm= LSTM(units=units, return_sequences=True,activation='linear')
decoder_out=decoder_lstm(decoder_emb,[state_1, state_2])
class VariationalLayer(Layer):
"""
`Bottle Neck Layer`. This layer provides the kl-divergence loss function
"""
def __init__(self, **kwargs):
self.is_placeholder = True
self.target = Input(shape=(None,units)) # original time sequence shift once
super(VariationalLayer, self).__init__(**kwargs)
def vae_loss(self,x, x_decoded_mean):
x_decoded_mean=tf.reshape(x_decoded_mean,[-1,256])
self.xent_loss = K.mean(metrics.mean_squared_error(self.target, x_decoded_mean), axis=1) # sum error over time
self.kl_loss = -0.5 * K.sum(1 + z_log_var - K.square(z_mu) - K.exp(z_log_var), axis=-1)
return K.mean(self.xent_loss + self.kl_loss)
def call(self, inputs):
x = inputs[0]
x_decoded_mean = inputs[1]
loss = self.vae_loss(x, x_decoded_mean)
self.add_loss(loss, inputs=inputs)
# We won't actually use the output.
return x
# build model graph
vae_layer = VariationalLayer(name='loss_layer')
y = vae_layer([encoder_inputs, decoder_out])
vae = Model([encoder_inputs,vae_layer.target],y)
vae.compile(optimizer="rmsprop", loss=None)
我收到此错误:
ValueError:图形已断开:无法获得张量的值 层上的Tensor(“ input_112:0”,shape =(?,?),dtype = float32) “ input_112”。访问以下先前的层时没有 问题:['input_111','embedding_77','lstm_77','dense_192', 'dense_193','lambda_40','dense_194']
我是深度学习的初学者,我无法理解错在哪里。