Created
June 16, 2021 00:33
-
-
Save shawwn/7b799fa7cf94da4346e5f85708b88543 to your computer and use it in GitHub Desktop.
A script to test M1 GPU training via the tensorflow-metal plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow.compat.v2 as tf | |
import tensorflow_datasets as tfds | |
tf.get_logger().setLevel('DEBUG') | |
tf.enable_v2_behavior() | |
from tensorflow.python.framework.ops import disable_eager_execution | |
disable_eager_execution() | |
# from tensorflow.python.compiler.mlcompute import mlcompute | |
# mlcompute.set_mlc_device(device_name='gpu') | |
import os | |
os.environ['TF_MLC_DEVICE_TYPE'] = os.environ.get('TF_MLC_DEVICE_TYPE', '1') | |
if 'mixed_precision' and False: | |
from tensorflow import keras | |
from tensorflow.keras import layers | |
from tensorflow.keras import mixed_precision | |
policy = mixed_precision.Policy('mixed_float16') | |
mixed_precision.set_global_policy(policy) | |
(ds_train, ds_test), ds_info = tfds.load( | |
'mnist', | |
split=['train', 'test'], | |
shuffle_files=True, | |
as_supervised=True, | |
with_info=True, | |
) | |
def normalize_img(image, label): | |
"""Normalizes images: `uint8` -> `float32`.""" | |
return tf.cast(image, tf.float32) / 255., label | |
batch_size = 128 | |
ds_train = ds_train.map( | |
normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE) | |
ds_train = ds_train.cache() | |
ds_train = ds_train.shuffle(ds_info.splits['train'].num_examples) | |
ds_train = ds_train.batch(batch_size) | |
ds_train = ds_train.prefetch(tf.data.experimental.AUTOTUNE) | |
ds_test = ds_test.map( | |
normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE) | |
ds_test = ds_test.batch(batch_size) | |
ds_test = ds_test.cache() | |
ds_test = ds_test.prefetch(tf.data.experimental.AUTOTUNE) | |
model = tf.keras.models.Sequential([ | |
tf.keras.layers.Conv2D(32, kernel_size=(3, 3), | |
activation='relu'), | |
tf.keras.layers.Conv2D(64, kernel_size=(3, 3), | |
activation='relu'), | |
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)), | |
# tf.keras.layers.Dropout(0.25), | |
tf.keras.layers.Flatten(), | |
tf.keras.layers.Dense(128, activation='relu'), | |
# tf.keras.layers.Dropout(0.5), | |
tf.keras.layers.Dense(10, activation='softmax') | |
]) | |
model.compile( | |
loss='sparse_categorical_crossentropy', | |
optimizer=tf.keras.optimizers.Adam(0.001), | |
metrics=['accuracy'], | |
) | |
model.fit( | |
ds_train, | |
epochs=4, | |
validation_data=ds_test, | |
) | |
""" | |
469/469 [==============================] - 12s 20ms/step - batch: 234.0000 - size: 1.0000 - loss: 0.1537 - accuracy: 0.9546 - val_loss: 0.0655 - val_accuracy: 0.9785 | |
Epoch 2/4 | |
469/469 [==============================] - 10s 20ms/step - batch: 234.0000 - size: 1.0000 - loss: 0.0425 - accuracy: 0.9869 - val_loss: 0.0365 - val_accuracy: 0.9875 | |
Epoch 3/4 | |
469/469 [==============================] - 10s 20ms/step - batch: 234.0000 - size: 1.0000 - loss: 0.0268 - accuracy: 0.9919 - val_loss: 0.0427 - val_accuracy: 0.9859 | |
Epoch 4/4 | |
469/469 [==============================] - 10s 20ms/step - batch: 234.0000 - size: 1.0000 - loss: 0.0184 - accuracy: 0.9940 - val_loss: 0.0377 - val_accuracy: 0.9886 | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment