层“模型”的输入0与层不兼容:预期形状=(无, 250, 3),在经过训练的变压器模型中发现形状=(无, 3)

发布于 2025-01-14 20:46:52 字数 700 浏览 3 评论 0原文

我有一个用tensorflow 2.7.0 和 python 3.7 训练的 keras transformer 模型,输入形状:(None, 250, 3) 和形状为:(250, 3)(不是图像)的二维数组输入

使用以下方式进行预测时:

prediction = model.predict(state)

我得到 ValueError: Layer "model" 的输入 0 与该层不兼容:预期 shape=(None, 250, 3),找到 shape=(None, 3)

项目代码: https://github.com/MikeSifanele/TT

这是 state 的样子:

<代码>状态= np.array([[-0.07714844,-0.06640625,-0.140625],[-0.140625,-0.1650391,-0.22 65625]...[0.6376953,0.6005859,0.6083984],[0.7714844,0.7441406,0.7578125]], np.float32)

I have a keras transformer model trained with tensorflow 2.7.0 and python 3.7 with input shape: (None, 250, 3) and a 2D array input with shape: (250, 3)(not an image)

When making a prediction with:

prediction = model.predict(state)

I get ValueError: Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 250, 3), found shape=(None, 3)

project code: https://github.com/MikeSifanele/TT

This is how state looks like:

state = np.array([[-0.07714844,-0.06640625,-0.140625],[-0.140625,-0.1650391,-0.2265625]...[0.6376953,0.6005859,0.6083984],[0.7714844,0.7441406,0.7578125]], np.float32)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软甜啾 2025-01-21 20:46:52

一些解释:

对于模型的输入形状,即 (None, 250, 3),第一个轴(由 None 表示)是“样本”轴,而其余轴即250,3表示输入维度。因此,当输入形状为 (250, 3) 时,它假定第一个轴为“样本”轴,其余轴为输入维度,即 3。因此,为了使其一致,我们需要在开头添加一个维度,如下所示:

state = np.expand_dims(state, axis=0)

state 的形状则变为 (1, 250, 3) ~(无,250,3)

Some explanation:

For input shape to the model i.e. (None, 250, 3), the first axis (represented by None) is the "sample" axis, while the rest i.e. 250,3 denotes the input dimension. Thus, when the input shape is (250, 3) it assumes the first axis as the "sample" axis and the rest as the input dimension i.e. just 3. So, to make it consistent we need to add a dimension at the beginning described in the following:

state = np.expand_dims(state, axis=0)

The shape of state then becomes (1, 250, 3) ~(None, 250, 3).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文