In [9]:
import numpy as np
import matplotlib.pyplot as plt
N = 100 # points per class
D = 2 # dimensionality at 2 so we can eyeball it
K = 3 # number of classes
X = np.zeros((N*K, D)) # generate an empty matrix to hold X features
y = np.zeros(N*K, dtype='int32') # switching this to int32
# for 3 classes, evenly generates spiral arms
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
plt.scatter(X[:,0], X[:,1], c=y, s=20, cmap=plt.cm.Spectral)
plt.show()
In [24]:
import tensorflow as tf
# what should the classifier expect in terms of features
feature_columns = [tf.contrib.layers.real_valued_column("", dimension=D)]
# defining the actual classifier
dnn_spiral_classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
activation_fn = tf.nn.softmax, # softmax activation
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01), #GD with LR of 0.01
hidden_units = [10], # one hidden layer, containing 10 neurons
n_classes = K, # K target classes
model_dir="/tmp/spiral_model") # directory for saving model checkpoints
# turn data into tensors to feed into the computational graph
# honestly input_fn could also handle these as np.arrays but this is here to show you that the tf.constant operation can run on np.array input
def get_inputs():
X_tensor = tf.constant(X)
y_tensor = tf.constant(y)
return X_tensor, y_tensor
# fit the model
dnn_spiral_classifier.fit(input_fn=get_inputs, steps=200)
# interestingsly, you can continue training the model by continuing to call fit
dnn_spiral_classifier.fit(input_fn=get_inputs, steps=300)
#evaluating the accuracy
accuracy_score = dnn_spiral_classifier.evaluate(input_fn=get_inputs,
steps=1)["accuracy"]
print("\n Accuracy: {0:f}\n".format(accuracy_score))
The higher level library vastly simplied the following mechanics:
For most use cases, it's likely that the many common models built into to tf will be able to solve your problem. You'll have to do model tuning by figuring out the correct parameters. Building your computational graph node by doing isn't likely needed unless you're doing academic research or working with very specialized datasets where default performance plataues.'
In [14]:
%ls '/tmp/spiral_model/'
In [28]:
def new_points():
return np.array([[1.0, 1.0],
[-1.5, -1.0]], dtype = np.int32)
predictions = list(dnn_spiral_classifier.predict(input_fn=new_points))
print(
"New Samples, Class Predictions: {}\n"
.format(predictions))
The DNNClassifier is one fo the (Estimators)[https://www.tensorflow.org/api_guides/python/contrib.learn#Estimators] available in the tf.contrib.learn libary. Other estimators include:
Each one of these can perform various actions on the graph, including:
Each one of these can read in batched input data with types including:
Keep an eye on the documentation and updates. TensorFlow is under constant development. Things change very quickly!
In [ ]:
# watch out for this, tf.classifier.evaluate is going to be deprecated, so keep an eye out for a long-term solution to calculating accuracy
accuracy_score = dnn_spiral_classifier.evaluate(input_fn=get_inputs,
steps=1)["accuracy"]
Get into the (TensorFlow API docs)[https://www.tensorflow.org/api_docs/python/tf]. Try the following and see how it impacts the final scores
/
Reimplement the Spiral Classifier as a 2 Layer Neural Network in TensorFlow Core
In [ ]:
# sample code to use for the gold star challenge from https://www.tensorflow.org/get_started/get_started
import numpy as np
import tensorflow as tf
# Model parameters
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
# Model input and output
x = tf.placeholder(tf.float32)
linear_model = W * x + b
y = tf.placeholder(tf.float32)
# loss
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
# optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
# training data
x_train = [1,2,3,4]
y_train = [0,-1,-2,-3]
# training loop
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # reset values to wrong
for i in range(1000):
sess.run(train, {x:x_train, y:y_train})
# evaluate training accuracy
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x:x_train, y:y_train})
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))