Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

python - how to pass a layer into two layers in parallel at the same time in TensorFlow

I have implemented a neural network in Keras, but for some reasons, I need to implement the network in TensorFlow. My problem is that I need to pass h1 into two layers in parallel. I have searched a lot to pass a layer into two layers at the same time but I cannot find the correct way. The Keras code is as below:

  x = keras.layers.Input(shape=(input_dim))
  hp_units1 = hp.Int('units', min_value=1, max_value=15, step=1)
  h1 = keras.layers.Dense(hp_units1, activation = 'sigmoid', name='dense_1')(x)
  t = keras.layers.Dense(1, activation='sigmoid', name='time_prediction')(h1)
  decoded = keras.layers.Dense(input_dim, activation='linear', name='decoded_mean')(h1)

Thus, I need to pass h1 to t and decoded at the same time.

question from:https://stackoverflow.com/questions/65944621/how-to-pass-a-layer-into-two-layers-in-parallel-at-the-same-time-in-tensorflow

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just import keras from tensorflow 2. The same syntax is available in tensorflow 2 -

from tensorflow import keras

x = keras.layers.Input(shape=(input_dim))
hp_units1 = hp.Int('units', min_value=1, max_value=15, step=1)
h1 = keras.layers.Dense(hp_units1, activation = 'sigmoid', name='dense_1')(x)
t = keras.layers.Dense(1, activation='sigmoid', name='time_prediction')(h1)
decoded = keras.layers.Dense(input_dim, activation='linear', name='decoded_mean')(h1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...