manually setting connection between neurons of two dense layers in keras

36 views
Skip to first unread message

salvatore raieli

unread,
Oct 14, 2021, 10:56:05 AM10/14/21
to Keras-users

I want to connect to create two dense layer that are not fully connected. To be more precise, I have a layer A with x neurons and layer B with y neurons. I have a matrix with shape (x,y). The idea is that according to the matrix the neurons are connected.

I am posting also an image to better clarify the idea. neuron 1 in layer A (x1) is connected to neuron 1 in layer B (y1) but not to neuron 2 (y2). X1 is connected also to y3 and other so on. At the same time neuron 3 of A (x3) is conneccted to y1 and other neurons in B (and clearly not connected with other neurons in B).

NN sparse.png


the idea is that the connections between the two layer are decided at priori, without to prune after. The neural network learn a weight just for these weights. I want to do in keras and I would like your help for a simple way to do it (preferably avoiding custom layer, since I want to do the whole architechture with a priori sparsity).

Imaging I am using a Dense layer, how I can modify the neural network; let's the two first layer are A and B:

model = keras.Sequential(

 [ keras.layers.Dense( 512, activation="relu", input_shape=(X_train.shape[-1],) ), keras.layers.Dense(256, activation="relu"), 

 ...

 keras.layers.Dense(y_train_enc.shape[-1], activation="softmax"), ] 

)

thank you for all you suggestions


Reply all
Reply to author
Forward
0 new messages