zero_shubham1

manual_NN

Mar 13th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.09 KB | None | 0 0
  1.  
  2. # coding: utf-8
  3.  
  4. # In[37]:
  5.  
  6.  
  7. #opertaion class
  8.  
  9. class Operation():
  10.     def __init__(self,input_nodes=[]):
  11.         self.input_nodes = input_nodes
  12.         self.output_nodes = []
  13.        
  14.         for node in input_nodes:
  15.             node.output_nodes.append(self)
  16.        
  17.         _default_graph.operations.append(self)
  18.     def compute(self):
  19.             pass
  20.  
  21.  
  22. # In[38]:
  23.  
  24.  
  25. #class add extending class operation
  26.  
  27. class add(Operation):
  28.     def __init__(self,x,y):
  29.         super().__init__([x,y])
  30.     def compute(self,x_var,y_var):
  31.         self.inputs = [x_var,y_var]
  32.         return x_var + y_var
  33.  
  34.  
  35. # In[39]:
  36.  
  37.  
  38. #class multiply extending class operation
  39.  
  40. class multiply(Operation):
  41.     def __init__(self,x,y):
  42.         super().__init__([x,y])
  43.     def compute(self,x_var,y_var):
  44.         self.inputs = [x_var,y_var]
  45.         return x_var * y_var
  46.  
  47.  
  48. # In[40]:
  49.  
  50.  
  51. #class matmul extending class operation
  52.  
  53. class matmul(Operation):
  54.     def __init__(self,x,y):
  55.         super().__init__([x,y])
  56.     def compute(self,x_var,y_var):
  57.         self.inputs = [x_var,y_var]
  58.         return x_var.dot(y_var)
  59.  
  60.  
  61. # In[41]:
  62.  
  63.  
  64. class Placeholder():
  65.     def __init__(self):
  66.         self.output_nodes = []
  67.         _default_graph.placeholders.append(self)
  68.  
  69.  
  70. # In[42]:
  71.  
  72.  
  73. class Variable():
  74.     def __init__(self,initial_value=None):
  75.         self.value = initial_value
  76.         self.output_nodes = []
  77.        
  78.         _default_graph.variables.append(self)
  79.  
  80.  
  81. # In[43]:
  82.  
  83.  
  84. class Graph():
  85.     def __init__(self):
  86.         self.operations = []
  87.         self.placeholders = []
  88.         self.variables = []
  89.        
  90.     def set_as_default(self):
  91.         global _default_graph
  92.         _default_graph = self
  93.  
  94.  
  95. # In[44]:
  96.  
  97.  
  98. def traverse_postorder(operation):
  99.     nodes_postorder = []
  100.     def recurse(node):
  101.         if isinstance(node, Operation):
  102.             for input_node in node.input_node:
  103.                 recurse(input_node)
  104.         node_postorder.append(node)
  105.     recurse(operation)
  106.     return nodes_postorder
  107.  
  108.  
  109. # In[45]:
  110.  
  111.  
  112. class Session():
  113.     def run(self,operation,feed_dict={}):
  114.         nodes_postorder = traverse_postorder(operation)
  115.         for node in nodes_postorder:
  116.             if type(node) == Placeholder:
  117.                 node.output = feed_dict[node]
  118.                
  119.             elif type(node) == Variable:
  120.                 node.output = node.value
  121.                
  122.             else:#operation
  123.                 node.inputs = [input_node.output for input_node in node.input_node]
  124.                 node.output = node.compute(*node.input)
  125.                
  126.             if type(node.output) == list:
  127.                 node.output = np.array(node.output)
  128.             return operation.output
  129.  
  130.  
  131. # In[46]:
  132.  
  133.  
  134. g= Graph()
  135.  
  136.  
  137. # In[47]:
  138.  
  139.  
  140. g.set_as_default()
  141.  
  142.  
  143. # In[48]:
  144.  
  145.  
  146. A = Variable(10)
  147.  
  148.  
  149. # In[49]:
  150.  
  151.  
  152. b = Variable(1)
  153.  
  154.  
  155. # In[50]:
  156.  
  157.  
  158. x = Placeholder()
  159.  
  160.  
  161. # In[51]:
  162.  
  163.  
  164. y = multiply(A,x)
  165.  
  166.  
  167. # In[52]:
  168.  
  169.  
  170. sess = Session()
  171.  
  172.  
  173. # In[53]:
  174.  
  175.  
  176. z = add(y,b)
  177.  
  178.  
  179. # In[55]:
  180.  
  181.  
  182. import numpy as np
  183.  
  184.  
  185. # In[56]:
  186.  
  187.  
  188. result = sess.run(operation=z, feed_dict={x:10})
Add Comment
Please, Sign In to add comment