Home > Software engineering >  Is TensorFlow supported on M1 max?
Is TensorFlow supported on M1 max?

Time:11-09

In my attempt to start using TensorFlow on my mac [Monterey 12.6.1] [chip Apple M1 MAX] I start to get errors that I did not observe on my mac mini [Monterey 12.6 - Chip M1 2020]

It is either an environment issue or a chipset issue. [Works on my windows machine Win-11 and Mac-Mini]

Code:

from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam
from tensorflow.keras import layers

model = Sequential([layers.Input((3, 1)),
                    layers.LSTM(64),
                    layers.Dense(32, activation='relu'),
                    layers.Dense(32, activation='relu'),
                    layers.Dense(1)])

model.compile(loss='mse', 
              optimizer=Adam(learning_rate=0.001),
              metrics=['mean_absolute_error'])

model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=100)

Error observed in DataSpell:

--------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
RuntimeError: module compiled against API version 0x10 but this version of numpy is 0xe

Traceback

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [14], in <cell line: 1>()
----> 1 from tensorflow.keras.models import Sequential
      2 from tensorflow.keras.optimizers import Adam
      3 from tensorflow.keras import layers

Following Greg Hogg tutorial: https://www.youtube.com/watch?v=CbTU92pbDKw

Note this code work on my mac mini machine but not on the MacBook Pro. Anaconda env -> Python 3.9


python --version
Python 3.9.12

conda list | grep tensorflow
tensorflow-deps           2.8.0                         0    apple
tensorflow-estimator      2.10.0                   pypi_0    pypi
tensorflow-macos          2.10.0                   pypi_0    pypi
tensorflow-metal          0.6.0                    pypi_0    pypi

What I am expecting is similar outcome like the windows environment and the Mac-mini where the model is constructed and fitted with the training data. (model object creation without an exception)

Example:

Epoch 99/100
7/7 [==============================] - 0s 5ms/step - loss: 6.1541 - mean_absolute_error: 1.8648 - val_loss: 9.5456 - val_mean_absolute_error: 2.6235
Epoch 100/100
7/7 [==============================] - 0s 5ms/step - loss: 6.7555 - mean_absolute_error: 2.0134 - val_loss: 9.4403 - val_mean_absolute_error: 2.6016

<keras.callbacks.History at 0x27a6590c6a0>

CodePudding user response:

From Apple documentation to install tensorflow-metal with tensorflow

Requirements

  • Mac computers with Apple silicon or AMD GPUs
  • macOS 12.0 or later (Get the latest beta)
  • Python 3.8 or later
  • Xcode command-line tools: xcode-select --install

Set up

// Download Conda environment
bash ~/miniconda.sh -b -p $HOME/miniconda
source ~/miniconda/bin/activate
conda install -c apple tensorflow-deps

// Install base TensorFlow
python -m pip install tensorflow-macos

// Install tensorflow-metal plug-in
python -m pip install tensorflow-metal

Verify the set up

import tensorflow as tf

cifar = tf.keras.datasets.cifar100
(x_train, y_train), (x_test, y_test) = cifar.load_data()
model = tf.keras.applications.ResNet50(
    include_top=True,
    weights=None,
    input_shape=(32, 32, 3),
    classes=100,)

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"])
model.fit(x_train, y_train, epochs=5, batch_size=64)

CodePudding user response:

The real answer to your problem though is that you have many version of numpy installed.

Please run pip3 install --upgrade numpy in your terminal and it should fix your problem.

  • Related