如何在 Tensorflow 2.2 中训练具有多个输入的 Keras 模型? [英] How to train Keras model with multiple inputs in Tensorflow 2.2?

查看:84
本文介绍了如何在 Tensorflow 2.2 中训练具有多个输入的 Keras 模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用两个输入(一个文本输入和一些数字特征)训练 Keras 模型,但我很难让它工作.我已经按照 Tensorflow 文档中关于多输入模型的描述设置了一个模型:

I'd like to train a Keras model with two inputs (one text input and some numerical features), but I struggle to get it working. I've setup a model as described in the Tensorflow documentation about models with multiple inputs:

import tensorflow as tf
from tensorflow.keras import Input, Model, models, layers


def build_model():
    input1 = Input(shape=(50,), dtype=tf.int32, name='x1')
    input2 = Input(shape=(1,), dtype=tf.float32, name='x2')
    y1 = layers.Embedding(1000, 10, input_length=50)(input1)
    y1 = layers.Flatten()(y1)
    y = layers.Concatenate(axis=1)([y1, input2])
    y = layers.Dense(1)(y)
    return Model(inputs=[input1, input2], outputs=y)

构建该模型也能正常工作:

Building that model works fine too:

model = build_model()
model.compile(loss='mse')
model.summary()

您可以在这个要点中找到summary()的输出一>.

You can find the output of summary() in this gist.

然后需要一些(虚拟)数据来拟合模型:

Then some (dummy) data is needed to get fit onto the model:

def make_dummy_data():
    X1 = tf.data.Dataset.from_tensor_slices(tf.random.uniform([100, 50], maxval=1000, dtype=tf.int32))
    X2 = tf.data.Dataset.from_tensor_slices(tf.random.uniform([100, 1], dtype=tf.float32))
    X = tf.data.Dataset.zip((X1, X2)).map(lambda x1, x2: {'x1': x1, 'x2': x2})
    y_true = tf.data.Dataset.from_tensor_slices(tf.random.uniform([100, 1], dtype=tf.float32))
    return X, y_true


X, y_true = make_dummy_data()
Xy = tf.data.Dataset.zip((X, y_true))
model.fit(Xy, batch_size=32)

...但现在 fit() 失败并显示难以理解的错误消息(参见 此处为完整消息),以(可能相关的)警告开头:

...but now fit() fails with an incomprehensible error message (see full message here), which starts with a (probably relevant) warning:

WARNING:tensorflow:Model was constructed with shape (None, 50) for input Tensor("x1:0", shape=(None, 50), dtype=int32), but it was called on an input with incompatible shape (50, 1).

咦,1 号的额外维度是从哪里来的?而且,我该如何摆脱它?

Huh, where did that extra dimension of size 1 come from? And, how do I get rid of it?

还有一件事:通过删除 Embedding 层来进一步简化这个虚拟模型确实会突然使模型运行.

One more thing: further simplification of this dummy model by removing the Embedding-layer does suddenly make the model run.

如果您想尝试上述示例,我准备了 Google Colab 上的笔记本.任何帮助表示赞赏.

If you want to play around with the above sample, I prepared a notebook on Google Colab for it. Any help appreciated.

推荐答案

作为 fit 状态:

As the documentation of fit states:

batch_size
整数或 None.每次梯度更新的样本数.如果未指定,batch_size 将默认为 32.如果您的数据采用数据集、生成器或 keras.utils.Sequence 实例(因为它们生成批次).

batch_size
Integer or None. Number of samples per gradient update. If unspecified, batch_size will default to 32. Do not specify the batch_size if your data is in the form of datasets, generators, or keras.utils.Sequence instances (since they generate batches).

也就是说,如果您使用数据集来训练模型,则需要提供批次,而不是单个示例.形状 (50, 1) 可能来自 Keras,假设单个 50 个元素的示例实际上是一批 50 个 1 个元素的示例.

That is, if you are using a dataset to train your model, it will be expected to provide batches, not individual examples. The shape (50, 1) probably comes from Keras assuming that a single 50-element example was actually a batch of 50 1-element examples.

您可以像这样简单地修复它:

You can fix it simply like this:

Xy = tf.data.Dataset.zip((X, y_true)).batch(32)
model.fit(Xy)

这篇关于如何在 Tensorflow 2.2 中训练具有多个输入的 Keras 模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