Lo scorso semestre ho seguito un corso di apprendimento automatico online da Standford tenuto dal professor Ng. http://www.ml-class.org/course/auth/welcome Ho pensato che fosse piuttosto informativo. Per rispolverare/comprendere meglio le reti neurali ho provato a scrivere il mio in python. Eccolo:Cercando di scrivere la mia rete neurale in 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]
Ma non funziona = (Controllo del costo vs iterazione posso vedere un puntino nel costo e la previsione per la A è lo stesso Qualcuno può aiutarmi a capire il perché.. la mia rete neurale non convergente
Grazie, Ci scusiamo per la quantità di codice (forse qualcuno lo troverà utile)
Update:?.
Invece di utilizzare dati casuali ho un po 'strutturato dati dall'UIC Machine Learning Rep ository. La particolare set di dati è l'area bruciata degli incendi boschivi, nella regione nord-est del Portogallo, utilizzando i dati meteorologici e altro: http://archive.ics.uci.edu/ml/datasets/Forest+Fires Ho modificato i dati in modo che i giorni ei mesi sono stati i numeri: 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()
Perché il costo scalare intorno a 4000 e perché il Data vs. Predicted non ha alcuna tendenza? È possibile vedere i grafici qui: https://docs.google.com/open?id=0B23oTptaLsExMTQ0OTAxNWEtYjE2NS00MjA5LTg1MjMtNDBhYjVmMTFhZDhm
Ho eseguito il tuo codice su alcuni semplici esempi, e sembra che stia funzionando tutto bene. Nel tuo esempio il costo diminuisce molto velocemente nelle prime cento iterazioni e poi rimane circa lo stesso, il che è, direi, il comportamento previsto – Sonya
Grazie per aver eseguito il mio codice. rete quale è l'output di n.predict (A)? Per me tutti i valori previsti sono gli stessi indipendentemente dalle caratteristiche di input (in genere vicino alla media (Target). Ad esempio Target = [4, 5, 6,7] n .predict (caratteristiche) = [5.5, 5.5, 5.5, 5.5]. – user1068430
Utilizzare i panda per i dati delle serie temporali! – ken