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
If you don't have python 3.10, install it using homebrew:
brew install python3.10
Test your installation using:
python3.10 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
You should get something like
tf.Tensor(-212.4931, shape=(), dtype=float32)
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])))
Run the model:
python3.10 ./test.py
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]]
In the last line, the tutorial mentioned to use this:
print(model.predict([10.0]))
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])))
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:
useful links that helped me solve my issues:
The tutorial:
The video tutorial:
Top comments (0)