You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TensorFlow模型输入错误:期望1个输入却收到48个张量的解决方法

16×3输入的神经网络报错:接收了48个输入张量

初始模型代码

def GenerateModel():
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.InputLayer((16,3)))
    model.add(tf.keras.layers.Dense(16*16, input_shape=(16,3)))
    model.add(tf.keras.layers.Dense(4*4))
    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.Dense(1))

    return model

模型结构输出

_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense_11 (Dense)            (None, 16, 256)           1024      
                                                                 
 dense_12 (Dense)            (None, 16, 16)            4112      
                                                                 
 flatten_1 (Flatten)         (None, 256)               0         
                                                                 
 dense_13 (Dense)            (None, 1)                 257       
                                                                 
=================================================================
Total params: 5,393
Trainable params: 5,393
Non-trainable params: 0

测试输入矩阵

[[15, 81, -25.169450961198],
 [53, 108, -36.4360540112101],
 [73, 84, -40.6695085658194],
 [0, 69, -20.7084454809044],
 [35, 97, -31.3954623571617],
 [117, 102, -44.2065629437328],
 [48, 68, -35.7085340456925],
 [17, 59, -23.1078535464318],
 [64, 24, -24.2111029108488],
 [101, 2, -25.97821118996],
 [57, 35, -23.7173409519286],
 [117, 101, -44.1413580763786],
 [88, 10, -25.4001185503816],
 [11, 14, -22.0778253042297],
 [46, 50, -24.7021623105999],
 [0, 0, -1]]

触发的错误信息

ValueError: Layer "sequential_5" expects 1 input(s), but it received 48 input tensors. Inputs received: [<tf.Tensor: shape=(), dtype=int32, numpy=15>, <tf.Tensor: shape=(), dtype=int32, numpy=81>, <tf.Tensor: shape=(), dtype=float64, numpy=-25.169450961198>, <tf.Tensor: shape=(), dtype=int32, numpy=53>, <tf.Tensor: shape=(), dtype=int32, numpy=108>, <tf.Tensor: shape=(), dtype=float64, numpy=-36.4360540112101>, <tf.Tensor: shape=(), dtype=int32, numpy=73>, <tf.Tensor: shape=(), dtype=int32, numpy=84>, <tf.Tensor: shape=(), dtype=float64, numpy=-40.6695085658194>, <tf.Tensor: shape=(), dtype=int32, numpy=0>, <tf.Tensor: shape=(), dtype=int32, numpy=69>, <tf.Tensor: shape=(), dtype=float64, numpy=-20.7084454809044>, <tf.Tensor: shape=(), dtype=int32, numpy=35>, <tf.Tensor: shape=(), dtype=int32, numpy=97>, <tf.Tensor: shape=(), dtype=float64, numpy=-31.3954623571617>, <tf.Tensor: shape=(), dtype=int32, numpy=117>, <tf.Tensor: shape=(), dtype=int32, numpy=102>, <tf.Tensor: shape=(), dtype=float64, numpy=-44.2065629437328>, <tf.Tensor: shape=(), dtype=int32, numpy=48>, <tf.Tensor: shape=(), dtype=int32, numpy=68>, <tf.Tensor: shape=(), dtype=float64, numpy=-35.7085340456925>, <tf.Tensor: shape=(), dtype=int32, numpy=17>, <tf.Tensor: shape=(), dtype=int32, numpy=59>, <tf.Tensor: shape=(), dtype=float64, numpy=-23.1078535464318>, <tf.Tensor: shape=(), dtype=int32, numpy=64>, <tf.Tensor: shape=(), dtype=int32, numpy=24>, <tf.Tensor: shape=(), dtype=float64, numpy=-24.2111029108488>, <tf.Tensor: shape=(), dtype=int32, numpy=101>, <tf.Tensor: shape=(), dtype=int32, numpy=2>, <tf.Tensor: shape=(), dtype=float64, numpy=-25.97821118996>, <tf.Tensor: shape=(), dtype=int32, numpy=57>, <tf.Tensor: shape=(), dtype=int32, numpy=35>, <tf.Tensor: shape=(), dtype=float64, numpy=-23.7173409519286>, <tf.Tensor: shape=(), dtype=int32, numpy=117>, <tf.Tensor: shape=(), dtype=int32, numpy=101>, <tf.Tensor: shape=(), dtype=float64, numpy=-44.1413580763786>, <tf.Tensor: shape=(), dtype=int32, numpy=88>, <tf.Tensor: shape=(), dtype=int32, numpy=10>, <tf.Tensor: shape=(), dtype=float64, numpy=-25.4001185503816>, <tf.Tensor: shape=(), dtype=int32, numpy=11>, <tf.Tensor: shape=(), dtype=int32, numpy=14>, <tf.Tensor: shape=(), dtype=float64, numpy=-22.0778253042297>, <tf.Tensor: shape=(), dtype=int32, numpy=46>, <tf.Tensor: shape=(), dtype=int32, numpy=50>, <tf.Tensor: shape=(), dtype=float64, numpy=-24.7021623105999>, <tf.Tensor: shape=(), dtype=int32, numpy=0>, <tf.Tensor: shape=(), dtype=int32, numpy=0>, <tf.Tensor: shape=(), dtype=int32, numpy=-1>]

尝试过的其他模型结构

结构一

def GenerateModel():
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.Dense(16*16, input_shape=(16,3)))
    model.add(tf.keras.layers.Dense(4*4))
    model.add(tf.keras.layers.Dense(1))

    return model

结构二

def GenerateModel():
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.InputLayer((16,3)))
    model.add(tf.keras.layers.Dense(16*16))
    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.Dense(4*4))
    model.add(tf.keras.layers.Dense(1))

    return model

解决方法

问题根本不在模型结构,而是输入数据缺少batch维度。Keras模型默认要求输入形状是(batch_size, 16, 3),但你直接传入了(16,3)的矩阵,导致模型把矩阵里的48个元素当成了48个独立输入张量。

只需给输入数据增加一个batch维度即可,示例代码:

import numpy as np

# 你的测试输入矩阵
test_input = [
    [15, 81, -25.169450961198],
    # ... 其他行
    [0, 0, -1]
]

# 增加batch维度,形状从(16,3)变为(1,16,3)
test_input_batch = np.expand_dims(test_input, axis=0)

# 调用模型预测
model = GenerateModel()
result = model.predict(test_input_batch)

或者直接把测试数据放进一个列表里,让形状符合要求:

result = model.predict([test_input])

这样就能解决输入张量数量不匹配的问题,你尝试的几种模型结构本身都是可行的,不需要调整Flatten层的位置。

内容的提问来源于stack exchange,提问作者trafon31

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 14:39:50