【问题标题】:Multiple features of X in KerasKeras 中 X 的多个特性
【发布时间】:2017-09-02 22:53:32
【问题描述】:

我的问题是:如果我想像这样使用多个输入数据(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 

【问题讨论】:

  • 只需将您的 input_dim 更改为 2。

标签: python-3.x machine-learning tensorflow keras


【解决方案1】:

请在此处提问之前阅读文档:https://keras.io

回答你的问题:

model.add(Dense(8, input_dim=1, activation='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')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 2022-11-11
    • 2017-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多