PyTorch Data Manipulation Practice
[
]
Just some small examples of
in pytorch.
I must’ve seen a dozen examples of these in Jupyter notebooks, but can’t seem to remember them unless I write it out like this myself.
Import
import torch
View
Instantitate a 3x4 tensor.
x = torch.tensor([[1,2,3,4],
[5,6,7,8],
[9,10,11,12]], dtype=torch.float32)
print(x)
# tensor([[ 1., 2., 3., 4.],
# [ 5., 6., 7., 8.],
# [ 9., 10., 11., 12.]])
View it as one dimension.
x = x.view(1,12)
print(x)
# tensor([[ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.]])
Move it back to three dimensions, but let pytorch figure out the rest with -1.
x = x.view(3,-1)
print(X)
# tensor([[ 1., 2., 3., 4.],
# [ 5., 6., 7., 8.],
# [ 9., 10., 11., 12.]])
View it as 2 dimensions.
x = x.view(2, 6)
print(x)
# tensor([[ 1., 2., 3., 4., 5., 6.],
# [ 7., 8., 9., 10., 11., 12.]])
Reshape
We can also use reshape, although the implementation is slightly different. See here for more details.
# Reshape it to 4 dimensions.
x = x.reshape(4, -1)
print(x)
# Reshape it to original 3 dimensions
x = x.reshape(3, -1)
print(x)
Output:
tensor([[ 1., 2., 3.],
[ 4., 5., 6.],
[ 7., 8., 9.],
[10., 11., 12.]])
tensor([[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.]])
Squeeze and Unsqueeze
Add an outer layer to the tensor.
y = x.view(1,2,-1)
print(y)
# Output:
# tensor([[[ 1., 2., 3., 4., 5., 6.],
# [ 7., 8., 9., 10., 11., 12.]]])
Squeeze removes these extra tensor layers.
y = y.squeeze()
print(y)
# tensor([[ 1., 2., 3., 4., 5., 6.],
# [ 7., 8., 9., 10., 11., 12.]])
Unsqueeze does the opposite.
y = y.unsqueeze(0)
print(y)
# tensor([[[ 1., 2., 3., 4., 5., 6.],
# [ 7., 8., 9., 10., 11., 12.]]])
The argument to unsqueeze is the axis.
y = y.unsqueeze(1)
print(y)
# tensor([[[ 1., 2., 3., 4., 5., 6.]],
# [[ 7., 8., 9., 10., 11., 12.]]])
Flatten
Flatten creates a one-dimensional tensor, sort of like view(1, -1).squeeze()
.
print(y.flatten())
# tensor([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.])
Compare that with this:
print(y.view(1,-1).squeeze())
# tensor([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.])
Rand
rand
deals with random numbers.
# Create a tensor of size 1 with random number between 0 and 1.
z = torch.rand(1)
print(z)
# tensor([0.7780])
# Or pull out the value like this.
print(z[0])
# tensor(0.7780)
Archive
chinese
tang-dynasty-poetry
李白
python
王维
rl
pytorch
numpy
emacs
杜牧
spinningup
networking
deep-learning
贺知章
白居易
王昌龄
杜甫
李商隐
tips
reinforcement-learning
macports
jekyll
骆宾王
贾岛
孟浩然
xcode
time-series
terminal
regression
rails
productivity
pandas
math
macosx
lesson-plan
helicopters
flying
fastai
conceptual-learning
command-line
bro
黄巢
韦应物
陈子昂
王翰
王之涣
柳宗元
杜秋娘
李绅
张继
孟郊
刘禹锡
元稹
youtube
visdom
system
sungho
stylelint
stripe
softmax
siri
sgd
scipy
scikit-learn
scikit
safari
research
qtran
qoe
qmix
pyhton
poetry
pedagogy
papers
paper-review
optimization
openssl
openmpi
nyc
node
neural-net
multiprocessing
mpi
morl
ml
mdp
marl
mandarin
macos
machine-learning
latex
language-learning
khan-academy
jupyter-notebooks
ios-programming
intuition
homebrew
hacking
google-cloud
github
flashcards
faker
docker
dme
deepmind
dec-pomdp
data-wrangling
craftsman
congestion-control
coding
books
book-review
atari
anki
analogy
3brown1blue
2fa