1

I am trying to fit an autoencoder network to my dataset containing a multi-dimensional array, but having issues with the shape of a layer within the decoder part of my autoencoder. The input data to my network contains fixed-length segments of shape (1,100,4) so in total, it contains (m, 1,100,4) for m observations.

To provide an MWE, I generate the following data which resembles the shape of my input data.

#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)

Here's the code of my network:

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 summary:

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
_________________________________________________________________

So creating an autoencoder object generates the error that I cannot figure out how to resolve:

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)

Can someone help fix this?

0

1 Answer 1

2

this is the simplest way to do this... remove the flatten in the first position, this can cause you some shape problems because you are passing from 4D to 2D and your target is still 4D. use the last layer in your decoder which matches the input dimensionality

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.