11.md 27.7 KB
Newer Older
W
wizardforcel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# torch.Tensor

`torch.Tensor`是一种包含单一数据类型元素的多维矩阵。

Torch 定义了七种 CPU 张量类型和八种 GPU 张量类型:

| Data tyoe | CPU tensor | GPU tensor |
| --- | --- | --- |
| 32-bit floating point | `torch.FloatTensor` | `torch.cuda.FloatTensor` |
| 64-bit floating point | `torch.DoubleTensor` | `torch.cuda.DoubleTensor` |
| 16-bit floating point | N/A | `torch.cuda.HalfTensor` |
| 8-bit integer (unsigned) | `torch.ByteTensor` | `torch.cuda.ByteTensor` |
| 8-bit integer (signed) | `torch.CharTensor` | `torch.cuda.CharTensor` |
| 16-bit integer (signed) | `torch.ShortTensor` | `torch.cuda.ShortTensor` |
| 32-bit integer (signed) | `torch.IntTensor` | `torch.cuda.IntTensor` |
| 64-bit integer (signed) | `torch.LongTensor` | `torch.cuda.LongTensor` |

`torch.Tensor`是默认的 tensor 类型(`torch.FlaotTensor`)的简称。

张量可以从 Python 的`list`或序列构成:

W
wizardforcel 已提交
22
```py
W
wizardforcel 已提交
23 24 25 26 27 28 29 30
>>> torch.FloatTensor([[1, 2, 3], [4, 5, 6]])
1 2 3
4 5 6
[torch.FloatTensor of size 2x3] 
```

可以通过指定它的大小来构建一个空的张量:

W
wizardforcel 已提交
31
```py
W
wizardforcel 已提交
32 33 34 35 36 37 38 39
>>> torch.IntTensor(2, 4).zero_()
0 0 0 0
0 0 0 0
[torch.IntTensor of size 2x4] 
```

可以使用 Python 的索引和切片符号来访问和修改张量的内容:

W
wizardforcel 已提交
40
```py
W
wizardforcel 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54
>>> x = torch.FloatTensor([[1, 2, 3], [4, 5, 6]])
>>> print(x[1][2])
6.0
>>> x[0][1] = 8
>>> print(x)
 1 8 3
 4 5 6
[torch.FloatTensor of size 2x3] 
```

每一个张量 tensor 都有一个相应的`torch.Storage`保存其数据。张量类提供了一个多维的、横向视图的存储,并定义了数字操作。

> 注意: 改变张量的方法可以用一个下划线后缀来标示。比如,`torch.FloatTensor.abs_()`会在原地计算绝对值并返回修改的张量,而`tensor.FloatTensor.abs()`将会在新张量中计算结果。

W
wizardforcel 已提交
55
```py
W
wizardforcel 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
class torch.Tensor
class torch.Tensor(*sizes)
class torch.Tensor(size)
class torch.Tensor(sequence)
class torch.Tensor(ndarray)
class torch.Tensor(tensor)
class torch.Tensor(storage) 
```

从可选的大小或数据创建新的张量。 如果没有给出参数,则返回空的零维张量。如果提供了`numpy.ndarray`,`torch.Tensor``torch.Storage`,将会返回一个相同数据的新张量.如果提供了 python 序列,则从序列的副本创建一个新的张量。

#### abs()

参考`torch.abs()`,矩阵数组绝对值

#### abs_()

`abs()`的直接运算形式

#### acos()

参考`torch.acos()`

#### acos_()

`acos()`的直接运算形式,即直接执行并且返回修改后的张量

#### add(value)

参考`torch.add()`

#### add_(value)

`add()`的直接运算形式,即直接执行并且返回修改后的张量

#### addbmm(beta=1, mat, alpha=1, batch1, batch2)

参考`torch.addbmm()`

#### addbmm_(beta=1, mat, alpha=1, batch1, batch2)

`addbmm()`的直接运算形式,即直接执行并且返回修改后的张量

#### addcdiv(value=1, tensor1, tensor2)

参考`torch.addcdiv()`

#### addcdiv_(value=1, tensor1, tensor2)

`addcdiv()`的直接运算形式,即直接执行并且返回修改后的张量

#### addcmul(value=1, tensor1, tensor2)

参考`torch.addcmul()`

#### addcmul_(value=1, tensor1, tensor2)

`addcmul()`的直接运算形式,即直接执行并且返回修改后的张量

#### addmm(beta=1, mat, alpha=1, mat1, mat2)

参考`torch.addmm()`

#### addmm_(beta=1, mat, alpha=1, mat1, mat2)

`addmm()`的直接运算形式,即直接执行并且返回修改后的张量

#### addmv(beta=1, tensor, alpha=1, mat, vec)

参考`torch.addmv()`

#### addmv_(beta=1, tensor, alpha=1, mat, vec)

`addmv()`的直接运算形式,即直接执行并且返回修改后的张量

#### addr(beta=1, alpha=1, vec1, vec2)

参考`torch.addr()`

#### addr_(beta=1, alpha=1, vec1, vec2)

`addr()`的直接运算形式,即直接执行并且返回修改后的张量

#### apply_(callable)

将函数`callable`作用于 tensor 中每一个元素,并替换每个元素用`callable`函数返回的值。

> 注意: 该函数只能在 CPU tensor 中使用,并且不应该用在有较高性能要求的代码块。

#### asin()

参考`torch.asin()`

#### asin_()

`asin()`的直接运算形式,即直接执行并且返回修改后的张量

#### atan()

参考`torch.atan()`

#### atan2()

参考`torch.atan2()`

#### atan2_()

`atan2()`的直接运算形式,即直接执行并且返回修改后的张量

#### atan_()

`atan()`的直接运算形式,即直接执行并且返回修改后的张量

#### baddbmm(beta=1, alpha=1, batch1, batch2)

参考`torch.baddbmm()`

#### baddbmm_(beta=1, alpha=1, batch1, batch2)

`baddbmm()`的直接运算形式,即直接执行并且返回修改后的张量

#### bernoulli()

参考`torch.bernoulli()`

#### bernoulli_()

`bernoulli()`的直接运算形式,即直接执行并且返回修改后的张量

#### bmm(batch2)

参考`torch.bmm()`

#### byte()

将 tensor 改为 byte 类型

#### bmm(median=0, sigma=1, *, generator=None)

将 tensor 中元素用柯西分布得到的数值填充: P(x)=1/π * σ/(x−median)2+σ2

#### ceil()

参考`torch.ceil()`

#### ceil_()

`ceil()`的直接运算形式,即直接执行并且返回修改后的张量

#### char()

将 tensor 元素改为 char 类型

#### chunk(n_chunks, dim=0)

将 tensor 分割为 tensor 元组. 参考`torch.chunk()`

#### clamp(min, max)

参考`torch.clamp()`

#### clamp_(min, max)

`clamp()`的直接运算形式,即直接执行并且返回修改后的张量

#### clone()

返回与原 tensor 有相同大小和数据类型的 tensor

#### contiguous()

返回一个内存连续的有相同数据的 tensor,如果原 tensor 内存连续则返回原 tensor

#### copy_(src, async=False)

`src`中的元素复制到 tensor 中并返回这个 tensor。 如果 broadcast 是 True,则源张量必须可以使用该张量广播。否则两个 tensor 应该有相同数目的元素,可以是不同的数据类型或存储在不同的设备上。

参数:

*   src(Tensor) - 要复制的源张量
*   async(bool) - 如果为 True,并且此副本位于 CPU 和 GPU 之间,则副本可能会相对于主机异步发生。对于其他副本,此参数无效。
*   broadcast(bool) - 如果为 True,src 将广播到底层张量的形状。

#### cos()

参考`torch.cos()`

#### cos_()

`cos()`的直接运算形式,即直接执行并且返回修改后的张量

#### cosh()

参考`torch.cosh()`

#### cosh_()

`cosh()`的直接运算形式,即直接执行并且返回修改后的张量

#### cpu()

如果在 CPU 上没有该 tensor,则会返回一个 CPU 的副本

#### cross(other, dim=-1)

参考`torch.cross()`

#### cuda(device=None, async=False)

返回此对象在 CPU 内存中的一个副本 如果该对象已经在 CUDA 内存中,并且在正确的设备上,则不会执行任何副本,并返回原始对象。

参数:

*   device(int) :目标 GPU ID。默认为当前设备。
*   async(bool) :如果为 True 并且源处于固定内存中,则该副本将相对于主机是异步的。否则,该参数没有意义。

