Is TensorFlow supported on M1 max? - python-3.x

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>
Attempting the numpy upgrade posted answer, I did the "numpy upgrade" yet had the output below on the terminal and the same exception still observed.
pip install numpy --upgrade
Requirement already satisfied: numpy in ./opt/anaconda3/lib/python3.9/site-packages (1.21.5)
Collecting numpy
Using cached numpy-1.23.4-cp39-cp39-macosx_11_0_arm64.whl (13.4 MB)
Installing collected packages: numpy
Attempting uninstall: numpy
Found existing installation: numpy 1.21.5
Uninstalling numpy-1.21.5:
Successfully uninstalled numpy-1.21.5
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
scipy 1.7.3 requires numpy<1.23.0,>=1.16.5, but you have numpy 1.23.4 which is incompatible.
numba 0.55.1 requires numpy<1.22,>=1.18, but you have numpy 1.23.4 which is incompatible.
Successfully installed numpy-1.23.4
==================================
So combination of multiple approaches fixed the issue:
pip uninstall keras
pip uninstall keras-preprocessing
pip uninstall tensorboard
pip install --upgrade numpy
If step 4 does not work [error or concerning warning], then pip uninstall numpy ; followed by pip install numpy
python -m pip install tensorflow-macos
That fix my environment problem.

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)

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.

So combination of multiple approaches fixed the issue:
pip uninstall keras
pip uninstall keras-preprocessing
pip uninstall tensorboard
pip install --upgrade numpy
If step 4 does not work [error or concerning warning], then pip uninstall numpy; followed by pip install numpy
python -m pip install tensorflow-macos
That fix my environment problem.

Related

segmentation fault python after import torch on mac M1

I struggled to install pytorch on my Mac M1 chip.
I fixed the previous issue with mkl here
Now I do:
conda install ipykernel jupyter numpy pandas matplotlib nomkl
pip install torch torchvision
python
import torch
and I get:
zsh:segmentation fault python
from terminal, when I run jupyter - the kernel just crashes
how to fix it?
i just met this error, mac m1, python3.8 & pyTorch 1.14.
installed from this command:
pip3 install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu
and meet this error too.
The following command help's me to solve the problem
conda install "libblas==mkl"
I refered this article:
https://github.com/pytorch/pytorch/issues/66782

ImportError after installing torchtext 0.11.0 with conda

