0

I have recently started learning about Deep Learning and Reinforcement Learning, and I am trying to figure out how to code a Convolutional Neural Network using Keras for a matrix of 0s and 1s with 10 rows and 3 columns.

The input matrix would look like this for example

[
 [1, 0, 0], 
 [0, 1, 0], 
 [0, 0, 0], 
 ...
]

The output should be another matrix of 0s and 1s, different from the aforementioned input matrix and with a different number of rows and columns.

The location of 0s and 1s in the output matrix is dependent on the location of the 0s and 1s in the input matrix.

There is also a second output, an array where the values are dependent on the location of the 1 in the input matrix.

I have searched the internet for code examples but couldn't find anything useful.

Edit:

The input to the neural network is a 2D array with 10 rows and each row has 3 columns. The output (for now at least) is a 2D array with 12 rows and each row has 10 columns (the same as the number of rows in the input 2D array).

This is what I came up with so far and I have no idea if it's correct or not.

nbre_users = 10 # number of rows in the input 2D matrix
nbre_subchannels = 12 # number of rows in the output 2D matrix

model = Sequential()
model.add(Dense(50, input_shape=(nbre_users, 3), kernel_initializer="he_normal" ,activation="relu"))
model.add(Dense(20, kernel_initializer="he_normal", activation="relu"))
model.add(Dense(5, kernel_initializer="he_normal", activation="relu"))
model.add(Flatten())
model.add(Dense(nbre_subchannels))
model.add(Dense(nbre_users, activation = 'softmax'))
model.compile(optimizer=Adam(learning_rate=1e-4), loss='mean_squared_error')

Here is the model summary:

Model Summary

Ness
  • 158
  • 1
  • 12
  • Welcome to Stack Overflow. Please read how to ask good [questions](https://stackoverflow.com/help/how-to-ask). Make sure your question covers these 3 elements: 1. Problem Statement 2. Your Code (it should be [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) 3. Error Message (preferably full Traceback to help others review and provide feedback). Sometimes the same question may have already been asked. Make sure your question is not a [duplicate](https://stackoverflow.com/help/duplicates) – Joe Ferndz Nov 16 '20 at 23:21
  • `The input to the neural network is a 2D array with 10 rows and each row has 3 columns. The output (for now at least) is a 2D array with 12 rows and each row has 10 columns (the same as the number of rows in the input 2D array).` When you say you are looking for a 2D output, are you just reshaping the flattened layer or you actually want to generate an image output? – Akshay Sehgal Nov 17 '20 at 12:09
  • It's not an image, it's just a 2D output with 0s and 1s. And I would like to generate it as a 2D matrix cause the position of the 1s and 0s is important. – Ness Nov 17 '20 at 12:14
  • A 2D matrix can be imagined as a grayscale image as well. Imagining that, I assume you are trying to provide a gray scale image (a 2d matrix of 1s and 0s) and outputing another gray scale image (another 2d matrix of 1s and 0s) right? AND what you want is that you use CNN to capture spatial features in the input in you neural network right? – Akshay Sehgal Nov 17 '20 at 12:15
  • Yes, what you said is correct. – Ness Nov 17 '20 at 12:17
  • Ok, so you dont need 1D convolution OR multi-input multi-output network. What you need is a network with convolution and deconvolution layers. Convolution will extract spatial features (positions of 1s and 0s) from your grayscale image and downsample the image to a flat list. Deconv layers will take this flat list and apply 2Dconv transposes to regenerate a 2D image map (2D matrix) – Akshay Sehgal Nov 17 '20 at 12:21
  • Check this image for intuition - https://miro.medium.com/max/1400/1*LW8Anre45o9nfamxIVTY8Q.png – Akshay Sehgal Nov 17 '20 at 12:23
  • Thank you so much for your help ^^ Is it possible to provide me with an example code or a link to a tutorial please ? – Ness Nov 17 '20 at 12:24
  • Check my answer, that should clear some stuff. – Akshay Sehgal Nov 17 '20 at 13:00

2 Answers2

1

You can use 1D convolutional layers if you want to convolve in a single spatial, which from what I understood what you want.

e.g.

# assuming 3x10 matrix with single batch
input_shape = (1, 3, 10)
y = tf.keras.layers.Conv1D(32, 3, activation='relu',input_shape=input_shape[1:])(x)
Menethan
  • 41
  • 6
  • Thank you for your answer. Bare with me a little. So I should add that instead of the 3 Dense layers before the Flatten layer ? – Ness Nov 17 '20 at 12:11
  • I dont think OP is looking for a 1D convolution. Its a 2D image with a single channel. Input itself is a 2D matrix. Your answer would be relevant if OP's input was 10 separate samples of 3 features. – Akshay Sehgal Nov 17 '20 at 12:13
  • Yes evan and if Akshay is right you should use 2D convolutional layers. If you are going to use conv2d just reshape your matrix to (batch, column, row, 1) last spatial is 1 because you will be using grayscale. See [here](https://keras.io/api/layers/convolution_layers/convolution2d/) – Menethan Nov 17 '20 at 12:29
1

After clarifications, here is my answer.

The problem you are trying to solve seems to be a neural network that transforms a 2D grayscale image of size (10,3,1) to a 2D grayscale image of size (12,10,1).

A 2D grayscale image is nothing but a 2D matrix with an extra axis set to 1.

a = np.array([[0,1,0],
              [1,0,1],
              [0,1,0]])

a.shape

#OUTPUT = (3,3)

a.reshape((3,3,1)) #reshape to 3,3,1

#OUTPUT - 
#array([[[0],
#        [1],
#        [0]],
#
#       [[1],
#        [0],
#        [1]],
#
#       [[0],
#        [1],
#        [0]]])

So a 2D matrix of (10,3) can be called a 3D image with a single channel (10,3,1). This will allow you to properly apply convolutions to your input.

If this part is clear, then in the forward computation of the network, since you want to ensure that spatial positions of the 1s and 0s are captured, you want to use convolution layers. Using Dense layers here is not the right step.

However, a series convolution operation help to Downsample and image. Since you need an output 2D matrix (gray scale image), you want to Upsample as well. Such a network is called a Deconv network.

The first series of layers convolve over the input, 'flattening' them into a vector of channels. The next set of layers use 2D Conv Transpose operations to change the channels back into a 2D matrix (Gray scale image)

Refer to this image for reference - enter image description here

Here is a sample code that shows you how you can take a (10,3,1) image to a (12,10,1) image using a deconv net.

from tensorflow.keras import layers, Model

inp = layers.Input((10,3,1))     ##
x = layers.Conv2D(2, (2,2))(inp) ##  Convolution part
x = layers.Conv2D(4, (2,2))(x)   ##

x = layers.Conv2DTranspose(4, (3,4))(x)   ##
x = layers.Conv2DTranspose(2, (2,4))(x)   ##  Deconvolution part
out = layers.Conv2DTranspose(1, (2,4))(x) ##

model = Model(inp, out)
model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_33 (InputLayer)        [(None, 10, 3, 1)]        0         
_________________________________________________________________
conv2d_49 (Conv2D)           (None, 9, 2, 2)           10        
_________________________________________________________________
conv2d_50 (Conv2D)           (None, 8, 1, 4)           36        
_________________________________________________________________
conv2d_transpose_46 (Conv2DT (None, 10, 4, 4)          196       
_________________________________________________________________
conv2d_transpose_47 (Conv2DT (None, 11, 7, 2)          66        
_________________________________________________________________
conv2d_transpose_48 (Conv2DT (None, 12, 10, 1)         17        
=================================================================
Total params: 325
Trainable params: 325
Non-trainable params: 0
_________________________________________________________________

Obviously, feel free to add activations, dropouts, pooling layers etc etc etc. The above code just shows how you can use downsampling and upsampling to get from a given single-channel image to another single-channel image.


On a side note - I would really advise that you spend some time understanding how CNNs work. Deconv nets are complex and if you are solving a problem that involves them, before properly understanding how 2D CNNs work, it may cause some foundational problems especially if you are starting to learn this domain.


Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51