在我尝试在我的mac蒙特利12.6.1上开始使用TensorFlow时,我开始发现在我的mac迷你蒙特利12.6芯片M1 2020上没有观察到的错误
这要么是环境问题,要么是芯片组问题。在我的windows机器Win-11和Mac-Mini上工作。
代码:
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)在DataSpell中观察到的错误:
--------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
RuntimeError: module compiled against API version 0x10 but this version of numpy is 0xe回溯
---------------------------------------------------------------------------
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下面是Greg教程:https://www.youtube.com/watch?v=CbTU92pbDKw
注意,这段代码工作在我的machine机器上,而不是在MacBook Pro上。Anaconda env -> Python3.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我期待的是类似的结果,如windows环境和Mac-mini,其中的模型是构建和安装的培训数据。(毫无例外地创建模型对象)
示例:
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>在尝试numpy升级时,我做了"numpy升级“,但是终端上仍然有下面的输出,同样的异常仍然被观察到。
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==================================
因此,多种方法的结合解决了这一问题:
发布于 2022-11-09 00:41:50
从苹果文档到tensorflow安装
要求
设置
// 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验证设置
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)发布于 2022-11-09 00:46:54
但是,解决问题的真正答案是,您安装了许多版本的numpy。
请在您的终端上运行pip3 install --upgrade numpy,它会解决您的问题。
发布于 2022-11-09 17:33:10
因此,多种方法的结合解决了这一问题:
pip uninstall keraspip uninstall keras-preprocessingpip uninstall tensorboardpip install --upgrade numpypip uninstall numpy;后面跟着pip install numpypython -m pip install tensorflow-macos解决了我的环境问题。
https://stackoverflow.com/questions/74368411
复制相似问题