“顺序”层需要 1 个输入,但它收到了 48 个输入张量

问题描述 投票:0回答:2

我正在尝试构建一个简单的神经网络,它获取 2D 矩阵 (16x3) 并输出单个值,以下是我尝试构建该网络的方式;

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

model.summary()结果如下;

_________________________________________________________________
 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

因此,当我尝试使用GenerateModel 创建模型并向其传递以下矩阵时,我收到错误。

[[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

我尝试在不同图层之后添加一个Flatten图层,但没有解决我的问题。

如何解决这个问题?

python tensorflow machine-learning keras
2个回答
0
投票

代码中有两个错误

  1. 您必须在第一个密集层之前展平输入。我不知道你为什么把它放在密集层之间。
  2. 模型需要一批输入。如果只是一个例子,你只需将它分批传递,大小为 1。
import tensorflow as tf
import numpy as np

def GenerateModel():
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.InputLayer(input_shape=(16, 3)))
    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.Dense(256, activation='relu'))
    model.add(tf.keras.layers.Dense(16, activation='relu'))
    model.add(tf.keras.layers.Dense(1))
    return model

input_matrix = np.array([[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]])

model = GenerateModel()

output = model.predict(np.expand_dims(input_matrix, axis=0))
print()
print(output)


-1
投票

您似乎正在尝试输入一个(列表中的)列表。
相反,您应该将其转换为所需形状的张量,然后将其向前传递。

© www.soinside.com 2019 - 2024. All rights reserved.