簡體   English   中英

神經網絡收斂到零輸出

[英]Neural Network converging to zero output

我正在嘗試訓練該神經網絡對一些數據進行預測。 我在一個小的數據集(大約100條記錄)上進行了嘗試,它的工作原理像一個魅力。 然后,我插入了新的數據集,發現NN收斂到0輸出,並且誤差近似收斂到正樣本數與樣本總數之間的比率。

我的數據集由是/否特征(1.0 / 0.0)組成,基本事實也是是/否。

我的假設:
1)有一個局部最小值,輸出為0(但是我嘗試了很多學習率和初始權重的值,似乎總是在那兒收斂)
2)我的體重更新不正確(但對我來說不錯)
3)這只是一個輸出縮放問題。 我試圖縮放輸出(即output / max(output)和output / mean(output)),但結果並不理想,如您在下面提供的代碼中所見。 我應該以其他方式縮放它嗎? SOFTMAX?

這是代碼:

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層網絡來融合。

嘗試使用具有更多隱藏節點的3層網絡。 對於大多數前饋問題,您只需要1個隱藏層即可有效收斂。

深度神經網絡的訓練要比淺層神經網絡難得多。

就像其他人所說的那樣,您的學習率應該小得多[.01,.3]是一個不錯的范圍,此外,迭代次數也需要大得多。

10層太多了。

暫無
暫無

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

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