简体   繁体   English

如何在 Windows 中安装 pytorch?

[英]How to install pytorch in windows?

我正在尝试在 Windows 上安装 pytorch,并且有一个可用但显示错误。

conda install -c peterjc123 pytorch=0.1.12

Warning: Unless you have a very specific reason not to, just follow the official installation instructions from https://pytorch.org .警告:除非您有非常具体的理由不这样做,否则只需按照https://pytorch.org的官方安装说明进行操作即可。 They are far more likely to be accurate and up-to-date.它们更有可能是准确和最新的。


Here is how to install the PyTorch package from the official channel, on Windows using Anaconda, as of the time of writing this comment (31/03/2020):以下是截至撰写此评论时 (31/03/2020) 在 Windows 上使用 Anaconda 从官方渠道安装 PyTorch 包的方法:

PyTorch without CUDA:没有 CUDA 的 PyTorch:

conda install pytorch torchvision cpuonly -c pytorch

PyTorch with CUDA 10.1:带有 CUDA 10.1 的 PyTorch:

conda install pytorch torchvision cudatoolkit=10.1 -c pytorch

go to the official website: http://pytorch.org/去官网: http : //pytorch.org/

  1. Select Windows as your operating system选择 Windows 作为您的操作系统
  2. Select your Package Manager such as pip or conda选择您的包管理器,例如 pip 或 conda
  3. Select you python version选择你的python版本
  4. Select CUDA or choose none You will get the command that will install pytorch on your system based on your selection.选择 CUDA 或选择 none 您将获得将根据您的选择在您的系统上安装 pytorch 的命令。

For example, if you choose Windows, pip, python 3.6 and none in the listed steps, you will get the following commands:例如,如果您在列出的步骤中选择Windows、pip、python 3.6none ,您将获得以下命令:

pip3 install http://download.pytorch.org/whl/cpu/torch-0.4.0-cp36-cp36m-win_amd64.whl 
pip3 install torchvision

Actual answer :实际答案


Best way is to check on the official website for up-to-date options.最好的方法是在官方网站上查看最新选项。 Here are the ones working as of 2020-03:以下是截至 2020-03 年的工作:

# for windows 10, CUDA 10.1
conda install pytorch torchvision cudatoolkit=10.1 -c pytorch

#for windows 10, CUDA 9
conda install pytorch torchvision cudatoolkit=9.2 -c pytorch -c defaults -c numba/label/dev 

Previous answer(out-of-date)以前的答案(过时)


It seems that the author (peterjc123) released 2 days ago conda packages to install PyTorch 0.3.0 on windows.似乎作者(peterjc123)在 2 天前发布了 conda 包来在 Windows 上安装 PyTorch 0.3.0。 Here is a copy:这是一个副本:

# for Windows 10 and Windows Server 2016, CUDA 8
conda install -c peterjc123 pytorch cuda80

# for Windows 10 and Windows Server 2016, CUDA 9
conda install -c peterjc123 pytorch cuda90

# for Windows 7/8/8.1 and Windows Server 2008/2012, CUDA 8
conda install -c peterjc123 pytorch_legacy cuda80

source: https://github.com/pytorch/pytorch/issues/494#issuecomment-350527200来源: https : //github.com/pytorch/pytorch/issues/494#issuecomment-350527200

If you are trying to install on windows 10 and you are not having the anaconda installation than the best options are below:如果您尝试在 Windows 10 上安装并且您没有安装 anaconda,那么最好的选择如下:

Python 2.7蟒蛇 2.7

pip install https://download.pytorch.org/whl/cpu/torch-1.0.1-cp27-cp27mu-linux_x86_64.whl
pip install torchvision

If the above command does not work, then you have python 2.7 UCS2, use this command如果上面的命令不起作用,那么你有python 2.7 UCS2,使用这个命令

pip install https://download.pytorch.org/whl/cpu/torch-1.0.1-cp27-cp27m-linux_x86_64.whl

