- 1.以TensorFlow為例
- 2.以gensim為例
- 2.1報錯:PackagesNotFoundError: The following packages are not available from current channels:
- 2.2 報錯2: RuntimeError: Cython extensions are unavailable. Without them, this gensim functionality is disabled.
- 2.3報錯3:ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject
- 3.以Keras為例
- 4.以pytorch為例
持續更新~
1.以TensorFlow為例
1.1 報錯1:zsh: illegal hardware instruction
實驗環境:macOS Big Sur 11.5.1 + anaconda3 + python3.8
直接用 pip install tensorflow 安裝,能夠順利安裝tensorflow2.5.0,但是import tensorflow 的時候會報如上錯誤.
需要注意TensorFlow版本和Python版本的適配。
1.1.1 解決方案1:下載 安裝miniforge3然后創建虛擬環境
蘋果提供的tensorflow僅支持python 3.8+,而python官網只有3.9+版本支持ARM。
我們的做法是:選擇通過ARM版conda創建python3.8虛擬環境,再使用創建的ARM python3.8安裝tensorflow.
可參考:
MacBook m1芯片采用miniforge安裝python3.9
macOS M1(Apple Silicon) 安裝配置 Conda 環境
Step 1:下載ARM版Miniforge3:
選擇最新的arm64版本;
https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-MacOSX-arm64.sh
具體可以去GitHub看:
https://github.com/conda-forge/miniforge/#download
下載Miniforge3-MacOSX-arm64.sh到自己的Download目錄里:
終端cd Downlaod/
后,開始運行腳本:
bash Miniforge3-MacOSX-arm64.sh
這里安裝開始,一定要慢慢回車,問題全部選擇yes.
因為這里我就踩坑了,選了個No,但是沒關系,可以重裝Miniforge3:具體終端里刪除相關文件就好了。具體操作如下:
rm -rf ~/miniforge3
rm -rf .conda
rm -rf .condarc
然后重新執行安裝命令:
bash Miniforge3-MacOSX-arm64.sh
安裝完成后重啟終端,然后輸入Python就可以看到Python 3.9了。
這樣miniforge就安裝好了。
Step2:創建虛擬環境
創建Python3.8虛擬環境並安裝TensorFlow:
conda create -n py38 python=3.8
conda activate py38
進入虛擬環境后,直接conda install 就能安裝TensorFlow庫了。
漫長的等待各個庫安裝完畢即可。
最后進入Python來驗證下能否import tensorflow
.
1.2 報錯2:NotImplementedError: Cannot convert a symbolic Tensor
實驗環境
TensorFlow2.4.1(Macbook m1 with conda-forge)
報錯:NotImplementedError: Cannot convert a symbolic Tensor (bidirectional/forward_lstm/strided_slice:0)
1.2.1 解決方案
numpy版本過高,conda安裝指定版本的numpy 1.19.5即可解決問題。
conda search numpy
conda install numpy=1.19.5 -c conda-forge
建議conda用戶先切換到tensorflow所在的虛擬環境(conda activate tensorflow_env
), 然后使用conda search --full --name numpy
, 其中顯示的numpy版本逐個嘗試(建議先嘗試1.19.X這個大版本)應該會解決問題.
2.以gensim為例
2.1報錯:PackagesNotFoundError: The following packages are not available from current channels:
conda install X
出現找不到X包問題,如下所示:默認的channels里找不到。
2.1.1 解決方案1:用pip install X
來安裝X包,發現可以找到包並下載,然后等待能否successfully。
如果出現一直等待的Installing build dependencies ...
這里應該是缺少Python 依賴,可以嘗試添加新鏡像:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
之后再試一下能否正常安裝X包:Python是否可以import。
如果出現pip的warning:
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
因為pip安裝第三方庫需要ssl模塊,而python默認安裝ssl功能是不可用的.
2.1.1.1 解決方法1-1:針對pip is configured with locations that require TLS/SSL問題:重新編譯Python3安裝文件,加上--with-ssl參數讓pip3使用SSL功能。
# 重新編譯安裝
./configure --enable-optimizations --with-ssl
注意:可以參考:異常處理pip is configured with locations that require TLS/SSL
2.1.1.2 解決方法1-2:針對pip is configured with locations that require TLS/SSL問題:更換pip源,即添加國內鏡像。
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
國內一些常用的軟件源如下:
阿里雲:http://mirrors.aliyun.com/pypi/simple/
中國科技大學:https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣:http://pypi.douban.com/simple/
清華大學:https://pypi.tuna.tsinghua.edu.cn/simple/
中國科學技術大學:http://pypi.mirrors.ustc.edu.cn/simple/
注意:添加這個國內鏡像,可能會與你已經存在的來自conda-forge的包產生沖突,因為版本不統一,就造成import一個包(來自國內鏡像)時,因為需要另幾個包(可能來自conda-forge和Anaconda默認鏡像)的支持,而引發導入錯誤,所以還是統一下。
個人還是推崇用default的,雖然下載比較慢,但是版本更新及時且很少有沖突。因為吃過清華源的虧。
如果嫌麻煩,安裝了Anaconda的請看解決方案2.
2.1.1.2 解決方案2:不要終端命令了,直接使用Anaconda Navigator界面Environments里手動添加吧。
如果安裝成功,檢查下Python里能否import了,如果import不成功,又出幺蛾子了,那就繼續修行。可以看異常2了。
2.2 報錯2: RuntimeError: Cython extensions are unavailable. Without them, this gensim functionality is disabled.
這個是我在安裝gensim時出錯,這個之前在Windows上就經常安裝報錯,這次在macOS M1芯片上依舊坑了。
利用miniforge3創建的conda環境(base) [/Users/dan/miniforge3/bin/python]是可以通過pip install gensim安裝成功且可以import的。如下所示。
但是在自己創建的虛擬環境(py38)[/Users/dan/miniforge3/envs/py38/bin/python]下就報錯了。
Anaconda上顯示安裝成功,但是import時就不行了。
然后卸了,重新pip install gensim 裝下。就出現異常1-1中的warning以及Could not fetch URL https://pypi.org/simple/gensim/。
看起來貌似是證書的錯誤,后面又好像是URL來源的問題,那就換成國內的pip源試一試。默認的是:
pip install gensim -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
然后就成功了。
然后驗證下吧。
有個小warning,根據提示再把這個安裝下就好了。
2.3報錯3:ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject
實驗環境:TensorFlow2.6.0, numpy 1.19.5
報錯:NotImplementedError: Cannot convert a symbolic Tensor (sequential/simple_rnn/strided_slice:0) to a numpy array. This error may indicate that you’re trying to pass a Tensor to a NumPy call, which is not supported
問題:發現是numpy
的版本問題,出錯時numpy
版本是1.19.5
,因為安裝了TensorFlow2.6.0版本,將原來的numpy 2.0.5版本降級到了1.19.5.之后在from gensim import models時出現了這個問題。
解決:考慮卸載gensim,重裝。
3.以Keras為例
3.1 報錯1:ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via pip install tensorflow
實驗環境:
tensorflow_env:
TensorFlow目前的版本是2.4.1,Keras目前版本是2.4.3,Python 3.8.6 (Macbook m1 with conda-forge)
解決:
Keras版本太高,通過降低版本到2.2來解決。
根據https://docs.floydhub.com/guides/environments/里列出的TensorFlow與Keras的版本對應關系,選擇合適的版本:
3.2 報錯2:TypeError: ('Keyword argument not understood:', 'input')
這是Keras版本的問題,keras 2.4.3中Model的參數設置:inputs, outputs
class Model(tensorflow.python.keras.engine.base_layer.Layer, tensorflow.python.keras.utils.version_utils.ModelVersionSelector)
| Model(*args, **kwargs)
|
|
Model
groups layers into an object with training and inference features.|
| Arguments:
| inputs: The input(s) of the model: a
keras.Input
object or list of|
keras.Input
objects.| outputs: The output(s) of the model. See Functional API example below.
| name: String, the name of the model.
|
將self.encoder = Model(input=inp, output=encoder_output)
改為
self.encoder = Model(inputs=inp, outputs=encoder_output)
即可。
4.以pytorch為例
如果安裝Pytorch,進入官網https://pytorch.org/get-started/locally/選擇版本:官網給出了各種安裝方法。
在前面安裝好miniforge3的基礎上,在Anaconda上的可以安裝Pytorch1.8.0版本。
conda install pytorch torchvision -c pytorch