Libraries used in this example:
- tensorflow: The machine learning library
- numpy: Library to create inputs for TensorFlow
- matplotlib.pyplot: Utility library to draw, eg. loss curve, accuracy curve, etc.
Utility functions:
- log: Print a log line using tf.logging.info
- log_sep: Print separator in log
TensorFlow functions:
- tf.feature_column.numeric_column: Create a feature of numeric input values
- tf.estimator.inputs.numpy_input_fn: Create TensorFlow input function from numpy array
TensorFlow classes:
- tf.estimator.DNNClassifier: The ready-to-use DNN classifier class
#core import sys,json; #libs import tensorflow as tf; import numpy as np; import matplotlib.pyplot as pyplot; ''' \brief Log ''' def log(*Args): Str = ""; for I in range(len(Args)): Str += str(Args[I]); if (I<len(Args)-1): Str += "\x20"; tf.logging.info(Str); #end def ''' \brief Log a separator ''' def log_sep(): tf.logging.info("\n"+"="*80); #end def ''' \brief Train/eval using ready-to-use estimator DNNClassifier ''' def train_eval_estimator(X,Y): Feature = tf.feature_column.numeric_column(key="X",shape=[2]); Classifier = tf.estimator.DNNClassifier( feature_columns = [Feature], hidden_units = [8,4], n_classes = 2 ); Train_Input_Fn = tf.estimator.inputs.numpy_input_fn( x = {"X":X}, y = Y, batch_size = 4, num_epochs = None, shuffle = True ); Eval_Input_Fn = tf.estimator.inputs.numpy_input_fn( x = {"X":X}, y = Y, batch_size = 4, num_epochs = 1, shuffle = False ); STEPS = 1000; Losses = []; for I in range(10): log_sep(); log("\nTraining steps from {}...".format(I*STEPS)); Classifier.train(input_fn=Train_Input_Fn, steps=STEPS); log_sep(); log("\nEvaluating after {} steps...".format((I+1)*STEPS)); Result = Classifier.evaluate(input_fn=Eval_Input_Fn); Losses += [Result["loss"]]; #end for log_sep(); log("\nLosses:",Losses); pyplot.plot(Losses); #end def #PROGRAMME ENTRY POINT========================================================== tf.logging.set_verbosity(tf.logging.INFO); log("TensorFlow version:",tf.__version__); #data X = [[0,0],[0,1],[1,0],[1,1]]; Y = [ 0, 1, 1, 0 ]; X = np.array(X); Y = np.array(Y); train_eval_estimator(X,Y); #eof
Colab link:
https://colab.research.google.com/drive/1CkpVsVbHKxrgLFqoGee-B6iLpyRdiode
No comments:
Post a Comment