繁体   English   中英

Python / TensorFlow / Keras-要整形的输入是具有300个值的张量,但请求的形状具有200 [[{{nodecoder_1 / reshape_1 / Reshape}}]]]

[英]Python/TensorFlow/Keras - Input to reshape is a tensor with 300 values, but the requested shape has 200 [[{{node decoder_1/reshape_1/Reshape}}]]

我想将数据从2d转换为3d,因为它创建了Autoencoder,其中代码(隐藏层)具有3个神经元。 训练开始时会引发异常。

import numpy as np
import tensorflow as tf
from tensorflow.keras import layers
from sklearn.datasets import make_circles

input_vector = layers.Input(shape=(1,2))
encoded = layers.Dense(3,activation="relu")(input_vector)

input_encoded = layers.Input(shape=(3,))
x = layers.Dense(3,activation="relu")(input_encoded)
decoded = layers.Reshape((1,2))(x)

encoder = tf.keras.Model(input_vector, encoded, name="encoder")
decoder = tf.keras.Model(input_encoded, decoded, name="decoder")
autoencoder = tf.keras.Model(input_vector, decoder(encoder(input_vector)), name="autoencoder")

autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

count = 1000
X, y = make_circles(n_samples=count, noise=0.05)
x_test, y = make_circles(n_samples=count, noise=0.05)

X = np.reshape(X,(count,1,2))
x_test = np.reshape(x_test,(count,1,2))

autoencoder.fit(X, X,
                epochs=5,
                batch_size=100,
                shuffle=True,
                validation_data=(x_test, x_test))

实际结果抛出异常

---------------------------------------------------------------------------
InvalidArgumentError: Input to reshape is a tensor with 300 values, but the requested shape has 200
     [[{{node decoder_1/reshape_1/Reshape}}]]

x = layers.Dense(3,activation="relu")(input_encoded)x = layers.Dense(2,activation="relu")(input_encoded)将解决您的问题。

原因是输入到layers.Reshape((1,2))的形状应该是layers.Reshape((1,2)) (100, 2) (在您的情况下100是批处理大小),但是您要输入形状(100, 3) layers.Reshape((1,2))张量,因此,错误。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM