0

I was wondering if it is possible to create a customized network structure where the input layer has an extra connection to a hidden layer that is not adjacent to the input layer by using tensorflow. As an example, suppose I have a simple network structure as shown below.

import numpy as np
import random
import tensorflow as tf
from tensorflow import keras 

m = 200
n = 5
my_input=  np.random.random([m,n])
my_output =  np.random.random([m,1])

          

my_model = tf.keras.Sequential([  
    tf.keras.layers.Flatten(input_shape=(my_input.shape[1],)),
    tf.keras.layers.Dense(32, activation='softmax'),
    tf.keras.layers.Dense(32, activation='tanh'),
    tf.keras.layers.Dense(32, activation='relu'),
    tf.keras.layers.Dense(1) 
])   
                    

my_model.compile(loss='mse',optimizer = tf.keras.optimizers.Adam(learning_rate=0.001))
res = my_model.fit(my_input,  my_output, epochs=50, batch_size=1,verbose=0)

Is there a way that the first layer having the input values can have an extra connection to the third layer that has the ReLU activation? While doing so, I'd like to have different constraints in each connection. For example, for the connection coming from the previous layer, I'd like to use GlorotNormal as my weight initialization. As for the extra connection coming from the input layer, I'd like to use HeUniform initialization.

I tried to visualize what I have in mind below.

enter image description here

whitepanda
  • 471
  • 2
  • 12

1 Answers1

1

Use the Keras functional API and tf.concat:

import numpy as np
import random
import tensorflow as tf
from tensorflow import keras 

m = 200
n = 5
my_input=  np.random.random([m,n])
my_output =  np.random.random([m,1])

inputs = tf.keras.layers.Input((my_input.shape[1],))  
x = tf.keras.layers.Flatten()(inputs)
x = tf.keras.layers.Dense(32, activation='softmax')(x)
x = tf.keras.layers.Dense(32, activation='tanh', kernel_initializer=tf.keras.initializers.GlorotNormal())(x)
y = tf.keras.layers.Dense(my_input.shape[1], kernel_initializer=tf.keras.initializers.HeUniform())(inputs)
x = tf.keras.layers.Dense(32, activation='relu')(tf.concat([x, y], axis=1))
outputs = tf.keras.layers.Dense(1)(x)

my_model = tf.keras.Model(inputs, outputs)

dot_img_file = 'model_1.png'
tf.keras.utils.plot_model(my_model, to_file=dot_img_file, show_shapes=True)
my_model.compile(loss='mse',optimizer = tf.keras.optimizers.Adam(learning_rate=0.001))
res = my_model.fit(my_input,  my_output, epochs=50, batch_size=1,verbose=0)

enter image description here

AloneTogether
  • 25,814
  • 5
  • 20
  • 39
  • Thanks! I will give it a try. May I ask where you found this info? I went through the website and could not find any information. – whitepanda Dec 01 '21 at 17:41
  • 1
    https://keras.io/guides/functional_api/ – AloneTogether Dec 01 '21 at 17:42
  • Thanks for the image. Looking at it now, how am I going to constraint weights in those two connections differently. My intention is to use different weight structures for ````x```` and ````inputs````? – whitepanda Dec 01 '21 at 17:48
  • Updated answer. – AloneTogether Dec 01 '21 at 17:57
  • Thanks for the help! I was wondering how you created the diagram representing the network. It looks really neat. I'm referring to ````model1_1.png````. – whitepanda Dec 02 '21 at 16:09
  • Check the code in the answer before I call model.fit() – AloneTogether Dec 02 '21 at 16:10
  • 1
    Oh okay! I thought you were reading a png file and calling it thru tensorflow. – whitepanda Dec 02 '21 at 16:12
  • Sorry to bug you again, but I have a quick question. If I want to add another connection from Input to tanh, should I define a new ````y_extra = tf.keras.layers.Dense(my_input.shape[1], kernel_initializer=tf.keras.initializers.HeUniform())(inputs)```` or can I go ahead and use ````y```` that is already defined. Sadly, I'm not able to use plot_model since tf gives me error. – whitepanda Dec 08 '21 at 23:07
  • 1
    Yeah you can define a new layer. You probably need to install the Graphviz library to plot your model. – AloneTogether Dec 09 '21 at 06:04
  • Thanks for the info! Yes, I did but it still gives me an error. I followed every step shown at https://stackoverflow.com/questions/47605558/importerror-failed-to-import-pydot-you-must-install-pydot-and-graphviz-for-py – whitepanda Dec 09 '21 at 13:59