-- Oct 25 In-Class Exercise Thread
import tensorflow as tf
def perceptron(weights, inputs, biases, activation):
nodes = tf.add(tf.matmul(weights,inputs), biases)
return activation(nodes)
def step(nodes):
return tf.ceil(tf.clip_by_value(nodes, 0, 1))
uniform_init = tf.random_uniform_initializer(0, 1)
- x = tf.Variable([1,0,0], dtype=tf.float32)
x = tf.placeholder(tf.float32, shape=(3,1))
W1 = tf.Variable( ([[2,2,2], [-2/3, -2/3, -2/3], [1/2.5, 1/2.5, 1/2.5]]), dtype=tf.float32)
b1 = tf.Variable([[-1],[1],[-1]], dtype=tf.float32)
W2 = tf.Variable( (
1/2, 1/2, 1 ), dtype=tf.float32)
b2 = tf.Variable([-1], dtype=tf.float32)
- G1 checks x+y+z > 1/2
- W1 = tf.Variable([2,2,2], dtype=tf.float32)
- G2 checks x+y+z < 3/2
- W2 = tf.Variable([-2/3,-2/3,-2/3], dtype=tf.float32)
- G2 checks x+y+z > 2.5
- W3 = tf.Variable([1/2.5,1/2.5,1/2.5], dtype=tf.float32)
- b1 = tf.placeholder(tf.float32, shape=(3,3))
- W1 = tf.get_variable("W1", shape=[2, 2], initializer=uniform_init)
- b1 = tf.get_variable("b1", initializer=tf.zeros_initializer)
my_layer1 = perceptron(W1, x, b1, step)
session = tf.Session()
init = tf.global_variables_initializer()
- l1_out = tf.placeholder(tf.float32)
my_layer2 = perceptron(W2, my_layer1,b2, step)
print(session.run(init))
print(session.run(my_layer1, {x: [[0], [1], [1]]}))
print(session.run(my_layer2, {x: [[0], [1], [1]]}))
print(session.run(my_layer2, {x: [[0], [1], [1]]}))
- print(session.run(my_layer2, {x: [[1], [1], [1]]}))
- print(session.run(my_layer2, {x: [[1], [0], [1]]}))
- print(session.run(my_layer2, {x: [[0], [0], [1]]}))
- print(session.run(my_layer2, {x: [[0], [0], [0]]}))
import tensorflow as tf
def perceptron(weights, inputs, biases, activation):
nodes = tf.add(tf.matmul(weights,inputs), biases)
return activation(nodes)
def step(nodes):
return tf.ceil(tf.clip_by_value(nodes, 0, 1))
uniform_init = tf.random_uniform_initializer(0, 1)
# x = tf.Variable([1,0,0], dtype=tf.float32)
x = tf.placeholder(tf.float32, shape=(3,1))
W1 = tf.Variable( ([[2,2,2], [-2/3, -2/3, -2/3], [1/2.5, 1/2.5, 1/2.5]]), dtype=tf.float32)
b1 = tf.Variable([[-1],[1],[-1]], dtype=tf.float32)
W2 = tf.Variable( ([[1/2, 1/2, 1]]), dtype=tf.float32)
b2 = tf.Variable([-1], dtype=tf.float32)
# G1 checks x+y+z > 1/2
# W1 = tf.Variable([2,2,2], dtype=tf.float32)
# G2 checks x+y+z < 3/2
# W2 = tf.Variable([-2/3,-2/3,-2/3], dtype=tf.float32)
# G2 checks x+y+z > 2.5
# W3 = tf.Variable([1/2.5,1/2.5,1/2.5], dtype=tf.float32)
# b1 = tf.placeholder(tf.float32, shape=(3,3))
# W1 = tf.get_variable("W1", shape=[2, 2], initializer=uniform_init)
# b1 = tf.get_variable("b1", initializer=tf.zeros_initializer)
my_layer1 = perceptron(W1, x, b1, step)
session = tf.Session()
init = tf.global_variables_initializer()
# l1_out = tf.placeholder(tf.float32)
#OutG1+OutG2+2OutG3≥2
my_layer2 = perceptron(W2, my_layer1,b2, step)
print(session.run(init))
print(session.run(my_layer1, {x: [[0], [1], [1]]}))
print(session.run(my_layer2, {x: [[0], [1], [1]]}))
print(session.run(my_layer2, {x: [[0], [1], [1]]}))
# print(session.run(my_layer2, {x: [[1], [1], [1]]}))
# print(session.run(my_layer2, {x: [[1], [0], [1]]}))
# print(session.run(my_layer2, {x: [[0], [0], [1]]}))
# print(session.run(my_layer2, {x: [[0], [0], [0]]}))
#