当前位置:   article > 正文

02/03_Pytorch安装、Conda安装Pythorch,换源、pytorch官网、验证、安装jupyter、卸载、安装、启动jupyter、配置Jupyter notebook、使用_serving notebooks from

serving notebooks from

1.2.Pytorch安装

1.2.1.Conda安装Pythorch,换源

1 conda添加清华镜像源
查看源

conda config --show-sources
  • 1

由于从官方的conda源中下载速度过于缓慢,我们需要配置清华镜像源:

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
  • 1
  • 2
  • 3

为了保险起见,建议同时添加第三方conda源:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --set show_channel_urls yes
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2 创建并进入虚拟环境

conda create -n pytorch

conda activate pytorch
  • 1
  • 2
  • 3

1.2.2.pytorch官网

官网开始学习的地址:https://pytorch.org/get-started/locally/;点击相关的内容,可以看到如下的内容:
在这里插入图片描述
在图片的最下方可以看到Run this Command,这个就是要运行的命令。如上显示的内容(注意:如果下载的慢,就使用-i命令指定清华的源):

pip install torch===1.7.1 torchvision===0.8.2 torchaudio===0.7.2 -f https://download.pytorch.org/whl/torch_stable.html -i https://pypi.tuna.tsinghua.edu.cn/simple
  • 1

选择另外的,可能会出现类似:

NOTE: Python 3.9 users will need to add '-c=conda-forge' for installation
conda install pytorch torchvision torchaudio cudatoolkit=10.1 -c pytorch (但是这里一定要注意,去掉-c pytorch,安装的时候才会默认从清华源下载相应的包,因此这里用命令行)
conda install pytorch torchvision torchaudio cudatoolkit=10.1
  • 1
  • 2
  • 3

接着等待安装成功就好了。

1.2.3.验证

为了确保PyTorch被正确安装,我们可以运行样例PyTorch代码来验证安装。这里我们将构造一个随机初始化张量。

# -*- coding: UTF-8 -*-

import torch
x = torch.rand(5,3)
print(x)

print(torch.__version__)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

结果应该是:

tensor([[0.2966, 0.2469, 0.2908],
        [0.1125, 0.0722, 0.1402],
        [0.4008, 0.0162, 0.7278],
        [0.3127, 0.6203, 0.2005],
        [0.2571, 0.6653, 0.8652]])
1.7.1+cpu
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

1.3.安装jupyter

参考博文:https://blog.csdn.net/yu1014745867/article/details/84191485

Jupyter Notebook(此前被称为IPython notebook)是一个交互式笔记本,支持运行40多种编程语言。

Jupyter Notebook的本质是一个为web应用程序,便于创建和共享程序文档,支持实时代码,数学方程,可视化和markdown

1.3.1.卸载、安装、启动jupyter

打开Anaconda PowerShell,然后输入:

pip install jupyter 
  • 1

执行效果如下:

(base) PS C:\Users\toto> pip install jupyter
Requirement already satisfied: jupyter in d:\installed\anaconda3\lib\site-packages (1.0.0)
Requirement already satisfied: notebook in d:\installed\anaconda3\lib\site-packages (from jupyter) (6.0.1)
Requirement already satisfied: jupyter-console in d:\installed\anaconda3\lib\site-packages (from jupyter) (6.0.0)
Requirement already satisfied: ipywidgets in d:\installed\anaconda3\lib\site-packages (from jupyter) (7.5.1)
Requirement already satisfied: nbconvert in d:\installed\anaconda3\lib\site-packages (from jupyter) (5.6.0)
Requirement already satisfied: qtconsole in d:\installed\anaconda3\lib\site-packages (from jupyter) (4.5.5)
Requirement already satisfied: ipykernel in d:\installed\anaconda3\lib\site-packages (from jupyter) (5.1.2)
Requirement already satisfied: nbformat in d:\installed\anaconda3\lib\site-packages (from notebook->jupyter) (4.4.0)
Requirement already satisfied: jupyter-client>=5.3.1 in d:\installed\anaconda3\lib\site-packages (from notebook->jupyter) (5.3.3)
Requirement already satisfied: prometheus-client in d:\installed\anaconda3\lib\site-packages (from notebook->jupyter) (0.7.1)
Requirement already satisfied: pyzmq>=17 in d:\installed\anaconda3\lib\site-packages (from notebook->jupyter) (18.1.0)
Requirement already satisfied: jupyter-core>=4.4.0 in d:\installed\anaconda3\lib\site-packages (from notebook->jupyter) (4.5.0)
Requirement already satisfied: terminado>=0.8.1 in d:\installed\anaconda3\lib\site-packages (from notebook->jupyter) (0.8.2)
Requirement already satisfied: tornado>=5.0 in d:\installed\anaconda3\lib\site-packages (from notebook->jupyter) (6.0.3)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

