Skip to content

Instantly share code, notes, and snippets.

@monk1337
Created June 7, 2018 15:32
Show Gist options
  • Save monk1337/1e96a8590932f417580325be2b00b87b to your computer and use it in GitHub Desktop.
Save monk1337/1e96a8590932f417580325be2b00b87b to your computer and use it in GitHub Desktop.
#best part with pytorch is you can treat pytorch object as python object
var=torch.rand(5,2,dtype=torch.double)
#we can loop over it
for i in var:
for k in i:
print(k)
# output:
# tensor(0.9142, dtype=torch.float64)
# tensor(0.9585, dtype=torch.float64)
# tensor(0.5760, dtype=torch.float64)
# tensor(0.2111, dtype=torch.float64)
# tensor(0.4621, dtype=torch.float64)
# tensor(0.4718, dtype=torch.float64)
# tensor(0.8199, dtype=torch.float64)
# tensor(1.00000e-02 *
# 8.4888, dtype=torch.float64)
# tensor(1.00000e-02 *
# 7.0895, dtype=torch.float64)
# tensor(0.9623, dtype=torch.float64)
#we can slice it ?
print(var[:2])
# output:
# tensor([[ 0.9142, 0.9585],
# [ 0.5760, 0.2111]], dtype=torch.float64)
#we can play with it
print(list(map(lambda x:x[0],var)))
# output:
# [tensor(0.9142, dtype=torch.float64), tensor(0.5760, dtype=torch.float64), tensor(0.4621, dtype=torch.float64), tensor(0.8199, dtype=torch.float64), tensor(1.00000e-02 *
# 7.0895, dtype=torch.float64)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment