-- Oct 25 In-Class Exercise Thread
import numpy as np
import tensorflow as tf
def perceptron(weights, inputs, biases, activation, silent=True):
if not silent:
print(f'Shape of weights: {tf.shape(weights)}')
print(f'Shape of inputs: {tf.shape(inputs)}')
nodes = tf.matmul(weights, inputs) + biases
return activation(nodes)
def step(nodes):
"""
Step activation fnc
"""
return tf.ceil(tf.clip_by_value(nodes, 0, 1))
def class_exercise():
"""
For Oct 25 exercise.
"""
# Data
x_train = [
np.array([1, 0, 0]),
np.array([0, 1, 0]),
np.array([0, 0, 1]),
np.array([1, 1, 1]),
np.array([0, 0, 0]),
np.array([1, 1, 0]),
np.array([0, 1, 1]),
np.array([1, 0, 1])
]
y_train = np.array([1, 1, 1, 1, 0, 0, 0, 0])
# Model Input
x = tf.placeholder(tf.float32, shape=(3, 1))
y = tf.placeholder(tf.float32)
# Perceptron init
uniform_init = tf.random_uniform_initializer(0, 1)
W1 = tf.get_variable("W1", shape=(3,3), initializer = uniform_init)
b1 = tf.get_variable("b1", shape=(3,1), initializer = uniform_init)
W4 = tf.get_variable("W4", shape=(1, 3), initializer = uniform_init)
b4 = tf.get_variable("b4", shape=(1,1), initializer = uniform_init)
# Init TF Session
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
# Train
l1 = perceptron(W1, x, b1, step, silent=False)
l2 = perceptron(W4, l1, b4, step, silent=False)
for i in range(8):
print(sess.run(l2, {x: x_train[i]}))
class_exercise()
(
Edited: 2017-10-29)
import numpy as np
import tensorflow as tf
def perceptron(weights, inputs, biases, activation, silent=True):
if not silent:
print(f'Shape of weights: {tf.shape(weights)}')
print(f'Shape of inputs: {tf.shape(inputs)}')
nodes = tf.matmul(weights, inputs) + biases
return activation(nodes)
def step(nodes):
"""
Step activation fnc
"""
return tf.ceil(tf.clip_by_value(nodes, 0, 1))
def class_exercise():
"""
For Oct 25 exercise.
"""
# Data
x_train = [
np.array([1, 0, 0]),
np.array([0, 1, 0]),
np.array([0, 0, 1]),
np.array([1, 1, 1]),
np.array([0, 0, 0]),
np.array([1, 1, 0]),
np.array([0, 1, 1]),
np.array([1, 0, 1])
]
y_train = np.array([1, 1, 1, 1, 0, 0, 0, 0])
# Model Input
x = tf.placeholder(tf.float32, shape=(3, 1))
y = tf.placeholder(tf.float32)
# Perceptron init
uniform_init = tf.random_uniform_initializer(0, 1)
W1 = tf.get_variable("W1", shape=(3,3), initializer = uniform_init)
b1 = tf.get_variable("b1", shape=(3,1), initializer = uniform_init)
W4 = tf.get_variable("W4", shape=(1, 3), initializer = uniform_init)
b4 = tf.get_variable("b4", shape=(1,1), initializer = uniform_init)
# Init TF Session
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
# Train
l1 = perceptron(W1, x, b1, step, silent=False)
l2 = perceptron(W4, l1, b4, step, silent=False)
for i in range(8):
print(sess.run(l2, {x: x_train[i]}))
class_exercise()