With some help, I was able to setup a NN within a NN.
Basically the architecture is as follows:
Has anyone encountered these type of situations before?
In code:
# Main NN: Simple convolutional autoencoder
input_img = Input(shape=(1, 80, 80), name="input_layer")
intermediate = Convolution2D(32, 3, 3, name="conv1")(input_img)
decoded = Convolution2D(10, 3, 3, name="conv2")(intermediate)
fc_flatten = Flatten()(intermediate)
# Sub NN: Uses output from encoding part to do supervised learning on FC layers
input_NN2 = Input( (None,80*80*32))
y = Dense(64, name="dense3")(input_NN2)
fc_output = Dense(1)(y)
model2 = Model([input_NN2], [fc_output], name="submodel")
fc_output = model2([fc_flatten])
model = Model(input=[input_img], output=[decoded, fc_output])
model.compile(loss=["mse","mse"],optimizer="adam")
The major difficulty is that the weights of the layer "dense3" is not stored as a layer in the previous step. Instead all the weights are saved in the model, "submodel". I can't figure out how to "separate out" the weights of all layers inside "submodel".
input_img = Input(shape=(1, 80, 80), name="input_layer")
intermediate = Convolution2D(32, 3, 3, name="conv1")(input_img)
fc_flatten = Flatten()(intermediate)
y = Dense(64, name="dense3")(fc_flatten)
new_output = Dense(4)(y)
newmodel = Model(input=[input_img], output=[new_output])
newmodel.compile(loss=["mse"],optimizer="adam")
I tried opening the .hdf5 file, but that format is not readable in text. Any suggestions? Thanks!
Using save_weights(), and load_weights() works.
You can also use model.get_layer( either by name or index) to get the layer and you can then call get_weights() or set_weights(). And do the mapping by matching names.
Alternatively when the structure of the network is known :
Usually when you build a network, you can return a tuple of model
(combined_network, subnetwork1, subnetwork2 ) which all share the same shared variables
If you need to transfer the weights you to another network or subnetwork for which you have a tuple.
(new_combined_network, new_subnetwork1, new_subnetwork2)
doing new_combined_network.set_weights( combined_network.get_weights() ) will set all the weights including the subnetworks.
doing new_subnetwork1.set_weights( subnetwork1.get_weights()) will only transfer the weight of the subnetwork1
Most helpful comment
Using save_weights(), and load_weights() works.
You can also use model.get_layer( either by name or index) to get the layer and you can then call get_weights() or set_weights(). And do the mapping by matching names.
Alternatively when the structure of the network is known :
Usually when you build a network, you can return a tuple of model
(combined_network, subnetwork1, subnetwork2 ) which all share the same shared variables
If you need to transfer the weights you to another network or subnetwork for which you have a tuple.
(new_combined_network, new_subnetwork1, new_subnetwork2)
doing new_combined_network.set_weights( combined_network.get_weights() ) will set all the weights including the subnetworks.
doing new_subnetwork1.set_weights( subnetwork1.get_weights()) will only transfer the weight of the subnetwork1