Last active
December 21, 2022 16:37
-
-
Save chuongmep/83cdf198ee226ec23288ce6b22b2615c to your computer and use it in GitHub Desktop.
This file contains 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
{ | |
"cells": [ | |
{ | |
"attachments": {}, | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"1. https://developer.apple.com/metal/tensorflow-plugin/\n", | |
"\n", | |
"2. https://docs.conda.io/projects/conda/en/latest/user-guide/install/macos.html" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"2.11.0\n" | |
] | |
} | |
], | |
"source": [ | |
"import tensorflow as tf\n", | |
"print(tf.__version__)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"2022-12-21 23:19:45.471986: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:306] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.\n", | |
"2022-12-21 23:19:45.472006: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:272] Created TensorFlow device (/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"'/device:GPU:0'" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# test if GPU is available\n", | |
"devices = tf.config.list_physical_devices('GPU')\n", | |
"print(devices)\n", | |
"# show details of GPU\n", | |
"tf.test.gpu_device_name()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Metal device set to: Apple M1 Max\n", | |
"\n", | |
"systemMemory: 32.00 GB\n", | |
"maxCacheSize: 10.67 GB\n", | |
"\n", | |
"tf.Tensor(\n", | |
"[[22. 28.]\n", | |
" [49. 64.]], shape=(2, 2), dtype=float32)\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"2022-12-21 23:23:26.156834: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:306] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.\n", | |
"2022-12-21 23:23:26.156856: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:272] 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>)\n" | |
] | |
} | |
], | |
"source": [ | |
"# try to use GPU\n", | |
"import tensorflow as tf\n", | |
"with tf.device('/GPU:0'):\n", | |
" a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')\n", | |
" b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')\n", | |
" c = tf.matmul(a, b)\n", | |
" print(c)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Metal device set to: Apple M1 Max\n", | |
"\n", | |
"systemMemory: 32.00 GB\n", | |
"maxCacheSize: 10.67 GB\n", | |
"\n", | |
"Epoch 1/5\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"2022-12-22 00:31:44.719296: 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.\n", | |
"2022-12-22 00:31:44.719451: 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>)\n", | |
"2022-12-22 00:31:44.907184: W tensorflow/core/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz\n", | |
"2022-12-22 00:31:45.084837: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:113] Plugin optimizer for device_type GPU is enabled.\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1875/1875 [==============================] - 10s 5ms/step - loss: 0.2905 - accuracy: 0.9152\n", | |
"Epoch 2/5\n", | |
"1875/1875 [==============================] - 10s 5ms/step - loss: 0.1372 - accuracy: 0.9589\n", | |
"Epoch 3/5\n", | |
"1875/1875 [==============================] - 10s 5ms/step - loss: 0.0988 - accuracy: 0.9699\n", | |
"Epoch 4/5\n", | |
"1875/1875 [==============================] - 10s 6ms/step - loss: 0.0799 - accuracy: 0.9757\n", | |
"Epoch 5/5\n", | |
"1875/1875 [==============================] - 10s 5ms/step - loss: 0.0649 - accuracy: 0.9798\n", | |
" 18/313 [>.............................] - ETA: 1s - loss: 0.0440 - accuracy: 0.9931" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"2022-12-22 00:32:35.081429: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:113] Plugin optimizer for device_type GPU is enabled.\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"313/313 [==============================] - 2s 5ms/step - loss: 0.0738 - accuracy: 0.9790\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"[0.07381662726402283, 0.9790000319480896]" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import tensorflow as tf\n", | |
"mnist = tf.keras.datasets.mnist\n", | |
"(x_train, y_train),(x_test, y_test) = mnist.load_data()\n", | |
"x_train, x_test = x_train / 255.0, x_test / 255.0\n", | |
"model = tf.keras.models.Sequential([\n", | |
" tf.keras.layers.Flatten(input_shape=(28, 28)),\n", | |
" tf.keras.layers.Dense(128, activation='relu'),\n", | |
" tf.keras.layers.Dropout(0.2),\n", | |
" tf.keras.layers.Dense(10, activation='softmax')\n", | |
"])\n", | |
"\n", | |
"model.compile(optimizer='adam',\n", | |
" loss='sparse_categorical_crossentropy',\n", | |
" metrics=['accuracy'])\n", | |
"\n", | |
"model.fit(x_train, y_train, epochs=5)\n", | |
"model.evaluate(x_test, y_test)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Collecting tensorflow-macos==2.9\n", | |
" Downloading tensorflow_macos-2.9.0-cp39-cp39-macosx_11_0_arm64.whl (200.6 MB)\n", | |
"\u001b[K |████████████████████████████████| 200.6 MB 5.2 MB/s eta 0:00:011 |████████████████████████ | 150.6 MB 11.3 MB/s eta 0:00:05 |████████████████████████▎ | 151.9 MB 11.3 MB/s eta 0:00:05\n", | |
"\u001b[?25hRequirement already satisfied: protobuf>=3.9.2 in /opt/miniconda3/lib/python3.9/site-packages (from tensorflow-macos==2.9) (3.19.6)\n", | |
"Requirement already satisfied: packaging in /opt/miniconda3/lib/python3.9/site-packages (from tensorflow-macos==2.9) (22.0)\n", | |
"Requirement already satisfied: wrapt>=1.11.0 in /opt/miniconda3/lib/python3.9/site-packages (from tensorflow-macos==2.9) (1.14.1)\n", | |
"Requirement already satisfied: opt-einsum>=2.3.2 in /opt/miniconda3/lib/python3.9/site-packages (from tensorflow-macos==2.9) (3.3.0)\n", | |
"Collecting flatbuffers<2,>=1.12\n", | |
" Downloading flatbuffers-1.12-py2.py3-none-any.whl (15 kB)\n", | |
"Requirement already satisfied: six>=1.12.0 in /opt/miniconda3/lib/python3.9/site-packages (from tensorflow-macos==2.9) (1.16.0)\n", | |
"Requirement already satisfied: termcolor>=1.1.0 in /opt/miniconda3/lib/python3.9/site-packages (from tensorflow-macos==2.9) (2.1.1)\n", | |
"Requirement already satisfied: libclang>=13.0.0 in /opt/miniconda3/lib/python3.9/site-packages (from tensorflow-macos==2.9) (14.0.6)\n", | |
"Collecting keras<2.10.0,>=2.9.0rc0\n", | |
" Downloading keras-2.9.0-py2.py3-none-any.whl (1.6 MB)\n", | |
"\u001b[K |████████████████████████████████| 1.6 MB 22.2 MB/s eta 0:00:01\n", | |
"\u001b[?25hRequirement already satisfied: absl-py>=1.0.0 in /opt/miniconda3/lib/python3.9/site-packages (from tensorflow-macos==2.9) (1.3.0)\n", | |
"Requirement already satisfied: astunparse>=1.6.0 in /opt/miniconda3/lib/python3.9/site-packages (from tensorflow-macos==2.9) (1.6.3)\n", | |
"Requirement already satisfied: typing-extensions>=3.6.6 in /opt/miniconda3/lib/python3.9/site-packages (from tensorflow-macos==2.9) (4.4.0)\n", | |
"Requirement already satisfied: numpy>=1.20 in /opt/miniconda3/lib/python3.9/site-packages (from tensorflow-macos==2.9) (1.24.0)\n", | |
"Requirement already satisfied: gast<=0.4.0,>=0.2.1 in /opt/miniconda3/lib/python3.9/site-packages (from tensorflow-macos==2.9) (0.4.0)\n", | |
"Collecting tensorboard<2.10,>=2.9\n", | |
" Downloading tensorboard-2.9.1-py3-none-any.whl (5.8 MB)\n", | |
"\u001b[K |████████████████████████████████| 5.8 MB 16.3 MB/s eta 0:00:01\n", | |
"\u001b[?25hRequirement already satisfied: grpcio<2.0,>=1.24.3 in /opt/miniconda3/lib/python3.9/site-packages (from tensorflow-macos==2.9) (1.42.0)\n", | |
"Collecting tensorflow-estimator<2.10.0,>=2.9.0rc0\n", | |
" Downloading tensorflow_estimator-2.9.0-py2.py3-none-any.whl (438 kB)\n", | |
"\u001b[K |████████████████████████████████| 438 kB 23.5 MB/s eta 0:00:01\n", | |
"\u001b[?25hCollecting keras-preprocessing>=1.1.1\n", | |
" Downloading Keras_Preprocessing-1.1.2-py2.py3-none-any.whl (42 kB)\n", | |
"\u001b[K |████████████████████████████████| 42 kB 8.3 MB/s eta 0:00:01\n", | |
"\u001b[?25hRequirement already satisfied: setuptools in /opt/miniconda3/lib/python3.9/site-packages (from tensorflow-macos==2.9) (61.2.0)\n", | |
"Requirement already satisfied: h5py>=2.9.0 in /opt/miniconda3/lib/python3.9/site-packages (from tensorflow-macos==2.9) (3.6.0)\n", | |
"Requirement already satisfied: google-pasta>=0.1.1 in /opt/miniconda3/lib/python3.9/site-packages (from tensorflow-macos==2.9) (0.2.0)\n", | |
"Requirement already satisfied: wheel<1.0,>=0.23.0 in /opt/miniconda3/lib/python3.9/site-packages (from astunparse>=1.6.0->tensorflow-macos==2.9) (0.37.1)\n", | |
"Requirement already satisfied: markdown>=2.6.8 in /opt/miniconda3/lib/python3.9/site-packages (from tensorboard<2.10,>=2.9->tensorflow-macos==2.9) (3.4.1)\n", | |
"Requirement already satisfied: tensorboard-data-server<0.7.0,>=0.6.0 in /opt/miniconda3/lib/python3.9/site-packages (from tensorboard<2.10,>=2.9->tensorflow-macos==2.9) (0.6.1)\n", | |
"Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /opt/miniconda3/lib/python3.9/site-packages (from tensorboard<2.10,>=2.9->tensorflow-macos==2.9) (0.4.6)\n", | |
"Requirement already satisfied: google-auth<3,>=1.6.3 in /opt/miniconda3/lib/python3.9/site-packages (from tensorboard<2.10,>=2.9->tensorflow-macos==2.9) (2.15.0)\n", | |
"Requirement already satisfied: requests<3,>=2.21.0 in /opt/miniconda3/lib/python3.9/site-packages (from tensorboard<2.10,>=2.9->tensorflow-macos==2.9) (2.27.1)\n", | |
"Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /opt/miniconda3/lib/python3.9/site-packages (from tensorboard<2.10,>=2.9->tensorflow-macos==2.9) (1.8.1)\n", | |
"Requirement already satisfied: werkzeug>=1.0.1 in /opt/miniconda3/lib/python3.9/site-packages (from tensorboard<2.10,>=2.9->tensorflow-macos==2.9) (2.2.2)\n", | |
"Requirement already satisfied: cachetools<6.0,>=2.0.0 in /opt/miniconda3/lib/python3.9/site-packages (from google-auth<3,>=1.6.3->tensorboard<2.10,>=2.9->tensorflow-macos==2.9) (5.2.0)\n", | |
"Requirement already satisfied: pyasn1-modules>=0.2.1 in /opt/miniconda3/lib/python3.9/site-packages (from google-auth<3,>=1.6.3->tensorboard<2.10,>=2.9->tensorflow-macos==2.9) (0.2.8)\n", | |
"Requirement already satisfied: rsa<5,>=3.1.4 in /opt/miniconda3/lib/python3.9/site-packages (from google-auth<3,>=1.6.3->tensorboard<2.10,>=2.9->tensorflow-macos==2.9) (4.9)\n", | |
"Requirement already satisfied: requests-oauthlib>=0.7.0 in /opt/miniconda3/lib/python3.9/site-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.10,>=2.9->tensorflow-macos==2.9) (1.3.1)\n", | |
"Requirement already satisfied: importlib-metadata>=4.4 in /opt/miniconda3/lib/python3.9/site-packages (from markdown>=2.6.8->tensorboard<2.10,>=2.9->tensorflow-macos==2.9) (5.2.0)\n", | |
"Requirement already satisfied: zipp>=0.5 in /opt/miniconda3/lib/python3.9/site-packages (from importlib-metadata>=4.4->markdown>=2.6.8->tensorboard<2.10,>=2.9->tensorflow-macos==2.9) (3.11.0)\n", | |
"Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in /opt/miniconda3/lib/python3.9/site-packages (from pyasn1-modules>=0.2.1->google-auth<3,>=1.6.3->tensorboard<2.10,>=2.9->tensorflow-macos==2.9) (0.4.8)\n", | |
"Requirement already satisfied: certifi>=2017.4.17 in /opt/miniconda3/lib/python3.9/site-packages (from requests<3,>=2.21.0->tensorboard<2.10,>=2.9->tensorflow-macos==2.9) (2022.12.7)\n", | |
"Requirement already satisfied: idna<4,>=2.5 in /opt/miniconda3/lib/python3.9/site-packages (from requests<3,>=2.21.0->tensorboard<2.10,>=2.9->tensorflow-macos==2.9) (3.3)\n", | |
"Requirement already satisfied: charset-normalizer~=2.0.0 in /opt/miniconda3/lib/python3.9/site-packages (from requests<3,>=2.21.0->tensorboard<2.10,>=2.9->tensorflow-macos==2.9) (2.0.4)\n", | |
"Requirement already satisfied: urllib3<1.27,>=1.21.1 in /opt/miniconda3/lib/python3.9/site-packages (from requests<3,>=2.21.0->tensorboard<2.10,>=2.9->tensorflow-macos==2.9) (1.26.9)\n", | |
"Requirement already satisfied: oauthlib>=3.0.0 in /opt/miniconda3/lib/python3.9/site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.10,>=2.9->tensorflow-macos==2.9) (3.2.2)\n", | |
"Requirement already satisfied: MarkupSafe>=2.1.1 in /opt/miniconda3/lib/python3.9/site-packages (from werkzeug>=1.0.1->tensorboard<2.10,>=2.9->tensorflow-macos==2.9) (2.1.1)\n", | |
"Installing collected packages: tensorflow-estimator, tensorboard, keras-preprocessing, keras, flatbuffers, tensorflow-macos\n", | |
" Attempting uninstall: tensorflow-estimator\n", | |
" Found existing installation: tensorflow-estimator 2.11.0\n", | |
" Uninstalling tensorflow-estimator-2.11.0:\n", | |
" Successfully uninstalled tensorflow-estimator-2.11.0\n", | |
" Attempting uninstall: tensorboard\n", | |
" Found existing installation: tensorboard 2.11.0\n", | |
" Uninstalling tensorboard-2.11.0:\n", | |
" Successfully uninstalled tensorboard-2.11.0\n", | |
" Attempting uninstall: keras\n", | |
" Found existing installation: keras 2.11.0\n", | |
" Uninstalling keras-2.11.0:\n", | |
" Successfully uninstalled keras-2.11.0\n", | |
" Attempting uninstall: flatbuffers\n", | |
" Found existing installation: flatbuffers 22.12.6\n", | |
" Uninstalling flatbuffers-22.12.6:\n", | |
" Successfully uninstalled flatbuffers-22.12.6\n", | |
" Attempting uninstall: tensorflow-macos\n", | |
" Found existing installation: tensorflow-macos 2.11.0\n", | |
" Uninstalling tensorflow-macos-2.11.0:\n", | |
" Successfully uninstalled tensorflow-macos-2.11.0\n", | |
"Successfully installed flatbuffers-1.12 keras-2.9.0 keras-preprocessing-1.1.2 tensorboard-2.9.1 tensorflow-estimator-2.9.0 tensorflow-macos-2.9.0\n", | |
"Collecting tensorflow-metal==0.5.0\n", | |
" Downloading tensorflow_metal-0.5.0-cp39-cp39-macosx_11_0_arm64.whl (1.4 MB)\n", | |
"\u001b[K |████████████████████████████████| 1.4 MB 7.2 MB/s eta 0:00:01\n", | |
"\u001b[?25hCollecting six~=1.15.0\n", | |
" Downloading six-1.15.0-py2.py3-none-any.whl (10 kB)\n", | |
"Requirement already satisfied: wheel~=0.35 in /opt/miniconda3/lib/python3.9/site-packages (from tensorflow-metal==0.5.0) (0.37.1)\n", | |
"Installing collected packages: six, tensorflow-metal\n", | |
" Attempting uninstall: six\n", | |
" Found existing installation: six 1.16.0\n", | |
" Uninstalling six-1.16.0:\n", | |
" Successfully uninstalled six-1.16.0\n", | |
" Attempting uninstall: tensorflow-metal\n", | |
" Found existing installation: tensorflow-metal 0.7.0\n", | |
" Uninstalling tensorflow-metal-0.7.0:\n", | |
" Successfully uninstalled tensorflow-metal-0.7.0\n", | |
"Successfully installed six-1.15.0 tensorflow-metal-0.5.0\n" | |
] | |
} | |
], | |
"source": [ | |
"!pip install tensorflow-macos==2.9\n", | |
"!pip install tensorflow-metal==0.5.0" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "base", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.9.12" | |
}, | |
"orig_nbformat": 4, | |
"vscode": { | |
"interpreter": { | |
"hash": "3d597f4c481aa0f25dceb95d2a0067e73c0966dcbd003d741d821a7208527ecf" | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment