I'm trying to get the output and input parameters after concatenation in keras, more specifically in "concat_" and "hidden 6" layers.
input_A=keras.layers.Input(shape=X1_Train.shape[1])
input_B=keras.layers.Input(shape=X2_Train.shape[1])
hidden1=keras.layers.Dense(activation='linear',units=25)(input_A)
hidden2=keras.layers.Dense(activation='linear',units=25)(hidden1)
hidden3=keras.layers.Dense(activation='linear',units=25)(hidden2)
hidden4=keras.layers.Dense(activation='linear',units=10)(hidden3)
hidden5=keras.layers.Dense(activation='linear',units=1)(hidden4)
concat_=keras.layers.concatenate([hidden5, input_B])
hidden6=keras.layers.Dense(activation='linear',units=1)(concat_)
output=keras.layers.Dense(activation='linear',units=1)(hidden6)
model1=keras.Model(inputs=[input_A,input_B], outputs=[output])
Is there way to obtain the parameters by layer name? Also, is there any way to run the model (after training) until the concatenation point?
CodePudding user response:
You could give each layer that you want to later retrieve, a specific name, like this:
from tensorflow import keras
input_A=keras.layers.Input(shape=1)
input_B=keras.layers.Input(shape=2)
hidden1=keras.layers.Dense(activation='linear',units=25)(input_A)
hidden2=keras.layers.Dense(activation='linear',units=25)(hidden1)
hidden3=keras.layers.Dense(activation='linear',units=25)(hidden2)
hidden4=keras.layers.Dense(activation='linear',units=10)(hidden3)
hidden5=keras.layers.Dense(activation='linear',units=1)(hidden4)
concat_=keras.layers.concatenate([hidden5, input_B], name="concat_layer")
hidden6=keras.layers.Dense(activation='linear',units=1, name="hidden_layer")(concat_)
output=keras.layers.Dense(activation='linear',units=1)(hidden6)
model1=keras.Model(inputs=[input_A,input_B], outputs=[output])
Once you retrieve the layer by name, you can access different attributes, like the inputs, outputs, weights:
out = model1.get_layer("concat_layer").output
inp = model1.get_layer("concat_layer").input
weights = model1.get_layer("concat_layer").get_weights()
To run the model up until a specific layer:
# create an shorter model, up until the concatenation layer included
model_intermediate = keras.Model(inputs=model1.input, outputs = model1.get_layer("concat_layer").output)
# run inference
pred = model_intermediate.predict(...)