TensorFlow, an open-source machine learning framework developed by Google Brain, has become a cornerstone in the field of artificial intelligence since its release in 2015. It provides a comprehensive ecosystem for developing and deploying machine learning models, particularly deep learning applications. TensorFlow's flexibility and scalability have made it a popular choice among researchers, developers, and enterprises.

One of the key strengths of TensorFlow is its support for multiple platforms. It allows models to be deployed on a variety of devices, including CPUs, GPUs, and TPUs, as well as mobile and embedded systems. This versatility ensures that TensorFlow can be used for a wide range of applications, from research prototypes to large-scale production systems.

TensorFlow offers an extensive collection of pre-built models and libraries, simplifying the process of building complex neural networks. Its high-level APIs, such as Keras, make it accessible to beginners, while its low-level operations provide the control and customization needed by advanced users. This dual approach helps cater to a diverse user base with varying levels of expertise.

The framework also emphasizes efficient computation and performance. Its computation graph approach allows for optimized execution of complex mathematical operations, making it suitable for both training and inference tasks. TensorFlow's distributed computing capabilities enable the training of large models on multiple machines, significantly reducing training time.

TensorFlow has a strong community and extensive documentation, which contribute to its widespread adoption. The active community continually develops new tools, tutorials, and resources, ensuring that users can find support and stay updated with the latest advancements. Google's backing and integration with other Google services also bolster TensorFlow's credibility and usability.

In summary, TensorFlow's comprehensive ecosystem, platform flexibility, robust performance, and supportive community have established it as a leading framework in the machine learning landscape. Its ability to cater to both novice and expert users alike has helped it maintain a dominant position in the rapidly evolving field of AI and machine learning.

I am getting started into Astroomy datasets. One of the first things I wanted to do is get adjusted and acquainted with TensorFlow. Since I have an Apple M1, I wanted to get tensorflow installed and using the integrated libraries so when the datasets are written, they are more native using the Apple Metal framework.

Here are the scratch notes.

  1. Install Xcode Command Line Tools
  2. Install Miniforge
  3. Install Tensorflow 2.5 and its dependencies
  4. Install Jupyter Notebook, Pandas
  5. Run a Benchmark by training the MNIST dataset

Step 1: I have already installed Xcode Command Line Tools on my mac. If it’s not already installed in your system, you can install it by running the following command below in your terminal.

xcode-select --install

Step 2. Install Miniforge

Install miniforge for arm64 (Apple Silicon) from miniforge GitHub.

Miniforge enables installing python packages natively compiled for Apple Silicon.

After the installation of miniforge, by default, it gives us one base environment. You can turn off the default base env by running

conda config --set auto_activate_base true

Step 3. Installing Tensorflow-MacOS

Install the Tensorflow dependencies:

conda install -c apple tensorflow-deps

Install base TensorFlow:

pip install tensorflow-macos

Install metal plugin:

pip install tensorflow-metal

Step 4. Install Jupyter Notebook & Pandas

conda install -c conda-forge -y pandas jupyter

Step 5. Run a Benchmark by training the MNIST dataset

Let’s install Tensorflow Datasets

pip install tensorflow_datasets

Make sure conda environment is activated.

In your terminal run

jupyter notebook

It will open a browser window

Create a new python3 notebook

Let’s first import TensorFlow and check

import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))

Let’s run a benchmark
I got the code snippet from TensorFlow Issues

Copy and paste code below in the new notebook

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='gpu')


(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=12,
    validation_data=ds_test,
)

Examine the results.