import HW2.graph as tfg import HW2.session as tfs apple_price = 100 apple_num = 2 tax = 1.1 g = tfg.Graph() # Create variables a = tfg.Variable(apple_price, name="a") b = tfg.Variable(apple_num, name="b") c = tfg.Variable(tax, name="c") # Create Mul operation node d = tfg.Mul(a, b, name="d") # Create Mul operation node e = tfg.Mul(d, c, name="e") session = tfs.Session() # forward total_apple_price = session.run(d, verbose=False) print("total_apple_price: {:f}".format(float(total_apple_price))) final_price = session.run(e, verbose=False) print("final_price: {:f}".format(float(final_price))) print() #backward d_in = 1
def initialize_param(self): self.param = tfg.Variable(np.random.random(size=self.shape), name=self.name)
def initialize_param(self): self.param = tfg.Variable(tff.get_truncated_normal(shape=self.shape, mean=self.mean, sd=self.sd, low=self.low, upp=self.upp), name=self.name)
def initialize_param(self): self.param = tfg.Variable(np.ones(shape=self.shape) * 0.1, name=self.name)
def initialize_param(self): self.param = tfg.Variable(np.random.normal(loc=self.mean, scale=self.sd, size=self.shape), name=self.name)
def initialize_param(self): self.param = tfg.Variable(np.random.randn(self.shape[0], self.shape[1]), name=self.name)
def initialize_param(self): self.param = tfg.Variable(np.zeros(shape=self.shape), name=self.name)
def initialize_param(self): self.param = tfg.Variable(self.value, name=self.name)
# Reference: http://www.deepideas.net/deep-learning-from-scratch-i-computational-graphs/ import networkx as nx import matplotlib.pyplot as plt import HW2.graph as tfg import HW2.session as tfs import HW2.functions as tff # Create a new graph g = tfg.Graph() g.initialize() # Create variables a = tfg.Variable(5.0, name="A") b = tfg.Variable(1.0, name="b") # Create placeholder x = tfg.Placeholder(name="x") # Create hidden node y y = tfg.Mul(a, x, name="y") # Create output node z z = tfg.Add(y, b, name="z") # nx.draw_networkx(g, with_labels=True) # plt.show(block=True) session = tfs.Session() output = session.run(z, feed_dict={x: 1.0}) print(output)
import HW2.graph as tfg import HW2.session as tfs apple_price = 100 apple_num = 2 orange_price = 150 orange_num = 3 tax = 1.1 g = tfg.Graph() # Create variables a = tfg.Variable(apple_price, name="a") b = tfg.Variable(apple_num, name="b") c = tfg.Variable(orange_price, name="c") d = tfg.Variable(orange_num, name="d") e = tfg.Variable(tax, name="e") # Create Mul operation node f = tfg.Mul(a, b, name="f") g = tfg.Mul(c, d, name="g") # Create Add operation node h = tfg.Add(f, g, name="h") # Create Mul operation node i = tfg.Mul(h, e, name="i")