Jan-27-2022, 07:06 PM
Hi,
I am trying to build a neural network this is my code
I do not get why this happens since in line 80 I make the same call yet no error is raised.
Thanks for your time!
I am trying to build a neural network this is my code
import numpy as np
import random
class QuadraticCost(object):
@staticmethod
def cost(a,y):
return 0.5 * np.linalg.norm(a-y) ** 2
def delta_L(a,y,z):
return (a-y)*sigmoid_derivative(z)
class CrossEntropyCost(object):
@staticmethod
def cost(a,y):
return np.sum(np.nan_to_num(-y*np.log(a)-(1-y)*np.log(1-a)))
@staticmethod
def delta_L(a,y,z):
return a-y
class FullyConnectedNN(object):
def __init__(self, sizes,cost=CrossEntropyCost):
self.num_layers = len(sizes)
self.sizes = sizes
self.cost_function = cost
self.init_weights()
def init_weights(self):
self.biases = [np.random.rand(bias,1) for bias in self.sizes[1:]]
self.weights = [np.random.randn(i,j)/np.sqrt(j) for i,j in zip(self.sizes[1:],self.sizes[:-1])]
def feedforward(self, a):
for b, w in zip(self.biases, self.weights):
a = self.sigmoid(np.dot(w, a)+b)
print(len(a))
print(a.shape)
return a
def SGD(self,training_data,learning_rate,batch_size,epochs,test_data=None):
for i in range(epochs):
print("Epoch " + str(i))
#for some reason this is buggy
#training_data = random.shuffle(training_data)
for k in range(0,len(training_data),batch_size):
mini_batch = training_data[k:k+batch_size]
self.update_mini_batch(mini_batch,learning_rate)
def update_mini_batch(self, mini_batch, learning_rate):
biases = [np.zeros(b.shape) for b in self.biases]
weights = [np.zeros(w.shape) for w in self.weights]
for x,y in mini_batch:
updated_b, updated_w = self.backpropagation(x,y)
biases = [b+upd_b for b,upd_b in zip(biases,updated_b)]
weights = [w+upd_w for w,upd_w in zip(weights,updated_w)]
self.weights = [weight - (learning_rate/len(mini_batch))*upd_w \
for weight,upd_w in zip(self.weights, weights)]
self.biases = [bias - (learning_rate/len(mini_batch))*upd_b \
for bias,upd_b in zip(self.biases,biases)]
def backpropagation(self, x, y):
nabla_b = [np.zeros(b.shape) for b in self.biases]
nabla_w = [np.zeros(w.shape) for w in self.weights]
# feedforward
activation = x
activations = [x] # list to store all the activations, layer by layer
zs = [] # list to store all the z vectors, layer by layer
for b, w in zip(self.biases, self.weights):
z = np.dot(w, activation)+b
zs.append(z)
activation = sigmoid(z)
activations.append(activation)
# backward pass
delta = (self.cost_function).delta_L(zs[-1], activations[-1], y)
nabla_b[-1] = delta
nabla_w[-1] = np.dot(delta, activations[-2].transpose())
for l in range(2, self.num_layers):
z = zs[-l]
sp = sigmoid_derivative(z)
delta = np.dot(self.weights[-l+1].transpose(), delta) * sp
nabla_b[-l] = delta
nabla_w[-l] = np.dot(delta, activations[-l-1].transpose())
return (nabla_b, nabla_w)
def sigmoid(z):
return 1.0/(1.0+np.exp(-z))
def sigmoid_derivative(z):
return sigmoid(z) * (1-sigmoid(z))
def save(self, file):
data = {"sizes": self.sizes,
"weights": [w.tolist() for w in self.weights],
"biases": [b.tolist() for b in self.biases],
"cost": str(self.cost.__name__)}
f = open(file, "w")
json.dump(data, f)
f.close()
def load(file):
f = open(file, "r")
data = json.load(f)
f.close()
cost = getattr(sys.modules[__name__], data["cost"])
net = Network(data["sizes"], cost=cost)
net.weights = [np.array(w) for w in data["weights"]]
net.biases = [np.array(b) for b in data["biases"]]
return net
def vectorize(y,dim):
res = np.zeros((dim,1))
res[y] = 1
return res
"""
from mnist import MNIST
mndata = MNIST('MNISTdata')
images, labels = mndata.load_training()
labels = [vectorize(label,10) for label in labels]
nn = FullyConnectedNN([784,30,10])
training_data = list(zip(images,labels))
print(nn.weights[1])
nn.SGD(training_data,1,30,1)
print(nn.weights[1])
"""
network = FullyConnectedNN([2,3,4])
"""
for i in range(len(network.weights)):
print("weight " + str(i))
print(network.weights[i])
print("bias " + str(i))
print(network.biases[i])
"""
test_data = [([1,2],7),([0,1],3)]
network.SGD(test_data,1,1,1)when i try to run it it throws an error in line 89 and says "list object has no attribute transpose()".I do not get why this happens since in line 80 I make the same call yet no error is raised.
Thanks for your time!
