【问题标题】:Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected检查模型目标时出错:您传递给模型的 Numpy 数组列表不是模型预期的大小
【发布时间】:2019-05-30 17:35:00
【问题描述】:

我正在尝试根据 4 个输入来预测 3 个材料属性。 输入:加热速率、温度、压力、时间 输出:密度、粒度、硬度

我已经尝试使用 np.array()tf.reshape()np.ndarray() 没有任何效果

 def build_model():
        model = keras.Sequential([
            layers.Dense(4, activation = tf.nn.relu, input_shape = [len(train_dataset.keys())]),
            layers.Dense(64, activation = tf.nn.sigmoid),
            layers.Dense(64, activation = tf.nn.relu),
            layers.Dense(64, activation = tf.nn.relu),
            layers.Dense(3)
        ])

    optimizer = keras.optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=None, decay=0.0)

    model.compile(loss = 'mean_squared_error',
                 optimizer = optimizer,
                 metrics = ['mean_absolute_error', 'mean_squared_error'])
    return model

model.summary()

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_35 (Dense)             (None, 4)                 20        
_________________________________________________________________
dense_36 (Dense)             (None, 64)                320       
_________________________________________________________________
dense_37 (Dense)             (None, 64)                4160      
_________________________________________________________________
dense_38 (Dense)             (None, 64)                4160      
_________________________________________________________________
dense_39 (Dense)             (None, 3)                 195       
=================================================================
Total params: 8,855
Trainable params: 8,855
Non-trainable params: 0
_________________________________________________________________



 print(np.array(normed_train_data))
    print(train_labels)

[[0.78865053 0.75882112 0.66666667 1.        ]
 [0.78865053 0.92764634 1.         0.33333333]
 [1.         0.84323373 1.         0.33333333]
 [1.         0.95176422 1.         0.33333333]
 [0.78865053 0.95176422 1.         0.        ]
 [0.78865053 0.95176422 1.         0.33333333]
 [0.78865053 0.92764634 1.         0.66666667]
 [1.         0.92764634 1.         0.33333333]
 [1.         0.87941056 1.         0.33333333]
 [0.78865053 0.97588211 1.         0.2       ]
 [0.78865053 0.90352845 0.66666667 1.        ]
 [1.         0.80705689 1.         0.33333333]
 [0.78865053 1.         0.66666667 0.66666667]
 [1.         0.83117478 1.         0.33333333]
 [0.78865053 0.97588211 1.         0.2       ]
 [0.78865053 1.         1.         0.2       ]
 [0.78865053 0.71058534 0.66666667 1.        ]
 [0.78865053 0.90352845 1.         0.33333333]
 [0.78865053 0.95176422 0.66666667 1.        ]
 [0.78865053 0.85046909 0.66666667 1.        ]
 [1.         0.80705689 1.         0.33333333]]
[[2      66.5
28     96.8
13     96.4
10     99.0
26     98.2
24    100.0
27     97.6
11     99.0
17     96.2
22    100.0
5      81.8
16     71.4
8      96.0
14     91.9
23    100.0
20    100.0
1      65.8
29     90.3
6      94.4
4      71.8
18     91.9
Name: density, dtype: float64], [2      4.000
28     0.688
13     0.474
10     0.564
26     0.688
24     0.811
27     0.688
11     0.564
17     0.082
22    25.000
5      4.000
16     0.564
8      4.000
14     0.444
23     0.877
20    51.000
1      4.000
29     0.688
6      4.000
4      4.000
18     0.074
Name: grain size, dtype: float64], [2       400000000
28    29400000000
13    31800000000
10    31800000000
26    32300000000
24    35600000000
27    31800000000
11    31800000000
17    31800000000
22    30700000000
5      6700000000
16    31800000000
8     33300000000
14    31800000000
23    34200000000
20    28500000000
1       200000000
29    22800000000
6     32000000000
4      2400000000
18    31800000000
Name: hardness, dtype: int64]]

class PrintDot(keras.callbacks.Callback):
    def on_epoch_end(self,epoch,logs):
        if epoch % 100 == 0: print('')
        print('.',end='')

EPOCH=1000

history = model.fit(normed_train_data, train_labels,
epochs=EPOCH, validation_split = 0.2, verbose=0,
callbacks=[PrintDot()])

ValueError:检查模型目标时出错:您传递给模型的 Numpy 数组列表不是模型预期的大小。预计会看到 1 个数组,但得到了以下 3 个数组的列表

我希望训练机器给我这 3 个输出,并用相应的实际值绘制它们并得到错误。

【问题讨论】:

  • model.fit方法中需要传递np.array(normed_train_data)

标签: tensorflow keras


【解决方案1】:

我认为您正在尝试传递一个形状为 [行、列] 的数组作为输入,其中您提到了列 = 4。如果是这种情况,下面的代码应该适合您。

输入形状中的 None 对应于数据(或行)的长度。

model = keras.Sequential([
            layers.Dense(64, activation = tf.nn.sigmoid, input_shape = [None, 4]),
            layers.Dense(64, activation = tf.nn.relu),
            layers.Dense(64, activation = tf.nn.relu),
            layers.Dense(3)
        ])

上述模型接受一个大小为 4 的输入,并将其通过 64 个神经元的 3 个隐藏层,并输出一个大小为 3 的向量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 2021-02-18
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多