here is the example code:
import sys
import time
import tensorflow as tf
import tensorflow.keras
import pandas as pd
import sklearn as sk
try:
import tensorflow_datasets as tfds
except:
!pip install -q tensorflow_datasets
import tensorflow_datasets as tfds
import tensorflow.compat.v2 as tf
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')
# (mlcompute cannot be found)
(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'],
)
start_time = time.time()
model.fit(
ds_train,
epochs=10,
validation_data=ds_test,
)
print("--- %s minutes with GPU ---" % ((time.time() - start_time)/60))
here are the outputs:
commenting
from tensorflow.python.framework.ops import disable_eager_execution
disable_eager_execution()
kernel died:
Metal device set to: Apple M1 Pro
systemMemory: 16.00 GB
maxCacheSize: 5.33 GB
Epoch 1/10
2021-10-31 19:10:20.277599: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: SSE4.2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-10-31 19:10:20.278606: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.
2021-10-31 19:10:20.279185: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)
with
from tensorflow.python.framework.ops import disable_eager_execution
disable_eager_execution()
RuntimeError: Caught an unknown exception!:
I installed tensorflow-macos and tensorflow-metal by following https://developer.apple.com/metal/tensorflow-plugin/
I am struggling of using tensorflow on new MBP for many days and it just totally a nightmare. I met tons of issues, searched ways to solved them and tried tons of methods and none of them can really help me to train a NN.
Honestly, I am out of patience right now. I brought MBP as I suppose it can facilitate my work and it turns out I even can use tensorflow! so ridiculous!
- Tensorflow: 2.6.0
- Keras Version: 2.6.0
- Python 3.8.12
- macOS: 12.0.1
- Anaconda: 2.0.3
- tensorflow-macos: 2.6.0
- tensorflow-metal: 0.2.0