LoginSignup
0
0

More than 3 years have passed since last update.

pytorch tensor_tutorial の適当和訳

Posted at
%matplotlib inline

WHAT IS PYTORCH?

It’s a Python-based scientific computing package targeted at two sets of audiences:

  • A replacement for NumPy to use the power of GPUs
  • a deep learning research platform that provides maximum flexibility and speed

Tensors

Tensors are similar to Numpy's ndarrays, with the addtion being that Tensors can also be used on GPU to accelerate computing.

PyTorchとは?

Pytorchは,以下の目的を持ったユーザに向けて開発されたPythonベースの
科学計算パッケージである.

  • GPUを使うことができるNumpyの代替
  • スピードと柔軟性を兼ね備えたディープラーニングプラットフォーム

Getting Started

テンソル(tensors)

テンソルはGPUで計算できることを除き,Numpyのndarraysに似ている.

from __future__ import print_function
import torch

Note

An uninitialized matrix is declared, but does not contain definite known values before it is used. When an uninitialized matrix is created, whatever values were in the allocated memory at the time will appear as the initial values.

Construct a 5x3 matrix, uninitialized:
初期化されてない5x3の行列を生成

x = torch.empty(5, 3)
print(x)

Construct a randomly initialized matrix:

ランダムに初期化された行列を生成

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

Construct a matrix filled zeros and of dtype long:

ゼロで初期化されたlong型の行列を生成

x = torch.zeros(5, 3, dtype=torch.long)
print(x)

Construct a tensor directly from data:

データから行列を生成

x = torch.tensor([5.5, 3])
print(x)

or create a tensor based on an existing tensor. These methods
will reuse properties of the input tensor, e.g. dtype, unless
new values are provided by user

もしくは,既存のテンソルからテンソルを生成.これらのメソッドは,ユーザが指定しない限り,入力されたテンソルのプロパティを再利用する. 例:型

x = x.new_ones(5, 3, dtype=torch.double)      # new_* methods take in sizes
print(x)

x = torch.randn_like(x, dtype=torch.float)    # override dtype!
print(x)                                      # result has the same size

Get its size:

テンソルの大きさを取得

print(x.size())

Note

``torch.Size`` is in fact a tuple, so it supports all tuple operations.

Operations

There are multiple syntaxes for operations. In the following
example, we will take a look at the addition operation.

Addition: syntax 1

操作

操作には様々な分布が存在する.以下に足し算の文法を示す.

足し算1

y = torch.rand(5, 3)
print(x + y)
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

<ipython-input-1-3846198563ac> in <module>
----> 1 y = torch.rand(5, 3)
      2 print(x + y)


NameError: name 'torch' is not defined

Addition: syntax 2

足し算2

print(torch.add(x, y))

Addition: providing an output tensor as argument

足し算: 出力テンソルを引数として渡す

result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)

Addition: in-place

足し算: その場で

# adds x to y
y.add_(x)
print(y)

Note

Any operation that mutates a tensor in-place is post-fixed with an ``_``. For example: ``x.copy_(y)``, ``x.t_()``, will change ``x``.

You can use standard NumPy-like indexing with all bells and whistles!

ほぼすべてのNumpy インデクシングを使えます!!

print(x[:, 1])

Resizing: If you want to resize/reshape tensor, you can use torch.view:

リサイズ:テンソルの形を変えたいときはtorch.viewを使えます:

x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)  # the size -1 is inferred from other dimensions
print(x.size(), y.size(), z.size())

If you have a one element tensor, use .item() to get the value as a
Python number

要素が一つのテンソルから,Pythonの数値を取り出すとき.item()を使いましょう

x = torch.randn(1)
print(x)
print(x.item())

Read later:

100+ Tensor operations, including transposing, indexing, slicing,
mathematical operations, linear algebra, random numbers, etc.,
are described
here <https://pytorch.org/docs/torch>_.

あとで読んで

詳しいことは
https://pytorch.org/docs/torch

NumPy Bridge

Converting a Torch Tensor to a NumPy array and vice versa is a breeze.

The Torch Tensor and NumPy array will share their underlying memory
locations (if the Torch Tensor is on CPU), and changing one will change
the other.

Converting a Torch Tensor to a NumPy Array
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Numpy Bridge

torchテンソルをNumpy arrayに変換するのは簡単

TorchテンソルとNumpy arrayはメモリーを共有する(ポインタ的な)
もし,テンソルを変更したら,Numpy arrayも変更される.逆も然り

a = torch.ones(5)
print(a)
b = a.numpy()
print(b)

See how the numpy array changed in value.

torchテンソルがどのように変更されるか見てみよう.

a.add_(1)
print(a)
print(b)

Converting NumPy Array to Torch Tensor
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
See how changing the np array changed the Torch Tensor automatically

Numpy arrayをTorchテンソルに変換

import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)

All the Tensors on the CPU except a CharTensor support converting to
NumPy and back.

CUDA Tensors

Tensors can be moved onto any device using the .to method.

CharTensorを除くすべてのテンソルがNumpy arrayへの変換をサポートする.

CUDA Tensors

テンソルは.toを使って,好きなデバイスに移動させることができる.

# let us run this cell only if CUDA is available
# We will use ``torch.device`` objects to move tensors in and out of GPU
if torch.cuda.is_available():
    device = torch.device("cuda")          # a CUDA device object
    y = torch.ones_like(x, device=device)  # directly create a tensor on GPU
    x = x.to(device)                       # or just use strings ``.to("cuda")``
    z = x + y
    print(z)
    print(z.to("cpu", torch.double))       # ``.to`` can also change dtype together!
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0