Keras:精度保持为零 [英] Keras: Accuracy stays zero

查看:102
本文介绍了Keras:精度保持为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试与Keras进行机器学习.

I am trying to get into machine learning with Keras.

我不是数学家,而且我对神经网络的工作原理只有一个基本的了解(哈哈!),所以对我轻松一点.

I am not a Mathematician and I have only a basic understanding of how neural net-works (haha get it?), so go easy on me.

这是我当前的代码:

from keras.utils import plot_model
from keras.models import Sequential
from keras.layers import Dense
from keras import optimizers
import numpy

# fix random seed for reproducibility
numpy.random.seed(7)

# split into input (X) and output (Y) variables
X = []
Y = []
count = 0

while count < 10000:
    count += 1
    X += [count / 10000]
    numpy.random.seed(count)
    #Y += [numpy.random.randint(1, 101) / 100]
    Y += [(count + 1) / 100]
print(str(X) + ' ' + str(Y))

# create model
model = Sequential()
model.add(Dense(50, input_dim=1, kernel_initializer = 'uniform', activation='relu'))
model.add(Dense(50, kernel_initializer = 'uniform', activation='relu'))
model.add(Dense(1, kernel_initializer = 'uniform', activation='sigmoid'))

# Compile model
opt = optimizers.SGD(lr=0.01)
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])

# Fit the model
model.fit(X, Y, epochs=150, batch_size=100)

# evaluate the model
scores = model.evaluate(X, Y)
predictions = model.predict(X)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
print (str(predictions))
##plot_model(model, to_file='C:/Users/Markus/Desktop/model.png')

精度保持为零,并且预测为1的数组.我在做什么错了?

The accuracy stays zero and the predictions are an array of 1's. What am I doing wrong?

推荐答案

据我所知,您正在尝试解决回归问题(浮点函数输出)而不是分类问题(一个热向量样式输出/输入)类别).

From what I can see you are trying to solve a regression problem (floating point function output) rather than a classification problem (one hot vector style output/put input into categories).

您的S形最终层只会提供0到1之间的输出,这显然限制了您的NN预测所需的Y值范围的能力,该范围会更高.您的NN试图尽可能地接近,但是您要限制它!输出层中的S形对于单类是"/否"输出是好的,但对回归没有好处.

Your sigmoid final layer will only give an output between 0 and 1, which clearly limits your NNs ability to predict the desired range of Y values which go up much higher. Your NN is trying to get as close as it can, but you are limiting it! Sigmoids in the output layer are good for single class yes/no output, but not regression.

因此,您希望最后一层具有线性激活,其中输入被求和.这样的东西,而不是乙状结肠. model.add(Dense(1, kernel_initializer='lecun_normal', activation='linear'))

So, you want your last layer to have a linear activation where the inputs are just summed. Something like this instead of the sigmoid. model.add(Dense(1, kernel_initializer='lecun_normal', activation='linear'))

那么至少在学习率足够低的情况下,它可能会起作用.

Then it will likely work, at least if the learning rate is low enough.

Google"keras回归"以获取有用的链接.

Google "keras regression" for useful links.

这篇关于Keras:精度保持为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