#### cumprod(dim)

参考`torch.cumprod()`

#### cumsum(dim)

参考`torch.cumsum()`

#### data_ptr() → int

返回 tensor 第一个元素的地址

#### diag(diagonal=0)

参考`torch.diag()`

#### dim() → int

返回 tensor 的维数

#### dist(other, p=2)

参考`torch.dist()`

#### div(value)

参考`torch.div()`

#### div_(value)

`div()`的直接运算形式,即直接执行并且返回修改后的张量

#### dot(tensor2) → float

参考`torch.dot()`

#### double()

将该 tensor 投射为 double 类型

#### eig(eigenvectors=False) -> (Tensor, Tensor)

参考`torch.eig()`

#### element_size() → int

返回单个元素的字节大小。 例:

W
wizardforcel 已提交
320
```py
W
wizardforcel 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
>>> torch.FloatTensor().element_size()
4
>>> torch.ByteTensor().element_size()
1 
```

#### eq(other)

参考`torch.eq()`

#### eq_(other)

`eq()`的直接运算形式,即直接执行并且返回修改后的张量

#### equal(other) → bool

参考`torch.equal()`

#### exp()

参考`torch.exp()`

#### exp_()

`exp()`的直接运算形式,即直接执行并且返回修改后的张量

#### expand(*sizes)

返回 tensor 的一个新视图,单个维度扩大为更大的尺寸。 tensor 也可以扩大为更高维,新增加的维度将附在前面。 扩大 tensor 不需要分配新内存,只是仅仅新建一个 tensor 的视图,其中通过将`stride`设为 0,一维将会扩展位更高维。任何一个一维的在不分配新内存情况下可扩展为任意的数值。

参数:

*   sizes(torch.Size or int...)-需要扩展的大小

例:

W
wizardforcel 已提交
357
```py
W
wizardforcel 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
>>> x = torch.Tensor([[1], [2], [3]])
>>> x.size()
torch.Size([3, 1])
>>> x.expand(3, 4)
 1 1
 1 1
 2 2 2 2
 3 3 3 3
 [torch.FloatTensor of size 3x4] 
```

#### expand_as(tensor)

将 tensor 扩展为参数 tensor 的大小。 该操作等效与:

W
wizardforcel 已提交
373
```py
W
wizardforcel 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
self.expand(tensor.size()) 
```

#### exponential_(lambd=1, *, generator=None) $to$ Tensor

将该 tensor 用指数分布得到的元素填充: $$ P(x)= \lambda e^{- \lambda x} $$

#### fill_(value)

将该 tensor 用指定的数值填充

#### float()

将 tensor 投射为 float 类型

#### floor()

参考`torch.floor()`

#### floor_()

`floor()`的直接运算形式,即直接执行并且返回修改后的张量

#### fmod(divisor)

参考`torch.fmod()`

#### fmod_(divisor)

`fmod()`的直接运算形式,即直接执行并且返回修改后的张量

#### frac()

参考`torch.frac()`

#### frac_()

`frac()`的直接运算形式,即直接执行并且返回修改后的张量

#### gather(dim, index)

参考`torch.gather()`

#### ge(other)

参考`torch.ge()`

#### ge_(other)

`ge()`的直接运算形式,即直接执行并且返回修改后的张量

#### gels(A)

参考`torch.gels()`

#### geometric_(p, *, generator=None)

将该 tensor 用几何分布得到的元素填充: $$ P(X=k)= (1-p)^{k-1}p $$

#### geqrf() -> (Tensor, Tensor)

参考`torch.geqrf()`

#### ger(vec2)

参考`torch.ger()`

#### gesv(A), Tensor

参考`torch.gesv()`

#### gt(other)

参考`torch.gt()`

#### gt_(other)

`gt()`的直接运算形式,即直接执行并且返回修改后的张量

#### half()

将 tensor 投射为半精度浮点类型

#### histc(bins=100, min=0, max=0)

参考`torch.histc()`

#### index(m)

用一个二进制的掩码或沿着一个给定的维度从 tensor 中选取元素。`tensor.index(m)``tensor[m]`完全相同。

参数:

*   m(int or Byte Tensor or slice)-用来选取元素的维度或掩码

    #### index*add*(dim, index, tensor)

    按参数 index 中的索引数确定的顺序,将参数 tensor 中的元素加到原来的 tensor 中。参数 tensor 的尺寸必须严格地与原 tensor 匹配,否则会发生错误。

参数:

*   dim(int)-索引 index 所指向的维度
*   index(LongTensor)-需要从 tensor 中选取的指数
*   tensor(Tensor)-含有相加元素的 tensor

例:

W
wizardforcel 已提交
481
```py
W
wizardforcel 已提交
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
>>> x = torch.Tensor([[1, 1, 1], [1, 1, 1], [1, 1, 1]])
>>> t = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> index = torch.LongTensor([0, 2, 1])
>>> x.index_add_(0, index, t)
>>> x
  2 3 4
  8 9 10
  5 6 7
[torch.FloatTensor of size 3x3] 
```

#### index*copy*(dim, index, tensor)

按参数 index 中的索引数确定的顺序,将参数 tensor 中的元素复制到原来的 tensor 中。参数 tensor 的尺寸必须严格地与原 tensor 匹配,否则会发生错误。

参数:

*   dim (int)-索引 index 所指向的维度
*   index (LongTensor)-需要从 tensor 中选取的指数
*   tensor (Tensor)-含有被复制元素的 tensor

例:

W
wizardforcel 已提交
505
```py
W
wizardforcel 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
>>> x = torch.Tensor(3 3)
>>> t = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> index = torch.LongTensor([0, 2, 1])
>>> x.index_copy_(0, index, t)
>>> x
  1 2 3
  7 8 9
  4 5 6
[torch.FloatTensor of size 3x3] 
```

#### index*fill*(dim, index, val)

按参数 index 中的索引数确定的顺序,将原 tensor 用参数`val`值填充。

参数:

*   dim (int)-索引 index 所指向的维度
*   index (LongTensor)-索引
*   val (Tensor)-填充的值

例:

W
wizardforcel 已提交
529
```py
W
wizardforcel 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
>>> x = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> index = torch.LongTensor([0, 2])
>>> x.index_fill_(0, index, -1)
>>> x
  -1 2 -1
  -1 5 -1
  -1 8 -1
[torch.FloatTensor of size 3x3] 
```

#### index_select(dim, index)

参考`torch.index_select()`

#### int()

将该 tensor 投射为 int 类型

#### inverse()

参考`torch.inverse()`

#### is_contiguous() → bool

如果该 tensor 在内存中是连续的则返回 True。

#### is_pinned()

如果该 tensor 在固定内内存中则返回 True

#### is_set_to(tensor) → bool

如果此对象引用与 Torch C API 相同的`THTensor`对象作为给定的张量,则返回 True。

#### kthvalue(k, dim=None) -> (Tensor, LongTensor)

参考`torch.kthvalue()`

#### le(other)

参考`torch.le()`

#### le_(other)

`le()`的直接运算形式,即直接执行并且返回修改后的张量

#### lerp(start, end, weight)

参考`torch.lerp()`

#### lerp*(_start, end, weight*)

`lerp()`的直接运算形式,即直接执行并且返回修改后的张量

#### log()

参考`torch.log()`

#### loglp()

参考`torch.loglp()`

#### loglp_()

`loglp()`的直接运算形式,即直接执行并且返回修改后的张量

#### log_()→ Tensor

`log()`的直接运算形式,即直接执行并且返回修改后的张量

#### log*normal*(*mwan=1, std=2,* , gegnerator=None*)

将该 tensor 用均值为$\mu$,标准差为$\sigma$的对数正态分布得到的元素填充。要注意`mean``stdv`是基本正态分布的均值和标准差,不是返回的分布: $$ P(X)= \frac {1} {x \sigma \sqrt {2 \pi}}e^{- \frac {(lnx- \mu)^2} {2 \sigma^2}} $$

#### long()

将 tensor 投射为 long 类型

#### lt(*other*)

参考`torch.lt()`

#### lt*(_other*)

`lt()`的直接运算形式,即直接执行并且返回修改后的张量

#### map*(_tensor, callable*)

`callable`作用于本 tensor 和参数 tensor 中的每一个元素,并将结果存放在本 tensor 中。`callable`应该有下列标志:

W
wizardforcel 已提交
620
```py
W
wizardforcel 已提交
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
def callable(a, b) -> number 
```

#### masked*copy*(*mask, source*)

