DEV Community

Cover image for Run TensorFlow Tutorial on Mac
mehdi
mehdi

Posted on

Run TensorFlow Tutorial on Mac

I encountered some issues while trying to run the simple TensorFlow tutorial on my local Mac M1 environment.
The main issue had to do with Python and pip (python package manager) versioning.
Use the following commands:

python3 -m venv ./venv
source venv/bin/activate
#upgrade pip
pip install --upgrade pip

# higher version of python do no work well
python3.10 -m pip install tensorflow
Enter fullscreen mode Exit fullscreen mode

If you don't have python 3.10, install it using homebrew:

brew install python3.10
Enter fullscreen mode Exit fullscreen mode

Test your installation using:

python3.10 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
Enter fullscreen mode Exit fullscreen mode

You should get something like

tf.Tensor(-212.4931, shape=(), dtype=float32)
Enter fullscreen mode Exit fullscreen mode

create a file called test.py

import tensorflow as tf
import numpy as np
from tensorflow import keras

model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])

model.compile(optimizer='sgd', loss='mean_squared_error')

xs = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-2.0, 1.0, 4.0, 7.0, 10.0, 13.0], dtype=float)

model.fit(xs, ys, epochs=500)

print(model.predict(x=np.array([10.0])))
Enter fullscreen mode Exit fullscreen mode

Run the model:

python3.10 ./test.py
Enter fullscreen mode Exit fullscreen mode

It should finish with something like this:

Epoch 500/500
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 12ms/step - loss: 4.0766e-06
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 22ms/step
[[30.99411]]
Enter fullscreen mode Exit fullscreen mode

In the last line, the tutorial mentioned to use this:

print(model.predict([10.0]))
Enter fullscreen mode Exit fullscreen mode

However it might fail with an error like this:
raise ValueError(f"Unrecognized data type: x={x} (of type {type(x)})") ValueError: Unrecognized data type: x=10.0
to fix it, use :

print(model.predict(x=np.array([10.0])))
Enter fullscreen mode Exit fullscreen mode

Installation links I tried to follow:
Installation steps:

https://medium.com/r/?url=https%3A%2F%2Fwww.tensorflow.org%2Finstall%2Fpip%23macos

More detailed installation steps:

https://medium.com/r/?url=https%3A%2F%2Fwww.tensorflow.org%2Finstall%2Fpip%23step-by-step_instructions

useful links that helped me solve my issues:

https://medium.com/r/?url=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F75608323%2Fhow-do-i-solve-error-externally-managed-environment-every-time-i-use-pip-3

https://medium.com/r/?url=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F63011755%2Ferror-could-not-find-a-version-that-satisfies-the-requirement-tensorflow-addons

https://medium.com/r/?url=https%3A%2F%2Fcommunity.deeplearning.ai%2Ft%2Fvalueerror-unrecognized-data-type-x-10-0-of-type-class-list%2F514700

The tutorial:

https://medium.com/r/?url=https%3A%2F%2Fdevelopers.google.com%2Fcodelabs%2Ftensorflow-1-helloworld%230

The video tutorial:

https://youtu.be/KNAWp2S3w94?si=RKVbqQNSd106jnEf

Top comments (0)