I have installed pytorch version 1.10.0 alongside torchtext, torchvision and torchaudio using conda. My PyTorch is cpu-only, and I have experimented with both conda install pytorch-mutex -c pytorch and conda install pytorch cpuonly -c pytorch to install the cpuonly version, both yielding the same eror that I will describe in the following lines.
I have also installed pytorch-lightning in conda, alongside jsonargparse[summaries via pip in the environment.
I have written this code to see whether LightningCLI works or not.
# script.py
import torch
import pytorch_lightning as pl
class BoringModel(LightningModule):
def __init__(self):
super().__init__()
self.layer = torch.nn.Linear(32, 2)
def forward(self, x):
return self.layer(x)
def training_step(self, batch, batch_idx):
loss = self(batch).sum()
self.log("train_loss", loss)
return {"loss": loss}
def validation_step(self, batch, batch_idx):
loss = self(batch).sum()
self.log("valid_loss", loss)
def test_step(self, batch, batch_idx):
loss = self(batch).sum()
self.log("test_loss", loss)
def configure_optimizers(self):
return torch.optim.SGD(self.layer.parameters(), lr=0.1)
cli = LightningCLI(BoringModel)
But when I run it using python -m script fit --print_config, I get the following error:
ImportError: /home/farhood/miniconda3/envs/pytorch_dummy_environment/lib/python3.9/site-packages/torchtext/_torchtext.so: undefined symbol: _ZNK5torch3jit6MethodclESt6vectorIN3c106IValueESaIS4_EERKSt13unordered_mapISsS4_St4hashISsESt8equal_toISsESaISt4pairIKSsS4_EEE
Which indicates that there is something broken with my Conda installation, and it's probably related to torchtext somehow.
This is the versions of the installed torch related packages:
pytorch 1.10.0 cpu_py39hc5866cc_0 conda-forge
pytorch-lightning 1.5.2 pyhd8ed1ab_0 conda-forge
pytorch-mutex 1.0 cuda pytorch
torchaudio 0.10.0 py39_cpu pytorch
torchmetrics 0.6.0 pyhd8ed1ab_0 conda-forge
torchtext 0.11.0 py39 pytorch
torchvision 0.11.1 py39_cpu pytorch
So in order to fix the problem, I had to change my environment.yaml in order to force pytorch to install from the pytorch channel.
So this is my environment.yaml now:
channels:
- defaults
- pytorch
- conda-forge
dependencies:
# ML section
- pytorch::pytorch
- pytorch::torchtext
- pytorch::torchvision
- pytorch::torchaudio
- pytorch::cpuonly
- mlflow=1.21.0
- pytorch-lightning>=1.5.2
- pip:
- jsonargparse[signatures]
Using this I don't get the error anymore. The pytorch related stuff installed now is:
cpuonly 2.0 0 pytorch
pytorch 1.10.0 py3.9_cpu_0 pytorch
pytorch-lightning 1.5.2 pyhd8ed1ab_0 conda-forge
pytorch-mutex 1.0 cpu pytorch
torchaudio 0.10.0 py39_cpu [cpuonly] pytorch
torchtext 0.11.0 py39 pytorch
torchvision 0.11.1 py39_cpu [cpuonly] pytorch

ImportError: TensorBoard logging requires TensorBoard version 1.15 or above

I follow the tutorials in pytorch.org
It occurs error:TensorBoard logging requires TensorBoard version 1.15 or above,but I have install TensorBoard already.
Here is the code:
#from torch.utils.tensorboard import SummaryWriter
from tensorboardX import SummaryWriter
writer = SummaryWriter('runs/fashion_mnist_experiment_1')
#get some random training images
dataiter = iter(trainloader)
images , labels = dataiter.next()
#create grid of images
img_grid = torchvision.utils.make_grid(images)
matplotlib_imshow(img_grid,one_channel=True)
writer.add_image('four_fashion_images',img_grid)
writer.add_graph(net, images)
writer.close()
Error:
ImportError Traceback (most recent call last)
<ipython-input-12-d38808675cb4> in <module>
----> 1 writer.add_graph(net, images)
2 writer.close()
~\anaconda3\envs\torch2\lib\site-packages\tensorboardX\writer.py in add_graph(self, model, input_to_model, verbose)
791
792 """
--> 793 from torch.utils.tensorboard._pytorch_graph import graph
794 self._get_file_writer().add_graph(graph(model, input_to_model, verbose))
795
~\anaconda3\envs\torch2\lib\site-packages\torch\utils\tensorboard\__init__.py in <module>
2 from distutils.version import LooseVersion
3 if not hasattr(tensorboard, '__version__') or LooseVersion(tensorboard.__version__) < LooseVersion('1.15'):
----> 4 raise ImportError('TensorBoard logging requires TensorBoard version 1.15 or above')
5 del LooseVersion
6 del tensorboard
ImportError: TensorBoard logging requires TensorBoard version 1.15 or above
Environment:
tensorboard 2.3.0 pypi_0 pypi
tensorboard-plugin-wit 1.7.0 pypi_0 pypi
tensorboardx 2.1 pypi_0 pypi
tensorflow 1.2.1 py36_0 defaults
pytorch 1.6.0 py3.6_cuda102_cudnn7_0 pytorch
torchvision 0.7.0 py36_cu102 pytorch
future 0.18.2 py36_1 defaults
protobuf 3.12.3 py36h33f27b4_0 defaults
I use from torch.utils.tensorboard import SummaryWriter at the beginning,but it occurs the error as same as above.Then I use from tensorboardX import SummaryWriter
Uninstall tensorflow, tensorboard, tensorboardx and tensorboard-plugin-wit.
Install only tensorboard with conda after that.
If this doesn't work, recreate your conda environment only with tensorboard. If you need tensorflow as well install it beforehand.
EDIT:
tensorboard-plugin-wit is a dependency of tensorboard and should be installed automatically as per their pypi description when installing tensorboard itself.
My problem was that my directory I was working in was named tensorboard, so it tried to import from the current directory instead of from the installed package.
So I suggest trying to rename the directory to see if it helps.
Do
conda uninstall tensorflow
conda uninstall tensorboard
conda uninstall tensorboardx
conda uninstall tensorboard-plugin-wit
conda uninstall cloud-tpu-client
pip uninstall tensorflow
pip uninstall tensorboard
pip uninstall tensorboardx
pip uninstall tensorboard-plugin-wit
pip uninstall cloud-tpu-client
then
conda install tensorboard
I both tried conda install and pip install, but this error still occurred when I used from torch.utils.tensorboard import SummaryWriter in an ipynb file. And I checked not hasattr(tensorboard, '__version__') or LooseVersion(tensorboard.__version__) < LooseVersion('1.15'), which both are False, meaning that it won't raise an error.
At last, I closed this ipynb file and restarted it, it worked. You should try this and no need to be struggling in reinstalling your conda env.
For my case, I had only Tensorboard without Tensorflow when I faced the error, so I had to:
  conda uninstall tensorboard - this removed PyTorch Lightning as well
  conda install tensorboard
  conda install -c conda-forge pytorch-lightning

can not import TensorFlow in Spyder or Python (ModuleNotFoundError: No module named 'tensorflow')

I have tried to install both CPU and GPU version of TensorFlow according to the manual from here https://docs.anaconda.com/anaconda/user-guide/tasks/tensorflow/ and all was ok:
(tf-gpu) C:\Users\Kosh>conda create -n tf tensorflow Collecting
package metadata (current_repodata.json): done Solving environment:
done
==> WARNING: A newer version of conda exists. <== current version: 4.7.12 latest version: 4.8.3
Please update conda by running
$ conda update -n base -c defaults conda
Package Plan
environment location: C:\Anaconda3\envs\tf
added / updated specs:
- tensorflow
The following packages will be downloaded:
package | build
---------------------------|-----------------
_tflow_select-2.2.0 | eigen 3 KB
tensorflow-2.1.0 |eigen_py37hd727fc0_0 4 KB
tensorflow-base-2.1.0 |eigen_py37h49b2757_0 35.4 MB
------------------------------------------------------------
Total: 35.4 MB
The following NEW packages will be INSTALLED:
_tflow_select pkgs/main/win-64::_tflow_select-2.2.0-eigen
absl-py pkgs/main/win-64::absl-py-0.9.0-py37_0 asn1crypto
pkgs/main/win-64::asn1crypto-1.3.0-py37_0 astor
pkgs/main/win-64::astor-0.8.0-py37_0 blas
pkgs/main/win-64::blas-1.0-mkl blinker
pkgs/main/win-64::blinker-1.4-py37_0 ca-certificates
pkgs/main/win-64::ca-certificates-2020.1.1-0 cachetools
pkgs/main/noarch::cachetools-3.1.1-py_0 certifi
pkgs/main/win-64::certifi-2020.4.5.1-py37_0 cffi
pkgs/main/win-64::cffi-1.14.0-py37h7a1dbc1_0 chardet
pkgs/main/win-64::chardet-3.0.4-py37_1003 click
pkgs/main/noarch::click-7.1.1-py_0 cryptography
pkgs/main/win-64::cryptography-2.8-py37h7a1dbc1_0 gast
pkgs/main/win-64::gast-0.2.2-py37_0 google-auth
pkgs/main/noarch::google-auth-1.13.1-py_0 google-auth-oauth~
pkgs/main/noarch::google-auth-oauthlib-0.4.1-py_2 google-pasta
pkgs/main/noarch::google-pasta-0.2.0-py_0 grpcio
pkgs/main/win-64::grpcio-1.27.2-py37h351948d_0 h5py
pkgs/main/win-64::h5py-2.10.0-py37h5e291fa_0 hdf5
pkgs/main/win-64::hdf5-1.10.4-h7ebc959_0 icc_rt
pkgs/main/win-64::icc_rt-2019.0.0-h0cc432a_1 idna
pkgs/main/noarch::idna-2.9-py_1 intel-openmp
pkgs/main/win-64::intel-openmp-2020.0-166 keras-applications
pkgs/main/noarch::keras-applications-1.0.8-py_0 keras-preprocessi~
pkgs/main/noarch::keras-preprocessing-1.1.0-py_1 libprotobuf
pkgs/main/win-64::libprotobuf-3.11.4-h7bd577a_0 markdown
pkgs/main/win-64::markdown-3.1.1-py37_0 mkl
pkgs/main/win-64::mkl-2020.0-166 mkl-service
pkgs/main/win-64::mkl-service-2.3.0-py37hb782905_0 mkl_fft
pkgs/main/win-64::mkl_fft-1.0.15-py37h14836fe_0 mkl_random
pkgs/main/win-64::mkl_random-1.1.0-py37h675688f_0 numpy
pkgs/main/win-64::numpy-1.18.1-py37h93ca92e_0 numpy-base
pkgs/main/win-64::numpy-base-1.18.1-py37hc3f5095_1 oauthlib
pkgs/main/noarch::oauthlib-3.1.0-py_0 openssl
pkgs/main/win-64::openssl-1.1.1f-he774522_0 opt_einsum
pkgs/main/noarch::opt_einsum-3.1.0-py_0 pip
pkgs/main/win-64::pip-20.0.2-py37_1 protobuf
pkgs/main/win-64::protobuf-3.11.4-py37h33f27b4_0 pyasn1
pkgs/main/noarch::pyasn1-0.4.8-py_0 pyasn1-modules
pkgs/main/noarch::pyasn1-modules-0.2.7-py_0 pycparser
pkgs/main/noarch::pycparser-2.20-py_0 pyjwt
pkgs/main/win-64::pyjwt-1.7.1-py37_0 pyopenssl
pkgs/main/win-64::pyopenssl-19.1.0-py37_0 pyreadline
pkgs/main/win-64::pyreadline-2.1-py37_1 pysocks
pkgs/main/win-64::pysocks-1.7.1-py37_0 python
pkgs/main/win-64::python-3.7.7-h60c2a47_0_cpython requests
pkgs/main/win-64::requests-2.23.0-py37_0 requests-oauthlib
pkgs/main/noarch::requests-oauthlib-1.3.0-py_0 rsa
pkgs/main/noarch::rsa-4.0-py_0 scipy
pkgs/main/win-64::scipy-1.4.1-py37h9439919_0 setuptools
pkgs/main/win-64::setuptools-46.1.3-py37_0 six
pkgs/main/win-64::six-1.14.0-py37_0 sqlite
pkgs/main/win-64::sqlite-3.31.1-he774522_0 tensorboard
pkgs/main/noarch::tensorboard-2.1.0-py3_0 tensorflow
pkgs/main/win-64::tensorflow-2.1.0-eigen_py37hd727fc0_0
tensorflow-base
pkgs/main/win-64::tensorflow-base-2.1.0-eigen_py37h49b2757_0
tensorflow-estima~
pkgs/main/noarch::tensorflow-estimator-2.1.0-pyhd54b08b_0 termcolor
pkgs/main/win-64::termcolor-1.1.0-py37_1 urllib3
pkgs/main/win-64::urllib3-1.25.8-py37_0 vc
pkgs/main/win-64::vc-14.1-h0510ff6_4 vs2015_runtime
pkgs/main/win-64::vs2015_runtime-14.16.27012-hf0eaf9b_1 werkzeug
pkgs/main/win-64::werkzeug-0.14.1-py37_0 wheel
pkgs/main/win-64::wheel-0.34.2-py37_0 win_inet_pton
pkgs/main/win-64::win_inet_pton-1.1.0-py37_0 wincertstore
pkgs/main/win-64::wincertstore-0.2-py37_0 wrapt
pkgs/main/win-64::wrapt-1.12.1-py37he774522_1 zlib
pkgs/main/win-64::zlib-1.2.11-h62dcd97_3
Proceed ([y]/n)? y
Downloading and Extracting Packages
_tflow_select-2.2.0 | 3 KB | ############################################################################ | 100% tensorflow-2.1.0 | 4 KB |
###################################################################### | 100% tensorflow-base-2.1. | 35.4 MB |
###################################################################### | 100% Preparing transaction: done Verifying transaction: done
Executing transaction: done
#
To activate this environment, use
#
$ conda activate tf
#
To deactivate an active environment, use
#
$ conda deactivate
(tf-gpu) C:\Users\Kosh> (tf-gpu) C:\Users\Kosh> (tf-gpu)
C:\Users\Kosh>conda activate tf-2 Could not find conda environment:
tf-2 You can list all discoverable environments with conda info
--envs.
(tf-gpu) C:\Users\Kosh>conda activate tf
but when I try to import it in Spyder or in Python I get the same result:
import tensorflow as tf
Traceback (most recent call last):
File "<ipython-input-1-64156d691fe5>", line 1, in <module>
import tensorflow as tf
ModuleNotFoundError: No module named 'tensorflow'
Can somebody help me with this?
I have the same problem using Python 3.7 and Tensorflow 2.1.
I created the environment tf-2 using conda create -n tf-2 tensorflow pandas.
There is no error using command prompt, but Spyder didn't find the Tensorflow library.
I verified that the IPython window was showing Python 3.6.9 (base version), while the command prompt was returning 3.7 to this command:
python -c "import platform; print(platform.python_version())"
The reason were:
the new environment didn't have Spyder installed;
conda install Tensorflow 1.13 by default.
Then, I removed the environment tf2 and recreated it including the Python version and Spyder:
conda create -n tf2 python=3.6 spyder
Then, I installed tensorflow by pip.
conda activate tf2
pip install tensorflow
It worked for me:
Run the anaconda prompt as administrator.(right click-> run as administrator).
pip uninstall tensorflow.
Close anaconda prompt.
Again run anaconda prompt as administrator.
type:
conda install tensorflow
It will ask for some y / n
Type y.
Now after all done, change your python interpreter's environment to anaconda environment where you installed tensorflow.
After that tensorflow imported successfully!

Module 'tensorflow' has no attribute 'placeholder' but tfv1 is imported

I'm trying to run following code:
input_img = Input((height, width, 1), name='img')
model = get_unet(input_img, n_filters=16, dropout=0.05, batchnorm=True)
model.compile(optimizer=Adam(), loss="binary_crossentropy", metrics=["accuracy"])
But I'm getting "AttributeError: module 'tensorflow' has no attribute 'placeholder'".
Everywhere it is recommended to use
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
But I still keep getting such error.
If you are using Anaconda distribution the try to reinstall TensorFlow by using following commands
Remove tenserflow
conda remove tensorflow-gpu tensorflow tensorflow-base
re-installed tensorflow
conda install -c anaconda tensorflow
Reference:- https://anaconda.org/anaconda/tensorflow
If your code needs to run on GPU it is better to install tensorflow-gpu by using
conda install -c anaconda tensorflow-gpu
Reference:- https://anaconda.org/anaconda/tensorflow-gpu
Also if your project uses keras(Or distribution has keras(ex:-Colab)) then you can use tensorflow insides on keras by using "tensorflow.keras"
Ex:-
from keras.models import Sequential
Change to
from tensorflow.keras.models import Sequential
Python version of Colab has updated .
Add this
!pip uninstall keras-nightly
!pip install h5py==2.10.0
!pip install q keras==2.1.6
%tensorflow_version 1.x

Resources