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
|
||||
|
||||
class Network:
|
||||
def __init__(self, layers):
|
||||
def __init__(self, layers, W_file_list, b_file_list):
|
||||
self.layers = layers
|
||||
self.W_file_list = W_file_list
|
||||
self.b_file_list = b_file_list
|
||||
self.n_layers = 4
|
||||
self.n_inputs = 784
|
||||
self.n_outputs = 10
|
||||
self.n = [self.n_inputs, 512, 256, self.n_outputs]
|
||||
self.x = np.random.rand(self.n_inputs)
|
||||
|
||||
|
||||
def run(self):
|
||||
|
||||
result = []
|
||||
for n in self.layers:
|
||||
l = n(self.x, W_file = 'W_1.txt', b_file = 'b_1.txt')
|
||||
result = self.x
|
||||
for n, W_file, b_file in zip(self.layers, self.W_file_list, self.b_file_list):
|
||||
y = deepcopy(result)
|
||||
l = n(y, W_file = W_file, b_file = b_file)
|
||||
result = l.run()
|
||||
return result
|
||||
class Layer:
|
||||
def __init__(self, x, W_file=None, b_file=None):
|
||||
# define dimensions
|
||||
self.n_layers = 4
|
||||
self.n_inputs = 784
|
||||
self.n_outputs = 10
|
||||
self.n = [self.n_inputs, 512, 256, self.n_outputs]
|
||||
def __init__(self, x, W_file, b_file):
|
||||
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)
|
||||
self.W = np.load(files.get('W'))
|
||||
self.b = np.load(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):
|
||||
self.W = files.get('W')
|
||||
self.b = files.get('b')
|
||||
|
||||
return layer(self.W_list, self.b_list, self.x)
|
||||
def run(self):
|
||||
return layer(self.W, self.x, self.b)
|
||||
|
||||
def read(W_file, b_file):
|
||||
with open(W_file) as f:
|
||||
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}
|
||||
return {'W': np.loadtxt(W_file), 'b': np.loadtxt(b_file)}
|
||||
|
||||
# define activation function
|
||||
def sigma(y):
|
||||
@ -62,16 +44,9 @@ sigma_vec = np.vectorize(sigma)
|
||||
def layer(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():
|
||||
network = Network([Layer, Layer])
|
||||
network.run()
|
||||
network = Network([Layer, Layer, Layer], ['W_1.txt', 'W_2.txt', 'W_3.txt'], ['b_1.txt', 'b_2.txt', 'b_3.txt'])
|
||||
print(network.run())
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in New Issue
Block a user