Keras输入问题

时间:2017-01-09 11:53:42

标签: python keras

我在与Keras(版本1.2.0)的非常奇怪的问题上苦苦挣扎。 这是我的NN模型:

model = Sequential()
lindata=302
model.add(Dense(lindata, input_dim=lindata, activation='sigmoid'))
model.add(Dense(60,activation='sigmoid'))
model.add(Dense(30, activation='softplus'))  
model.add(Dense(loutdata, activation='softplus'))
model.compile(optimizer='rmsprop',
              loss='mean_squared_error',
              metrics=['accuracy'])

输入是302个浮点数的向量,我在训练样本中有376个浮点数。 现在,当我运行model.fit()时,我收到错误消息:

Traceback (most recent call last):
  File "nn.py", line 142, in <module>
    model.fit(all_indata,all_outdata,nb_epoch=35, batch_size=4) # nb_epoch=65 - not enough
  File "/usr/lib/python2.7/site-packages/keras/models.py", line 664, in fit
    sample_weight=sample_weight)
  File "/usr/lib/python2.7/site-packages/keras/engine/training.py", line 1068, in fit
    batch_size=batch_size)
  File "/usr/lib/python2.7/site-packages/keras/engine/training.py", line 981, in _standardize_user_data
    exception_prefix='model input')
  File "/usr/lib/python2.7/site-packages/keras/engine/training.py", line 54, in standardize_input_data
    '...')
ValueError: 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 376 arrays: [array([[  1.25000000e+11],
       [  1.20000000e-09],
       [  0.00000000e+00],
       [  0.00000000e+00],
       [  0.00000000e+00],
       [  0.00000000e+00],
       [  0.00000000e+00],
       [  ...

这是我创建输入的方式:

for line in datasample:
    elems=line.split(",")
    indata=np.array([[float(elems[i])] for i in range(2,len(elems)-1)])
    all_indata.append(indata)

我尝试使用asarray等将indata转换为numpy数组,但没有任何效果!你有什么建议吗?这是第二天我正在寻找解决方案......

马里乌什

1 个答案:

答案 0 :(得分:0)

尝试修改你的循环:

for line in datasample:
elems=line.split(",")
indata= [[float(elems[i])] for i in range(2,len(elems)-1)]
all_indata.append(indata)

循环后应用numpy trasformation

numpy.array(all_indata)