【问题标题】:how to pass a layer into two layers in parallel at the same time in TensorFlow如何在TensorFlow中同时将一层并行传入两层
【发布时间】:2021-01-28 20:19:54
【问题描述】:

我在 Keras 中实现了一个神经网络,但由于某些原因,我需要在 TensorFlow 中实现该网络。我的问题是我需要将h1 并行传递到两层。我进行了很多搜索以同时将一层传递到两层,但我找不到正确的方法。 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)

因此,我需要同时将h1 传递给tdecoded

【问题讨论】:

  • 你在使用 tf1 和 keras 吗?还是tf2?因为在 tf2 中你可以使用与 keras 完全相同的代码。
  • 我使用 tf2.您能否解释一下如何在 tf2 中使用相同的 keras 代码?谢谢。
  • 请检查答案以了解如何做到这一点。

标签: python tensorflow keras deep-learning neural-network


【解决方案1】:

只需从 tensorflow 2 导入 keras。在 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    相关资源
    最近更新 更多