機械学習練習----ニューラルネットワークの標準BPアルゴリズム(誤差逆伝播アルゴリズム)
2642 ワード
このアルゴリズムの実現は以下の理論のそこのスイカの本の偽のコードに基づいて、データの部分を読むのは直接大神の1段のコードを使って、ソースの住所https://blog.csdn.net/qdbszsj/article/details/79110888
同時にいくつかの理論を与えます.https://blog.csdn.net/aaalswaaa1/article/details/83046813
同時にいくつかの理論を与えます.https://blog.csdn.net/aaalswaaa1/article/details/83046813
import pandas as pd
import numpy as np
import random
import math
#
def sigmoid(X, d):
if d == 1:
for i in range(len(X)):
X[i] = 1 / (1 + math.exp(-X[i]))
else:
for i in range(len(X)):
X[i] = sigmoid(X[i], d-1)
return X
#
dataset = pd.read_csv('watermelon3.0.csv', delimiter=",",
header=None) # , , header
#
attributeMap = {}
attributeMap[' '] = 0
attributeMap[' '] = 0.5
attributeMap[' '] = 1
attributeMap[' '] = 0
attributeMap[' '] = 0.5
attributeMap[' '] = 1
attributeMap[' '] = 0
attributeMap[' '] = 0.5
attributeMap[' '] = 1
attributeMap[' '] = 0
attributeMap[' '] = 0.5
attributeMap[' '] = 1
attributeMap[' '] = 0
attributeMap[' '] = 0.5
attributeMap[' '] = 1
attributeMap[' '] = 0
attributeMap[' '] = 1
attributeMap[' '] = 0
attributeMap[' '] = 1
dataset = np.array(dataset)
m, n = np.shape(dataset)
for i in range(m):
for j in range(n):
if dataset[i][j] in attributeMap:
dataset[i][j] = attributeMap[dataset[i][j]]
dataset[i][j] = round(dataset[i][j], 3)
#
X = dataset[:, :n-1] #
Y = dataset[:, -1] #
m, n = np.shape(X)
eta = 0.2 #
q = 10 #
l = 1 #
w = [[random.random() for i in range(l)] for j in range(q)] #
v = [[random.random() for i in range(q)] for j in range(n)] #
theta = [random.random() for i in range(l)] #
gamma = [random.random() for i in range(q)] #
Ek = 4 #
time = 0 #
#
while(time < 5000 and Ek > 0.1):
Ek = 0
time += 1
for k in range(m):
alpha = np.dot(X[k], v) #
b = sigmoid(alpha-gamma, 1) #
beta = np.dot(b, w) #
y = sigmoid(beta-theta, 1) #
g = y*(1-y)*(Y[k]-y)
e = b*(1-b)*((np.dot(w, g)).T)
w += eta*np.dot(b.reshape((q, 1)), g.reshape((1, l)))
theta -= eta*g
v += eta*np.dot(X[k].reshape((n, 1)), e.reshape((1, q)))
gamma -= eta*e
E = sum((y-Y[k])*(y-Y[k]))/2
Ek += E
alpha = np.dot(X, v)
b = sigmoid(alpha-gamma, 2)
beta = np.dot(b, w)
y = sigmoid(beta - theta, 2)
print(y)
print(Ek)
print(time)