Created
April 3, 2018 15:26
-
-
Save jennyonjourney/eb51f1d02a6606b30a8faa4a6922e4ee to your computer and use it in GitHub Desktop.
NN back propagation practice (ted viewer prediction) - 1layer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import numpy as np | |
tf.set_random_seed(777) # for reproducibility | |
# Predicting animal type based on various features | |
xy = np.loadtxt('ted_main_ranking_viewer.csv', delimiter=',', dtype=np.float32) | |
X_data = xy[:, 0:-1] | |
def MinMaxScaler(x_data): | |
numerator = x_data - np.min(x_data, 0) | |
denominator = np.max(x_data, 0) - np.min(x_data, 0) | |
return numerator / (denominator + 1e-10) | |
N = X_data.shape[0] | |
y_data = xy[:, [-1]] | |
print("y has one of the following values") | |
print(np.unique(y_data)) | |
print("Shape of X data: ", X_data.shape) | |
print("Shape of y data: ", y_data.shape) | |
nb_classes = 4 | |
X = tf.placeholder(tf.float32, [None, 2]) | |
y = tf.placeholder(tf.int32, [None, 1]) # 0 ~ 6 | |
target = tf.one_hot(y, nb_classes) # one hot | |
target = tf.reshape(target, [-1, nb_classes]) | |
target = tf.cast(target, tf.float32) | |
W = tf.Variable(tf.random_normal([2, nb_classes]), name='weight') | |
b = tf.Variable(tf.random_normal([nb_classes]), name='bias') | |
def sigma(x): | |
# sigmoid function | |
# σ(x) = 1 / (1 + exp(-x)) | |
return 1. / (1. + tf.exp(-x)) | |
def sigma_prime(x): | |
# derivative of the sigmoid function | |
# σ'(x) = σ(x) * (1 - σ(x)) | |
return sigma(x) * (1. - sigma(x)) | |
# Forward propagtion | |
layer_1 = tf.matmul(X, W) + b | |
y_pred = sigma(layer_1) | |
# Loss Function (end of forwad propagation) | |
loss_i = - target * tf.log(y_pred) - (1. - target) * tf.log(1. - y_pred) | |
loss = tf.reduce_sum(loss_i) | |
# Dimension Check | |
assert y_pred.shape.as_list() == target.shape.as_list() | |
# Back prop (chain rule) | |
# How to derive? please read "Neural Net Backprop in one slide!" | |
d_loss = (y_pred - target) / (y_pred * (1. - y_pred) + 1e-7) | |
d_sigma = sigma_prime(layer_1) | |
d_layer = d_loss * d_sigma | |
d_b = d_layer | |
d_W = tf.matmul(tf.transpose(X), d_layer) | |
# Updating network using gradients | |
learning_rate = 0.001 | |
train_step = [ | |
tf.assign(W, W - learning_rate * d_W), | |
tf.assign(b, b - learning_rate * tf.reduce_sum(d_b)), | |
] | |
# Prediction and Accuracy | |
prediction = tf.argmax(y_pred, 1) | |
acct_mat = tf.equal(tf.argmax(y_pred, 1), tf.argmax(target, 1)) | |
acct_res = tf.reduce_mean(tf.cast(acct_mat, tf.float32)) | |
# Launch graph | |
with tf.Session() as sess: | |
sess.run(tf.global_variables_initializer()) | |
for step in range(500): | |
sess.run(train_step, feed_dict={X: X_data, y: y_data}) | |
if step % 10 == 0: | |
# Within 300 steps, you should see an accuracy of 100% | |
step_loss, acc = sess.run([loss, acct_res], feed_dict={ | |
X: X_data, y: y_data}) | |
print("Step: {:5}\t Loss: {:10.5f}\t Acc: {:.2%}" .format( | |
step, step_loss, acc)) | |
# Let's see if we can predict | |
pred = sess.run(prediction, feed_dict={X: X_data}) | |
for p, y in zip(pred, y_data): | |
msg = "[{}]\t Prediction: {:d}\t True y: {:d}" | |
print(msg.format(p == int(y[0]), p, int(y[0]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment