【问题标题】:Python/TensorFlow/Keras - Input to reshape is a tensor with 300 values, but the requested shape has 200 [[{{node decoder_1/reshape_1/Reshape}}]]Python/TensorFlow/Keras - reshape 的输入是一个具有 300 个值的张量,但请求的形状有 200 个 [[{{node decoder_1/reshape_1/Reshape}}]]
【发布时间】:2019-12-01 04:29:44
【问题描述】:

我想将我的数据从 2d 转换为 3d,因为我创建了自动编码器,其中代码(隐藏层)有 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}}]]

【问题讨论】:

    标签: python tensorflow keras autoencoder tf.keras


    【解决方案1】:

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

    原因是 layers.Reshape((1,2)) 的输入应该是 (100, 2) 的形状(在你的情况下,100 是批量大小)但是你输入的是形状为 (100, 3) 的张量,因此会出现错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-25
      • 2023-01-30
      • 2017-06-26
      相关资源
      最近更新 更多