简体   繁体   English

ValueError:密集层的输入 0 与层不兼容:预期轴 -1。 tensorflow

[英]ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1. tensorflow

I want to use the Conditional Variational AutoEncoder, here is my dataset.我想使用条件变分自动编码器,这是我的数据集。 My inputs is a time-series data with a shape 1000 20, and my output is 50 5. when I try to train the model, this error appears.我的输入是形状为 1000 20 的时间序列数据,我的 output 是 50 5。当我尝试训练 model 时,会出现此错误。 Could you help me to solve that?你能帮我解决这个问题吗?

import numpy as np
from keras.layers import Input, Dense, Lambda, concatenate
from keras.models import Model
from keras import backend as K
from keras import objectives
from keras.utils import to_categorical
from scipy.stats import norm

x_tr    = np.random.rand(1000, 20)
x_te     = np.random.rand(50, 20)
y_tr    = np.random.rand(1000, 5)
y_te     = np.random.rand(50,  5)

batch_size, n_epoch = 50, 50
n_hidden, z_dim = 512, 2

x = Input(shape=(x_tr.shape[1:]))
condition = Input(shape=(y_tr.shape[1],))

inputs = concatenate([x, condition])
x_encoded = Dense(n_hidden, activation='relu')(inputs)
x_encoded = Dense(n_hidden//2, activation='relu')(x_encoded)
mu = Dense(z_dim, activation='linear')(x_encoded)
log_var = Dense(z_dim, activation='linear')(x_encoded)

# sampling function
def sampling(args):
    mu, log_var = args
    eps = K.random_normal(shape=(batch_size, z_dim), mean=0., stddev=1.0)
    return mu + K.exp(log_var/2.) * eps

z = Lambda(sampling, output_shape=(z_dim,))([mu, log_var])
z_cond = concatenate([z, condition])

z_decoder1 = Dense(n_hidden//2, activation='relu')
z_decoder2 = Dense(n_hidden, activation='relu')
y_decoder = Dense(x_tr.shape[1], activation='sigmoid')

z_decoded = z_decoder1(z_cond)
z_decoded = z_decoder2(z_decoded)
y = y_decoder(z_decoded)

# loss
reconstruction_loss = objectives.binary_crossentropy(x, y) * x_tr.shape[1]
kl_loss = 0.5 * K.sum(K.square(mu) + K.exp(log_var) - log_var - 1, axis = -1)
cvae_loss = reconstruction_loss + kl_loss

# build model
cvae = Model([x, condition], y)
cvae.add_loss(cvae_loss)
cvae.compile(optimizer='adam')
cvae.fit([x_tr, y_tr],  epochs=n_epoch, batch_size=batch_size, validation_data=([x_te, y_te], None), verbose=1)```

here is the error

``` ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 40 but received input with shape [50, 4]``` 

you just messed up the dimensions.你只是搞砸了尺寸。

Just change只是改变

x_tr    = np.random.rand(1000, 20)
x_te     = np.random.rand(50, 20)
y_tr    = np.random.rand(1000, 5)
y_te     = np.random.rand(50,  5)

to

x_tr    = np.random.rand(1000, 20)
y_tr    = np.random.rand(1000, 5)
x_te     = np.random.rand(50, 20)
y_te     = np.random.rand(50,  5)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:密集层的输入 0 与层不兼容:预期轴 -1 的值为 8,但接收到形状为 [None, 1] 的输入 - ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 to have value 8 but received input with shape [None, 1] ValueError:层密集的输入 0 与层不兼容::预期 min_ndim=2,发现 ndim=1。 收到的完整形状:[无] - ValueError: Input 0 of layer dense is incompatible with the layer: : expected min_ndim=2, found ndim=1. Full shape received: [None] ValueError: 层密集的输入 0 与层不兼容 - ValueError: Input 0 of layer dense is incompatible with the layer ValueError: 层dense_1的输入0与层不兼容 - ValueError: Input 0 of layer dense_1 is incompatible with the layer ValueError:dense_24 层的输入 0 不兼容:输入形状的预期轴 -1 具有值 1024,但接收到形状为 [16, 512] 的输入 - ValueError: Input 0 of layer dense_24 is incompatible: expected axis -1 of input shape to have value 1024 but received input with shape [16, 512] TensorFlow ValueError:层顺序的输入0与层不兼容 - TensorFlow ValueError: Input 0 of layer sequential is incompatible with the layer 层密集的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3,但收到的输入形状为 (None, 1) - Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape (None, 1) 层“dense3”的输入 0 与层不兼容:预期 min_ndim=2,发现 ndim=1。 收到的完整形状:(4096,) - Input 0 of layer "dense3" is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (4096,) 'ValueError:层顺序的输入0与层不兼容:输入形状的预期轴-1具有值3但接收到输入 - 'ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input ValueError:层顺序的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3,但接收到的输入具有形状 - ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM