Keras CNN for non-image matrix

倾然丶 夕夏残阳落幕 提交于 2020-12-12 10:53:23

问题


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:


回答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 -

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.





回答2:


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)


来源:https://stackoverflow.com/questions/64866833/keras-cnn-for-non-image-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!