09.05.2023 Views

pdfcoffee

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 2

There, tf.feature_column.numeric_column() represents real valued or numerical

features (https://www.tensorflow.org/api_docs/python/tf/feature_column/

numeric_column). Efficiency Estimators should be trained using tf.Datasets

as input. Here is an example where MNIST is loaded, scaled, shuffled, and batched:

import tensorflow as tf

import tensorflow_datasets as tfds

BUFFER_SIZE = 10000

BATCH_SIZE = 64

def input_fn(mode):

datasets, info = tfds.load(name='mnist',

with_info=True,

as_supervised=True)

mnist_dataset = (datasets['train'] if mode == tf.estimator.ModeKeys.

TRAIN else datasets['test'])

def scale(image, label):

image = tf.cast(image, tf.float32)

image /= 255

return image, label

return mnist_dataset.map(scale).shuffle(BUFFER_SIZE).batch(BATCH_SIZE)

test = input_fn('test')

train = input_fn(tf.estimator.ModeKeys.TRAIN)

print(test)

print(train)

Then, the Estimator can be trained and evaluated by using tf.estimator.train_

and_evaluate() and passing the input_fn which will iterate on the data:

tf.estimator.train_and_evaluate(

classifier,

train_spec=tf.estimator.TrainSpec(input_fn=input_fn),

eval_spec=tf.estimator.EvalSpec(input_fn=input_fn)

)

TensorFlow contains Estimators for both regression and classification, which are

largely adopted by the community and they will continue to be supported through

at least the lifetime of TensorFlow 2.x.

[ 73 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!