【问题标题】:python keras neural network prediction not working (outputs 0 or 1)python keras 神经网络预测不起作用(输出 0 或 1)
【发布时间】:2018-04-12 17:13:17
【问题描述】:

我用 keras 创建了一个用于预测加法的神经网络。 我有 2 个输入和 1 个输出(添加 2 个输入的结果)。

我用 tensorflow 训练了我的神经网络,然后我尝试预测加法,但程序返回 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,...等

【问题讨论】:

  • 你为什么评论Dense(12)Dense(2)层?
  • 只是为了测试。我忘了取消注释。
  • 尝试Dense(8) 而不是Dense(2) 或其他架构(参见编辑后的答案)。

标签: python tensorflow keras


【解决方案1】:

正如@ebeneditos 提到的,您需要将最后一层的激活函数更改为 sigmoid 以外的其他东西。您可以尝试将其更改为线性。

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

您还应该将损失函数更改为均方误差,因为您的问题更多是回归问题而不是分类问题(binary_crossentropy 用作二元分类问题的损失函数)

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

【讨论】:

  • 感谢您的帮助,我用您的损失函数更新了我的代码,但我有同样的问题,outout=1,1,1,3,1,1,...等。
【解决方案2】:

这是由于您在最后一层中的Sigmoid function。定义如下:

它只能取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)

【讨论】:

猜你喜欢
  • 2019-09-10
  • 2021-01-07
  • 2020-01-26
  • 2021-09-22
  • 2017-12-02
  • 1970-01-01
  • 2017-11-21
  • 1970-01-01
  • 2013-04-03
相关资源
最近更新 更多