1 m1 mac 安装xcode命令工具
在Terminal终端执行以下代码:
xcode-select --install
2 下载支持m1芯片arm64的miniconda
在miniconda官网,找到下图中保护Apple M1 的bash安装包,
Miniconda — Anaconda documentation
3 安装 miniconda
在Terminal执行下列代码:
1)cd "miniconda下载目录"
2)bash Miniconda3_macOS_Apple_M1_ARM64.sh
遇到输入 y/n,一直yes就好
4 为zsh 的shell 添加环境变量,
(如果使用的bash请跳过这一步)
4.1 vim 打开 ~/.bash_profile
将conda相关的环境变量拷贝到zsh的配置文件.zshrc中即可完成zsh环境变量的配置
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/opt/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/opt/miniconda3/etc/profile.d/conda.sh" ]; then
. "/opt/miniconda3/etc/profile.d/conda.sh"
else
export PATH="/opt/miniconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
4.2 激活.zshrc文件
在Terminal终端执行如下命令:
source ~/.zshrc
4.3 安装必要的源
miniconda 必须要添加一些源,否则可能conda install 包 失败
在Terminal终端输入如下命令,添加源:
conda config --add channels conda-forge
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
conda config --set show_channel_urls yes
5 安装tensorflow2.16.1
5.1 下载tensorflow2.16.1
tensorflow与python版本的对应参考tensorflow官网最后的表格: https://www.tensorflow.org/install/source
因为我的系统本身装有python3.11.7, 就选择了最新的tensorflow2.16.1
5.2 采用apple官网的方式安装tensorflow
5.2.1、创建一个python3.8虚拟环境( 如果安装出错可以直接删除这个虚拟环境 )
1) conda create -n py3.8_tf2.5_torch1.8 python=3.8
2) 激活:
conda activate py3.8_tf2.5_torch1.8
5.2.2、安装cython
conda install cython
5.2.3、使用apple官网安装tensorflow
1) 设置虚拟环境变量
python3 -m venv ~/venv-metal
source ~/venv-metal/bin/activate
python -m pip install -U pip
2)安装 apple 的 TensorFlow dependencies
终端Terminal输入如下命令:
conda install -c apple tensorflow-deps==2.16.1
3)Install base TensorFlow 2.16.1
python -m pip install tensorflow-macos==2.16.1
4)Install tensorflow-metal plugin
python -m pip install tensorflow-metal
5)验证是否可以使用tensorflow
在terminal输入:python 进入python编码模式,
输入:import tensorflow as tf 如果运行成功,则说明安装成功
再输入如下完整的代码段,测试其他功能
import tensorflow as tf
import time
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
model.summary()
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
start = time.time()
model.fit(x_train, y_train, epochs=5)
end = time.time()
model.evaluate(x_test, y_test)
print(end - start)
6 升级已有安装的apple tensorflow
来自 apple官网的方法:
# uninstall existing tensorflow-macos and tensorflow-metal
python -m pip uninstall tensorflow-macos
python -m pip uninstall tensorflow-metal
# Upgrade tensorflow-deps
conda install -c apple tensorflow-deps==版本号 --force-reinstall
# or point to specific conda environment
conda install -c apple tensorflow-deps==版本号 --force-reinstall -n my_env
# 重新安装
python -m pip install tensorflow-macos==版本号
python -m pip install tensorflow-metal
标签:python3.11,apple,python,tensorflow2.16,Air,--,conda,install,tensorflow From: https://blog.csdn.net/pisceshsu/article/details/141362944