本地连接的 1D 自动编码器在 keras 中



我正在尝试通过重新利用本教程中的"最简单的"密集自动编码器来在 Keras 中创建 LocalConnected1D 自动编码器。

我一直收到以下错误,我相信这是由我的input_shape尺寸引起的。

Traceback (most recent call last):
File "localdendritic.py", line 38, in <module>
kernel_size=6)
File "localdendritic.py", line 15, in __init__
activation='relu')(input_placeholder)
File "/Users/me/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py", line 573, in __call__
self.assert_input_compatibility(inputs)
File "/Users/me/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py", line 472, in assert_input_compatibility
str(K.ndim(x)))
ValueError: Input 0 is incompatible with layer encoded_layer: expected ndim=3, found ndim=2

我的代码如下。我尝试将input_shape数组更改为[None, 1, input_size][1, 1, input_size][1, input_size][None, input_size],但它似乎没有改变任何东西。我想我缺少一些关于输入形状的见解。

import numpy as np
from keras.models import Model, Sequential
from keras.layers import Input, LocallyConnected1D

class Localautoencoder:
  def __init__(self, input_size, encoded_size, kernel_size, **kwargs):
    input_shape = np.array([input_size])
    input_placeholder = Input(shape=(input_size, 1))
    encoded = LocallyConnected1D(encoded_size, kernel_size,
                                 input_shape=input_shape,
                                 name='encoded_layer',
                                 activation='relu')(input_placeholder)
    decoded = LocallyConnected1D(input_size, kernel_size,
                                activation='sigmoid',
                                name='decoded_layer')(encoded)
    self.localae = Model(input_placeholder, decoded)
    self.encoder = Model(input_placeholder, encoded)
    encoded_input = Input(shape=(1, encoded_size))
    decoded_layer = self.localae.layers[-1]
    self.decoder = Model(encoded_input, decoded_layer(encoded_input))
    self.localae.compile(optimizer='adam', loss='binary_crossentropy')

from keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.astype('float32')/255.
x_test = x_test.astype('float32')/255.
x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))
print(x_train.shape)
print(x_test.shape)
lae = Localautoencoder(input_size=x_train.shape[1],
                   encoded_size=100,
                   kernel_size=6)

LocallyConnected1D层采用三维输入,但input_placeholder只是二维输入。解决此问题的方法是添加一个Reshape图层,该图层将 2D 输入转换为 3D 输入。

相关内容

  • 没有找到相关文章

最新更新