import numpy as np
import torch
a = np. array( [ 2 , 3.3 ] )
print ( "torch from numpy:" , torch. from_numpy( a) )
torch from numpy: tensor([2.0000, 3.3000], dtype=torch.float64)
b = np. ones( [ 2 , 3 ] )
print ( "torch from numpy" , torch. from_numpy( b) )
torch from numpy tensor([[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
c = torch. tensor( [ 2 . , 3.2 ] )
print ( "c:" , c)
print ( "c:" , c. type ( ) )
c: tensor([2.0000, 3.2000])
c: torch.FloatTensor
d = torch. Tensor( 2 , 3 )
print ( "d:" , d)
print ( "d:" , d. type ( ) )
d: tensor([[0.0000e+00, 4.1530e-05, 1.8754e+28],
[6.4978e-07, 4.1727e-08, 1.0129e-11]])
d: torch.FloatTensor
e = torch. FloatTensor( 2 , 2 , 3 , 4 )
print ( "e:" , e)
print ( "e:" , e. type ( ) )
e: tensor([[[[ 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
[ 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
[ 1.0000e+00, 0.0000e+00, -6.3164e-34, 8.7441e-43]],
[[ 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
[ 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
[ 9.8091e-45, 0.0000e+00, 1.1210e-44, 0.0000e+00]]],
[[[ 1.0000e+00, 0.0000e+00, -6.3167e-34, 8.7441e-43],
[ 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
[ 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00]],
[[ 9.8091e-45, 0.0000e+00, 1.1210e-44, 0.0000e+00],
[ 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
[ 0.0000e+00, 0.0000e+00, 1.4013e-45, 0.0000e+00]]]])
e: torch.FloatTensor
f = torch. FloatTensor( [ 2 . , 3.2 ] )
print ( "f:" , f)
print ( "f:" , f. type ( ) )
f: tensor([2.0000, 3.2000])
f: torch.FloatTensor
g = torch. empty( ( 2 , 3 ) )
print ( "g:" , g)
print ( "g:" , g. type ( ) )
g: tensor([[0., 0., 0.],
[0., 0., 0.]])
g: torch.FloatTensor
torch. set_default_tensor_type( torch. DoubleTensor)
print ( "set_default_tensor_type:" , torch. tensor( [ 1.2 , 3 ] ) . type ( ) )
print ( torch. tensor( 1 . ) )
print ( "type" , torch. tensor( 1 . ) . type ( ) )
set_default_tensor_type: torch.DoubleTensor
tensor(1.)
type torch.DoubleTensor
h = 10 * torch. rand( 3 , 3 )
print ( "torch.rand:" , h)
print ( "torch.rand_like:" , torch. rand_like( h) )
print ( "torch.randint:" , torch. randint( 1 , 10 , [ 3 , 3 ] ) )
torch.rand: tensor([[4.5408, 1.3240, 9.3553],
[9.6625, 2.3309, 2.1998],
[6.9066, 4.8293, 3.6077]])
torch.rand_like: tensor([[0.1560, 0.0928, 0.0979],
[0.9117, 0.4084, 0.8880],
[0.5676, 0.9840, 0.7720]])
torch.randint: tensor([[4, 8, 3],
[3, 3, 5],
[8, 3, 9]])
j = torch. normal( mean= torch. full( [ 10 ] , 0 ) , std= torch. arange( 1 , 0 , - 0.1 ) )
print ( "normal:" , j)
print ( "生成一个值全为10的tensor:" , torch. full( [ 10 ] , 0 ) )
print ( "生成一个等差数列:" , torch. arange( 1 , 0 , - 0.1 ) )
print ( "生成一个标量:" , torch. full( [ ] , 2 ) , ",dim:" , torch. full( [ ] , 2 ) . dim( ) , ",size:" , torch. full( [ ] , 2 ) . size( ) )
print ( "生成一个向量:" , torch. full( [ 1 ] , 2 ) , ",dim:" , torch. full( [ 1 ] , 2 ) . dim( ) , ",size:" , torch. full( [ 1 ] , 2 ) . size( ) )
normal: tensor([ 0.1456, 0.9674, -1.1302, 0.7635, -1.1897, -0.4885, -0.2525, 0.1764,
-0.1095, -0.2154])
生成一个值全为10的tensor: tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
生成一个等差数列: tensor([1.0000, 0.9000, 0.8000, 0.7000, 0.6000, 0.5000, 0.4000, 0.3000, 0.2000,
0.1000])
生成一个标量: tensor(2.) ,dim: 0 ,size: torch.Size([])
生成一个向量: tensor([2.]) ,dim: 1 ,size: torch.Size([1])
print ( torch. arange( 0 , 10 ) )
print ( torch. arange( 0 , 10 , 2 ) )
print ( torch. range ( 0 , 10 ) )
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
tensor([0, 2, 4, 6, 8])
tensor([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
D:\ProgramData\Anaconda3\envs\LearningProjects\lib\site-packages\ipykernel_launcher.py:4: 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].
after removing the cwd from sys.path.
print ( torch. linspace( 0 , 10 , steps= 4 ) )
print ( torch. linspace( 0 , 10 , steps= 10 ) )
print ( torch. linspace( 0 , 10 , steps= 11 ) )
tensor([ 0.0000, 3.3333, 6.6667, 10.0000])
tensor([ 0.0000, 1.1111, 2.2222, 3.3333, 4.4444, 5.5556, 6.6667, 7.7778,
8.8889, 10.0000])
tensor([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
print ( 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])
print ( torch. ones( 3 , 3 ) )
print ( torch. zeros( 3 , 3 ) )
print ( torch. eye( 4 , 4 ) )
print ( torch. eye( 4 ) )
l = torch. zeros( 3 , 3 )
print ( "l:" , torch. ones_like( l) )
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
tensor([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
tensor([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
l: tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
print ( "randperm:" , torch. randperm( 10 ) )
n = torch. rand( 2 , 3 )
m = torch. rand( 2 , 2 )
idx = torch. randperm( 2 )
idx
print ( "idx:" , idx)
idx
print ( "idx:" , idx)
print ( "n[idx]:" , n[ idx] )
print ( "m[idx]:" , m[ idx] )
randperm: tensor([9, 0, 2, 7, 4, 1, 8, 5, 6, 3])
idx: tensor([0, 1])
idx: tensor([0, 1])
n[idx]: tensor([[0.8147, 0.7831, 0.0456],
[0.5734, 0.4782, 0.6875]])
m[idx]: tensor([[0.4919, 0.5207],
[0.0426, 0.8168]])