In [6]:
# Linear Support Vector Machine: Soft Margin
#----------------------------------
#
# This function shows how to use TensorFlow to
# create a soft margin SVM
#
# We will use the iris data, specifically:
#  x1 = Sepal Length
#  x2 = Petal Width
# Class 1 : I. setosa
# Class -1: not I. setosa
#
# We know here that x and y are linearly seperable
# for I. setosa classification.

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from sklearn import datasets
from tensorflow.python.framework import ops
ops.reset_default_graph()

# Create graph
sess = tf.Session()

# Load the data
# iris.data = [(Sepal Length, Sepal Width, Petal Length, Petal Width)]
iris = datasets.load_iris()
x_vals = np.array([[x[0], x[3]] for x in iris.data])
y_vals = np.array([1 if y==0 else -1 for y in iris.target])

# Split data into train/test sets
train_indices = np.random.choice(len(x_vals), int(len(x_vals)*0.8), replace=False)
test_indices = np.array(list(set(range(len(x_vals))) - set(train_indices)))
x_vals_train = x_vals[train_indices]
x_vals_test = x_vals[test_indices]
y_vals_train = y_vals[train_indices]
y_vals_test = y_vals[test_indices]

# Declare batch size
batch_size = 100

# Initialize placeholders
x_data = tf.placeholder(shape=[None, 2], dtype=tf.float32)
y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)

# Create variables for linear regression
A = tf.Variable(tf.random_normal(shape=[2,1]))
b = tf.Variable(tf.random_normal(shape=[1,1]))

# Declare model operations
model_output = tf.subtract(tf.matmul(x_data, A), b)

# Declare vector L2 'norm' function squared
l2_norm = tf.reduce_sum(tf.square(A))

# Declare loss function
# Loss = max(0, 1-pred*actual) + alpha * L2_norm(A)^2
# L2 regularization parameter, alpha
alpha = tf.constant([0.01])
# Margin term in loss
classification_term = tf.reduce_mean(tf.maximum(0., tf.subtract(1., tf.multiply(model_output, y_target))))
# Put terms together
loss = tf.add(classification_term, tf.multiply(alpha, l2_norm))

# Declare prediction function
prediction = tf.sign(model_output)
accuracy = tf.reduce_mean(tf.cast(tf.equal(prediction, y_target), tf.float32))

# Declare optimizer
my_opt = tf.train.GradientDescentOptimizer(0.01)
train_step = my_opt.minimize(loss)

# Initialize variables
init = tf.global_variables_initializer()
sess.run(init)

# Training loop
loss_vec = []
train_accuracy = []
test_accuracy = []
for i in range(500):
    rand_index = np.random.choice(len(x_vals_train), size=batch_size)
    rand_x = x_vals_train[rand_index]
    rand_y = np.transpose([y_vals_train[rand_index]])
    sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
    
    temp_loss = sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})
    loss_vec.append(temp_loss)
    
    train_acc_temp = sess.run(accuracy, feed_dict={x_data: x_vals_train, y_target: np.transpose([y_vals_train])})
    train_accuracy.append(train_acc_temp)
    
    test_acc_temp = sess.run(accuracy, feed_dict={x_data: x_vals_test, y_target: np.transpose([y_vals_test])})
    test_accuracy.append(test_acc_temp)
    
    if (i+1)%100==0:
        print('Step #' + str(i+1) + ' A = ' + str(sess.run(A)) + ' b = ' + str(sess.run(b)))
        print('Loss = ' + str(temp_loss))

# Extract coefficients
[[a1], [a2]] = sess.run(A)
[[b]] = sess.run(b)
slope = -a2/a1
y_intercept = b/a1

# Extract x1 and x2 vals
x1_vals = [d[1] for d in x_vals]

# Get best fit line
best_fit = []
for i in x1_vals:
  best_fit.append(slope*i+y_intercept)

# Separate I. setosa
setosa_x = [d[1] for i,d in enumerate(x_vals) if y_vals[i]==1]
setosa_y = [d[0] for i,d in enumerate(x_vals) if y_vals[i]==1]
not_setosa_x = [d[1] for i,d in enumerate(x_vals) if y_vals[i]==-1]
not_setosa_y = [d[0] for i,d in enumerate(x_vals) if y_vals[i]==-1]

# Plot data and line
plt.plot(setosa_x, setosa_y, 'o', label='I. setosa')
plt.plot(not_setosa_x, not_setosa_y, 'x', label='Non-setosa')
plt.plot(x1_vals, best_fit, 'r-', label='Linear Separator', linewidth=3)
plt.ylim([0, 10])
plt.legend(loc='lower right')
plt.title('Sepal Length vs Pedal Width')
plt.xlabel('Pedal Width')
plt.ylabel('Sepal Length')
plt.show()

# Plot train/test accuracies
plt.plot(train_accuracy, 'k-', label='Training Accuracy')
plt.plot(test_accuracy, 'r--', label='Test Accuracy')
plt.title('Train and Test Set Accuracies')
plt.xlabel('Generation')
plt.ylabel('Accuracy')
plt.legend(loc='lower right')
plt.show()

# Plot loss over time
plt.plot(loss_vec, 'k-')
plt.title('Loss per Generation')
plt.xlabel('Generation')
plt.ylabel('Loss')
plt.show()


