【问题标题】:Input Shape in Keras AutoencoderKeras 自动编码器中的输入形状
【发布时间】:2021-07-10 19:13:56
【问题描述】:

我正在尝试在以下代码中训练自动编码器:

encoder_input = keras.layers.Input(shape=(x_Train.shape[1]), name='img')
encoder_out = keras.layers.Dense(1, activation = "relu")(encoder_input)

encoder = keras.Model(encoder_input, encoder_out, name="encoder")

decoder_input = keras.layers.Dense(602896, activation = "relu")(encoder_out)
decoder_output = keras.layers.Reshape((769, 28, 28))(decoder_input)

opt = keras.optimizers.RMSprop(learning_rate=1e-3)

autoencoder = keras.Model(encoder_input, decoder_output, name = "autoencoder")
autoencoder.summary()

autoencoder.compile(opt, loss='mse')
autoencoder.fit(x_Train, x_Train, epochs=10, batch_size=64, validation_split = 0.1)

但是,它返回错误: “tensorflow:模型是用形状 (None, 28) 构建的输入 KerasTensor(type_spec=TensorSpec(shape=(None, 28), dtype=tf.float32, name='img'), name='img', description= "由层 'img' 创建"),但它是在形状不兼容的输入上调用的 (None, 28, 28)。"

我不知道如何处理或调整我的输入。我的 x_train 是一个大小为 [769,28,28] 的向量

有人可以帮我处理错误吗?

That's the summary

谢谢

【问题讨论】:

    标签: python tensorflow keras autoencoder


    【解决方案1】:

    您的自动编码器的输入形状有点奇怪,您的训练数据的形状为 28x28,批次为 769,因此修复应该是这样的:

    encoder_input = keras.layer.Input(shape=(28, 28), name='img')
    encoder_out = keras.layers.Dense(1, activation = "relu")(encoder_input)
    
    # For ur decoder, you need to change a bit as well
    decoder_input = keras.layers.Dense(784, activation = "sigmoid")(encoder_out) # Flatten until 28x28 =784
    decoder_output = keras.layers.Reshape((28, 28))(decoder_input) # From there reshape back to 28x28
    

    【讨论】:

    • 我仍然得到一个错误,在答案中指定
    【解决方案2】:

    我按照您的建议进行了更正,但它仍然返回以下错误:

    ValueError                                Traceback (most recent call last)
    <ipython-input-48-78f8cac6c724> in <module>()
          6 
          7 decoder_input = keras.layers.Dense(784, activation = "sigmoid")(encoder_out)
    ----> 8 decoder_output = keras.layers.Reshape((28, 28))(decoder_input)
          9 
         10 opt = keras.optimizers.RMSprop(learning_rate=1e-3)
    
    6 frames
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/core.py in _fix_unknown_dimension(self, input_shape, output_shape)
        534       output_shape[unknown] = original // known
        535     elif original != known:
    --> 536       raise ValueError(msg)
        537     return output_shape
        538 
    
    ValueError: total size of new array must be unchanged, input_shape = [28, 784], output_shape = [28, 28]
    

    代码:

    encoder_input = keras.layers.Input(shape=(28,28), name='img')
    encoder_out = keras.layers.Dense(1, activation = "relu")(encoder_input)
    
    encoder = keras.Model(encoder_input, encoder_out, name="encoder")
    
    decoder_input = keras.layers.Dense(784, activation = "sigmoid")(encoder_out)
    decoder_output = keras.layers.Reshape((28, 28))(decoder_input)
    
    opt = keras.optimizers.RMSprop(learning_rate=1e-3)
    
    autoencoder = keras.Model(encoder_input, decoder_output, name = "autoencoder")
    autoencoder.summary()
    

    【讨论】:

      猜你喜欢
      • 2019-10-19
      • 1970-01-01
      • 2017-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多