Created
February 9, 2018 04:47
-
-
Save mwdchang/dd1dbac1c9d02d1933670638a6c67ff7 to your computer and use it in GitHub Desktop.
Tensorflow DNN scratch pad
This file contains 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 os | |
os.environ["TF_CPP_MIN_LOG_LEVEL"]="2" | |
import tensorflow as tf | |
import numpy as np | |
## TODO | |
## - Move learning rate | |
## - Save model, restore model | |
class NNModel(): | |
def __init__(self, layers, num_classes): | |
print("In NNModel constructor") | |
self.layers = layers | |
self.num_classes = num_classes | |
self.learning_rate = 0.1 | |
self.model = tf.estimator.Estimator(self.build) | |
def build_layers(self, dictionary): | |
tmp = dictionary["x"] | |
for layer in self.layers: | |
tmp = tf.layers.dense(tmp, layer) | |
tmp = tf.layers.dense(tmp, self.num_classes) | |
return tmp | |
def train(self, input_fn, num_steps): | |
self.model.train(input_fn, steps = num_steps) | |
def evaluate(self, input_fn): | |
return self.model.evaluate(input_fn) | |
def predict(self, input_fn): | |
return self.model.predict(input_fn) | |
def build(self, features, labels, mode): | |
logits = self.build_layers(features) | |
pred_classes = tf.argmax(logits, axis=1) | |
pred_probas = tf.nn.softmax(logits) | |
if mode == tf.estimator.ModeKeys.PREDICT: | |
return tf.estimator.EstimatorSpec(mode, predictions=pred_classes) | |
loss_op = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits( | |
logits=logits, labels=tf.cast(labels, dtype=tf.int32))) | |
optimizer = tf.train.GradientDescentOptimizer(learning_rate=self.learning_rate) | |
train_op = optimizer.minimize(loss_op, global_step=tf.train.get_global_step()) | |
# Evaluate the accuracy of the model | |
acc_op = tf.metrics.accuracy(labels=labels, predictions=pred_classes) | |
# TF Estimators requires to return a EstimatorSpec, that specify | |
# the different ops for training, evaluating, ... | |
estim_specs = tf.estimator.EstimatorSpec( | |
mode=mode, | |
predictions=pred_classes, | |
loss=loss_op, | |
train_op=train_op, | |
eval_metric_ops={'accuracy': acc_op}) | |
return estim_specs |
This file contains 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 os | |
os.environ["TF_CPP_MIN_LOG_LEVEL"]="2" | |
import tensorflow as tf | |
import numpy as np | |
from dnn import NNModel | |
# Generates a linear relationship, then jiggle | |
# the original data points in order to add some | |
# random noise | |
def generate_data(num, jiggle=0.5): | |
data = [] | |
label = [] | |
for i in range(num): | |
r1 = np.random.randint(5) | |
r2 = np.random.randint(5) | |
l = (r1 + r2) | |
d1 = (r1 * 1.0) + (jiggle * np.random.random()) | |
d2 = (r2 * 1.0) + (jiggle * np.random.random()) | |
if len(data) == 0: | |
data = [d1, d2] | |
label = [l] | |
else: | |
data = np.vstack((data, [d1, d2])) | |
label = np.concatenate((label, [l])) | |
return data, label | |
data, label = generate_data(40000) | |
print("Data", data.shape, data[0:10]) | |
print("Label", label.shape, label[0:10]) | |
eval_data, eval_label = generate_data(200) | |
model = NNModel([30, 20], 10) | |
batch_size = 500 | |
# Training model | |
input_fn = tf.estimator.inputs.numpy_input_fn( | |
x={"x": data}, | |
y=label, | |
batch_size=batch_size, | |
num_epochs=None, | |
shuffle=True) | |
model.train(input_fn, 1500) | |
# Evaluate model | |
eval_fn = tf.estimator.inputs.numpy_input_fn( | |
x={"x": eval_data}, | |
y=eval_label, | |
batch_size=batch_size, | |
shuffle=False) | |
eval_result = model.evaluate(eval_fn) | |
print(eval_result, str(eval_result)) | |
This file contains 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 os | |
os.environ["TF_CPP_MIN_LOG_LEVEL"]="2" | |
import tensorflow as tf | |
import numpy as np | |
from tensorflow.examples.tutorials.mnist import input_data | |
mnist = input_data.read_data_sets("./data/", one_hot=False) | |
from dnn import NNModel | |
batch_size = 128 | |
model = NNModel([20, 10], 10) | |
# Training model | |
input_fn = tf.estimator.inputs.numpy_input_fn( | |
x={"x": mnist.train.images}, | |
y=mnist.train.labels, | |
batch_size=batch_size, | |
num_epochs=None, | |
shuffle=True) | |
model.train(input_fn, 500) | |
# Evaluate model | |
eval_fn = tf.estimator.inputs.numpy_input_fn( | |
x={"x": mnist.test.images}, | |
y=mnist.test.labels, | |
batch_size=batch_size, | |
shuffle=False) | |
eval_result = model.evaluate(eval_fn) | |
print(eval_result, str(eval_result)) | |
# Prediction | |
randomIdx = np.random.randint(len(mnist.test.images), size=20) | |
sample = mnist.test.images[randomIdx,:] | |
sample_label = mnist.test.labels[randomIdx,] | |
prediction_fn = tf.estimator.inputs.numpy_input_fn( | |
x={"x": sample}, | |
num_epochs=1, | |
shuffle = False) | |
predictions = model.predict(prediction_fn) | |
print("Prediction -> Actual") | |
for pred, label in zip(predictions, sample_label): | |
print(pred, "->", label) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment