macm1安装tensorflow以及pycharm配置
本文目录
- macm1安装tensorflow以及pycharm配置
- 使用MacOS 12
- 安装conda
- 创建一个conda环境
- 安装tensorflow
- pycharm配置
使用MacOS 12
必需条件:macOS 12+
安装conda
安装Miniforge(包含conda及一个python环境)
下载地址1:https://github.com/conda-forge/miniforge/#download
下载地址2:https://gitcode.net/mirrors/conda-forge/miniforge?utm_source=csdn_github_accelerator
chmod +x ~/Downloads/Miniforge3-MacOSX-arm64.sh
sh ~/Downloads/Miniforge3-MacOSX-arm64.sh
// 按照提示输入,剩下的一直回车键
source ~/miniforge3/bin/activate
具体过程
xxMacBook-Pro ~ % chmod +x ~/Downloads/Miniforge3-MacOSX-arm64.sh
xxMacBook-Pro ~ % sh ~/Downloads/Miniforge3-MacOSX-arm64.sh
Welcome to Miniforge3 22.9.0-3
In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>> enter (需要填写)
...
Do you accept the license terms? [yes|no]
>>> yes (需要填写)
...
Miniforge3 will now be installed into this location:
/Users/guohaonan/miniforge3
- Press ENTER to confirm the location
- Press CTRL-C to abort the installation
- Or specify a different location below
[/Users/xxx/miniforge3] >>> enter (可以选填,可以不修改,默认为miniforge3,第一次填以为是enter...,将错就错)
PREFIX=/Users/xxx/enter
...
__
__ ______ ___ ____ _____ ___ / /_ ____ _
/ / / / __ `__ \/ __ `/ __ `__ \/ __ \/ __ `/
/ /_/ / / / / / / /_/ / / / / / / /_/ / /_/ /
/ .___/_/ /_/ /_/\__,_/_/ /_/ /_/_.___/\__,_/
/_/
conda-forge/osx-arm64 Using cache
conda-forge/noarch Using cache
Transaction
Prefix: /Users/xxx/enter
...
Package Version Build Channel Size
──────────────────────────────────────────────────────────────────────────────────────────────
Install:
──────────────────────────────────────────────────────────────────────────────────────────────
+ brotlipy 0.7.0 py310h8e9501a_1005 conda-forge/osx-arm64 Cached
+ bzip2 1.0.8 h3422bc3_4 conda-forge/osx-arm64 Cached
+ ca-certificates 2022.12.7 h4653dfc_0 conda-forge/osx-arm64 Cached
...
Transaction finished
installation finished.
Do you wish the installer to initialize Miniforge3
by running conda init? [yes|no]
[no] >>> yes (需要填写)
...
Thank you for installing Miniforge3!
// enter目录将错就错
source ~/enter/bin/activate
执行完之后,环境改变,终端显示当前环境
(base) xxMacBook-Pro ~ % conda --version
conda 22.9.0
创建一个conda环境
conda create -n tf python==3.9
切换到新环境
conda activate tf
(base) xxMacBook-Pro ~ % conda activate tf
(tf) xxMacBook-Pro ~ %
安装tensorflow
在新环境下使用命令,每次使用TensorFlow的时候都需要激活conda环境
// 从Apple的仓库安装tensorflow的依赖
conda install -c apple tensorflow-deps
// 安装MacOS版本的TensorFlow
python -m pip install tensorflow-macos
// 安装tensorflow-metal,使TensorFlow支持GPU计算
python -m pip install tensorflow-metal
conda环境激活后,可以测试
(base) xx@xxMacBook-Pro ~ % conda activate tf
(tf) xx@xxMacBook-Pro ~ % python
Python 3.9.0 | packaged by conda-forge | (default, Nov 26 2020, 07:55:15)
[Clang 11.0.0 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> print(tf.__version__)
2.11.0
>>>
关闭tensorflow环境
conda deactivate
pycharm配置
创建项目
PyCharm
菜单,进入Preferences
菜单,选择对于的项目的选项Project Interpreter
查询tf环境使用的python
(tf) xxMacBook-Pro ~ % which python
/Users/xxx/enter/envs/tf/bin/python
找到对应路径下的python,选中即可
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
// 'Hello, TensorFlow!'