El último semestre tomé un curso de aprendizaje de máquina en línea de Standford impartido por el profesor Ng. http://www.ml-class.org/course/auth/welcome Pensé que era bastante informativo. Para mejorar las redes neuronales, traté de escribir las mías en python. Aquí está:Intentando escribir mi propia red neuronal en Python
import numpy
class NN:
def __init__(self, sl):
#sl = number of units (not counting bias unit) in layer l
self.sl = sl
self.layers = len(sl)
#Create weights
self.weights = []
for idx in range(1, self.layers):
self.weights.append(numpy.matrix(numpy.random.rand(self.sl[idx-1]+1, self.sl[idx])/5))
self.cost = []
def update(self, input):
if input.shape[1] != self.sl[0]:
raise ValueError, 'The first layer must have a node for every feature'
self.z = []
self.a = []
#Input activations. I'm expecting inputs as numpy matrix (Examples x Featrues)
self.a.append(numpy.hstack((numpy.ones((input.shape[0], 1)), input)))#Set inputs ai + bias unit
#Hidden activations
for weight in self.weights:
self.z.append(self.a[-1]*weight)
self.a.append(numpy.hstack((numpy.ones((self.z[-1].shape[0], 1)), numpy.tanh(self.z[-1])))) #tanh is a fancy sigmoid
#Output activation
self.a[-1] = self.z[-1] #Not logistic regression thus no sigmoid function
del self.z[-1]
def backPropagate(self, targets, lamda):
m = float(targets.shape[0]) #m is number of examples
#Calculate cost
Cost = -1/m*sum(numpy.power(self.a[-1] - targets, 2))
for weight in self.weights:
Cost = Cost + lamda/(2*m)*numpy.power(weight[1:, :], 2).sum()
self.cost.append(abs(float(Cost)))
#Calculate error for each layer
delta = []
delta.append(self.a[-1] - targets)
for idx in range(1, self.layers-1): #No delta for the input layer because it is the input
weight = self.weights[-idx][1:, :] #Ignore bias unit
dsigmoid = numpy.multiply(self.a[-(idx+1)][:,1:], 1-self.a[-(idx+1)][:,1:]) #dsigmoid is a(l).*(1-a(l))
delta.append(numpy.multiply(delta[-1]*weight.T, dsigmoid)) #Ignore Regularization
Delta = []
for idx in range(self.layers-1):
Delta.append(self.a[idx].T*delta[-(idx+1)])
self.weight_gradient = []
for idx in range(len(Delta)):
self.weight_gradient.append(numpy.nan_to_num(1/m*Delta[idx] + numpy.vstack((numpy.zeros((1, self.weights[idx].shape[1])), lamda/m*self.weights[idx][1:, :]))))
def train(self, input, targets, alpha, lamda, iterations = 1000):
#alpha: learning rate
#lamda: regularization term
for i in range(iterations):
self.update(input)
self.backPropagate(targets, lamda)
self.weights = [self.weights[idx] - alpha*self.weight_gradient[idx] for idx in range(len(self.weights))]
def predict(self, input):
self.update(input)
return self.a[-1]
Pero no funciona = (Inspeccionar el costo vs. iteración puedo ver un pequeño punto en el costo y la predicción para A es todo lo mismo ¿Puede alguien ayudarme a entender por qué.. mi red neuronal no está convergiendo
Gracias, lo sentimos acerca de la cantidad de código (tal vez alguien le resultará útil)
actualización:?.
En lugar de utilizar datos aleatorios tengo un poco estructurada datos del representante de aprendizaje de máquina UCI Ository. El conjunto de datos en particular es el área quemada de incendios forestales, en la región noreste de Portugal, a partir de datos meteorológicos y otros: http://archive.ics.uci.edu/ml/datasets/Forest+Fires he modificado los datos, de modo que los días y meses eran números: https://docs.google.com/spreadsheet/ccc?key=0Am3oTptaLsExdC1PeXl1eTczRnRNejl3QUo5RjNLVVE
data = numpy.loadtxt(open('FF-data.csv', 'rb'), delimiter = ',', skiprows = 1)
features = data[:,0:11]
targets = numpy.matrix(data[:,12]).T
nfeatures = (features-features.mean(axis=0))/features.std(axis=0)
n = NN([11, 10, 1]) #The class takes the list of how many nodes in each layer
n.train(nfeatures, targets, 0.003, 0.0)
import matplotlib.pyplot
matplotlib.pyplot.subplot(221)
matplotlib.pyplot.plot(n.cost)
matplotlib.pyplot.title('Cost vs. Iteration')
matplotlib.pyplot.subplot(222)
matplotlib.pyplot.scatter(n.predict(nfeatures), targets)
matplotlib.pyplot.title('Data vs. Predicted')
matplotlib.pyplot.savefig('Report.png', format = 'png')
matplotlib.pyplot.close()
¿Por qué el costo tope alrededor de 4000 y por qué los datos Vs. ¿Previsto no tener ninguna tendencia? Se puede ver los gráficos aquí: https://docs.google.com/open?id=0B23oTptaLsExMTQ0OTAxNWEtYjE2NS00MjA5LTg1MjMtNDBhYjVmMTFhZDhm
Ejecuto tu código en algunos ejemplos simples, y parece que está funcionando bien. ¿Por qué? Creo que no está funcionando En su ejemplo, el costo disminuye muy rápido en las primeras cien iteraciones y luego se mantiene aproximadamente igual, que es, diría yo, el comportamiento esperado. – Sonya
Gracias por ejecutar mi código. ¿Cuál es la salida de n.predict (A)? Para mí, todos los valores predichos son los mismos independientemente de las características de entrada (por lo general, cerca del promedio (Objetivos). Por ejemplo, Objetivos = [4, 5, 6,7] n .predict (características) = [5.5, 5.5, 5.5, 5.5]. – user1068430
¡Utilice pandas para datos de series de tiempo! – ken