[英]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: Input 0 of layer dense is incompatible with the layer: expected axis -1. tensorflow
我想使用條件變分自動編碼器,這是我的數據集。 我的輸入是形狀為 1000 20 的時間序列數據,我的 output 是 50 5。當我嘗試訓練 model 時,會出現此錯誤。 你能幫我解決這個問題嗎?
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]```
你只是搞砸了尺寸。
只是改變
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)
至
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.