我试图在PYTHON中执行BPN(反向传播)算法,假设它可以工作,但是,当我尝试执行它时,发送给我这个错误。你能帮帮我吗。我添加了整个代码,以防有人需要。谢谢
我试着改变隐藏神经元的数量,但没有成功
"""
Created on Tue Sep 12 11:02:48 2017
Train function
-------------
Parameters:
P: is a matrix with the data of the patterns with which
train the neural network. The examples should be in columns.
T: is a matrix with the expected output for each example. This matrix
must have as many rows as output neurons have the network
T2: classes with their original value (0 .. n-1) (Only used to graph)
hidden: the number of hidden neurons that the network will have
Alpha: learning speed
moment: term term
fun_oculta: activation function in the neurons of the hidden layer
fun_salida: activation function in the neurons of the output layer
MAX_ITERA: the maximum number of iterations in which it is going to
execute the algorithm
cota_error: minimum error accepted to end with the algorithm
draw: if True (and the data is in two dimensions) draw the
examples and discriminating straights.
Returns:
w_O: the weight matrix of the neurons in the hidden layer
b_O: bias vector of the hidden layer neurons
w_S: the matrix of weights of the neurons of the output layer
b_S: bias vector of output layer neurons
ite: number of iterations executed during the algorithm
error_prom: errorAverage finished the algorithm
Example of use:
(w_O, b_O, w_S, b_S, ite, error_prom) = train(P, T, T2, 10, 0.25, 1.2, 'logsig', 'tansig', 25000, 0.001, True);
"""
import numpy as np
import matplotlib.pyplot as plt
marcadores = {0:('+','b'), 1:('o','g'), 2:('x', 'y'), 3:('*', 'm'), 4:('.', 'r'), 5:('+', 'k')}
def plot(P, T, W, b, title = ''):
plt.clf()
#Ejemplos
for class_value in np.unique(T):
x = []
y = []
for i in range(len(T)):
if T[i] == class_value:
x.append(P[i, 0])
y.append(P[i, 1])
plt.scatter(x, y, marker=marcadores[class_value][0], color=marcadores[class_value][1])
#ejes
minimos = np.min(P, axis=0)
maximos = np.max(P, axis=0)
diferencias = maximos - minimos
minimos = minimos - diferencias * 0.1
maximos = maximos + diferencias * 0.1
plt.axis([minimos[0], maximos[0], minimos[-1], maximos[1]])
#rectas discriminantes
x1 = minimos[0]
x2 = maximos[0]
(neuronas, patr) = W.shape
for neu in range(neuronas):
m = W[neu,0] / W[neu,1] * -1
n = b[neu] / W[neu,1] * -1
y1 = x1 * m + n
y2 = x2 * m + n
plt.plot([x1, x2],[y1, y2], color='r')
plt.title(title)
plt.draw()
plt.pause(0.00001)
def purelin(x):
return x
def dpurelin(x):
return np.ones_like(x)
def logsig(x):
return 1 / (1 + np.exp(-x))
def dlogsig(x):
return logsig(x) * (1 - logsig(x))
def tansig(x):
return np.tanh(x)
#return 2 / (1 + np.exp(-2 * x)) - 1
def dtansig(x):
return 1.0 - np.square(tansig(x))
def softmax(x):
shiftx = x - np.max(x)
exps = np.exp(shiftx)
return exps / np.sum(exps)
def dsoftmax(x):
s = softmax(x)
n = s.shape[0]
jacobian_m = np.zeros((n,n))
for i in range(len(jacobian_m)):
for j in range(len(jacobian_m)):
if i == j:
jacobian_m[i][j] = s[i] * (1-s[i])
else:
jacobian_m[i][j] = -s[i]*s[j]
return jacobian_m
def train_con_pesos(P, T, T2, ocultas, alfa, momento, fun_oculta, fun_salida, max_itera, cota_error, dibujar, w_O, b_O, w_S, b_S):
(cant_patrones, cant_atrib) = P.shape
momento_w_S = np.zeros(w_S.shape)
momento_b_S = np.zeros(b_S.shape)
momento_w_O = np.zeros(w_O.shape)
momento_b_O = np.zeros(b_O.shape)
ite = 0;
error_prom = cota_error + 1
ultimoError = cota_error +1
anteultimoError = cota_error +2
while (ite < max_itera) and (abs(ultimoError - anteultimoError) > cota_error):
suma_error = 0
for p in range(cant_patrones):
neta_oculta = w_O.dot(P[p,:][np.newaxis].T) + b_O
salida_oculta = eval(fun_oculta + '(neta_oculta)')
neta_salida = w_S.dot(salida_oculta) + b_S
salida_salida = eval(fun_salida + '(neta_salida)')
error_ejemplo = T[p,:] - salida_salida.T[0]
suma_error = suma_error + np.sum(error_ejemplo**2)
delta_salida = error_ejemplo[np.newaxis].T * eval('d' + fun_salida + '(neta_salida)')
delta_oculta = eval('d' + fun_oculta + '(neta_oculta)') * w_S.T.dot(delta_salida)
w_S = w_S + alfa * delta_salida * salida_oculta.T + momento * momento_w_S
b_S = b_S + alfa * delta_salida + momento * momento_b_S
w_O = w_O + alfa * delta_oculta * P[p,:] + momento * momento_w_O
b_O = b_O + alfa * delta_oculta + momento * momento_b_O
momento_w_S = alfa * delta_salida * salida_oculta.T + momento * momento_w_S
momento_b_S = alfa * delta_salida + momento * momento_b_S
momento_w_O = alfa * delta_oculta * P[p,:].T + momento * momento_w_O
momento_b_O = alfa * delta_oculta + momento * momento_b_O
error_prom = suma_error / cant_patrones
anteultimoError = ultimoError
ultimoError = error_prom
ite = ite + 1
print(ite, error_prom, abs(ultimoError - anteultimoError))
if dibujar and (cant_atrib == 2):
plot(P, T2, w_O, b_O, 'Iteración: ' + str(ite) + ' - Error promedio: ' + str(error_prom))
return (w_O, b_O, w_S, b_S, ite, error_prom)
def train(P, T, T2, ocultas, alfa, momento, fun_oculta, fun_salida, max_itera, cota_error, dibujar):
(cant_patrones, cant_atrib) = P.shape
(cant_patrones, cant_salidas) = T.shape
w_O = np.random.rand(ocultas, cant_atrib) - 0.5
b_O = np.random.rand(ocultas,1) - 0.5
w_S = np.random.rand(cant_salidas, ocultas) - 0.5
b_S = np.random.rand(cant_salidas,1) - 0.5
return train_con_pesos(P, T, T2, ocultas, alfa, momento, fun_oculta, fun_salida, max_itera, cota_error, dibujar, w_O, b_O, w_S, b_S)
# IMPORT DATASET DRUG.XLS
import numpy as np
import pandas as pd
P= pandas.read_excel('C:\\MAESTRIA DATAMINING\\Drug4.xlsx', 'Sheet1')
T = P.copy()
del P['CLASE']
T.drop(['X1','X2', 'X3', 'X4', 'X5', 'X6'], axis='columns', inplace=True)
P_TRANSPOSE = P.transpose()
T_TRANSPOSE = T.transpose()
T_Ori=T.copy()
P_Ori=P.copy()
T=T_TRANSPOSE
P=P_TRANSPOSE
T = T.values
P = P.values
(w_O, b_O, w_S, b_S, ite, error_prom) = train(P, T, T_matriz, 10, 0.25, 1.2, 'logsig', 'logsig', 25000, 0.001, True)
索引器回溯(最后一次最近调用) 在() 13列车(P、T、T2、10、0.25、1.2,‘logsig’、‘tansig’、25000、0.001,正确); 14 ---&燃气轮机;15(w_O,b_O,w_S,b_S,ite,error_prom)=列车(P,T,T_matriz,10,0.25,1.2,'logsig','logsig',25000,0.001,真)
列车内(p、T、T2、ocultas、alfa、momento、fun_oculta、fun_salida、max_itera、cota_error、dibujar) 176 b_S=np.rand.rand(cant_salidas,1)-0.5 177 --&燃气轮机;178往返列车(P、T、T2、ocultas、alfa、momento、fun_oculta、fun_salida、max_itera、cota_错误、dibujar、w_O、b_O、w_S、b_S) 179
火车上的比索(p、T、T2、ocultas、alfa、momento、fun_oculta、fun_salida、max_itera、cota_error、dibujar、w_O、b_O、w_S、b_S) 136 salida_salida=评估(有趣的salida+'(neta_salida)) 137 --&燃气轮机;138错误=T[p,:]-salida\u salida.T[0] 139 suma_error=suma_error+np.sum(error_ejempo**2) 140
索引器:索引1超出大小为1的轴0的界限
目前没有回答
相关问题 更多 >
编程相关推荐