【问题标题】:Neural Network converging to zero output神经网络收敛到零输出
【发布时间】:2017-10-28 01:27:49
【问题描述】:

我正在尝试训练这个神经网络来对一些数据进行预测。 我在一个小型数据集(大约 100 条记录)上进行了尝试,它的工作原理非常棒。然后我插入新的数据集,我发现 NN 收敛到 0 输出,误差近似收敛到正例数与总例数之比。

我的数据集由是/否特征 (1.0/0.0) 组成,基本事实也是是/否。

我的假设:
1) 有一个输出为 0 的局部最小值(但我尝试了许多学习率和初始权重值,它似乎总是在那里收敛)
2) 我的体重更新是错误的(但对我来说看起来不错)
3)这只是一个输出缩放问题。我尝试缩放输出(即输出/最大值(输出)和输出/平均值(输出)),但结果并不好,正如您在下面提供的代码中看到的那样。我应该以不同的方式缩放它吗?软最大?

代码如下:

import pandas as pd
import numpy as np
import pickle
import random
from collections import defaultdict

alpha = 0.1
N_LAYERS = 10
N_ITER = 10
#N_FEATURES = 8
INIT_SCALE = 1.0

train = pd.read_csv("./data/prediction.csv")

y = train['y_true'].as_matrix()
y = np.vstack(y).astype(float)
ytest = y[18000:]
y = y[:18000]

X = train.drop(['y_true'], axis = 1).as_matrix()
Xtest = X[18000:].astype(float)
X = X[:18000]

def tanh(x,deriv=False):
    if(deriv==True):
        return (1 - np.tanh(x)**2) * alpha
    else:
        return np.tanh(x)

def sigmoid(x,deriv=False):
    if(deriv==True):
        return x*(1-x)
    else:
        return 1/(1+np.exp(-x))

def relu(x,deriv=False):
    if(deriv==True):
        return 0.01 + 0.99*(x>0)
    else:
        return 0.01*x + 0.99*x*(x>0)

np.random.seed()

syn = defaultdict(np.array)

for i in range(N_LAYERS-1):
    syn[i] = INIT_SCALE * np.random.random((len(X[0]),len(X[0]))) - INIT_SCALE/2
syn[N_LAYERS-1] = INIT_SCALE * np.random.random((len(X[0]),1)) - INIT_SCALE/2

l = defaultdict(np.array)
delta = defaultdict(np.array)

for j in xrange(N_ITER):
    l[0] = X
    for i in range(1,N_LAYERS+1):
        l[i] = relu(np.dot(l[i-1],syn[i-1]))

    error = (y - l[N_LAYERS])

    e = np.mean(np.abs(error))
    if (j% 1) == 0:
        print "\nIteration " + str(j) + " of " + str(N_ITER)
        print "Error: " + str(e)

    delta[N_LAYERS] = error*relu(l[N_LAYERS],deriv=True) * alpha
    for i in range(N_LAYERS-1,0,-1):
        error = delta[i+1].dot(syn[i].T)
        delta[i] = error*relu(l[i],deriv=True) * alpha

    for i in range(N_LAYERS):
        syn[i] += l[i].T.dot(delta[i+1])



pickle.dump(syn, open('neural_weights.pkl', 'wb'))

# TESTING with f1-measure
# RECALL = TRUE POSITIVES / ( TRUE POSITIVES + FALSE NEGATIVES)
# PRECISION = TRUE POSITIVES / (TRUE POSITIVES + FALSE POSITIVES)

l[0] = Xtest
for i in range(1,N_LAYERS+1):
    l[i] = relu(np.dot(l[i-1],syn[i-1]))

out = l[N_LAYERS]/max(l[N_LAYERS])

tp = float(0)
fp = float(0)
fn = float(0)
tn = float(0)

for i in l[N_LAYERS][:50]:
    print i

for i in range(len(ytest)):
    if out[i] > 0.5 and ytest[i] == 1:
        tp += 1
    if out[i] <= 0.5 and ytest[i] == 1:
        fn += 1
    if out[i] > 0.5 and ytest[i] == 0:
        fp += 1
    if out[i] <= 0.5 and ytest[i] == 0:
        tn += 1

print "tp: " + str(tp)
print "fp: " + str(fp)
print "tn: " + str(tn)
print "fn: " + str(fn)

print "\nprecision: " + str(tp/(tp + fp))
print "recall: " + str(tp/(tp + fn))

f1 = 2 * tp /(2 * tp + fn + fp)
print "\nf1-measure:" + str(f1)

这是输出:

Iteration 0 of 10
Error: 0.222500767998

Iteration 1 of 10
Error: 0.222500771157

Iteration 2 of 10
Error: 0.222500774321

Iteration 3 of 10
Error: 0.22250077749

Iteration 4 of 10
Error: 0.222500780663

Iteration 5 of 10
Error: 0.222500783841

Iteration 6 of 10
Error: 0.222500787024

Iteration 7 of 10
Error: 0.222500790212

Iteration 8 of 10
Error: 0.222500793405

Iteration 9 of 10
Error: 0.222500796602


[ 0.]
[ 0.]
[  5.58610895e-06]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[  4.62182626e-06]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[  5.58610895e-06]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[  4.62182626e-06]
[ 0.]
[ 0.]
[  5.04501079e-10]
[  5.58610895e-06]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]
[  5.04501079e-10]
[ 0.]
[ 0.]
[  4.62182626e-06]
[ 0.]
[  5.58610895e-06]
[ 0.]
[ 0.]
[ 0.]
[  5.58610895e-06]
[ 0.]
[ 0.]
[ 0.]
[  5.58610895e-06]
[ 0.]
[  1.31432294e-05]

tp: 28.0
fp: 119.0
tn: 5537.0
fn: 1550.0

precision: 0.190476190476
recall: 0.0177439797212

f1-measure:0.0324637681159

【问题讨论】:

  • 10 次迭代是什么都没有。将其扩展到至少 1000。我不确定它是否会解决您的问题,因为您的错误实际上通过迭代变得越来越高。但我还是建议你改变它。
  • 是的,这只是一个测试。误差增加是因为学习率有点太大了。无论如何,数据集是 27k 个示例,所以我认为不需要那么多迭代。
  • 27k 个样本!!!仅当 27k 个样本具有相同的样本并且没有任何噪声时,您的网络绝不会在 10 次迭代中收敛。增加迭代次数,并显示您的结果。
  • 1000 次迭代收敛到 Error: 0.222500000038 这基本上是相同的分数。事实是,无论您选择什么学习率或迭代次数,它总是收敛到相同的值,从而导致输出 0。

标签: python python-2.7 machine-learning neural-network classification


【解决方案1】:

根据您的模型,您的网络不太可能需要 10 层才能收敛。

尝试具有更多隐藏节点的 3 层网络。对于大多数前馈问题,您只需要 1 个隐藏层即可有效收敛。

深度神经网络比浅神经网络更难训练。

正如其他人所说,您的学习率应该小得多 [.01,.3] 是一个不错的范围,此外迭代次数需要大得多。

10 层太多了。

【讨论】:

    猜你喜欢
    • 2013-04-03
    • 2012-03-03
    • 2018-11-04
    • 2016-05-13
    • 2016-07-10
    • 1970-01-01
    • 2014-06-24
    • 2019-02-20
    相关资源
    最近更新 更多