Python 3.5蟒蛇 3.5

pip3 install https://download.pytorch.org/whl/cpu/torch-1.0.1-cp35-cp35m-win_amd64.whl
pip3 install torchvision

Python 3.6蟒蛇 3.6

pip3 install https://download.pytorch.org/whl/cpu/torch-1.0.1-cp36-cp36m-win_amd64.whl
pip3 install torchvision

Python 3.7蟒蛇 3.7

pip3 install https://download.pytorch.org/whl/cpu/torch-1.0.1-cp37-cp37m-win_amd64.whl
pip3 install torchvision

Update June 2019: pytorch has a dedicated conda channel now and can be installed easily with anaconda. 2019 年 6 月更新:pytorch 现在有一个专用的 conda 频道,可以使用 anaconda 轻松安装。 The command generated at pytorch will require dependencies before it can be executed successfully.在 pytorch 生成的命令需要依赖才能成功执行。 For example I chose stable pytorch 1.1 build with python 3.6 and Cuda 10.0.例如,我选择了带有 python 3.6 和 Cuda 10.0 的稳定 pytorch 1.1 版本。 The command generated by pytorch page was as follows: pytorch页面生成的命令如下:

conda install pytorch torchvision cudatoolkit=10.0 -c pytorch conda 安装 pytorch torchvision cudatoolkit=10.0 -c pytorch

But it will not work if you have created a new conda environment like me.但是如果你像我一样创建了一个新的 conda 环境,它就行不通了。 The step by step process for setting up pytorch is as follows:设置pytorch的分步过程如下:

  • First install the cudatoolkit as follows:首先安装cudatoolkit,如下所示:

conda install -c anaconda cudatoolkit=10.0 conda install -c anaconda cudatoolkit=10.0

  • Then install the mkl_fft as follows:然后安装 mkl_fft 如下:

conda install -c anaconda mkl_fft conda install -c anaconda mkl_fft

  • Assuming you will face no more dependency issues.假设您将不再面临依赖问题。 Use the following command to setup pytorch:使用以下命令设置 pytorch:

conda install -c pytorch pytorch conda install -c pytorch pytorch

This worked for me.这对我有用。 But I had setup my new conda environment with scikit-learn and jupyter notebook before starting the pytorch setup.但是在开始 pytorch 设置之前,我已经使用 scikit-learn 和 jupyter notebook 设置了我的新 conda 环境。 So if any dependency problem arise, it would be a good idea to install both scikit-learn and jupyter notebook as well.因此,如果出现任何依赖问题,最好同时安装 scikit-learn 和 jupyter notebook。

The trick is to go to the PyTorch website and select the things you need:诀窍是去 PyTorch网站并选择你需要的东西:

在此处输入图片说明

You may want to consider using Docker for Windows .您可能需要考虑使用Docker for Windows This would enable you to install pytorch as you would on Linux.这将使您能够像在 Linux 上一样安装 pytorch。 Although, I believe that DfW has limited CUDA support, so you may want to explore a different option if you plan on using CUDA.虽然,我相信 DfW 对 CUDA 的支持有限,因此如果您计划使用 CUDA,您可能需要探索不同的选项。

Try running:尝试运行:

conda install -c pytorch pytorch

The command will update/install: conda, cudatoolkit, pytorch.该命令将更新/安装:conda、cudatoolkit、pytorch。

pip install torch==1.5.0+cpu torchvision==0.6.0+cpu -f https://download.pytorch.org/whl/torch_stable.html

conda install -c peterjc123 pytorch   #for windows

This one simple line will get your work done for windows. 这一条简单的线将为Windows完成您的工作。

OR you may use this one for linux or osx. 或者,您可以将其用于linux或osx。

conda install -c soumith pytorch 

I was getting some kind of Rollback error on Git bash and Windows Cmd prompt so had to run Anaconda prompt as admin for:我在 Git bash 和 Windows Cmd 提示符下遇到某种Rollback error ,因此必须以管理员身份运行 Anaconda 提示符:

