自动编码器网络中的数组形状

Array shape in an autoencoder network

我正在尝试使自动编码器网络适合包含多维数组的数据集,但我的自动编码器的解码器部分中的层形状存在问题。 我网络的输入数据包含形状为 (1,100,4) 的固定长度段,因此总共包含 (m, 1,100,4) 用于 m 观察。

为了提供 MWE,我生成了以下类似于输入数据形状的数据。

#generate dummy data
X = np.random.randn(20, 1, 100, 4)
a,b,c = np.repeat(0, 7), np.repeat(1, 7), np.repeat(2, 6)
y = np.hstack((a,b,c))

X.shape
(20, 1, 100, 4)

x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=7)

这是我的网络代码:

class SingleEncoder:

    def __init__(self, train, test):
        self.x_train = train
        self.x_test = test
        self.first_dim = 1
        self.second_dim = 100
        self.channels = 4
        self.input_dim = (self.first_dim, self.second_dim, self.channels)

    def setSingleModel(self):
        input_layer = self.input_dim
        autoencoder = Sequential()
        activ='relu'

        # encoder
        autoencoder.add(Flatten(input_shape=input_layer))
        autoencoder.add(Dense(200,  activation='relu')) 
        autoencoder.add(Dense(100,  activation='relu')) 
        autoencoder.add(Dense(80,  activation='linear'))   
        
        #decoder
        autoencoder.add(Dense(80, activation='linear'))  
        autoencoder.add(Dense(100, activation='relu')) 
        autoencoder.add(Dense(200, activation='relu'))
        #autoencoder.add(Reshape(input_layer))   

        autoencoder.compile(optimizer='adam', loss='mae', metrics=['mean_squared_error'])
        autoencoder.summary()

        filepath = "weights.best.hdf5"
        checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='max')
        callbacks_list = [checkpoint]

        autoencoder.fit(self.x_train, self.x_train, epochs=250, batch_size=256, shuffle=True,callbacks=callbacks_list)

        return autoencoder
    

模型摘要:

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten_1 (Flatten)          (None, 400)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 200)               80200     
_________________________________________________________________
dense_2 (Dense)              (None, 100)               20100     
_________________________________________________________________
dense_3 (Dense)              (None, 80)                8080      
_________________________________________________________________
dense_4 (Dense)              (None, 80)                6480      
_________________________________________________________________
dense_5 (Dense)              (None, 100)               8100      
_________________________________________________________________
dense_6 (Dense)              (None, 200)               20200     
=================================================================
Total params: 143,160
Trainable params: 143,160
Non-trainable params: 0
_________________________________________________________________

所以创建一个自动编码器对象会产生我不知道如何解决的错误:

autoencoder = SingleEncoder(x_train, x_test)
autoencoder = autoencoder.setSingleModel()

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-7c9d08768298> in <module>()
      1 autoencoder = SingleEncoder(x_train, x_test)
----> 2 autoencoder = autoencoder.setSingleModel()

3 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    133                         ': expected ' + names[i] + ' to have ' +
    134                         str(len(shape)) + ' dimensions, but got array '
--> 135                         'with shape ' + str(data_shape))
    136                 if not check_batch_axis:
    137                     data_shape = data_shape[1:]

ValueError: Error when checking target: expected dense_6 to have 2 dimensions, but got array with shape (16, 1, 100, 4)

有人可以帮忙解决这个问题吗?

这是最简单的方法...删除第一个位置的展平,这可能会导致一些形状问题,因为您正在从 4D 过渡到 2D 而您的目标仍然是 4D。使用解码器中与输入维度相匹配的最后一层

class SingleEncoder:

    def __init__(self, X):
        self.X = X
        self.first_dim = 1
        self.second_dim = 100
        self.channels = 4
        self.input_dim = (self.first_dim, self.second_dim, self.channels)

    def setSingleModel(self):
        input_layer = self.input_dim
        autoencoder = Sequential()
        activ='relu'

        # encoder
        autoencoder.add(Dense(200,  activation='relu', input_shape=input_layer)) 
        autoencoder.add(Dense(100,  activation='relu')) 
        autoencoder.add(Dense(80,  activation='linear'))   
        
        #decoder
        autoencoder.add(Dense(80, activation='linear'))  
        autoencoder.add(Dense(100, activation='relu')) 
        autoencoder.add(Dense(200, activation='relu'))
        autoencoder.add(Dense(self.channels, activation='relu'))

        autoencoder.compile(optimizer='adam', loss='mae', 
                            metrics=['mean_squared_error'])
        autoencoder.summary()

        autoencoder.fit(self.X, self.X, epochs=3, batch_size=32)

        return autoencoder

X = np.random.randn(20, 1, 100, 4)

autoencoder = SingleEncoder(X)
autoencoder = autoencoder.setSingleModel()