MacBook 安装 PyTorch 环境

配置 anaconda 环境变量

zhaobo@zhaobodeMacBook-Pro ~ % /opt/anaconda3/bin/conda init zsh
no change     /opt/anaconda3/condabin/conda
no change     /opt/anaconda3/bin/conda
no change     /opt/anaconda3/bin/conda-env
no change     /opt/anaconda3/bin/activate
no change     /opt/anaconda3/bin/deactivate
no change     /opt/anaconda3/etc/profile.d/conda.sh
no change     /opt/anaconda3/etc/fish/conf.d/conda.fish
no change     /opt/anaconda3/shell/condabin/Conda.psm1
no change     /opt/anaconda3/shell/condabin/conda-hook.ps1
no change     /opt/anaconda3/lib/python3.8/site-packages/xontrib/conda.xsh
no change     /opt/anaconda3/etc/profile.d/conda.csh
modified      /Users/zhaobo/.zshrc

==> For changes to take effect, close and re-open your current shell. <==
zhaobo@zhaobodeMacBook-Pro ~ % cat ~/.zshrc

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/opt/anaconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/opt/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/opt/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/opt/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

zhaobo@zhaobodeMacBook-Pro ~ % source ~/.zshrc

执行conda命令:

(base) zhaobo@zhaobodeMacBook-Pro ~ % conda
usage: conda [-h] [-V] command ...

conda is a tool for managing and deploying applications, environments and packages.

Options:

positional arguments:
  command
    clean        Remove unused packages and caches.
    compare      Compare packages between conda environments.
    config       Modify configuration values in .condarc. This is modeled after the git config command. Writes to the
                 user .condarc file (/Users/zhaobo/.condarc) by default.
    create       Create a new conda environment from a list of specified packages.
    help         Displays a list of available conda commands and their help strings.
    info         Display information about current conda install.
    init         Initialize conda for shell interaction. [Experimental]
    install      Installs a list of packages into a specified conda environment.
    list         List linked packages in a conda environment.
    package      Low-level conda package utility. (EXPERIMENTAL)
    remove       Remove a list of packages from a specified conda environment.
    uninstall    Alias for conda remove.
    run          Run an executable in a conda environment. [Experimental]
    search       Search for packages and display associated information. The input is a MatchSpec, a query language
                 for conda packages. See examples below.
    update       Updates conda packages to the latest compatible version.
    upgrade      Alias for conda update.

optional arguments:
  -h, --help     Show this help message and exit.
  -V, --version  Show the conda version number and exit.

conda commands available from other packages:
  build
  convert
  debug
  develop
  env
  index
  inspect
  metapackage
  render
  server
  skeleton
  verify

打开anaconda prompt,激活自己建立的pytorch环境,进入到环境中,可以先看下是否已经安装相关镜像源:

(base) zhaobo@zhaobodeMacBook-Pro ~ % conda config --show
add_anaconda_token: True
add_pip_as_python_dependency: True
aggressive_update_packages:
  - ca-certificates
  - certifi
  - openssl
allow_conda_downgrades: False
allow_cycles: True
allow_non_channel_urls: False
allow_softlinks: False
always_copy: False
always_softlink: False
always_yes: None
anaconda_upload: None
auto_activate_base: True
auto_stack: 0
auto_update_conda: True
bld_path:
changeps1: True
channel_alias: https://conda.anaconda.org
channel_priority: flexible
channels:
  - defaults
client_ssl_cert: None
client_ssl_cert_key: None
clobber: False
conda_build: {}
create_default_packages: []
croot: /opt/anaconda3/conda-bld
custom_channels:
  pkgs/main: https://repo.anaconda.com
  pkgs/r: https://repo.anaconda.com
  pkgs/pro: https://repo.anaconda.com
custom_multichannels:
  defaults:
    - https://repo.anaconda.com/pkgs/main
    - https://repo.anaconda.com/pkgs/r
  local:
debug: False
default_channels:
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
default_python: 3.8
default_threads: None
deps_modifier: not_set
dev: False
disallowed_packages: []
download_only: False
dry_run: False
enable_private_envs: False
env_prompt: ({default_env})
envs_dirs:
  - /opt/anaconda3/envs
  - /Users/zhaobo/.conda/envs
