-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.py
More file actions
105 lines (92 loc) · 4.16 KB
/
Graph.py
File metadata and controls
105 lines (92 loc) · 4.16 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
105
#from tinyTensor.Node import Node
import tinyTensor
import plotly.plotly as py
from graphviz import render
#from tinyTensor.Operation import Operation
def init():
global _default_graph
_default_graph = None
def postOrder(node):
nodes_postorder = []
def recurse(node):
if isinstance(node, tinyTensor.Node.Node):
for input_node in node.inputNodes:
recurse(input_node)
nodes_postorder.append(node)
recurse(node)
return nodes_postorder
class Graph():
def __init__(self):
self.nodes = []
self.placeholderNames = []
def appendNode(self,node):
if(node.name in self.placeholderNames and node.isPlaceholder):
raise Exception("Placeholder name \"{}\" is already in use in current graph".format(node.name))
elif(node.isPlaceholder):
self.placeholderNames.append(node.name)
self.nodes.append(node)
def set_default(self):
init()
global _default_graph
_default_graph = self
def visualize_nodes(self, node):
# generating the .gv file
gv_file = "graph \"\" \n{\n"
global nodeCounter
nodeCounter = 0
def recurse(nodes,gv_file,parent_node_str = None):
global nodeCounter
nodes_list = []
if(isinstance(nodes,list)):
nodes_list.extend(nodes)
else:
nodes_list.append(nodes)
for node in nodes_list:
# node should add itself to the list
current_node_str = "n" + str(nodeCounter)
nodeCounter += 1
''' operation might contain non-node constants, hence need to make sure that they are converted to node'''
if(type(node) in (int,float)):
node = tinyTensor.Node.Node.variable(node) # creating a variable node
'''creating the node labels'''
if(isinstance(node,tinyTensor.Operation.Operation)):
gv_file += current_node_str + " [label=\"{} ({})\"] ;\n".format(node.operator,node.value)
elif(node.isPlaceholder):
gv_file += current_node_str + " [label=\"{}({})\"] ;\n".format(node.name,node.value)
else:
gv_file += current_node_str + " [label=\"{}({})\"] ;\n".format(node.name,node.value)
# now creating connection line to parent(s) TODO: make it possible to have many parents, (nodes should have output nodes list)
if(parent_node_str != None):
gv_file += parent_node_str + " -- " + current_node_str + "; \n"
# applying the same to the children of this node
if(len(node.inputNodes) > 0):
gv_file = recurse(node.inputNodes,gv_file,current_node_str)
return gv_file
gv_file = recurse(node,gv_file)
gv_file += "}\n"
with open("network.gv","w+") as file:
file.writelines(gv_file)
#render('dot','png','network.gv')
print(gv_file)
def visualize_layers(self,layer_list):
neuron_dict = {}
#generating dict of neurons
gv_file = "graph \"\" \n{\n"
#dealing with input nodes
for node in layer_list[0].inputList:
neuron_dict[node] = node.name
gv_file += neuron_dict[node] + " [label=\"{}({})\"] ;\n".format(node.name,node.value)
# creating dict for neurons
for layer in layer_list:
for neuron in layer.neuronList:
neuron_dict[neuron] = "{}".format(neuron.name)
gv_file += neuron_dict[neuron] + " [label=\"{}({})\"] ;\n".format(neuron.name,neuron.value)
# drawing links between neurons
for layer in layer_list:
for neuron in layer.neuronList:
for input_neuron in neuron.inputNeurons:
gv_file += neuron_dict[neuron] + " -- " + neuron_dict[input_neuron] + "; \n"
gv_file += "}\n"
with open("network.gv","w+") as file:
file.writelines(gv_file)
print(gv_file)