Skip to content

Instantly share code, notes, and snippets.

@flyman3046
Created March 26, 2017 23:56
Show Gist options
  • Save flyman3046/248583df6b181562e476b778f60c2950 to your computer and use it in GitHub Desktop.
Save flyman3046/248583df6b181562e476b778f60c2950 to your computer and use it in GitHub Desktop.
Used tensorflow to solve a simulated data classification
# Simulated data and plot comes from: http://cs231n.github.io/neural-networks-case-study/
import tensorflow as tf
import numpy as np
import random
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
np.random.seed(0)
N = 100 # number of points per class
D = 2 # dimensionality
K = 3 # number of classes
X = np.zeros((N * K, D))
y = np.zeros(N * K, dtype='uint8')
for j in xrange(K):
ix = range(N * j, N * (j + 1))
r = np.linspace(0.0, 1, N) # radius
t = np.linspace(j * 4, (j + 1) * 4, N) + np.random.randn(N) * 0.2 # theta
X[ix] = np.c_[r * np.sin(t), r * np.cos(t)]
y[ix] = j
fig = plt.figure()
plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral)
plt.xlim([-1, 1])
plt.ylim([-1, 1])
# plt.show()
# initialize parameters randomly
H = 100 # size of hidden layer
learning_rate = 1e-2
reg = 1e-3
def build_model():
with tf.variable_scope("parameters"):
data = tf.placeholder(tf.float32, [None, D])
label = tf.placeholder(tf.int64, [None])
W1 = tf.get_variable("policy_parameters_w1", shape=[D, H])
b1 = tf.get_variable("policy_parameters_b1", shape=[H])
W2 = tf.get_variable("policy_parameters_w2", shape=[H, K])
b2 = tf.get_variable("policy_parameters_b2", shape=[K])
hidden = tf.nn.relu(tf.matmul(data, W1) + b1)
probabilities = tf.nn.softmax(tf.matmul(hidden, W2) + b2)
prob_given_state = -tf.reduce_sum(probabilities * tf.one_hot(label, K), axis=1)
loss = tf.reduce_mean(prob_given_state)
loss += tf.nn.l2_loss(W1) * reg
loss += tf.nn.l2_loss(b1) * reg
loss += tf.nn.l2_loss(W2) * reg
loss += tf.nn.l2_loss(b2) * reg
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss)
# Evaluate model
correct_pred = tf.equal(tf.argmax(probabilities, 1), label)
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
return data, label, optimizer, loss, accuracy, W1, b1, W2, b2
model = build_model()
sess = tf.InteractiveSession()
init = tf.global_variables_initializer()
sess.run(init)
data, label, optimizer, loss, accuracy, W1, b1, W2, b2 = model
for epoch in range(1000): # run 1000 epoch
print sess.run([optimizer, loss, accuracy], feed_dict={data: X, label: y})
# plot the resulting classifier
h = 0.02
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
W1 = sess.run(W1)
b1 = sess.run(b1)
W2 = sess.run(W2)
b2 = sess.run(b2)
Z = np.dot(np.maximum(0, np.dot(np.c_[xx.ravel(), yy.ravel()], W1) + b1), W2) + b2
Z = np.argmax(Z, axis=1)
Z = Z.reshape(xx.shape)
fig = plt.figure()
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8)
plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment