torch安装过程

http://torch.ch/docs/getting-started.html#_

Getting started with TorchEdit on GitHub

 

Installing Torch

We provide a simple installation process for Torch on Mac OS X and Ubuntu 12+:

Torch can be installed to your home folder in ~/torch by running these three commands:

# in a terminal, run the commands WITHOUT sudo
git clone https://github.com/torch/distro.git ~/torch --recursive
cd ~/torch; bash install-deps;
./install.sh

The first script installs the basic package dependencies that LuaJIT and Torch require. The second script installs LuaJITLuaRocks, and then uses LuaRocks (the lua package manager) to install core packages like torchnnand paths, as well as a few other packages.

The script adds torch to your PATH variable. You just have to source it once to refresh your env variables. The installation script will detect what is your current shell and modify the path in the correct configuration file.

# On Linux with bash
source ~/.bashrc
# On Linux with zsh
source ~/.zshrc
# On OSX or in Linux with none of the above.
source ~/.profile

If you ever need to uninstall torch, simply run the command:

rm -rf ~/torch

If you want to install torch with Lua 5.2 instead of LuaJIT, simply run:

git clone https://github.com/torch/distro.git ~/torch --recursive
cd ~/torch

# clean old torch installation
./clean.sh
# optional clean command (for older torch versions)
# curl -s https://raw.githubusercontent.com/torch/ezinstall/master/clean-old.sh | bash

# https://github.com/torch/distro : set env to use lua
TORCH_LUA_VERSION=LUA52 ./install.sh

New packages can be installed using Luarocks from the command-line:

# run luarocks WITHOUT sudo
$ luarocks install image
$ luarocks list

Once installed you can run torch with the command th from you prompt!

The easiest way to learn and experiment with Torch is by starting an interactive session (also known as the torch read-eval-print loop or TREPL):

$ th
 
  ______             __   |  Torch7                                   
 /_  __/__  ________/ /   |  Scientific computing for Lua.         
  / / / _ \/ __/ __/ _ \  |                                           
 /_/  \___/_/  \__/_//_/  |  https://github.com/torch   
                          |  http://torch.ch            
			  
th> torch.Tensor{1,2,3}
 1
 2
 3
[torch.DoubleTensor of dimension 3]

th>

To exit the interactive session, type ^c twice — the control key together with the c key, twice, or type os.exit(). Once the user has entered a complete expression, such as 1 + 2, and hits enter, the interactive session evaluates the expression and shows its value.

To evaluate expressions written in a source file file.lua, write dofile "file.lua".

To run code in a file non-interactively, you can give it as the first argument to the th command::

$ th file.lua

There are various ways to run Lua code and provide options, similar to those available for the perl and rubyprograms:

 $ th -h
Usage: th [options] [script.lua [arguments]]

Options:
  -l name            load library name
  -e statement       execute statement
  -h,--help          print this help
  -a,--async         preload async (libuv) and start async repl (BETA)
  -g,--globals       monitor global variables (print a warning on creation/access)
  -gg,--gglobals     monitor global variables (throw an error on creation/access)
  -x,--gfx           start gfx server and load gfx env
  -i,--interactive   enter the REPL after executing a script

TREPL is full of convenient features likes:

  • Tab-completion on nested namespaces
  • Tab-completion on disk files (when opening a string)
  • History (preserved between sessions)
  • Pretty print (table introspection and coloring)
  • Auto-print after eval (can be stopped with ;)
  • Each command is profiled, timing is reported
  • No need for ‘=’ to print
  • Easy help with: ? funcname
  • Self help: ?
  • Shell commands with: $ cmd (example: $ ls)

Next steps

In addition to this manual, there are various other resources that may help new users get started with torch, all summarized in this Cheatsheet

The cheatsheet provides links to tutorials, demos, package summaries and a lot of useful information.

If you have a question, please come join the Torch users mailing list

### torch 安装错误解决方案 在 Windows 10 下安装 PyTorch 及其扩展包(如 `torch_scatter` 和 `torch_sparse`)时可能会遇到各种问题,以下是针对常见错误的综合解决方案。 #### 1. 环境配置 确保 Python 版本与 PyTorch 的兼容性。PyTorch 支持的最低版本通常为 Python 3.7 或更高版本[^1]。建议使用 Anaconda 创建虚拟环境来管理依赖项: ```bash conda create -n pytorch_env python=3.9 conda activate pytorch_env ``` 如果未安装 CUDA,则需选择 CPU-only 版本;否则应根据显卡驱动程序支持的 CUDA 版本来匹配合适的 PyTorch 轮子文件。 #### 2. 使用官方推荐的方式安装 PyTorch 访问 [PyTorch官网](https://pytorch.org/get-started/locally/) 并根据操作系统、CUDA 配置自动生成适合的安装命令。例如,在不启用 GPU 加速的情况下可以运行如下指令完成基础框架部署: ```bash pip install torch torchvision torchaudio cpuonly ``` 对于需要 GPU 支持的情况,请替换相应参数以加载对应于本地硬件条件下的预编译二进制文件。 #### 3. 处理特定组件安装失败情况 当尝试通过 pip 单独获取某些附加模块像 `torch_scatter`, 如果直接调用标准方法报错提示无法构建 whl 文件时, 推荐采用 conda 渠道或者手动指定源地址来进行操作: - **Conda 方式**: ```bash conda install -c pyg torch-scatter ``` - **Pre-built Wheels 方法**: 访问 https://pytorch-geometric.com/whl/, 找到适合自己计算平台(CPU/CUDA X.X)以及当前已安裝之 PyTorch 版本相吻合的链接後执行下载并安装动作. #### 4. 自定义目标目录安装(备用选项) 若常规途径依旧碰壁可考虑更改默认存储位置规避权限冲突等问题发生 : ```bash pip install --upgrade pip setuptools wheel pip install --target=D:\custom_path\lib\site-packages torch-scatter ``` 注意调整实际路径至个人需求处[^3]. --- ### 示例代码片段展示如何验证安装成功与否 下面提供了一段简单的测试脚本用于确认所装设库能否正常运作无误. ```python import torch from torch_geometric.data import Data edge_index = torch.tensor([[0, 1], [1, 0]], dtype=torch.long) data = Data(edge_index=edge_index, num_nodes=2) print(data) ``` 如果一切顺利的话应该能够看到类似于这样的输出结果而不抛异常 : ``` Data(edge_index=[2, 2], num_nodes=2) ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值