Step #100 A = [[ 0.07500271]
 [ 0.27466947]] b = [[ 2.04865742]]
Loss = [ 0.85233235]
Step #200 A = [[ 0.16632441]
 [-0.0785135 ]] b = [[ 1.94515848]]
Loss = [ 0.77005422]
Step #300 A = [[ 0.22219676]
 [-0.35946921]] b = [[ 1.84925878]]
Loss = [ 0.58534908]
Step #400 A = [[ 0.2618885 ]
 [-0.60698229]] b = [[ 1.76475847]]
Loss = [ 0.53734958]
Step #500 A = [[ 0.30879977]
 [-0.83936369]] b = [[ 1.68565893]]
Loss = [ 0.54511994]

In [2]:
x_vals_train


Out[2]:
array([[ 4.6,  0.2],
       [ 6. ,  1.6],
       [ 5.7,  0.4],
       [ 6.3,  1.9],
       [ 4.9,  0.1],
       [ 7.7,  2.2],
       [ 5.1,  1.1],
       [ 5.7,  0.3],
       [ 6.5,  2.2],
       [ 6.8,  2.1],
       [ 5.4,  0.2],
       [ 4.7,  0.2],
       [ 5.8,  2.4],
       [ 5.6,  1.3],
       [ 4.3,  0.1],
       [ 5.6,  2. ],
       [ 5.8,  0.2],
       [ 6.1,  1.2],
       [ 6.5,  1.8],
       [ 5.8,  1.2],
       [ 5.1,  0.2],
       [ 5.9,  1.8],
       [ 5.8,  1.9],
       [ 6. ,  1. ],
       [ 5. ,  0.4],
       [ 5. ,  0.3],
       [ 5.6,  1.5],
       [ 4.4,  0.2],
       [ 5.2,  0.2],
       [ 5.5,  0.2],
       [ 6.9,  2.3],
       [ 7.1,  2.1],
       [ 5.1,  0.3],
       [ 5. ,  0.2],
       [ 6. ,  1.5],
       [ 4.6,  0.3],
       [ 5. ,  0.2],
       [ 5.5,  1.3],
       [ 7.2,  1.6],
       [ 6.4,  2.2],
       [ 7.2,  1.8],
       [ 5. ,  1. ],
       [ 4.8,  0.1],
       [ 6.1,  1.4],
       [ 7.7,  2.3],
       [ 6.3,  1.3],
       [ 4.8,  0.2],
       [ 4.9,  1.7],
       [ 5.5,  1.1],
       [ 5.8,  1. ],
       [ 6.7,  1.4],
       [ 4.4,  0.2],
       [ 5.7,  2. ],
       [ 6.5,  2. ],
       [ 4.9,  0.1],
       [ 7.6,  2.1],
       [ 6.7,  2.1],
       [ 6.3,  1.5],
       [ 5. ,  0.6],
       [ 6.1,  1.4],
       [ 5.1,  0.3],
       [ 5. ,  0.2],
       [ 5. ,  0.2],
       [ 6.3,  1.8],
       [ 5.2,  0.1],
       [ 5.4,  0.2],
       [ 5.4,  0.4],
       [ 5.7,  1.2],
       [ 5.4,  1.5],
       [ 7. ,  1.4],
       [ 6.3,  1.8],
       [ 4.9,  1. ],
       [ 5.9,  1.5],
       [ 6.4,  1.9],
       [ 5.6,  1.3],
       [ 7.4,  1.9],
       [ 6.9,  1.5],
       [ 4.8,  0.3],
       [ 5. ,  1. ],
       [ 6.4,  1.5],
       [ 5.5,  1.2],
       [ 7.7,  2. ],
       [ 6.1,  1.4],
       [ 6.7,  2.3],
       [ 6.7,  2.5],
       [ 6.7,  1.7],
       [ 6.2,  2.3],
       [ 7.7,  2.3],
       [ 6.4,  2.1],
       [ 4.6,  0.2],
       [ 5.1,  0.5],
       [ 5.5,  1.3],
       [ 5.7,  1.3],
       [ 6. ,  1.8],
       [ 6.3,  2.5],
       [ 6.3,  1.5],
       [ 6.6,  1.4],
       [ 5.7,  1. ],
       [ 4.4,  0.2],
       [ 4.5,  0.3],
       [ 6.9,  2.3],
       [ 4.8,  0.2],
       [ 6.5,  2. ],
       [ 4.9,  0.2],
       [ 6.2,  1.5],
       [ 5. ,  0.2],
       [ 6.7,  2.4],
       [ 6. ,  1.6],
       [ 6.4,  2.3],
       [ 5.4,  0.4],
       [ 6.1,  1.3],
       [ 4.6,  0.2],
       [ 6.7,  1.8],
       [ 5.5,  0.2],
       [ 5.1,  0.4],
       [ 6.4,  1.8],
       [ 6.4,  1.3],
       [ 6.6,  1.3],
       [ 4.8,  0.2],
       [ 7.3,  1.8]])

In [5]:
y_vals_test.shape


Out[5]:
(30,)

In [7]:
[[a1], [a2]] = sess.run(A)

In [9]:
sess.run(A)


Out[9]:
array([[ 0.30879977],
       [-0.83936369]], dtype=float32)

In [ ]: