簡體   English   中英

python keras神經網絡預測不起作用(輸出0或1)

[英]python keras neural network prediction not working (outputs 0 or 1)

我已經用keras創建了一個神經網絡來預測加法。 我有2個輸入和1個輸出(將2個輸入相加的結果)。

我用張量流訓練了我的神經網絡,然后嘗試預測加法,但是程序返回01值,而不是3,4,5,etc

這是我的代碼:

from keras.models import Sequential
from keras.layers import Dense
import numpy
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load dataset
dataset = numpy.loadtxt("data.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:2]
Y = dataset[:,2]
# create model
model = Sequential()
model.add(Dense(12, input_dim=2, init='uniform', activation='relu'))
model.add(Dense(2, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, Y, epochs=150, batch_size=10,  verbose=2)



# calculate predictions
predictions = model.predict(X)
# round predictions
rounded = [round(x[0]) for x in predictions]
print(rounded)

還有我的文件data.csv

1,2,3
3,3,6
4,5,9
10,8,18
1,3,4
5,3,8

例如:

1+2=3
3+3=6
4+5=9
...etc.

但是我得到的是輸出: 0,1,0,0,1,0,1...為什么我沒有得到輸出為3,6,9...

我更新了代碼以使用其他損失函數,但我有同樣的錯誤:

from keras.models import Sequential
from keras.layers import Dense
import numpy
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load pima indians dataset
dataset = numpy.loadtxt("data.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:2]
Y = dataset[:,2]
# create model
model = Sequential()
model.add(Dense(12, input_dim=2, init='uniform', activation='relu'))
model.add(Dense(2, init='uniform', activation='relu'))
#model.add(Dense(1, init='uniform', activation='sigmoid'))
model.add(Dense(1, input_dim=2, init='uniform', activation='linear'))

# Compile model
#model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, Y, epochs=150, batch_size=10,  verbose=2)



# calculate predictions
predictions = model.predict(X)
# round predictions
rounded = [round(x[0]) for x in predictions]
print(rounded)

outout = 1,1,1,3,1,1,...等

如@ebeneditos所述,您需要將最后一層的激活功能更改為S型以外的其他功能。 您可以嘗試將其更改為線性。

model.add(Dense(1, init='uniform', activation='linear'))

您還應該將損失函數更改為均方誤差,因為您的問題更多的是回歸問題而不是分類問題(binary_crossentropy用作二進制分類問題的損失函數)

model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])

這是由於您在最后一層具有Sigmoid函數 根據定義:

乙狀結腸

它只能接受0到1之間的值。您應該更改最后一層的激活功能。

您可以嘗試以下方法(用Dense(8)代替Dense(2) ):

# Create model
model = Sequential()
model.add(Dense(12, input_dim=2, init='uniform', activation='relu'))
model.add(Dense(8, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='linear'))

# Compile model
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])

# Fit the model
model.fit(X, Y, epochs=150, batch_size=10,  verbose=2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM