Import from numpy
import torch
import numpy as np
a = np. array( [ 2 , 3.3 ] )
a
array([2. , 3.3])
a = np. ones( [ 2 , 3 ] )
torch. from_numpy( a)
tensor([[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
Import from List
torch. tensor( [ 2 . , 3.2 ] )
tensor([2.0000, 3.2000])
torch. FloatTensor( [ 2 . , 3.2 ] )
tensor([2.0000, 3.2000])
torch. tensor( [ [ 2 . , 3.2 ] , [ 1 . , 22.3300 ] ] )
tensor([[ 2.0000, 3.2000],
[ 1.0000, 22.3300]])
torch. FloatTensor( [ 1 , 2 ] )
tensor([1., 2.])
torch. tensor( [ 1 , 2 ] )
tensor([1, 2])
uninitialized
a = torch. empty( 1 )
print ( a)
print ( a. dim( ) )
print ( a. shape[ 0 ] )
print ( a. shape[ - 1 ] )
tensor([0.])
1
1
a= torch. empty( 1 , 2 )
print ( a)
print ( a. dim( ) )
print ( a. shape[ 0 ] )
print ( a. shape[ 1 ] )
tensor([[1.4013e-45, 0.0000e+00]])
2
1
2
a= torch. empty( 2 , 2 )
print ( a)
print ( a. dim( ) )
print ( a. shape[ 0 ] )
print ( a. shape[ 1 ] )
tensor([[-3.7422e+25, 4.5915e-41],
[ 0.0000e+00, 0.0000e+00]])
2
2
2
torch. Tensor( 2 , 3 )
tensor([[4.2498e+21, 1.3039e-11, 1.4013e-45],
[0.0000e+00, 1.4013e-45, 0.0000e+00]])
torch. IntTensor( 2 , 3 )
tensor([[1684169014, 1664573795, 1630943078],
[1664496741, 1647732274, 1681155427]], dtype=torch.int32)
torch. FloatTensor( 2 , 3 )
tensor([[0., 0., 0.],
[0., 0., 0.]])
set default type
torch. tensor( [ 1.2 , 3 ] ) . type ( )
'torch.FloatTensor'
torch. set_default_tensor_type( torch. DoubleTensor)
torch. tensor( [ 1.2 , 3 ] ) . type ( )
'torch.DoubleTensor'
rand/rand_like, randint
torch. rand( 3 , 3 )
tensor([[0.3042, 0.4359, 0.3267],
[0.1789, 0.7789, 0.4212],
[0.7396, 0.2619, 0.1755]])
a= torch. rand( 3 , 3 )
a
tensor([[0.5903, 0.7928, 0.5014],
[0.6092, 0.0908, 0.6695],
[0.2991, 0.9321, 0.1164]])
torch. rand_like( a)
tensor([[0.3394, 0.7368, 0.7235],
[0.5825, 0.9184, 0.0246],
[0.3461, 0.0210, 0.1008]])
a = torch. randint( 1 , 10 , [ 2 , 3 ] )
print ( a)
print ( a. dim( ) )
print ( a. shape[ 0 ] )
print ( a. shape[ 1 ] )
print ( "*" * 7 )
print ( a. shape[ - 1 ] )
print ( a. shape[ - 2 ] )
tensor([[2, 5, 2],
[6, 8, 9]])
2
2
3
*******
3
2
randn N(0, 1) N(u, std)
torch. randn( 3 , 3 )
tensor([[ 1.8307, -0.5371, 1.4034],
[-1.7542, -1.1900, 0.6335],
[-0.4314, 0.0911, 0.2340]])
std = torch. arange( 1 , 0 , - 0.1 )
print ( std)
tensor([1.0000, 0.9000, 0.8000, 0.7000, 0.6000, 0.5000, 0.4000, 0.3000, 0.2000,
0.1000])
mean= torch. full( [ 10 ] , 0.0 )
print ( mean)
tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
torch. normal( mean= torch. full( [ 10 ] , 0 ) , std= torch. arange( 1 , 0 , - 0.1 ) )
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-72-3d01f0f32e3a> in <module>
----> 1 torch.normal(mean=torch.full([10],0), std=torch.arange(1, 0, -0.1))
RuntimeError: Providing a bool or integral fill value without setting the optional `dtype` or `out` arguments is currently unsupported. In PyTorch 1.7, when `dtype` and `out` are not set a bool fill value will return a tensor of torch.bool dtype, and an integral fill value will return a tensor of torch.long dtype.
torch. normal( mean= torch. full( [ 10 ] , 0.0 ) , std= torch. arange( 1 , 0 , - 0.1 ) )
tensor([-0.1347, 1.0939, -0.3618, 1.3744, 0.8857, 0.4688, -0.5456, 0.0343,
0.0085, -0.2109])
full
torch. full( [ 2 , 3 ] , 7.0 )
tensor([[7., 7., 7.],
[7., 7., 7.]])
torch. full( [ ] , 7.0 )
tensor(7.)
torch. full( [ 1 ] , 7.0 )
tensor([7.])
arange/range 推荐使用arange
torch. arange( 0 , 10 )
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
torch. arange( 1 , 10 , 2 )
tensor([1, 3, 5, 7, 9])
torch. range ( 0 , 10 )
C:\Anaconda3\envs\pytorchTest\lib\site-packages\ipykernel_launcher.py:1: UserWarning: torch.range is deprecated in favor of torch.arange and will be removed in 0.5. Note that arange generates values in [start; end), not [start; end].
"""Entry point for launching an IPython kernel.
tensor([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
linspace/logspace
torch. linspace( 0 , 10 , steps = 4 )
tensor([ 0.0000, 3.3333, 6.6667, 10.0000])
torch. linspace( 0 , 10 , steps= 10 )
tensor([ 0.0000, 1.1111, 2.2222, 3.3333, 4.4444, 5.5556, 6.6667, 7.7778,
8.8889, 10.0000])
torch. linspace( 0 , 10 , steps= 11 )
tensor([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
torch. logspace( 0 , - 1 , steps= 10 )
tensor([1.0000, 0.7743, 0.5995, 0.4642, 0.3594, 0.2783, 0.2154, 0.1668, 0.1292,
0.1000])
torch. logspace( - 1 , 0 , steps= 10 )
tensor([0.1000, 0.1292, 0.1668, 0.2154, 0.2783, 0.3594, 0.4642, 0.5995, 0.7743,
1.0000])
10 ** ( - 1 )
0.1
torch. logspace( 0 , 1 , steps= 10 )
tensor([ 1.0000, 1.2915, 1.6681, 2.1544, 2.7826, 3.5938, 4.6416, 5.9948,
7.7426, 10.0000])
Ones/zeros/eye
torch. ones( 3 , 3 )
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
torch. zeros( 3 , 3 )
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
a = torch. eye( 3 , 4 )
a
tensor([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.]])
torch. zeros( 3 )
tensor([0., 0., 0.])
torch. eye( 3 )
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
torch. ones_like( a)
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
randperm
num = torch. randperm( 10 )
num
tensor([4, 1, 5, 7, 3, 9, 8, 6, 0, 2])
a = torch. rand( 2 , 3 )
a
tensor([[0.4931, 0.6169, 0.2774],
[0.8489, 0.4528, 0.6767]])
b = torch. rand( 2 , 2 )
b
tensor([[0.5017, 0.9038],
[0.5845, 0.4016]])
idx = torch. randperm( 2 )
print ( idx)
a[ idx]
tensor([0, 1])
tensor([[0.4931, 0.6169, 0.2774],
[0.8489, 0.4528, 0.6767]])
idx = torch. randperm( 2 )
print ( idx)
b[ idx]
tensor([1, 0])
tensor([[0.5845, 0.4016],
[0.5017, 0.9038]])
a, b
(tensor([[0.4931, 0.6169, 0.2774],
[0.8489, 0.4528, 0.6767]]),
tensor([[0.5017, 0.9038],
[0.5845, 0.4016]]))