`mask`中值为 1 元素对应的`source`中位置的元素复制到本 tensor 中。`mask`应该有和本 tensor 相同数目的元素。`source`中元素的个数最少为`mask`中值为 1 的元素的个数。

参数:

*   mask (*ByteTensor*)-二进制掩码
*   source (*Tensor*)-复制的源 tensor

> 注意: `mask`作用于`self`自身的 tensor,而不是参数中的`source`。
> 
> #### masked*fill*(*mask, value*)
> 
> 在`mask`值为 1 的位置处用`value`填充。`mask`的元素个数需和本 tensor 相同,但尺寸可以不同。形状 mask 必须 与下面的张量的形状一起广播。

参数:

*   mask (ByteTensor)-二进制掩码
*   value (Tensor)-用来填充的值

#### masked*select(_mask*)

参考`torch.masked_select()`

#### max(*dim=None*) -> *float or(Tensor, Tensor)*

参考`torch.max()`

#### mean(*dim=None*) -> *float or(Tensor, Tensor)*

参考`torch.mean()`

#### median(*dim=-1, value=None, indices=None*) -> *(Tensor, LongTensor)*

参考`torch.median()`

#### min(*dim=None*) -> *float or(Tensor, Tensor)*

参考`torch.min()`

#### mm(*mat2*)

参考`torch.mm()`

#### mode(*dim=-1, value=None, indices=None*) -> *(Tensor, LongTensor)*

参考`torch.mode()`

#### mul(*value*)

参考`torch.mul()`

#### mul*(_value*)

`mul()`的直接运算形式,即直接执行并且返回修改后的张量

#### multinomial(*num_samples, replacement=False,* , generator=None*)

参考`torch.multinomial()`

#### mv(*vec*)

参考`torch.mv()`

#### narrow(*dimension, start, length*) → Te

返回这个张量的缩小版本的新张量。维度`dim`缩小范围是`start``start+length`。返回的张量和该张量共享相同的底层存储。

参数:

*   dimension (*int*)-需要缩小的维度
*   start (*int*)-起始维度
*   length (*int*)-长度

例:

W
wizardforcel 已提交
700
```py
W
wizardforcel 已提交
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
>>> x = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> x.narrow(0, 0, 2)
 1  2  3
 4  5  6
[torch.FloatTensor of size 2x3]
>>> x.narrow(1, 1, 2)
 2  3
 5  6
 8  9
[torch.FloatTensor of size 3x2] 
```

#### ndimension() → int

`dim()`的另一种表示。

#### ne(*other*)

参考`torch.ne()`

#### ne*(_other*)

`ne()`的直接运算形式,即直接执行并且返回修改后的张量

#### neg()

参考`torch.neg()`

#### neg_()

`neg()`的直接运算形式,即直接执行并且返回修改后的张量

#### nelement() → int

`numel()`的另一种表示

#### new(*args, **kwargs)

构建一个有相同数据类型的 tensor

#### nonezero() → LongTensor

参考`torch.nonezero()

#### norm(*p=2*) → float

参考`torch.norm()

#### normal*(_mean=0, std=1,* , gengerator=None*)

将 tensor 用均值为`mean`和标准差为`std`的正态分布填充。

#### numel() → int

参考`numel()`

#### numpy() → ndarray

将该 tensor 以 NumPy 的形式返回`ndarray`,两者共享相同的底层内存。原 tensor 改变后会相应的在`ndarray`有反映,反之亦然。

#### orgqr(*input2*)

参考`torch.orgqr()`

#### ormqr(*input2, input3, left=True, transpose=False*)

参考`torch.ormqr()`

#### permute(*dims*)

将 tensor 的维度换位。

参数:

*   _dims (_int..*)-换位顺序

例:

W
wizardforcel 已提交
779
```py
W
wizardforcel 已提交
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
>>> x = torch.randn(2, 3, 5)
>>> x.size()
torch.Size([2, 3, 5])
>>> x.permute(2, 0, 1).size()
torch.Size([5, 2, 3]) 
```

#### pin_memory()

将张量复制到固定内存(如果尚未固定)。

#### potrf(*upper=True*)

参考`torch.potrf()`

#### potri(*upper=True*)

参考`torch.potri()`

#### potrs(*input2, upper=True*)

参考`torch.potrs()`

#### pow(*exponent*)

