@@ -0,0 +1,2481 @@
+#%0A# Copyright 2016 The BigDL Authors.%0A#%0A# Licensed under the Apache License, Version 2.0 (the %22License%22);%0A# you may not use this file except in compliance with the License.%0A# You may obtain a copy of the License at%0A#%0A#     http://www.apache.org/licenses/LICENSE-2.0%0A#%0A# Unless required by applicable law or agreed to in writing, software%0A# distributed under the License is distributed on an %22AS IS%22 BASIS,%0A# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A# See the License for the specific language governing permissions and%0A# limitations under the License.%0A#%0A#%0A# this file is adapted from the comments in https://github.com/apple/tensorflow_macos/issues/25%0A%0Aimport tensorflow.compat.v2 as tf%0Aimport tensorflow_datasets as tfds%0Afrom tensorflow.keras import Sequential%0A%0Atf.enable_v2_behavior()%0A%0Afrom tensorflow.python.framework.ops import disable_eager_execution%0Adisable_eager_execution()%0A%0A# from tensorflow.python.compiler.mlcompute import mlcompute%0A# %60mlcompute.set_mlc_device(device_name='gpu')%0A%0A%0A(ds_train, ds_test), ds_info = tfds.load(%0A    'mnist',%0A    split=%5B'train', 'test'%5D,%0A    shuffle_files=True,%0A    as_supervised=True,%0A    with_info=True,%0A)%0A%0Adef normalize_img(image, label):%0A  %22%22%22Normalizes images: %60uint8%60 -%3E %60float32%60.%22%22%22%0A  return tf.cast(image, tf.float32) / 255., label%0A%0Abatch_size = 128%0A%0Ads_train = ds_train.map(%0A    normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)%0Ads_train = ds_train.cache()%0Ads_train = ds_train.shuffle(ds_info.splits%5B'train'%5D.num_examples)%0Ads_train = ds_train.batch(batch_size)%0Ads_train = ds_train.prefetch(tf.data.experimental.AUTOTUNE)%0A%0A%0Ads_test = ds_test.map(%0A    normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)%0Ads_test = ds_test.batch(batch_size)%0Ads_test = ds_test.cache()%0Ads_test = ds_test.prefetch(tf.data.experimental.AUTOTUNE)%0A%0A%0Amodel = Sequential(%5B%0A  tf.keras.layers.Conv2D(32, kernel_size=(3, 3),%0A                 activation='relu'),%0A  tf.keras.layers.Conv2D(64, kernel_size=(3, 3),%0A                 activation='relu'),%0A  tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),%0A#   tf.keras.layers.Dropout(0.25),%0A  tf.keras.layers.Flatten(),%0A  tf.keras.layers.Dense(128, activation='relu'),%0A#   tf.keras.layers.Dropout(0.5),%0A  tf.keras.layers.Dense(10, activation='softmax')%0A%5D)%0Amodel.compile(%0A    loss='sparse_categorical_crossentropy',%0A    optimizer=tf.keras.optimizers.Adam(0.001),%0A    metrics=%5B'accuracy'%5D,%0A)%0A%0Amodel.fit(%0A    ds_train,%0A    epochs=12,%0A    validation_data=ds_test,%0A)

Add Original Code of Lenet TensorFlow Example (#4645)