2017-09-02 76 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')