2017-01-09 142 views
0

我在Keras(版本1.2.0)中遇到了很奇怪的問題。 這裏是我的神經網絡模型:Keras輸入問題

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) 

我已經試過轉換INDATA使用asarray到numpy的陣列等等,但沒有任何作品!你有什麼建議嗎?這是我在尋找一個解決方案第二天...

的Mariusz

+0

右循環後現在,'all_indata'是一個Python列表。當你嘗試'all_indata = np.array(all_indata)'時會發生什麼? –

+0

當我這樣做時,我收到以下錯誤消息: 'Traceback(最近調用最後一個): 文件「nn.py」,第142行,在 model.fit(all_indata,all_outdata,nb_epoch = 35,batch_size = 4) 文件「/usr/lib/python2.7/site-packages/keras/models.py」,行664,in fit sample_weight = sample_weight) 文件「/usr/lib/python2.7/site- (模塊輸入) ValueError:檢查模型輸入時出錯:期望的dense_input_1有形狀(無,302),但有形狀的數組(376) ,1)' – user3282997

+0

什麼是allindata.shape? – ginge

回答

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的改造中

numpy.array(all_indata)