tensorflow_macos 速度测试
MBP16 i9-9880h 5500M 8G
#!/usr/bin/env python # coding: utf-8 import tensorflow.compat.v2 as tf import tensorflow_datasets as tfds 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='cpu') (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 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(128) 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(128) ds_test = ds_test.cache() ds_test = ds_test.prefetch(tf.data.experimental.AUTOTUNE) model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28, 1)), tf.keras.layers.Dense(128, activation='relu'), 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=10, )
GPU 速度
Epoch 1/10 469/469 [==============================] – 10s 14ms/step – batch: 234.0000 – size: 1.0000 – loss: 0.3598 – accuracy: 0.9028
Epoch 2/10 469/469 [==============================] – 9s 14ms/step – batch: 234.0000 – size: 1.0000 – loss: 0.1623 – accuracy: 0.9535
Epoch 3/10 469/469 [==============================] – 9s 14ms/step – batch: 234.0000 – size: 1.0000 – loss: 0.1182 – accuracy: 0.9664
Epoch 4/10 469/469 [==============================] – 9s 14ms/step – batch: 234.0000 – size: 1.0000 – loss: 0.0911 – accuracy: 0.9735
Epoch 5/10 469/469 [==============================] – 9s 14ms/step – batch: 234.0000 – size: 1.0000 – loss: 0.0732 – accuracy: 0.9786
CPU 速度
Epoch 1/10 469/469 [==============================] – 3s 1ms/step – batch: 234.0000 – size: 1.0000 – loss: nan – accuracy: 0.0987
Epoch 2/10 469/469 [==============================] – 3s 1ms/step – batch: 234.0000 – size: 1.0000 – loss: nan – accuracy: 0.0987
Epoch 3/10 469/469 [==============================] – 3s 1ms/step – batch: 234.0000 – size: 1.0000 – loss: nan – accuracy: 0.0987
Epoch 4/10 469/469 [==============================] – 3s 1ms/step – batch: 234.0000 – size: 1.0000 – loss: nan – accuracy: 0.0987
Epoch 5/10 469/469 [==============================] – 3s 1ms/step – batch: 234.0000 – size: 1.0000 – loss: nan – accuracy: 0.0987