Categorizing Iris flowers by species by using Tensorflow's eager execution.
This guide uses these high-level TensorFlow concepts:
Enable an eager execution development environment,
Import data with the Datasets API
Build models and layers with TensorFlow's Keras API
Reference
from __future__ import absolute_import, division, print_function
import os, sys
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline
tf.enable_eager_execution()
print(tf.__version__)
1.12.0
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(10, activation = tf.nn.relu, input_shape = (4,)))
model.add(tf.keras.layers.Dense(10, activation = tf.nn.relu))
model.add(tf.keras.layers.Dense(3))
def loss_fn(model, features, label):
score = model(features)
return tf.losses.sparse_softmax_cross_entropy(labels = label, logits = score)
print(model.trainable_variables) # different from tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
[<tf.Variable 'dense/kernel:0' shape=(4, 10) dtype=float32, numpy= array([[-0.6016106 , 0.21193182, 0.15714651, 0.15312278, -0.28133038, 0.00746506, -0.148848 , -0.1853224 , 0.18714899, -0.25901568], [ 0.02185094, -0.08642298, 0.5632899 , -0.43347245, 0.1226458 , -0.49662638, -0.23893097, 0.49837744, 0.39349937, -0.3385544 ], [-0.27739072, -0.31779853, 0.64550364, 0.33254373, 0.23269486, -0.2784947 , 0.06808609, 0.21770716, 0.23193008, -0.16453654], [-0.5709686 , -0.24807453, 0.17099643, -0.20884967, -0.2562401 , 0.34987652, 0.52932966, -0.1443029 , 0.42169094, 0.46493506]], dtype=float32)>, <tf.Variable 'dense/bias:0' shape=(10,) dtype=float32, numpy=array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)>, <tf.Variable 'dense_1/kernel:0' shape=(10, 10) dtype=float32, numpy= array([[ 0.4289506 , 0.10450268, -0.04721606, -0.43415242, -0.47025183, -0.361524 , 0.0418207 , -0.0864554 , -0.0556342 , 0.15549362], [-0.44529125, 0.37460685, 0.01090676, -0.05481359, -0.27588695, 0.29222095, -0.41036385, -0.3664556 , -0.53682727, 0.31951696], [-0.12785482, 0.03862995, 0.35421294, 0.01573461, 0.34672868, -0.3728515 , 0.2816208 , -0.29301217, 0.11257732, -0.04643917], [-0.03176916, 0.5232763 , 0.35986876, 0.33061004, 0.26544142, -0.22060388, -0.14162892, 0.17554426, -0.49126202, 0.08825725], [-0.32824028, 0.21503484, -0.54436713, -0.50431615, -0.16609886, -0.20675969, -0.00118977, -0.23783684, 0.11200953, 0.393723 ], [ 0.04101104, -0.14152196, -0.47661275, -0.46934345, 0.10372204, 0.5409229 , 0.27753627, -0.10152608, 0.41975296, -0.2332783 ], [-0.33580142, 0.4723308 , -0.24120337, 0.18304974, -0.05336449, -0.37312585, 0.2332313 , -0.40402335, 0.36896586, -0.21605268], [-0.32411113, 0.01418477, -0.23935765, 0.521438 , 0.44553947, 0.3212368 , 0.02267909, 0.17611271, -0.22656313, -0.45787033], [-0.40987664, -0.01963365, 0.25425416, -0.12491649, -0.5176469 , 0.35165775, 0.4348483 , 0.48482704, 0.08984548, -0.42127824], [ 0.4564724 , 0.3914752 , -0.29007906, 0.18757671, 0.19446594, -0.36466774, -0.11595219, -0.1236656 , 0.26194257, 0.1289767 ]], dtype=float32)>, <tf.Variable 'dense_1/bias:0' shape=(10,) dtype=float32, numpy=array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)>, <tf.Variable 'dense_2/kernel:0' shape=(10, 3) dtype=float32, numpy= array([[-0.55178356, 0.5969744 , -0.20873287], [ 0.6190039 , -0.36399648, 0.49799395], [-0.02199602, -0.13632727, -0.6334673 ], [-0.5608484 , 0.47014666, 0.43292272], [ 0.24740952, 0.5413171 , 0.3306861 ], [-0.6199806 , 0.5774143 , -0.6118181 ], [-0.02008146, -0.57885826, -0.11845094], [-0.45090094, -0.34696335, -0.42613342], [-0.21294937, 0.35269964, -0.12020266], [-0.20987946, 0.14686137, -0.5125678 ]], dtype=float32)>, <tf.Variable 'dense_2/bias:0' shape=(3,) dtype=float32, numpy=array([0., 0., 0.], dtype=float32)>]
os.listdir('../data/lecture04/')
['iris_test.csv', 'iris_training.csv']
# define parsing function
def parse_single_example(record):
decoded = tf.decode_csv(record, [[.0],[.0],[.0],[.0],[]])
features = decoded[:4]
label = tf.cast(x = decoded[4], dtype = tf.int32)
return features, label
epochs = 10
batch_size = 8
learning_rate = .03
tr_dataset = tf.data.TextLineDataset(filenames = '../data/lecture04/iris_training.csv')
tr_dataset = tr_dataset.map(parse_single_example)
tr_dataset = tr_dataset.shuffle(200).batch(batch_size = batch_size)
opt = tf.train.GradientDescentOptimizer(learning_rate = learning_rate)
global_step = tf.Variable(0.)
for epoch in range(epochs):
avg_loss = 0
tr_step = 0
for mb_x, mb_y in tr_dataset:
with tf.GradientTape() as tape:
tr_loss = loss_fn(model, mb_x, mb_y)
grads = tape.gradient(tr_loss, model.variables)
opt.apply_gradients(zip(grads, model.variables), global_step = global_step)
avg_loss += tr_loss
tr_step += 1
else:
avg_loss /= tr_step
print('epoch : {:3}, ce_loss : {:.3f}'.format(epoch + 1, avg_loss))
epoch : 1, ce_loss : 1.154 epoch : 2, ce_loss : 0.829 epoch : 3, ce_loss : 0.706 epoch : 4, ce_loss : 0.591 epoch : 5, ce_loss : 0.553 epoch : 6, ce_loss : 0.553 epoch : 7, ce_loss : 0.449 epoch : 8, ce_loss : 0.451 epoch : 9, ce_loss : 0.514 epoch : 10, ce_loss : 0.395
tst_dataset = tf.data.TextLineDataset(filenames = '../data/lecture04/iris_test.csv')
tst_dataset = tst_dataset.map(parse_single_example)
tst_dataset = tst_dataset.batch(batch_size = 30)
tst_x, tst_y = next(iter(tst_dataset))
tst_yhat = tf.argmax(model(tst_x), axis = -1, output_type = tf.int32)
print('test accuracy : {:.2%}'.format(np.mean(tf.equal(tst_y, tst_yhat))))
test accuracy : 96.67%