-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrainable.py
More file actions
62 lines (47 loc) · 1.35 KB
/
trainable.py
File metadata and controls
62 lines (47 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import numpy as np
import random
from abc import ABC
class Trainable(ABC):
pass
def __init__(self):
self.val = 0.0
def update():
pass
def __add__(self,other):
return self.val + other
def __radd__(self,other):
return self.val + other
def __mul__(self,other):
return self.val * other
def __lmul__(self,other):
return other * self.val
def __truediv__(self,other):
return self.val / other
def reset(self):
pass
class Weight(Trainable):
"""
Create a weight vector for typical dense layer neurons
"""
def __init__(self,size):
if size:
self.size = size
self.val = np.sqrt(1/size) * self.createRandoms(-1,1,size)
def reset(self, size):
if size:
self.size = size
self.val = np.sqrt(1/size) * self.createRandoms(-1,1,size)
def createRandoms(self, min, max, quantity):
randoms = np.array([])
for _ in range(quantity):
randoms = np.append(randoms, random.uniform(min,max))
return randoms
def dot(self, other: np.ndarray):
return self.val.dot(other)
def shape(self):
return self.val.shape
class Bias(Trainable):
def __init__(self):
self.val = 0.0
def reset(self):
self.val = 0.0