Ferdig med deloppgave 2 uke 6. Nettverket funker 🥳️
This commit is contained in:
parent
ea9bf70eaa
commit
29bbee4165
55
uke6.py
55
uke6.py
@ -2,53 +2,35 @@ import numpy as np
|
|||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
|
||||||
class Network:
|
class Network:
|
||||||
def __init__(self, layers):
|
def __init__(self, layers, W_file_list, b_file_list):
|
||||||
self.layers = layers
|
self.layers = layers
|
||||||
|
self.W_file_list = W_file_list
|
||||||
|
self.b_file_list = b_file_list
|
||||||
self.n_layers = 4
|
self.n_layers = 4
|
||||||
self.n_inputs = 784
|
self.n_inputs = 784
|
||||||
self.n_outputs = 10
|
self.n_outputs = 10
|
||||||
self.n = [self.n_inputs, 512, 256, self.n_outputs]
|
self.n = [self.n_inputs, 512, 256, self.n_outputs]
|
||||||
self.x = np.random.rand(self.n_inputs)
|
self.x = np.random.rand(self.n_inputs)
|
||||||
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
result = self.x
|
||||||
result = []
|
for n, W_file, b_file in zip(self.layers, self.W_file_list, self.b_file_list):
|
||||||
for n in self.layers:
|
y = deepcopy(result)
|
||||||
l = n(self.x, W_file = 'W_1.txt', b_file = 'b_1.txt')
|
l = n(y, W_file = W_file, b_file = b_file)
|
||||||
result = l.run()
|
result = l.run()
|
||||||
return result
|
return result
|
||||||
class Layer:
|
class Layer:
|
||||||
def __init__(self, x, W_file=None, b_file=None):
|
def __init__(self, x, W_file, b_file):
|
||||||
# define dimensions
|
|
||||||
self.n_layers = 4
|
|
||||||
self.n_inputs = 784
|
|
||||||
self.n_outputs = 10
|
|
||||||
self.n = [self.n_inputs, 512, 256, self.n_outputs]
|
|
||||||
self.x = x
|
self.x = x
|
||||||
|
|
||||||
# define weights and biases
|
|
||||||
# generate random wheights if no file is provided. else read the file
|
|
||||||
files = read(W_file, b_file)
|
files = read(W_file, b_file)
|
||||||
self.W = np.load(files.get('W'))
|
self.W = files.get('W')
|
||||||
self.b = np.load(files.get('b'))
|
self.b = files.get('b')
|
||||||
print('Y dimensjon 1 lag: ', len(self.W_list[0]), len(self.b_list[0]), len(self.x))
|
|
||||||
print('Y dimensjon siste lag: ', len(self.W_list[-1]), len(self.b_list[-1]), len(self.x))
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
return layer(self.W, self.x, self.b)
|
||||||
return layer(self.W_list, self.b_list, self.x)
|
|
||||||
|
|
||||||
def read(W_file, b_file):
|
def read(W_file, b_file):
|
||||||
with open(W_file) as f:
|
return {'W': np.loadtxt(W_file), 'b': np.loadtxt(b_file)}
|
||||||
lines = f.readlines()
|
|
||||||
W_list = [x.split(' ') for x in lines]
|
|
||||||
W_list = [[float(n) for n in x] for x in W_list]
|
|
||||||
with open(b_file) as f:
|
|
||||||
lines = f.readlines()
|
|
||||||
b_list = [x.split(' ') for x in lines]
|
|
||||||
b_list = [[float(n) for n in x] for x in W_list]
|
|
||||||
|
|
||||||
return {'W': W_list, 'b': b_list}
|
|
||||||
|
|
||||||
# define activation function
|
# define activation function
|
||||||
def sigma(y):
|
def sigma(y):
|
||||||
@ -62,16 +44,9 @@ sigma_vec = np.vectorize(sigma)
|
|||||||
def layer(W, x, b):
|
def layer(W, x, b):
|
||||||
return sigma_vec(W @ x + b)
|
return sigma_vec(W @ x + b)
|
||||||
|
|
||||||
# define neural network with all weights W and all biases b in W_list and b_list
|
|
||||||
def f(W_list, b_list, x):
|
|
||||||
y = deepcopy(x) # deepcopy so that input is not changed
|
|
||||||
for W, b in zip(W_list, b_list):
|
|
||||||
y = layer(W, y, b) # call layer multiple times with all weights and biases
|
|
||||||
return y
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
network = Network([Layer, Layer])
|
network = Network([Layer, Layer, Layer], ['W_1.txt', 'W_2.txt', 'W_3.txt'], ['b_1.txt', 'b_2.txt', 'b_3.txt'])
|
||||||
network.run()
|
print(network.run())
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
Loading…
Reference in New Issue
Block a user