-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.py
More file actions
104 lines (86 loc) · 3.59 KB
/
Node.py
File metadata and controls
104 lines (86 loc) · 3.59 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'''
Nodes can be variables ,placeholders or operations.
'''
import random
import tinyTensor.Graph
class Node():
# variable node constructor
def __init__(self):
self.value = None
self.isPlaceholder = False
self.isDropout = False
self.isDropoutActive = False # dropout should only be active during training.
self.dropoutPercentage = 0
self.name = ""
self.inputNodes = []
self.step = 0 # the current learning / computation step (prevents nodes being computed twice or more)
@classmethod
def variable(cls,value,name=""):
if (value == None):
raise Exception("A variable node cannot have a value of 'None'")
variableNode = Node()
variableNode.value = value
variableNode.name = name
tinyTensor.Graph._default_graph.appendNode(variableNode)
return variableNode
@classmethod
def placeholder(cls,name,value=None):
if(name == None):
raise Exception("Placeholders need to be assigned a unique name to allow their value to be changed via feed dictionary.")
placeholderNode = Node()
placeholderNode.name = name
placeholderNode.value = value
placeholderNode.isPlaceholder = True
tinyTensor.Graph._default_graph.appendNode(placeholderNode)
return placeholderNode
@classmethod
def dropout(cls,dropout_percentage: float = 0):
if(dropout_percentage > 1 or dropout_percentage < 0):
raise Exception("invalid dropout percentage {}, value needs to be between 0 and 1 (inclusive)")
dropoutNode = Node()
dropoutNode.name = "dropout"
dropoutNode.isDropout = True
dropoutNode.dropoutPercentage = dropout_percentage
tinyTensor.Graph._default_graph.appendNode(dropoutNode)
return dropoutNode
def compute(self,step):
if (self.step == step):
return self
else:
self.step = step
if(self.isDropout):
random.seed()
rand = random.randrange(1001)/1000
if(rand < self.dropoutPercentage):
print("DROP {}:{}".format(self.dropoutPercentage,rand))
self.value = 0
else:
print("OK {}:{}".format(self.dropoutPercentage, rand))
self.value = self.inputNodes[0].value
return self
def addInputs(self,nodes):
if(not isinstance(nodes,list)):
nodes = [nodes]
if(not all(isinstance(x,Node) for x in nodes)):
raise Exception("nodes parameter should be a list of objects of type Node")
elif(self.isDropout and ((len(nodes) + len(self.inputNodes)) != 1)):
raise Exception("dropout nodes need to have 1 and only 1 input.")
self.inputNodes.extend(nodes)
def setInputs(self,nodes):
self.inputNodes.clear()
self.addInputs(nodes)
def __add__ (self,other):
operation = tinyTensor.Operation.Operation([self, other], "+")
return operation
def __sub__ (self,other):
operation = tinyTensor.Operation.Operation([self, other], "-")
return operation
def __mul__ (self,other):
operation = tinyTensor.Operation.Operation([self, other], "*")
return operation
def __truediv__ (self,other):
operation = tinyTensor.Operation.Operation([self, other], "/")
return operation
def __mod__ (self,other):
operation = tinyTensor.Operation.Operation([self, other], "%")
return operation