Skip to main content
 首页 » 编程设计

python之我如何将我的输入/输出数据解析为使用 keras 制作的模型

2025年04月02日9emanlee

我目前正在使用 keras 来训练回归网络。

网络已构建,但我不确定应如何将输入和输出数据传递给模型。

输入和输出都存储为 numpy 数组列表。 输入列表中的 numpy 数组的形状为(400 行,y 列)。 输出中的 numpy 数组的形状为(y 行,13 列)

网络的输入维度为400,输出维度为13。

根据documentation .

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: input data, as a Numpy array or list of Numpy arrays (if the model has multiple inputs).

y: labels, as a Numpy array.

y 在这种情况下不是标签,也是原始数据。但是如何确保模型。知道应该将每列作为输入,将行作为输出,对列表中的所有条目执行此操作...

只是解析数据而不对它做任何事情给我这个错误。

Traceback (most recent call last): 
  File "tensorflow_datapreprocess_mfcc_extraction_rnn.py", line 167, 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... 

代码:

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

请您参考如下方法:

尝试通过转置 numpy 数组来 reshape 您的训练输入,即

x = np.transpose(x) 

那么你的训练输入应该是 (number_samples, number_features) 的形状,这是所需的输入格式。您的训练输出已经采用了正确的格式。