def backward(self, gradient=1.0): """ Initiates the chain rule on the computational graph. """ self.grad = np.ones(self.value.shape) graph = topological_sort(self) for t in reversed(graph): t.grad_fn.backward(t.grad)
def sort_types_by_dependencies(self, tag_schema_map): alldeps = [] for tag, schema in tag_schema_map.iteritems(): depset = set( [self.prefix + v[5:] for v in all_types_in_schema(schema)]) alldeps.append((tag, depset)) return topological_sort(alldeps)
def __init__(self, graph, env): self.graph = graph[1] self.expr = graph[-1] self.env = env self.top_sort = topological_sort(self.graph['V'], self.graph['A']) self.vars_to_sample = [ var for var in self.top_sort if var not in self.graph['Y'] ] self.num_to_sample = len(self.vars_to_sample)
def mulProcessor(): """ Processor node for multiplication operation """ l, m, n = Input(), Input(), Input() f2 = Mul(l, m, n) feed_dict2 = {l: 4, m: 5, n: 10} sorted_nodes2 = topological_sort(feed_dict2) output2 = forward_pass(f2, sorted_nodes2) print ("{} + {} + {} = {} (according to miniflow - mul)".format(feed_dict2[l], feed_dict2[m], feed_dict2[n], output2))
def addProcessor(): """ Processor node for add operation """ x, y, z = Input(), Input(), Input() f1 = Add(x, y, z) feed_dict1 = {x: 4, y: 5, z: 10} sorted_nodes1 = topological_sort(feed_dict1) output1 = forward_pass(f1, sorted_nodes1) print ("{} + {} + {} = {} (according to miniflow - add)".format(feed_dict1[x], feed_dict1[y], feed_dict1[z], output1))
def __init__(self, graph, env): self.env = env self.graph = graph[1] self.expr = graph[-1] self.top_sort = topological_sort(self.graph['V'], self.graph['A']) self.proposals = {} # Initialize proposal distributions self.params = [] for node in self.top_sort: if node not in self.graph['Y']: dist = self.env['det_eval'](self.graph['P'][node][1], self.env).make_copy_with_grads() self.env['buffer'][node] = dist.sample().detach() self.proposals[node] = dist self.params += dist.Parameters() self.optimizer = torch.optim.Adam(self.params, lr=1e-2)
def linearProcessor(): """ Processor node for linear operation """ inputs, weights, bias = Input(), Input(), Input() f = Linear(inputs, weights, bias) feed_dict = { inputs: [6, 14, 3], weights: [0.5, 0.25, 1.4], bias: 2 } graph = topological_sort(feed_dict) output = forward_pass(f, graph) print(output , "(according to miniflow - linear)")
def mseProcessor(): y, a = Input(), Input() cost = MSE(y, a) y_ = np.array([1, 2, 3]) a_ = np.array([4.5, 5, 10]) feed_dict = {y: y_, a: a_} graph = topological_sort(feed_dict) # forward pass forward_pass_graph(graph) """ Expected output 23.4166666667 """ print(cost.value, "(according to miniflow - MSE)")
def linearMatrixProcessor(): """ Processor node for linear matrix operation """ X, W, b = Input(), Input(), Input() f = LinearMatrix(X, W, b) X_ = np.array([[-1., -2.], [-1, -2]]) W_ = np.array([[2., -3], [2., -3]]) b_ = np.array([-3., -5]) feed_dict = {X: X_, W: W_, b: b_} graph = topological_sort(feed_dict) output = forward_pass(f, graph) """ Output should be: [[-9., 4.], [-9., 4.]] """ print(output, "(according to miniflow - LinearMatrix)")
def linearSigmoidProcessor(): """ Processor node for linear and Sigmoid operation """ X, W, b = Input(), Input(), Input() f = LinearMatrix(X, W, b) g = Sigmoid(f) X_ = np.array([[-1., -2.], [-1, -2]]) W_ = np.array([[2., -3], [2., -3]]) b_ = np.array([-3., -5]) feed_dict = {X: X_, W: W_, b: b_} graph = topological_sort(feed_dict) output = forward_pass(g, graph) """ Output should be: [[ 1.23394576e-04 9.82013790e-01] [ 1.23394576e-04 9.82013790e-01]] """ print(output, "(according to miniflow - LinearSigmoid)")