Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tensorflow as tf
- from matplotlib import pyplot
- from matplotlib.image import imread
- from tensorflow.keras.layers import Dense, Flatten, Conv2D
- from tensorflow.keras import Model
- #tf.enable_eager_execution()
- print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
- mnist = tf.keras.datasets.mnist
- (x_train, y_train), (x_test, y_test) = mnist.load_data()
- x_train, x_test = x_train / 255.0, x_test / 255.0
- # Add a channels dimension
- x_train = x_train[..., tf.newaxis]
- x_test = x_test[..., tf.newaxis]
- train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(10000).batch(32)
- test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32)
- #----
- class MyModel(Model):
- def __init__(self):
- super(MyModel, self).__init__()
- self.conv1 = Conv2D(32, 3, activation='relu')
- self.flatten = Flatten()
- self.d1 = Dense(128, activation='relu')
- self.d2 = Dense(10)
- def call(self, x):
- x = self.conv1(x)
- x = self.flatten(x)
- x = self.d1(x)
- return self.d2(x)
- # Create an instance of the model
- model = MyModel()
- loss_object = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
- optimizer = tf.keras.optimizers.Adam()
- ####Crashes after this line:
- train_loss = tf.keras.metrics.Mean(name='train_loss')
- train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')
- test_loss = tf.keras.metrics.Mean(name='test_loss')
- test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy')
Advertisement
Add Comment
Please, Sign In to add comment