# 05_back_propagation.py# Back Propagation#----------------------------------## This python function shows how to implement back propagation# in regression and classification models.
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops
ops.reset_default_graph()
# Create graph
sess = tf.Session()
# Regression Example:# We will create sample data as follows:# x-data: 100 random samples from a normal ~ N(1, 0.1)# target: 100 values of the value 10.# We will fit the model:# x-data * A = target# Theoretically, A = 10.# Create data
x_vals = np.random.normal(1, 0.1, 100)
y_vals = np.repeat(10., 100)
x_data = tf.placeholder(shape=[1], dtype=tf.float32)
y_target = tf.placeholder(shape=[1], dtype=tf.float32)
# Create variable (one model parameter = A)
A = tf.Variable(tf.random_normal(shape=[1]))
# Add operation to graph
my_output = tf.multiply(x_data, A)
# Add L2 loss operation to graph
loss = tf.square(my_output - y_target)
# Initialize variables
init = tf.global_variables_initializer()
sess.run(init)
# Create Optimizer
my_opt = tf.train.GradientDescentOptimizer(0.02)
train_step = my_opt.minimize(loss)
# Run Loopfor i in range(100):
rand_index = np.random.choice(100)
rand_x = [x_vals[rand_index]]
rand_y = [y_vals[rand_index]]
sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
if (i+1)%25==0:
print('Step #' + str(i+1) + ' A = ' + str(sess.run(A)))
print('Loss = ' + str(sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})))
# Classification Example# We will create sample data as follows:# x-data: sample 50 random values from a normal = N(-1, 1)# + sample 50 random values from a normal = N(1, 1)# target: 50 values of 0 + 50 values of 1.# These are essentially 100 values of the corresponding output index# We will fit the binary classification model:# If sigmoid(x+A) < 0.5 -> 0 else 1# Theoretically, A should be -(mean1 + mean2)/2
ops.reset_default_graph()
# Create graph
sess = tf.Session()
# Create data
x_vals = np.concatenate((np.random.normal(-1, 1, 50), np.random.normal(3, 1, 50)))
y_vals = np.concatenate((np.repeat(0., 50), np.repeat(1., 50)))
x_data = tf.placeholder(shape=[1], dtype=tf.float32)
y_target = tf.placeholder(shape=[1], dtype=tf.float32)
# Create variable (one model parameter = A)
A = tf.Variable(tf.random_normal(mean=10, shape=[1]))
# Add operation to graph# Want to create the operstion sigmoid(x + A)# Note, the sigmoid() part is in the loss function
my_output = tf.add(x_data, A)
# Now we have to add another dimension to each (batch size of 1)
my_output_expanded = tf.expand_dims(my_output, 0)
y_target_expanded = tf.expand_dims(y_target, 0)
# Initialize variables
init = tf.global_variables_initializer()
sess.run(init)
# Add classification loss (cross entropy)
xentropy = tf.nn.sigmoid_cross_entropy_with_logits(logits=my_output_expanded, labels=y_target_expanded)
# Create Optimizer
my_opt = tf.train.GradientDescentOptimizer(0.05)
train_step = my_opt.minimize(xentropy)
# Run loopfor i in range(1400):
rand_index = np.random.choice(100)
rand_x = [x_vals[rand_index]]
rand_y = [y_vals[rand_index]]
sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
if (i+1)%200==0:
print('Step #' + str(i+1) + ' A = ' + str(sess.run(A)))
print('Loss = ' + str(sess.run(xentropy, feed_dict={x_data: rand_x, y_target: rand_y})))
# Evaluate Predictions
predictions = []
for i in range(len(x_vals)):
x_val = [x_vals[i]]
prediction = sess.run(tf.round(tf.sigmoid(my_output)), feed_dict={x_data: x_val})
predictions.append(prediction[0])
accuracy = sum(x==y for x,y in zip(predictions, y_vals))/100.
print('Ending Accuracy = ' + str(np.round(accuracy, 2)))
Step #25 A = [ 5.80915737]
Loss = [ 24.10683823]
Step #50 A = [ 8.42134571]
Loss = [ 2.82753682]
Step #75 A = [ 9.35519981]
Loss = [ 0.20543291]
Step #100 A = [ 9.85501003]
Loss = [ 0.00648173]
Step #200 A = [ 3.56371665]
Loss = [[ 1.70811594]]
Step #400 A = [ 0.37875155]
Loss = [[ 0.01940253]]
Step #600 A = [-0.77484697]
Loss = [[ 0.19809309]]
Step #800 A = [-1.0426774]
Loss = [[ 0.0866032]]
Step #1000 A = [-1.09708571]
Loss = [[ 0.1566353]]
Step #1200 A = [-1.08921075]
Loss = [[ 0.13325033]]
Step #1400 A = [-1.02976859]
Loss = [[ 0.02621525]]
Ending Accuracy = 0.99
TF/02_TensorFlow_Way/05 Implementing Back Propagation
# 05_back_propagation.py# Back Propagation#----------------------------------## This python function shows how to implement back propagation# in regression and classification models.import matplot