Guest User

Untitled

a guest
May 8th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. import tensorflow as tf
  2. from matplotlib import pyplot
  3. from matplotlib.image import imread
  4. from tensorflow.keras.layers import Dense, Flatten, Conv2D
  5. from tensorflow.keras import Model
  6.  
  7. #tf.enable_eager_execution()
  8. print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
  9.  
  10. mnist = tf.keras.datasets.mnist
  11.  
  12. (x_train, y_train), (x_test, y_test) = mnist.load_data()
  13. x_train, x_test = x_train / 255.0, x_test / 255.0
  14.  
  15. # Add a channels dimension
  16. x_train = x_train[..., tf.newaxis]
  17. x_test = x_test[..., tf.newaxis]
  18.  
  19.  
  20. train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(10000).batch(32)
  21. test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32)
  22.  
  23.  
  24.  
  25. #----
  26. class MyModel(Model):
  27. def __init__(self):
  28. super(MyModel, self).__init__()
  29. self.conv1 = Conv2D(32, 3, activation='relu')
  30. self.flatten = Flatten()
  31. self.d1 = Dense(128, activation='relu')
  32. self.d2 = Dense(10)
  33. def call(self, x):
  34. x = self.conv1(x)
  35. x = self.flatten(x)
  36. x = self.d1(x)
  37. return self.d2(x)
  38.  
  39. # Create an instance of the model
  40. model = MyModel()
  41.  
  42.  
  43. loss_object = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
  44. optimizer = tf.keras.optimizers.Adam()
  45.  
  46. ####Crashes after this line:
  47. train_loss = tf.keras.metrics.Mean(name='train_loss')
  48. train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')
  49.  
  50. test_loss = tf.keras.metrics.Mean(name='test_loss')
  51. test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy')
Advertisement
Add Comment
Please, Sign In to add comment