""" NOTE: Here we're using an Input node for more than a scalar. In the case of weights and inputs the value of the Input node is actually a python list! In general, there's no restriction on the values that can be passed to an Input node. """ import miniflow as mf inputs, weights, bias = (mf.Input() for _ in range(3)) res = mf.Linear(inputs, weights, bias) feed_dict = {inputs: [6, 14, 3], weights: [0.5, 0.25, 1.4], bias: 2} graph = mf.topological_sort(feed_dict) print(list(graph[i].value for i in range(3))) output = mf.forward_pass(res, graph) print(output) # should be 12.7 with this example
import numpy as np from miniflow import Input from miniflow import Linear from miniflow import Sigmoid from miniflow import topological_sort from miniflow import forward_pass from miniflow import MSE inputs, weights, bias = Input(), Input(), Input() f = Linear(inputs, weights, bias) g = Sigmoid(f) x = np.array([[-1., -2.], [-1, -2]]) w = np.array([[2., -3], [2., -3]]) b = np.array([-3., -5]) feed_dict = {inputs: x, weights: w, bias: b} graph = topological_sort(feed_dict) output = forward_pass(g, graph) ideal_output = np.array([[1.23394576e-04, 9.82013790e-01], [1.23394576e-04, 9.82013790e-01]]) cost = MSE(output, ideal_output, 1) # there's only 1 input """ Output should be on the order of 1e-22. """ print(cost)
except you're now using NumPy arrays instead of python lists. Update the Linear class in miniflow.py to work with numpy vectors (arrays) and matrices. Test your code here! """ import numpy as np import miniflow as mf X, W, b = (mf.Input() for _ in range(3)) f = mf.Linear(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 = mf.topological_sort(feed_dict) output = mf.forward_pass(f, graph) """ Output should be: [[-9., 4.], [-9., 4.]] """ print(output)
# ---------------------------------------------- X, W, b = Input(), Input(), Input() f = Linear(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) forward_pass(graph) print(g.value) # ---------------------------------------------- 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(graph) print(cost.value)
""" This script builds and runs a graph with miniflow. There is no need to change anything to solve this quiz! However, feel free to play with the network! Can you also build a network that solves the equation below? (x + y) + y """ import miniflow as mf x, y, z = (mf.Input() for _ in range(3)) f = mf.Add(x, y, z) f_mul = mf.Mul(x, y, z) feed_dict = {x: 10, y: 5, z: 20} sorted_nodes = mf.topological_sort(feed_dict) output_add = mf.forward_pass(f, sorted_nodes) output_mul = mf.forward_pass(f_mul, sorted_nodes) # NOTE: because topological_sort set the values for the `Input` nodes we could also access # the value for x with x.value (same goes for y). print("{} + {} + {} = {}, mul: {}(according to miniflow)".format( feed_dict[x], feed_dict[y], feed_dict[z], output_add, output_mul))
from miniflow import Input from miniflow import Linear from miniflow import topological_sort from miniflow import forward_pass x, y, z = Input(), Input(), Input() inputs = [x, y, z] weight_x, weight_y, weight_z = Input(), Input(), Input() weights = [weight_x, weight_y, weight_z] bias = Input() f = Linear(inputs, weights, bias) feed_dict = { x: 6, y: 14, z: 3, weight_x: 0.5, weight_y: 0.25, weight_z: 1.4, bias: 2 } graph = topological_sort(feed_dict) output = forward_pass(f, graph) print('Linear output: ', output) # should be 12.7 with this example
""" Test your MSE method with this script! No changes necessary, but feel free to play with this script to test your network. """ import numpy as np import miniflow as mf y, a = (mf.Input() for _ in range(2)) cost = mf.MSE(y, a) y_ = np.array([1, 2, 3]) a_ = np.array([4.5, 5, 10]) feed_dict = {y: y_, a: a_} graph = mf.topological_sort(feed_dict) # forward pass output = mf.forward_pass(cost, graph) """ Expected output 23.4166666667 """ print(output)