参考`torch.pow()`

#### pow_()

`pow()`的直接运算形式,即直接执行并且返回修改后的张量

#### prod()) → float

参考`torch.prod()`

#### pstrf(*upper=True, tol=-1*) -> (*Tensor, IntTensor*)

参考`torch.pstrf()`

#### qr()-> (*Tensor, IntTensor*)

参考`torch.qr()`

#### random_(from=0, to=None, *, generator=None)

将 tensor 用从在[from, to-1]上的正态分布或离散正态分布取样值进行填充。如果未指定,则该值仅由该张量数据类型限定。

#### reciprocal()

参考`torch.reciprocal()`

#### reciprocal_()

`reciprocal()`的直接运算形式,即直接执行并且返回修改后的张量

#### remainder(*divisor*)

参考`torch.remainder()`

#### remainder*(_divisor*)

`remainder()`的直接运算形式,即直接执行并且返回修改后的张量

#### renorm(*p, dim, maxnorm*)

参考`torch.renorm()`

#### renorm*(_p, dim, maxnorm*)

`renorm()`的直接运算形式,即直接执行并且返回修改后的张量

#### repeat(*sizes)

沿指定维度重复此张量。 与`expand()`功能不同,此功能可复制张量中的数据。

参数:

*   *sizes (torch.Size ot int...)-沿着每个维重复这个张量的次数

例:

W
wizardforcel 已提交
861
```py
W
wizardforcel 已提交
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
>>> x = torch.Tensor([1, 2, 3])
>>> x.repeat(4, 2)
 1  2  3  1  2  3
 1  2  3  1  2  3
 1  2  3  1  2  3
 1  2  3  1  2  3
[torch.FloatTensor of size 4x6]
>>> x.repeat(4, 2, 1).size()
torch.Size([4, 2, 3]) 
```

#### resize_(*sizes)

将 tensor 的大小调整为指定的大小。如果元素个数比当前的内存大小大,就将底层存储大小调整为与新元素数目一致的大小。如果元素个数比当前内存小,则底层存储不会被改变。原来 tensor 中被保存下来的元素将保持不变,但新内存将不会被初始化。

参数:

*   sizes (torch.Size or int...)-需要调整的大小

例:

W
wizardforcel 已提交
883
```py
W
wizardforcel 已提交
884 885 886 887 888 889 890 891 892 893 894 895
>>> x = torch.Tensor([[1, 2], [3, 4], [5, 6]])
>>> x.resize_(2, 2)
>>> x
 1  2
 3  4
[torch.FloatTensor of size 2x2] 
```

#### resize*as*(tensor)

将当前张量调整为与指定张量相同的大小。这相当于:

W
wizardforcel 已提交
896
```py
W
wizardforcel 已提交
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
self.resize_(tensor.size()) 
```

#### round()

参考`torch.round()`

#### round_()

`round()`的直接运算形式,即直接执行并且返回修改后的张量

#### rsqrt()

参考`torch.rsqrt()`

#### rsqrt_()

`rsqrt()`的直接运算形式,即直接执行并且返回修改后的张量

#### scatter_(dim, index, src)

`src`中的所有值按照`index`确定的索引写入本 tensor 中。其中索引是根据给定的 dimension,dim 按照`gather()`描述的规则来确定。

请注意,对于 collect,index 的值必须在 0 和(self.size(dim)-1)之间,包括所有值,并且指定维中的一行中的所有值必须是唯一的。

参数:

*   input (Tensor)-源 tensor
*   dim (int)-索引的轴向
*   index (LongTensor)-散射元素的索引指数
*   src (Tensor or float)-散射的源元素

例子:

W
wizardforcel 已提交
931
```py
W
wizardforcel 已提交
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
>>> x = torch.rand(2, 5)
>>> x

 0.4319  0.6500  0.4080  0.8760  0.2355
 0.2609  0.4711  0.8486  0.8573  0.1029
[torch.FloatTensor of size 2x5]

>>> torch.zeros(3, 5).scatter_(0, torch.LongTensor([[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]]), x)

 0.4319  0.4711  0.8486  0.8760  0.2355
 0.0000  0.6500  0.0000  0.8573  0.0000
 0.2609  0.0000  0.4080  0.0000  0.1029
[torch.FloatTensor of size 3x5]

>>> z = torch.zeros(2, 4).scatter_(1, torch.LongTensor([[2], [3]]), 1.23)
>>> z

 0.0000  0.0000  1.2300  0.0000
 0.0000  0.0000  0.0000  1.2300
[torch.FloatTensor of size 2x4] 
```

#### select(dim, index) or number

按照 index 中选定的维度将 tensor 切片。如果 tensor 是一维的,则返回一个数字。否则,返回给定维度已经被移除的 tensor。

参数:

*   dim (int)-切片的维度
*   index (int)-用来选取的索引

> 注意: `select()`等效于切片。例如:`tensor.select(0, index)`等效于`tensor[index]`,`tensor.select(2, index)`等效于`tensor[:,:,index]`

#### set(source=None, storage_offset=0, size=None, stride=None)

设置底层内存,大小和步长。如果 source 是张量,则该张量将共享相同的存储空间,并且具有与给定张量相同的大小和步长。另一个则反映在一个张量内的元素变化。

参数:

*   source (Tensor or Storage)-用到的 tensor 或内存
*   storage_offset (int)-内存的偏移量
*   size (torch.Size)-需要的大小,默认为源 tensor 的大小。
*   stride(tuple)-需要的步长,默认为 C 连续的步长。

#### share*memory*()

将底层存储器移动到共享内存。 如果底层存储已经在共享内存和 CUDA 张量中,不进行任何操作。共享内存中的 Tensors 无法调整大小。

#### short()

将 tensor 投射为 short 类型。

#### sigmoid()

参考`torch.sigmoid()`

#### sigmoid_()

`sidmoid()`的直接运算形式,即直接执行并且返回修改后的张量

#### sign()

参考`torch.sign()`

#### sign_()

`sign()`的直接运算形式,即直接执行并且返回修改后的张量

#### sin()

参考`torch.sin()`

#### sin_()

`sin()`的直接运算形式,即直接执行并且返回修改后的张量

#### sinh()

参考`torch.sinh()`

#### sinh_()

`sinh()`的直接运算形式,即直接执行并且返回修改后的张量

#### size() → torch.Size

返回 tensor 的大小。返回的值是`tuple`的子类。

例:

W
wizardforcel 已提交
1022
```py
W
wizardforcel 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
>>> torch.Tensor(3, 4, 5).size()
torch.Size([3, 4, 5]) 
```

#### sort(dim=None, descending=False) -> (Tensor, LongTensor)

参考`torhc.sort()`

#### split(split_size, dim=0)

将 tensor 分割成 tensor 数组。 参考`torhc.split()`

#### sqrt()

参考`torch.sqrt()`

#### sqrt_()

`sqrt()`的直接运算形式,即直接执行并且返回修改后的张量

#### squeeze(dim=None)

参考`torch.squeeze()`

#### squeeze_(dim=None)

`squeeze()`的直接运算形式,即直接执行并且返回修改后的张量

#### std() → float

参考`torch.std()`

#### storage() → torch.Storage

返回底层内存。

#### storage_offset() → int

以储存元素的个数的形式返回 tensor 在地城内存中的偏移量。 例:

W
wizardforcel 已提交
1063
```py
W
wizardforcel 已提交
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
>>> x = torch.Tensor([1, 2, 3, 4, 5])
>>> x.storage_offset()
0
>>> x[3:].storage_offset()
3 
```

#### stride()

返回 tesnor 的步长。

#### sub(value, other)

从该张量中排除一个标量或张量。如果`value``other`都是给定的,则在使用之前`other`的每一个元素都会被`value`缩放。 当`other`是一个张量,`other`的形状必须可以与下面的张量的形状一起[广播](http://pytorch.org/docs/master/notes/broadcasting.html#broadcasting-semantics)

#### sub_(x)

`sub()`的直接运算形式,即直接执行并且返回修改后的张量

#### sum(dim=None)

参考`torch.sum()`

#### svd(some=True) -> (Tensor, Tensor, Tensor)

参考`torch.svd()`

#### symeig(eigenvectors=False, upper=True) -> (Tensor, Tensor)

参考`torch.symeig()`

#### t()

参考`torch.t()`

#### t()

`t()`的直接运算形式,即直接执行并且返回修改后的张量

#### tan()

参考`torch.tan()`

#### tan_()

`tan()`的直接运算形式,即直接执行并且返回修改后的张量

#### tanh()

参考`torch.tanh()`

#### tanh_()

`tanh()`的直接运算形式,即直接执行并且返回修改后的张量

#### tolist()

返回一个 tensor 的嵌套列表表示。

#### topk(k, dim=None, largest=True, sorted=True) -> (Tensor, LongTensor)

参考`torch.topk()`

#### trace() → float

参考`torch.trace()`

#### transpose(dim0, dim1)

参考`torch.transpose()`

#### transpose(dim0, dim1)

`transpose()`的直接运算形式,即直接执行并且返回修改后的张量

#### tril(k=0)

参考`torch.tril()`

#### tril_(k=0)

`tril()`的直接运算形式,即直接执行并且返回修改后的张量

#### triu(k=0)

参考`torch.triu()`

#### triu(k=0)

`triu()`的直接运算形式,即直接执行并且返回修改后的张量

#### trtrs(A, upper=True, transpose=False, unitriangular=False) -> (Tensor, Tensor)

参考`torch.trtrs()`

#### trunc()

参考`torch.trunc()`

#### trunc()

`trunc()`的直接运算形式,即直接执行并且返回修改后的张量

#### type(new_type=None, async=False)

如果未提供 new_type,则返回类型,否则将此对象转换为指定的类型。 如果已经是正确的类型,则不会执行且返回原对象。

参数:

*   new_type (type 或 string)-需要的类型
*   async (bool)-如果为 True,并且源处于固定内存中,目标位于 GPU 上,反之亦然,则相对于主机异步执行该副本。否则,该参数不发挥作用。

#### type_as(tesnor)

将此张量转换为给定类型的张量。 如果张量已经是正确的类型,则不会执行操作。等效于:

W
wizardforcel 已提交
1180
```py
W
wizardforcel 已提交
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
self.type(tensor.type()) 
```

参数:

*   tensor (Tensor):有所需要类型的 tensor

#### unfold(dim, size, step)

返回一个张量,其中包含尺寸 size 中所有尺寸的维度。 如果*sizedim*是 dim 维度的原始大小,则返回张量中的维度是(sizedim-size)/step+1

维度大小的附加维度将附加在返回的张量中。

参数:

*   dim (int)- 展开的维度
*   size (int)- 展开的每个切片的大小
*   step (int)-相邻切片之间的步长

例子:

W
wizardforcel 已提交
1202
```py
W
wizardforcel 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
>>> x = torch.arange(1, 8)
>>> x

 1
 2
 3
 4
 5
 6
 7
[torch.FloatTensor of size 7]

>>> x.unfold(0, 2, 1)

 1  2
 2  3
 3  4
 4  5
 5  6
 6  7
[torch.FloatTensor of size 6x2]

>>> x.unfold(0, 2, 2)

 1  2
 3  4
 5  6
[torch.FloatTensor of size 3x2] 
```

#### uniform_(from=0, to=1)

用均匀分布采样的数字填充该张量:

#### unsqueeze(dim)

参考`torch.unsqueeze()`

#### unsqueeze_(dim)

`unsqueeze()`的直接运算形式,即直接执行并且返回修改后的张量

#### var()

参考`torch.var()`

#### view(*args)

返回具有相同数据但大小不同的新张量。 返回的张量必须有与原张量相同的数据和相同数量的元素,但可以有不同的大小。一个张量必须是连续`contiguous()`的才能被查看。

例子:

W
wizardforcel 已提交
1255
```py
W
wizardforcel 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
>>> x = torch.randn(4, 4)
>>> x.size()
torch.Size([4, 4])
>>> y = x.view(16)
>>> y.size()
torch.Size([16])
>>> z = x.view(-1, 8)  # the size -1 is inferred from other dimensions
>>> z.size()
torch.Size([2, 8]) 
```

#### view_as(tensor)

返回被视作与给定的 tensor 相同大小的原 tensor。 等效于:

W
wizardforcel 已提交
1271
```py
W
wizardforcel 已提交
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
self.view(tensor.size()) 
```

#### zero_()

用 0 填充这个张量。

### 译者署名

| 用户名 | 头像 | 职能 | 签名 |
| --- | --- | --- | --- |
| [Song](https://ptorch.com) | ![](img/2018033000352689884.jpeg) | 翻译 | 人生总要追求点什么 |