【问题标题】:Why can't i train my model为什么我不能训练我的模型
【发布时间】:2017-03-27 11:47:38
【问题描述】:

我目前正在尝试制作一个能够将我的输入数据映射到所需输出数据的线性回归网络。

我的输入和输出当前存储为存储为 numpy.ndarray 的矩阵列表。

回归网络的输入维度是 400 回归网络的输出维度为 13。

输入端的每个矩阵都有维度 [400,x] => 由 print input[0].shape 输出

输出端的每个矩阵都有维度 [13,x] => 由 print output[0].shape 输出

我当前定义的网络是这样的:

print "Training!"
model = Sequential()
model.add(Dense(output_dim=13, input_dim=400, init="normal"))
model.add(Activation("relu"))
print "Compiling"
model.compile(loss='mean_squared_error', optimizer='sgd')
model.fit(input,output,verbose=1)

这里的问题是在火车阶段。

不知何故,这需要很长时间,并且没有提供有关进度的信息。系统似乎停止了,并以此错误消息终止。

Traceback (most recent call last):
  File "tensorflow_datapreprocess_mfcc_extraction_rnn.py", line 169, in <module>
    model.fit(train_set_data,train_set_output,verbose=1)
  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 620, in fit
    sample_weight=sample_weight)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1034, in fit
    batch_size=batch_size)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 961, in _standardize_user_data
    exception_prefix='model input')
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 51, in standardize_input_data
    '...')
Exception: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 arrays but instead got the following list of 270 arrays: [array([[ -1.52587891e-04,   3.05175781e-05,  -1.52587891e-04,
         -5.18798828e-04,   3.05175781e-05,  -3.96728516e-04,
          1.52587891e-04,   3.35693359e-04,  -9.15527344e-05,
          3.3...

我猜这个错误可能是我解析输入数据的方式,这对我来说是黑魔法。文档指出

https://keras.io/models/model/

fit(self, x, y, batch_size=32, nb_epoch=10, verbose=1, callbacks=[], validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None)

x:训练数据的 Numpy 数组,如果是模型,则为 Numpy 数组列表 有多个输入。如果模型中的所有输入都已命名,则可以 还将一个字典映射输入名称传递给 Numpy 数组。

y:目标数据的 Numpy 数组,如果是模型,则为 Numpy 数组列表 有多个输出。如果模型中的所有输出都已命名,则可以 还将一个字典映射输出名称传递给 Numpy 数组。

我有一个 Numpy 数组列表是什么?它怎么知道它必须读入的行?...我不知道。 我猜 numpy.ndarrays 存储为 numpy.arrays 列表,其中每个数组都是一行。?

根据这个简单的例子似乎是这样:

输入:

import numpy as np

lis = []
output_data = np.random.rand(5,3)
output_data_1 = np.random.rand(5,2)
lis.append(output_data)
lis.append(output_data_1)
print output_data.shape
print output_data_1.shape
print lis

输出:

(5, 3)
(5, 2)
[array([[ 0.15509364,  0.20140267,  0.13678847],
       [ 0.27932102,  0.38430659,  0.87265863],
       [ 0.01053336,  0.28403731,  0.19749507],
       [ 0.95775409,  0.96032907,  0.46996195],
       [ 0.29515174,  0.74466708,  0.78720968]]), array([[ 0.34216058,  0.74972468],
       [ 0.97262113,  0.84451951],
       [ 0.72230052,  0.30852572],
       [ 0.47586734,  0.03382701],
       [ 0.37998285,  0.80772875]])]

那我做错了什么?为什么我不能将数据传递给模型?

【问题讨论】:

  • 你的形状中的“x”是什么? “输入端的每个矩阵的尺寸为 [400,x] => 由 print input[0].shape 输出。输出端的每个矩阵的尺寸为 [13,x] => 由 print output[0].shape 输出”
  • x 和 y 是 numpy.ndarrays 的列表..

标签: python arrays list numpy keras


【解决方案1】:

转置您的输入 numpy 数组。 Keras 要求输入数组的形状为(number_of_samples, number_of_features)

【讨论】:

  • 所以... x.shape?还是 x[0].shape?
  • 你试过了吗?
  • 因此,由于输入是 numpy.ndarrays 列表,我将无法输出 input.shape。但我可以给出 input[0].shape.. 即:(400, 288) 400 是输入特征的数量,288 是这个 ndarray 包含的样本数。
  • 输出也是如此,所以我想我也会转置它。我会尝试看看是否有帮助。
  • 我通过使用 numpy.vstack 堆叠所有 numpy.ndarrays 删除了列表部分。它奏效了。我只需要转置输入。
猜你喜欢
  • 1970-01-01
  • 2020-04-01
  • 1970-01-01
  • 2021-10-03
  • 1970-01-01
  • 2020-02-19
  • 1970-01-01
  • 1970-01-01
  • 2021-03-23
相关资源
最近更新 更多