gpt4 book ai didi

python-3.x - Keras 中 X 的多项功能

转载 作者:行者123 更新时间:2023-11-30 08:46:15 25 4
gpt4 key购买 nike

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

最佳答案

请在此处提问之前阅读文档: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')

关于python-3.x - Keras 中 X 的多项功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46018905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com