def setUp(self): """ Creating true multi-layer perceptron with one hidden layer """ np.random.seed(1337) batch_size = 16 input_size = 20 hidden_size = 40 output_size = 5 self.x_val = np.random.randn(batch_size, input_size) self.w1_val = np.random.randn(input_size, hidden_size) self.w2_val = np.random.randn(hidden_size, output_size) self.tf_x = tf.constant(self.x_val) self.tf_w1 = tf.constant(self.w1_val) self.tf_w2 = tf.constant(self.w2_val) self.tf_h = tf.nn.sigmoid(self.tf_x @ self.tf_w1) self.tf_o = tf.nn.sigmoid(self.tf_h @ self.tf_w2) self.my_x = ad.Variable(self.x_val, name="x_val") self.my_w1 = ad.Variable(self.w1_val, name="w1_val") self.my_w2 = ad.Variable(self.w2_val, name="w2_val") self.var_h = ad.Sigmoid(self.my_x @ self.my_w1) self.var_o = ad.Sigmoid(self.var_h @ self.my_w2) self.my_graph = self.var_o self.tf_graph = self.tf_o
def setUp(self): """ Graph looks like this: x_val w_val \ / MatMul | Sigmoid x_val.shape = (2, 3) w_val.shape = (3, 5) MatMul.shape = (2, 5) Sigmoid.shape = (2, 5) """ np.random.seed(1337) self.x_val = np.random.randn(2, 3) self.w_val = np.random.randn(3, 5) self.b_val = np.random.randn(5) self.tf_x = tf.constant(self.x_val, dtype=tf.float64) self.tf_w = tf.constant(self.w_val, dtype=tf.float64) self.tf_b = tf.constant(self.b_val, dtype=tf.float64) self.tf_mul = self.tf_x @ self.tf_w + self.tf_b self.tf_graph = tf.nn.sigmoid(self.tf_mul) self.my_x = ad.Variable(self.x_val, name="x_val") self.my_w = ad.Variable(self.w_val, name="w_val") self.my_b = ad.Variable(self.b_val, name="b_val") self.var_mul = self.my_x @ self.my_w + self.my_b self.my_graph = ad.Sigmoid(self.var_mul)
def output_layer(self, S_Old, X): S = S_Old val_z = ad.MatMul(X, self._Uz) + ad.MatMul(S, self._Wz) + self._bz Z = ad.Sigmoid(val_z) #print("Z",Z()) val_g = ad.MatMul(X, self._Ug) + ad.MatMul(S, self._Wg) + self._bg G = ad.Sigmoid(val_g) #print("G",G()) val_r = ad.MatMul(X, self._Ur) + ad.MatMul(S, self._Wr) + self._br R = ad.Sigmoid(val_r) #print("R",R()) val_h = ad.MatMul(X, self._Uh) + ad.MatMul(S * R, self._Wh) + self._bh H = ad.Sigmoid(val_h) #print("H",H()) S_New = ((ad.Variable(np.ones_like(G.eval())) - G) * H) + (Z * S) #print("Snew",S_New()) #val = (-G * ((ad.Variable(np.ones_like(G.eval()))- G ) * H) * (self._Ug+self._Wg*self._Wg*temp)) + (((ad.Variable(np.ones_like(G.eval()))- G ) * H*(ad.Variable(np.ones_like(H.eval()))- H ))*(self._Uz+self._Wh*self._Wg*R*temp)+ (self._Wg*S*(ad.Variable(np.ones_like(R.eval()))- R ) * R*(self._Ug+self._Wg*self._Wg*temp))) +(Z*self._Wg*temp) + (S*(self._Ug+self._Wg*self._Wg*temp)) #print(val()) return S_New
def output(self, X): S = ad.Sigmoid(ad.MatMul(X, self._W) + self._B) #print("S:",S()) S1 = self.layer1.output_layer(S, X) S_list = [] S_list.append(S1) for i in range(self.number_of_layers): S_list.append(self.layers[i].output_layer(S_list[i], X)) S_final = S_list[-1] #print(S_final.shape) #print(self.Wf.shape) #print(self.Bf.shape) val = ad.MatMul(S_final, self._Wf) + self._Bf #print("The output:",val()) return val
def test_sigmoid(self): my_graph = ad.Sigmoid(self.my_w0) tf_graph = tf.nn.sigmoid(self.tf_w0) wrt_vars = [self.my_w0] tf_vars = [self.tf_w0] utils.custom_test(self, my_graph, wrt_vars, tf_graph, tf_vars)