我正在使用python Keras运行VAE模型。 我在安装自动编码器模型时遇到错误(这里命名为"vae"(。
错误:
tensorflow.python.framework.errors_impl。无效参数错误:不兼容的形状:[128,784] 与 [96,784]
但我认为这两种形状在这里是相同的,如下所示。 如果有人能给我一些建议,这将非常有帮助。
我的代码:
from keras.layers import Lambda, Input, Dense
from keras.models import Model
from keras.datasets import mnist
from keras.losses import mse, binary_crossentropy
from keras.utils import plot_model
from keras import backend as K
original_dim = 784 #28 * 28
input_shape = (original_dim, )
intermediate_dim = 512
batch_size = 128
latent_dim = 2
epochs = 50
epsilon_std = 1.0
#encoding
x = Input(batch_shape=(batch_size, original_dim))
h = Dense(intermediate_dim, activation = 'relu')(x) #h = hidden layer
#creating latent variable = z
z_mean = Dense(latent_dim)(h) #mean of latent variable (2 dimension)
z_log_sigma = Dense(latent_dim)(h) #log_sigma of latent variable (2 dimension)
def sampling(args):
z_mean, z_log_sigma = args
epsilon = K.random_normal(shape=(batch_size, latent_dim),
mean=0.,
#std=epsilon_std
)
return(z_mean + K.exp(z_log_sigma) * epsilon)
z = Lambda(sampling, output_shape = (latent_dim,))([z_mean, z_log_sigma])
#encoder
encoder = Model(x, z_mean)
#decoding with z
decoder_h = Dense(intermediate_dim, activation='relu')
decoder_mean = Dense(original_dim, activation='sigmoid')
h_decoded = decoder_h(z)#input encoded z
x_decoded_mean = decoder_mean(h_decoded)
#autoencoder
vae = Model(x, x_decoded_mean)
#decoding to generate images
decoder_input = Input(shape=(latent_dim,))
_h_decoded = decoder_h(decoder_input)
_x_decoded_mean = decoder_mean(_h_decoded)
#generator
generator = Model(decoder_input, _x_decoded_mean)
#training
def vae_loss(x,x_decoded_mean):
xent_loss = binary_crossentropy(x,x_decoded_mean)
kl_loss = -0.5 * K.mean(1+z_log_sigma-K.square(z_mean)-K.exp(z_log_sigma), axis=-1)
return(xent_loss+kl_loss)
vae.compile(optimizer = 'rmsprop', loss=vae_loss)
#creating dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))
print('x_train', x_train.shape)
print('x_test', x_test.shape)
vae.fit(x_train, x_train,
shuffle=True,
epochs=epochs,
batch_size=batch_size,
validation_data=(x_test, x_test))
给定您的代码,您似乎正在对模型中的批大小进行硬编码,这通常是一个坏主意。
由于您将输入定义为x = Input(batch_shape=(batch_size, original_dim))
,因此网络期望数据完全符合形状(128, 784)
。如果您的输入数据不完全是 128 的倍数,这是有问题的,因为vae.fit()
会将数据分成批拆分,对于最后一批,如果它没有足够的数据来填充完整的批次,它将发送部分数据。看来,您的上一批只有 96 个样本而不是 128 个样本,因此模型会引发形状不匹配错误。
正确的方法是:不要在模型中的任何位置指定批大小,而只将参数传递给fit
。实际上,这归结为永远不要使用 Keras 层的batch_shape
参数,而是使用shape
并以一个样本的形式传递:
x = Input(shape=(original_dim,))
如果您有任何需要指定完整形状(即包括批大小(的操作,请使用None
:
epsilon = K.random_normal(shape=(None, latent_dim),
mean=0.,
#std=epsilon_std
)