Vet ikke hva jeg gjør
This commit is contained in:
parent
d5c072ba3f
commit
ea9bf70eaa
1
.~lock.2021-09-14_party distribution_1_st_2021.csv#
Normal file
1
.~lock.2021-09-14_party distribution_1_st_2021.csv#
Normal file
@ -0,0 +1 @@
|
||||
,trygve,trygves-laptop,19.09.2023 21:12,file:///home/trygve/.config/libreoffice/4;
|
1
uke3.py
1
uke3.py
@ -50,4 +50,3 @@ def print_imp_dir(path="./"):
|
||||
print(f'{Path.cwd()}/{f}: {get_imp_file(f)}')
|
||||
|
||||
print_imp_dir()
|
||||
# %%
|
||||
|
104
uke6.py
104
uke6.py
@ -2,46 +2,53 @@ import numpy as np
|
||||
from copy import deepcopy
|
||||
|
||||
class Network:
|
||||
def __init__(self):
|
||||
self.layers = []
|
||||
|
||||
class Layer:
|
||||
def __init__(self, w_file=None, b_file=None):
|
||||
# define dimensions
|
||||
def __init__(self, layers):
|
||||
self.layers = layers
|
||||
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 = 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]
|
||||
self.x = x
|
||||
|
||||
# define weights and biases
|
||||
# generate random wheights if no file is provided. else read the file
|
||||
if w_file == None:
|
||||
self.W_list = []
|
||||
for (self.n_cur, self.n_next) in zip(self.n[:-1], self.n[1:]):
|
||||
self.W_list.append(np.random.rand(self.n_next, self.n_cur))
|
||||
else:
|
||||
with open(w_file) as f:
|
||||
lines = f.readlines()
|
||||
self.W_list = [x.split(' ') for x in lines]
|
||||
self.W_list = [[float(n) for n in x] for x in self.W_list]
|
||||
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):
|
||||
|
||||
f.close()
|
||||
if b_file == None:
|
||||
self.b_list = []
|
||||
for (self.n_cur, self.n_next) in zip(self.n[:-1], self.n[1:]):
|
||||
self.b_list.append(np.random.rand(self.n_next))
|
||||
else:
|
||||
return layer(self.W_list, self.b_list, self.x)
|
||||
|
||||
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()
|
||||
self.b_list = [x.split(' ') for x in lines]
|
||||
self.b_list = [[float(n) for n in x] for x in self.W_list]
|
||||
b_list = [x.split(' ') for x in lines]
|
||||
b_list = [[float(n) for n in x] for x in W_list]
|
||||
|
||||
f.close()
|
||||
print(len(self.W_list[0]), len(self.b_list[0]), len(self.x))
|
||||
print(len(self.W_list[-1]), len(self.b_list[-1]), len(self.x))
|
||||
def run(self):
|
||||
print(f(self.W_list, self.b_list, self.x))
|
||||
return {'W': W_list, 'b': b_list}
|
||||
|
||||
# define activation function
|
||||
def sigma(y):
|
||||
@ -63,45 +70,8 @@ def f(W_list, b_list, x):
|
||||
return y
|
||||
|
||||
def main():
|
||||
l = Layer()
|
||||
l.run()
|
||||
l2 = Layer(w_file = 'W_1.txt', b_file = 'b_1.txt')
|
||||
l2.run()
|
||||
|
||||
def gamle_greier():
|
||||
# define dimensions
|
||||
n_layers = 4
|
||||
n_inputs = 64
|
||||
n_outputs = 10
|
||||
n = [n_inputs, 128, 128, n_outputs]
|
||||
|
||||
# define weights and biases
|
||||
W_list = []
|
||||
b_list = []
|
||||
for (n_cur, n_next) in zip(n[:-1], n[1:]):
|
||||
W_list.append(np.random.rand(n_next, n_cur))
|
||||
b_list.append(np.random.rand(n_next))
|
||||
|
||||
# generate random input (this would usually be pixels of an image)
|
||||
x = np.random.rand(n_inputs)
|
||||
|
||||
# call the network
|
||||
print(f(W_list, b_list, x))
|
||||
|
||||
for W in W_list:
|
||||
print(W.shape)
|
||||
network = Network([Layer, Layer])
|
||||
network.run()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
gamle_greier()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user