2017-09-02 75 views
2

我的問題是這樣的:我怎樣才能改變我的代碼,如果我想使用多個輸入數據(X的多種功能)這樣的(例子):多種功能

trainX = np.array([[1,2], [2,2] ,[3,3.23] ,[4.11,4] , [5,5.11] , [6,6] ,[7,7], [8,8.1], [9,9],[10,10]]) 

代碼:

import numpy as np 

from keras.models import Sequential 
from keras.layers import Dense, Activation 

# Teach "Table 3" to the network 
trainX = np.array([1, 2 ,3 ,4 , 5 , 6 ,7, 8, 9,10]) 
trainY = np.array([3, 6, 9, 12, 15, 18, 21, 24, 27, 30]) 

model = Sequential() 

model.add(Dense(8, input_dim=1, activation='relu')) 
model.add(Dense(1)) 
model.compile(loss='mean_squared_error', optimizer='adam') 
model.fit(trainX, trainY, nb_epoch=1200, batch_size=2, verbose=2) 


# Predict 3x20, answer = 60 
dataPrediction = model.predict(np.array([4])) 
print (int(dataPrediction[0][0]), '<--- Predicted number') 
print ('12 <-- Correct answer \n') 

輸出:

12 <--- Predicted number 
12 <-- Correct answer 
+0

只要改變你的'input_dim' 2。 –

回答

1

請閱讀文檔befor È問的問題在這裏:https://keras.io

回答您的問題:

在線路model.add(密集(8,input_dim = 1,活化= 'RELU'))輸入尺寸參數指定輸入形狀。當您使用二維的特徵向量input_dim將2

代碼:

import numpy as np 

from keras.models import Sequential 
from keras.layers import Dense, Activation 

# Teach "Table 3" to the network 
trainX = np.array([[1,2], [2,2] ,[3,3.23] ,[4.11,4] , [5,5.11] , [6,6] ,[7,7], [8,8.1], [9,9],[10,10]]) 

trainY = np.array([3, 6, 9, 12, 15, 18, 21, 24, 27, 30]) 

model = Sequential() 

model.add(Dense(8, input_dim=2, activation='relu')) 
model.add(Dense(1)) 
model.compile(loss='mean_squared_error', optimizer='adam') 
model.fit(trainX, trainY, nb_epoch=1200, batch_size=2, verbose=2) 


# Predict 3x20, answer = 60 
dataPrediction = model.predict(np.array([[4.11,4]])) 
print (dataPrediction, '<--- Predicted number') 
print ('12 <-- Correct answer \n')