forked from pytorch/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtensor_tutorial.py
141 lines (110 loc) · 3.36 KB
/
tensor_tutorial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
"""
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
Getting Started
---------------
Tensors
^^^^^^^
Tensors are similar to numpy’s ndarrays, with the addition being that
Tensors can also be used on a GPU to accelerate computing.
"""
from __future__ import print_function
import torch
###############################################################
# Construct a 5x3 matrix, uninitialized:
x = torch.Tensor(5, 3)
print(x)
###############################################################
# Construct a randomly initialized matrix
x = torch.rand(5, 3)
print(x)
###############################################################
# Get its size
print(x.size())
###############################################################
# .. note::
# ``torch.Size`` is in fact a tuple, so it supports the same operations
# Operations
# ^^^^^^^^^^
# There are multiple syntaxes for operations. Let's see addition as an example
#
# Addition: syntax 1
y = torch.rand(5, 3)
print(x + y)
###############################################################
# Addition: syntax 2
print(torch.add(x, y))
###############################################################
# Addition: giving an output tensor
result = torch.Tensor(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!
print(x[:, 1])
###############################################################
# **Read later:**
#
#
# 100+ Tensor operations, including transposing, indexing, slicing,
# mathematical operations, linear algebra, random numbers, etc are described
# `here <http://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, and changing one will change the other.
#
# Converting torch Tensor to numpy Array
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
a = torch.ones(5)
print(a)
###############################################################
#
b = a.numpy()
print(b)
###############################################################
# See how the numpy array changed in value.
a.add_(1)
print(a)
print(b)
###############################################################
# Converting numpy Array to torch Tensor
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# See how changing the np array changed the torch Tensor automatically
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 GPU using the ``.cuda`` function.
# let us run this cell only if CUDA is available
if torch.cuda.is_available():
x = x.cuda()
y = y.cuda()
x + y