python - 如何通过特定层的 TensorFlow 模型传递嵌入数据?

标签 python tensorflow

大家好。

我想要两个独立的 TensorFlow 模型(fg)并在 f(g(x)) 的损失上训练它们。但是,我想单独使用它们,例如 g(x) 或 f(e),其中 e 是嵌入向量但不是从 g 接收的。

例如,使用嵌入创建模型的经典方法如下所示:

# First block
model = Sequential([
  vectorize_layer,
  Embedding(vocab_size, embedding_dim, name="embedding"),
  GlobalAveragePooling1D(),
  Dense(16, activation='relu'),
  Dense(1)
])

我希望有一个选项可以分别通过嵌入层和所有其他层传递数据,但仍将模型作为一个整体进行训练,例如:

# Second block
g = Sequential([
  vectorize_layer,
  Embedding(vocab_size, embedding_dim, name="embedding")
])


f = Sequential([
  GlobalAveragePooling1D(),
  Dense(16, activation='relu'),
  Dense(1)
])

我可以在不使用低级 TensorFlow 的情况下做到这一点吗?第一个代码块正是我想要训练的,我可以保留它,但我需要以某种方式通过特定层传递数据。

最佳答案

这可以通过权重共享或共享层来实现。要在 keras 的不同模型中共享层,您只需将相同的层实例传递给两个模型。

示例代码:

#initialize them beforehand
em_layer=Embedding(vocab_size, embedding_dim, name="embedding")
firstfc=Dense(16, activation='relu')
secondfc=Dense(1)

main_model = Sequential([
  vectorize_layer,
  em_layer,
  GlobalAveragePooling1D(),
  firstfc,
  secondfc
])

g = Sequential([
  vectorize_layer,
  em_layer
])


f = Sequential([
  GlobalAveragePooling1D(),
  firstfc,
  secondfc
])

训练使用f.fit()/g.fit()将反射(reflect)在 main_model并且反之亦然

关于python - 如何通过特定层的 TensorFlow 模型传递嵌入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67980314/

相关文章:

python - 在 komodo 编辑中使用 time.sleep()?

python - MySQL、JSON、Python、Django - 完全错误

Python Selenium driver.execute_script WebDriverException : Message: unknown error: call function result missing 'value'

tensorflow - 如何在 tensorflow 中获得 top n arg max/min?

tensorflow - 如何保存 tf.data.Dataset 对象?

tensorflow - tensorflow 中是否有无操作(通过)操作?

python - 同步图内复制需要多少个 session 对象?

python - 无法理解 Perl 中的正则表达式修饰符以转换为 Python

python - 在 Python 2.7 中表示 µs

python - Tf记录错误:"Invalid argument: Input to reshape is a tensor with 71680 values, but the requested shape has 8960"