Search code examples
pythonkerasneural-networkkeras-layer

locally connected layer on the columns of an input matrix using keras in python


I have a 3D dataset with dimensions of [10000, k=5, m=1024] (10000 is the number of data) and I want to train a network which has a locally connected layer at the first layer. I have shown the first layer in the following image. How can I implement this network using keras in python?

enter image description here


Solution

  • I can solve the problem in an easier way. Suppose we have a 3D dataset with dimensions of [10000, 5, 1024]. I have first reshaped it to a matrix in which that for the inputs between 0 to 10000 in the original dataset, I have placed the 5 elements in the columns of the matrix [5, 1024] together, using the following code:

    mat =  np.reshape(mat,(mat.shape[0],mat.shape[1]*mat.shape[2]),'F')
    

    So the mat matrix has [10000, 5120] dimensions. Finally I have used the LocallyConnected1D predefined layer in tensorflow.keras:

    model.add(tf.keras.layers.LocallyConnected1D(filters=1, kernel_size=5, strides=5, data_format='channels_first', input_shape=(1,5120), activation='relu'))