在这里插入图片描述
启动jupyter

(base) PS C:\Users\toto> jupyter notebook
  • 1

界面效果:

(base) PS C:\Users\toto> jupyter notebook
[I 12:28:21.470 NotebookApp] The port 8888 is already in use, trying another port.
[I 12:28:21.533 NotebookApp] JupyterLab extension loaded from D:\installed\Anaconda3\lib\site-packages\jupyterlab
[I 12:28:21.533 NotebookApp] JupyterLab application directory is D:\installed\Anaconda3\share\jupyter\lab
[I 12:28:21.536 NotebookApp] Serving notebooks from local directory: C:\Users\toto
[I 12:28:21.536 NotebookApp] The Jupyter Notebook is running at:
[I 12:28:21.537 NotebookApp] http://localhost:8889/?token=c5fadcb59ade1027bfa184f07ca753f4acb13185241fa6be
[I 12:28:21.537 NotebookApp]  or http://127.0.0.1:8889/?token=c5fadcb59ade1027bfa184f07ca753f4acb13185241fa6be
[I 12:28:21.537 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 12:28:21.760 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///C:/Users/toto/AppData/Roaming/jupyter/runtime/nbserver-12424-open.html
    Or copy and paste one of these URLs:
        http://localhost:8889/?token=c5fadcb59ade1027bfa184f07ca753f4acb13185241fa6be
     or http://127.0.0.1:8889/?token=c5fadcb59ade1027bfa184f07ca753f4acb13185241fa6be
[I 12:31:25.070 NotebookApp] 302 GET /?token=c5fadcb59ade1027bfa184f07ca753f4acb13185241fa6be (127.0.0.1) 1.00ms
[I 12:31:32.990 NotebookApp] Creating new file in
[I 12:32:10.919 NotebookApp] Creating new notebook in
[I 12:32:13.228 NotebookApp] Kernel started: 082a09a5-e52a-40f2-904f-3870a737fdc6
[I 12:33:14.333 NotebookApp] Saving file at /Untitled1.ipynb
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

在这里插入图片描述

1.3.2.配置Jupyter notebook

1、命令行输入命令:

(base) PS C:\Users\toto> jupyter notebook --generate-config
Writing default config to: C:\Users\toto\.jupyter\jupyter_notebook_config.py
(base) PS C:\Users\toto>
  • 1
  • 2
  • 3

2、打开”.jupyter”文件夹,可以看到里面有个配置文件
在这里插入图片描述

3、修改jupyter_notebook_config.py配置文件
找到”c.NotebookApp.notebook_dir=…”,把路径改成自己的工作目录。
在这里插入图片描述
在这里插入图片描述
4、配置完成后,重新jupyter notebook启动即可
5、启动方式:
6、Anaconda–>Anaconda Prompt -->命令行输入:

jupyter notebook
  • 1
7、启动后默认的打开路径就是之前配置好的路径啦。
  • 1

1.3.3.使用

1、运行jupyter notebook
2、需要新建python文件的话,点击New–>Python3
在这里插入图片描述

3、新建后,页面显示如下,菜单栏也出来了,就可以开始编写文件啦。
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/127515
推荐阅读
相关标签
  

闽ICP备14008679号