BlueSky
BlueSky

Reputation: 11

How to specify neurons connection to the next layer in keras?

I am trying to build a neural network in Keras with pre-specified connections. For example:

partially connected layers

For example if my input X has feature 'a' I would like to only train neuron 'b' in the next layer.

I am not sure how to specify connections between layers in keras.

Thanks!

Upvotes: 1

Views: 1096

Answers (1)

Charles
Charles

Reputation: 83

In your case, you can use a combination of Lambda layers and merge.
So it can be done through something like :

input = Input((6,))
# Split input to 3 streams
a = Lambda(lambda x: x[:, [0,4]], output_shape=(2,))(input)
b = Lambda(lambda x: x[:, 0:5], output_shape=(5,))(input)
c = Lambda(lambda x: x[:, 5], output_shape=(1,))(input)

# Build the hidden layer
hidden = merge([a, c, b], mode='concat')

# Split the hidden output to 2 streams
aa = Lambda(lambda x: x[:, 0:1], output_shape=(2,))(hidden)
d = Lambda(lambda x: x[:, 2], output_shape=(1,))(hidden)

# Build the output layer
output = merge([aa, d], mode='concat')

model = Model(input, output)

Upvotes: 1

Related Questions