【问题标题】:Keras model with 2 inputs during training, but only 1 input during inferenceKeras 模型在训练期间有 2 个输入,但在推理期间只有 1 个输入
【发布时间】:2020-10-15 15:04:29
【问题描述】:

我有一个 Keras 模型,它在训练期间需要 2 个输入(并且有 2 个输出对应于 2 个损失函数)。

2 个输入和 2 个输出成对连接。

因此在推理中,我实际上不需要传递第二个输入,也不需要第二个输出。

有没有办法让 Keras/tf.keras 使用 predict 方法只接受第一个输入并产生第一个输出而忽略第二个输入和第二个输出。

我可以为第二个输入创建一个归零的 numpy 数组,但我想知道是否可以减少内存使用或计算。

Tensorflow 应该能够做到这一点,因为它的图表是惰性的。但是 Keras 有能力做到这一点吗?

例子:

# assume second_batch is not needed
second_batch = np.zeros(shape=first_batch.shape)
results = model.predict((first_batch, second_batch))
# i only care about results[0]
# not results[1]

【问题讨论】:

  • 是否有可能有一个虚拟/简单的模型架构,你可以在其中展示你的输入和输出是如何成对连接的
  • 它的 resnet50 但每一层都应用于目标和参考,目标最终计算输出1,参考计算输出2。它是 1 个神经网络共享权重和 2 个损失函数。

标签: python tensorflow keras


【解决方案1】:

您始终可以使用相同的共享权重并指定所需的输入和输出张量来构建新的keras 模型。

import tensorflow as tf

print('TensorFlow:', tf.__version__)


input_a = tf.keras.Input(shape=[224, 224, 3], name='input_a')
input_b = tf.keras.Input(shape=[224, 224, 3], name='input_b')

resnet_model = tf.keras.applications.ResNet50(include_top=False, pooling='avg')

xa = resnet_model(input_a)
xb = resnet_model(input_b)

output_a = tf.keras.layers.Dense(10, name='output_a', activation='softmax')(xa)
output_b = tf.keras.layers.Dense(10, name='output_b', activation='softmax')(xb)

training_model = tf.keras.Model(inputs=[input_a, input_b], outputs=[output_a, output_b])

[print('Training Model Input:', x.name, x.shape) for x in training_model.inputs]
print('')
[print('Training Model Output:', x.name, x.shape) for x in training_model.outputs]
print('')


inference_model = tf.keras.Model(inputs=[input_a], outputs=[output_a])

[print('Inference Model Input:', x.name, x.shape) for x in inference_model.inputs]
[print('Inference Model Output:', x.name, x.shape) for x in inference_model.outputs]

image = tf.random.uniform([1, 224, 224, 3])
predictions = inference_model(image, training=False)
print('')
print('Predictions:', predictions)

输出:

TensorFlow: 2.3.0-dev20200625
Training Model Input: input_a:0 (None, 224, 224, 3)
Training Model Input: input_b:0 (None, 224, 224, 3)

Training Model Output: output_a/Softmax:0 (None, 10)
Training Model Output: output_b/Softmax:0 (None, 10)

Inference Model Input: input_a:0 (None, 224, 224, 3)
Inference Model Output: output_a/Softmax:0 (None, 10)

Predictions: tf.Tensor(
[[0.01937425 0.17703871 0.08633    0.06593429 0.18057525 0.03161139
  0.01154568 0.09730788 0.01927926 0.31100336]], shape=(1, 10), dtype=float32)

【讨论】:

    猜你喜欢
    • 2021-09-10
    • 2021-08-13
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 2019-02-02
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多