conda install pytorch-cpu -c pytorch

and then I got another when I tried the following command on Anaconda prompt:然后当我在 Anaconda 提示符下尝试以下命令时又得到了另一个:

pip3 install torchvision

so I switched back to Windows prompt to enter it and it worked.所以我切换回 Windows 提示输入它并且它起作用了。

To test the installation, I ran this from Git Bash:为了测试安装,我从 Git Bash 运行了这个:

$ python reinforcement_q_learning.py

with source code that looks like (the snippet near the top anyways):源代码看起来像(无论如何靠近顶部的片段):

"""

import gym
import math
import random
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from collections import namedtuple
from itertools import count
from PIL import Image

import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torchvision.transforms as T


env = gym.make('CartPole-v0').unwrapped

# set up matplotlib
is_ipython = 'inline' in matplotlib.get_backend()
if is_ipython:
    from IPython import display

plt.ion()

# if gpu is to be used
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")


######################################################################
# Replay Memory
# -------------
#
# We'll be using experience replay memory for training our DQN. It stores
# the transitions that the agent observes, allowing us to reuse this data
# later. By sampling from it randomly, the transitions that build up a
# batch are decorrelated. It has been shown that this greatly stabilizes
# and improves the DQN training procedure.
#
# For this, we're going to need two classses:
#
# -  ``Transition`` - a named tuple representing a single transition in
#    our environment. It maps essentially maps (state, action) pairs
#    to their (next_state, reward) result, with the state being the
#    screen difference image as described later on.
# -  ``ReplayMemory`` - a cyclic buffer of bounded size that holds the
#    transitions observed recently. It also implements a ``.sample()``
#    method for selecting a random batch of transitions for training.
#

Transition = namedtuple('Transition',
                        ('state', 'action', 'next_state', 'reward'))


class ReplayMemory(object):

    def __init__(self, capacity):
        self.capacity = capacity
        self.memory = []
        self.position = 0

    def push(self, *args):
        """Saves a transition."""
        if len(self.memory) < self.capacity:
            self.memory.append(None)
        self.memory[self.position] = Transition(*args)
        self.position = (self.position + 1) % self.capacity

    def sample(self, batch_size):
        return random.sample(self.memory, batch_size)

    def __len__(self):
        return len(self.memory)

############continues to line 507...

A few steps as described here may help to install Pytorch in Windows: 此处描述的一些步骤可能有助于在Windows中安装Pytorch:

  1. First, we need to install Shapely. 首先,我们需要安装Shapely。 For this download Shapely as Shapely-1.6.3-cp36-cp36m-win_amd64.whl from here . 此处下载Shapely as Shapely-1.6.3-cp36-cp36m-win_amd64.whl。

Then go to the directory where you have downloaded the whl file and then press SHIFT and right click and select open command prompt here and then execute this: 然后转到下载了whl文件的目录,然后按SHIFT键并右键单击并在此处选择打开命令提示符,然后执行以下命令:

pip install Shapely-1.6.3-cp36-cp36m-win_amd64.whl pip安装Shapely-1.6.3-cp36-cp36m-win_amd64.whl

  1. Secondly, we need to install Fiona. 其次,我们需要安装Fiona。 For this go to Gohlke and download Fiona‑1.7.13‑cp36‑cp36m‑win_amd64.whl 为此,请转到Gohlke并下载Fiona‑1.7.13‑cp36‑cp36m‑win_amd64.whl

Then execute this command: 然后执行以下命令:

pip install Fiona‑1.7.13‑cp36‑cp36m‑win_amd64.whl pip安装Fiona‑1.7.13‑cp36‑cp36m‑win_amd64.whl

  1. Third and final step is to download PyTorch . 第三步也是最后一步是下载PyTorch Again just as before execute this in command prompt: 再次像以前一样在命令提示符下执行此操作:

pip install torch-0.4.0-cp36-cp36m-win_amd64.whl 点安装火炬-0.4.0-cp36-cp36m-win_amd64.whl

On windows 10, Python 3.6 with pip install following works for me, 在Windows 10上,具有pip install的Python 3.6对我有效,

    pip3 install https://download.pytorch.org/whl/cu90/torch-1.1.0-cp36-cp36m-win_amd64.whl

also you need to install torchvision 还需要安装torchvision

pip3 install https://download.pytorch.org/whl/cu90/torchvision-0.3.0-cp36-cp36m-win_amd64.whl

Try this尝试这个

  1. cd C:\\Program files\\Anaconda3\\condabin cd C:\\Program files\\Anaconda3\\condabin
  2. conda install pytorch-cpu torchvision-cpu -c pytorch conda 安装 pytorch-cpu torchvision-cpu -c pytorch

https://pytorch.org/get-started/locally/#anaconda https://pytorch.org/get-started/locally/#anaconda

for python 3.7 which is the latest till date对于最新的python 3.7

for pytorch on cpu用于 CPU 上的 pytorch

pip install https://download.pytorch.org/whl/cpu/torch-1.0.1-cp37-cp37m-win_amd64.whl

pip install torchvision

I was using the official website( https://pytorch.org/get-started/locally/ ) where the following command is mentioned for Windows 10 & Conda environment:我正在使用官方网站( https://pytorch.org/get-started/locally/ ),其中提到了以下命令适用于 Windows 10 和 Conda 环境:

conda install pytorch torchvision cudatoolkit=10.2 -c pytorch conda 安装 pytorch torchvision cudatoolkit=10.2 -c pytorch

I ran this command in Anaconda command prompt but I was getting stuck as there were following errors我在 Anaconda 命令提示符下运行了这个命令,但由于出现以下错误,我被卡住了

ERROR conda.core.link:_execute(502): An error occurred while uninstalling package 'defaults::pycurl-7.43.0.1-py36h74b6da3_0'.错误 conda.core.link:_execute(502): 卸载包“defaults::pycurl-7.43.0.1-py36h74b6da3_0”时出错。 WindowsError(5, 'Access is denied') WindowsError(5, '访问被拒绝')

To rectify this I opened the Anaconda command prompt as administrator and then ran the same command again.为了解决这个问题,我以管理员身份打开了 Anaconda 命令提示符,然后再次运行相同的命令。 It solved the access issue and allowed the package to get installed.它解决了访问问题并允许安装包。

So you just have to use following two steps:所以你只需要使用以下两个步骤:

Step 1: Open Anaconda prompt as administrator第 1 步:以管理员身份打开 Anaconda prompt

Step 2: run following command第二步:运行以下命令

conda install pytorch torchvision cudatoolkit=10.2 -c pytorch conda 安装 pytorch torchvision cudatoolkit=10.2 -c pytorch

Below is the working configuration:以下是工作配置:

Python 3.8蟒蛇 3.8

Pytorch version 1.5.1 pytorch 版本 1.5.1

Windows 10视窗 10

pytorch installation command: pytorch安装命令:

conda install pytorch torchvision cudatoolkit=9.2 -c pytorch -c defaults -c numba/label/dev

If @x0s answer gives dependency issues then try updating conda before that.如果@x0s 回答给出了依赖问题,那么在此之前尝试更新 conda。

conda update conda  
conda install -c peterjc123 pytorch_legacy cuda80

This Line of code did the trick for me:这行代码对我有用:

conda install -c peterjc123 pytorch

Check these links out in case you have any problem installing:如果您在安装时遇到任何问题,请查看这些链接:

Superdatascience Tutorial Explains Clearly how to do it.超级数据科学教程清楚地解释了如何做到这一点。

Or just go to the anaconda pytorch page: https://anaconda.org/peterjc123/pytorch或者直接访问 anaconda pytorch 页面: https ://anaconda.org/peterjc123/pytorch

It worked for me.Hope my answer was useful.它对我有用。希望我的回答有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM