【问题标题】:How to output a scalar variable as an output of a Keras model?如何输出标量变量作为 Keras 模型的输出?
【发布时间】:2019-04-12 11:06:27
【问题描述】:

例如,我想要类似的东西

模型(输入=某物,输出=标量)

这出现在您想在没有状态“X”的情况下调试模型/训练过程(生成)。所以你仍然有一个来自 Y 的 batch_size。这就是你想要的。

我正在尝试这样的事情:

V = K.variable(0, dtype=tf.float32)
V = tf.reduce_mean(x_input_not_used_by_this_branch, axis=1) * 0 + V # this is a stupid way to get things to work
model keras.models.Model(inputs=something, outputs=[V, some_other_stuff])

【问题讨论】:

  • 为什么投反对票?我会解决这个问题,因为我对答案感兴趣。

标签: keras


【解决方案1】:

一种简单的方法是使用 Keras 的功能 API:Keras API docu

inputs = Input(shape=(784,))

# a layer instance is callable on a tensor, and returns a tensor
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)

v = 0.25 * x

# This creates a model that includes
# the Input layer and three Dense layers
model = Model(inputs=inputs, outputs=[predictions, v])

【讨论】:

  • 这是一个不同的模型。 V 是输入的函数。我希望 V 成为没有任何外部因素的函数。注意乘以零。
猜你喜欢
  • 1970-01-01
  • 2017-09-23
  • 2021-06-18
  • 1970-01-01
  • 2019-04-14
  • 2019-11-19
  • 2020-06-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多