woodsja icon

eager_mnist.py

woodsja | PRO | 11/29/18 11:33:49 PM UTC | 0 ⭐ | 357 👁️ | Never ⏰ | []
Python |

2.11 KB

|

None

|

0 👍

/

0 👎

import tensorflow as tf
import numpy as np
 
print(tf.__version__)
 
tf.enable_eager_execution()
 
if tf.executing_eagerly():
  print('eager beaver')
 
x = [[2.]]
m = tf.matmul(x, x)
# print('hello, {}'.format(m))
# formatted string literals look weird
print(f'hello, {m}')
 
# get muh data
 
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
 
# it has to be as a dictionary
def train_input_fn(images, labels, batch_size):
  # images=dict(images)
  dataset = tf.data.Dataset.from_tensor_slices((
    dict(tf.cast(images / 255, tf.float32)), tf.cast(labels, tf.int64)
  ))
  dataset = dataset.shuffle(100000).repeat().batch(batch_size)
 
  return dataset
 
# train_input_fn(train_images, train_labels, 100)
# rather than providing the raw data directly, we need to provide a function that returns the data
 
# train_input_fn = tf.estimator.inputs.numpy_input_fn(
#   x={'x': np.array(data.x_train)},
#   y=np.array(data.y_train_cls),
#   num_epochs=None,
#   shuffle=True
# )
#
# test_input_fn = tf.estimator.inputs.numpy_input_fn(
#   x={'x': np.array(data.x_test)},
#   y=np.array(data.y_test_cls),
#   num_epochs=1,
#   shuffle=False
# )
 
# build muh feature_column
my_feature_columns = [tf.feature_column.numeric_column('x', shape=[28, 28])]
 
# Spec a classifier
classifier = tf.estimator.DNNClassifier(
  feature_columns=my_feature_columns,
  hidden_units=[256, 32],
  optimizer=tf.train.AdamOptimizer(1e-4),
  n_classes=10,
  dropout=0.1,
  model_dir="./tmp/eager_mnist"
)
 
classifier.train(
  input_fn=lambda: train_input_fn(train_images, train_labels, 100), steps=2000)
 
#cust_train_input_fn = lambda: train_input_fn(mnist_train_images, mnist_train_labels, batch_size=64)
 
 
 
 
 
# # generate testing function
# test_input_function = tf.estimator.inputs.numpy_input_fn(
#   x={'x': input(mnist.test)[0]},
#   y=input(mnist.test)[1],
#   num_epochs=1,
#   shuffle=False
# )
 
# how shall we evaluate our training?
# accuracy_score = classifier.evaluate(input_fn=test_input_function)
# ['accuracy']
# print(f'\nTest Accuracy: {accuracy_score*100}')

Comments