error_upload_url: https://conda.io/conda-post/unexpected-error
execute_threads: 1
extra_safety_checks: False
force: False
force_32bit: False
force_reinstall: False
force_remove: False
ignore_pinned: False
json: False
local_repodata_ttl: 1
migrated_channel_aliases: []
migrated_custom_channels: {}
non_admin_enabled: True
notify_outdated_conda: True
offline: False
override_channels_enabled: True
path_conflict: clobber
pinned_packages: []
pip_interop_enabled: False
pkgs_dirs:
  - /opt/anaconda3/pkgs
  - /Users/zhaobo/.conda/pkgs
proxy_servers: {}
quiet: False
remote_backoff_factor: 1
remote_connect_timeout_secs: 9.15
remote_max_retries: 3
remote_read_timeout_secs: 60.0
repodata_fns:
  - current_repodata.json
  - repodata.json
repodata_threads: None
report_errors: None
restore_free_channel: False
rollback_enabled: True
root_prefix: /opt/anaconda3
safety_checks: warn
sat_solver: pycosat
separate_format_cache: False
shortcuts: True
show_channel_urls: None
solver_ignore_timestamps: False
ssl_verify: True
subdir: osx-64
subdirs:
  - osx-64
  - noarch
target_prefix_override:
track_features: []
unsatisfiable_hints: True
unsatisfiable_hints_check_depth: 2
update_modifier: update_specs
use_index_cache: False
use_local: False
use_only_tar_bz2: False
verbosity: 0
verify_threads: 1
whitelist_channels: []
(base) zhaobo@zhaobodeMacBook-Pro ~ % conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

再次查看

(base) zhaobo@zhaobodeMacBook-Pro ~ % conda config --show
add_anaconda_token: True
add_pip_as_python_dependency: True
aggressive_update_packages:
  - ca-certificates
  - certifi
  - openssl
allow_conda_downgrades: False
allow_cycles: True
allow_non_channel_urls: False
allow_softlinks: False
always_copy: False
always_softlink: False
always_yes: None
anaconda_upload: None
auto_activate_base: True
auto_stack: 0
auto_update_conda: True
bld_path:
changeps1: True
channel_alias: https://conda.anaconda.org
channel_priority: flexible
channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
  - defaults
client_ssl_cert: None
client_ssl_cert_key: None
clobber: False
conda_build: {}
create_default_packages: []
croot: /opt/anaconda3/conda-bld
custom_channels:
  pkgs/main: https://repo.anaconda.com
  pkgs/r: https://repo.anaconda.com
  pkgs/pro: https://repo.anaconda.com
custom_multichannels:
  defaults:
    - https://repo.anaconda.com/pkgs/main
    - https://repo.anaconda.com/pkgs/r
  local:
debug: False
default_channels:
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
default_python: 3.8
default_threads: None
deps_modifier: not_set
dev: False
disallowed_packages: []
download_only: False
dry_run: False
enable_private_envs: False
env_prompt: ({default_env})
envs_dirs:
  - /opt/anaconda3/envs
  - /Users/zhaobo/.conda/envs
error_upload_url: https://conda.io/conda-post/unexpected-error
execute_threads: 1
extra_safety_checks: False
force: False
force_32bit: False
force_reinstall: False
force_remove: False
ignore_pinned: False
json: False
local_repodata_ttl: 1
migrated_channel_aliases: []
migrated_custom_channels: {}
non_admin_enabled: True
notify_outdated_conda: True
offline: False
override_channels_enabled: True
path_conflict: clobber
pinned_packages: []
pip_interop_enabled: False
pkgs_dirs:
  - /opt/anaconda3/pkgs
  - /Users/zhaobo/.conda/pkgs
proxy_servers: {}
quiet: False
remote_backoff_factor: 1
remote_connect_timeout_secs: 9.15
remote_max_retries: 3
remote_read_timeout_secs: 60.0
repodata_fns:
  - current_repodata.json
  - repodata.json
repodata_threads: None
report_errors: None
restore_free_channel: False
rollback_enabled: True
root_prefix: /opt/anaconda3
safety_checks: warn
sat_solver: pycosat
separate_format_cache: False
shortcuts: True
show_channel_urls: None
solver_ignore_timestamps: False
ssl_verify: True
subdir: osx-64
subdirs:
  - osx-64
  - noarch
