0

I have an autoencoder type tandem network consisting of a pre-trained forward DNN (weights frozen) taking the output from an untrained inverse DNN. I wish to have a direct mapping between the models such that the output layer of the first network represents the input tensor to the second. I am currently using Keras API sequential model to add dense layers, however, these are fully connected.I've included a diagram here (please have a look)

Here is a snippet of my code:

(`#tandem architecture (with weights loaded from pre trained model)
Tandem = keras.models.Sequential()
Tandem.add(Dense(2, name = 'CIE_input'))
Tandem.add(Dense(1000, activation='relu', name = 'IH1'))
Tandem.add(Dense(1000, activation='relu', name = 'IH2'))
Tandem.add(Dense(3, name = 'Iout')) #need to feed a 3 layer input to FDNN
#FDNN for prediction:
Tandem.add(Dense(3, name = 'input',trainable = False))
Tandem.add(Dense(1000, activation='relu', name = 'FH1', trainable = False))
Tandem.add(Dense(1000, activation='relu', name = 'FH2', trainable = False))
Tandem.add(Dense(1000, activation='relu', name = 'FH3', trainable = False))
Tandem.add(Dense(1000, activation='relu', name = 'FH4', trainable = False))
Tandem.add(Dense(1000, activation='relu', name = 'FH5', trainable = False))
Tandem.add(Dense(1000, activation='relu', name = 'FH6', trainable = False))
Tandem.add(Dense(1000, activation='relu', name = 'FH7', trainable = False))
Tandem.add(Dense(1000, activation='relu', name = 'FH8', trainable = False))
Tandem.add(Dense(1000, activation='relu', name = 'FH9', trainable = False))
Tandem.add(Dense(1000, activation='relu', name = 'FH10', trainable = False)) 
Tandem.add(Dense(2, name = 'output')) # output layer (predicted colour (CIE))
Tandem.compile(loss='mse', optimizer='adam',metrics=['mean_squared_error','accuracy'])
#train the model for one batch to initialize variables (needed before loading weights by name)
Tandem.train_on_batch(y_train[:1], y_train[:1])
#load weights from pre-trained model 
Tandem.load_weights('/content/gdrive/My Drive/Colab Notebooks/Models/FDNN_Weights.h5', by_name=True)`

In addition, I would like to make the connection between the two networks fixed and not allow rescaling. I am new to TensorFlow and Keras (as well as StackOverflow) so I'd be very appreciative of any advice on how to do this simply.

msoler
  • 2,930
  • 2
  • 18
  • 30

2 Answers2

0

I recommend using the Functional API in tf.keras. It helps you to create models with several inner connections and multiple inputs and outputs.

  • Here is the official TensorFlow document.
  • Also, I recommend these posts (1 , 2 , 3 ).



enter image description here

0

I found an error in my code; I had not defined the input shape to the sequential model. I have now removed the layers named 'input CIE' and 'input', defining the input dimensions of layers 'IH1' and 'FH1' to be 2 and 3 respectively. This proper model definition allows for the models to be connected directly, forcing the output of the inverse model to converge to 3 values.