target_prefix_override:
track_features: []
unsatisfiable_hints: True
unsatisfiable_hints_check_depth: 2
update_modifier: update_specs
use_index_cache: False
use_local: False
use_only_tar_bz2: False
verbosity: 0
verify_threads: 1
whitelist_channels: []

在命令终端输入指令,注意官网给的指令是:

conda install pytorch torchvision cpuonly -c pytorch

但是一定要去掉后面的-c pytorch 。因为命令最后是-c pytorch,所以默认还是从conda源下载,新安装的清华等源没有用上。

(base) zhaobo@zhaobodeMacBook-Pro ~ % conda install pytorch torchvision cpuonly
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /opt/anaconda3

  added / updated specs:
    - cpuonly
    - pytorch
    - torchvision


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    cpuonly-1.0                |                0           2 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
    libuv-1.40.0               |       haf1e3a3_0         334 KB
    ninja-1.10.2               |   py38hf7b0b51_0         101 KB
    pytorch-1.7.1              |          py3.8_0        64.5 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
    torchvision-0.8.2          |         py38_cpu         6.5 MB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
    ------------------------------------------------------------
                                           Total:        71.5 MB

The following NEW packages will be INSTALLED:

  cpuonly            anaconda/cloud/pytorch/noarch::cpuonly-1.0-0
  libuv              pkgs/main/osx-64::libuv-1.40.0-haf1e3a3_0
  ninja              pkgs/main/osx-64::ninja-1.10.2-py38hf7b0b51_0
  pytorch            anaconda/cloud/pytorch/osx-64::pytorch-1.7.1-py3.8_0
  torchvision        anaconda/cloud/pytorch/osx-64::torchvision-0.8.2-py38_cpu


Proceed ([y]/n)? y


Downloading and Extracting Packages
ninja-1.10.2         | 101 KB    | ############################################################################ | 100%
pytorch-1.7.1        | 64.5 MB   | ############################################################################ | 100%
cpuonly-1.0          | 2 KB      | ############################################################################ | 100%
libuv-1.40.0         | 334 KB    | ############################################################################ | 100%
torchvision-0.8.2    | 6.5 MB    | ############################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

进入 jupyter notebook 命令行,执行:

jupyter notebook
(base) zhaobo@zhaobodeMacBook-Pro ~ % jupyter notebook
[I 14:00:56.699 NotebookApp] JupyterLab extension loaded from /opt/anaconda3/lib/python3.8/site-packages/jupyterlab
[I 14:00:56.699 NotebookApp] JupyterLab application directory is /opt/anaconda3/share/jupyter/lab
[I 14:00:56.702 NotebookApp] 启动notebooks 在本地路径: /Users/zhaobo
[I 14:00:56.702 NotebookApp] Jupyter Notebook 6.1.4 is running at:
[I 14:00:56.702 NotebookApp] http://localhost:8888/?token=d014715bd72fb8abbe086104a667af6e878f264dc1310298
[I 14:00:56.702 NotebookApp]  or http://127.0.0.1:8888/?token=d014715bd72fb8abbe086104a667af6e878f264dc1310298
[I 14:00:56.702 NotebookApp] 使用control-c停止此服务器并关闭所有内核(两次跳过确认).
[C 14:00:56.709 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///Users/zhaobo/Library/Jupyter/runtime/nbserver-39288-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=d014715bd72fb8abbe086104a667af6e878f264dc1310298
     or http://127.0.0.1:8888/?token=d014715bd72fb8abbe086104a667af6e878f264dc1310298
[I 14:02:47.707 NotebookApp] Creating new notebook in
[I 14:02:47.715 NotebookApp] Writing notebook-signing key to /Users/zhaobo/Library/Jupyter/notebook_secret
[I 14:02:48.942 NotebookApp] Kernel started: a11ef32e-bb84-4764-9f50-1e6b82f09fd6, name: python3
[I 14:04:49.683 NotebookApp] Saving file at /未命名.ipynb

浏览器跳转到jupyter notebook 控制台。
在这里插入图片描述
点击新建,选择 Python3
在这里插入图片描述
每输入一行代码,点击运行,实现换行,如果没有报错,说明pytorch安装成功。

import torch
torch.FloatTensor([5,3,5])

在这里插入图片描述
参考:
https://www.jianshu.com/p/377480121388
https://blog.csdn.net/weixin_44457768/article/details/105640758

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值