import torch
a = torch. randn( 2 , 3 )
print ( a)
print ( a. type ( ) )
print ( type ( a) )
tensor([[ 0.2685, -0.2450, -0.1803],
[-0.5779, 1.8447, -2.2098]])
torch.FloatTensor
<class 'torch.Tensor'>
isinstance ( a, torch. FloatTensor)
True
isinstance ( a, torch. cuda. FloatTensor)
False
data = a. cuda( )
isinstance ( data, torch. cuda. FloatTensor)
True
Dimension / rank0
torch. tensor( 1 . )
tensor(1.)
torch. tensor( 1.3 )
tensor(1.3000)
a = torch. tensor( 2.2 )
a. shape
torch.Size([])
len ( a. shape)
0
a. size( )
torch.Size([])
b = torch. randn( 3 , 3 )
b
tensor([[ 0.4503, 1.4839, -0.2727],
[ 0.7436, -0.7034, -1.6083],
[ 0.4326, -0.2785, 0.4935]])
b = torch. randn( [ 3 , 3 ] )
b
tensor([[ 0.2073, 0.0413, 0.0724],
[-1.5728, -0.5479, -0.8047],
[ 1.7011, 0.8673, -0.3816]])
b. size( )
torch.Size([3, 3])
b. size( 0 )
3
c = torch. randn( [ 2 , 3 ] )
print ( c. size( ) )
print ( c. size( 0 ) )
print ( c. size( 1 ) )
print ( c. size( 2 ) )
torch.Size([2, 3])
2
3
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-43-d437c71a5a89> in <module>
3 print(c.size(0))
4 print(c.size(1))
----> 5 print(c.size(2))
IndexError: Dimension out of range (expected to be in range of [-2, 1], but got 2)
Dimension 1 / rank 1
torch. tensor( [ 1.1 ] )
tensor([1.1000])
torch. tensor( [ 1.1 , 1.2 ] )
tensor([1.1000, 1.2000])
torch. FloatTensor( 1 )
tensor([2.])
torch. FloatTensor( 2 )
tensor([0., 0.])
import numpy as np
import torch
data = np. ones( 2 )
print ( data)
torch. from_numpy( data)
[1. 1.]
tensor([1., 1.], dtype=torch.float64)
e = torch. tensor( [ 1 , 2 ] )
e
tensor([1, 2])
e. size( 0 )
2
e. size( - 1 )
2
a= torch. ones( 2 )
a. shape
torch.Size([2])
a. size( 0 )
2
a. size( - 1 )
2
a= torch. ones( 1 )
a. shape
torch.Size([1])
a. size( 0 )
1
a. size( - 1 )
1
Dimension 2
a = torch. randn( 2 , 3 )
a
tensor([[ 0.1747, 1.2848, -0.4329],
[-2.2200, 2.2615, -0.6228]])
a. shape
torch.Size([2, 3])
a. size( 0 )
2
a. size( 1 )
3
a. shape[ 0 ]
2
a. shape[ 1 ]
3
Dimension 3
a = torch. rand( 1 , 2 , 3 )
a
tensor([[[0.4654, 0.6985, 0.7499],
[0.0807, 0.6140, 0.5457]]])
a = torch. rand( 2 , 2 , 3 )
a
tensor([[[0.4916, 0.7213, 0.5852],
[0.0629, 0.4214, 0.5142]],
[[0.4386, 0.4954, 0.8755],
[0.3940, 0.2207, 0.4918]]])
a. shape
torch.Size([2, 2, 3])
a. shape[ 0 ]
2
a. shape[ 1 ]
2
a. shape[ 2 ]
3
print ( a[ 0 ] )
print ( a[ 1 ] )
tensor([[0.4916, 0.7213, 0.5852],
[0.0629, 0.4214, 0.5142]])
tensor([[0.4386, 0.4954, 0.8755],
[0.3940, 0.2207, 0.4918]])
list ( a. shape)
[2, 2, 3]
Dimension 4
a = torch. randn( 2 , 3 , 28 , 28 )
a
tensor([[[[ 0.9770, -1.5422, -1.4004, ..., 1.4447, 0.3687, -1.1456],
[-0.0616, 0.7368, 0.2303, ..., 0.2191, -0.9384, 0.1178],
[-0.6595, -0.6673, 0.0521, ..., 0.2304, -1.2156, -0.5742],
...,
[-2.0420, -0.2665, 0.5631, ..., -1.1558, 0.1600, 0.2163],
[-0.6479, 0.3045, -0.6552, ..., -0.4643, 0.5231, -0.7562],
[ 0.1142, 0.4732, -1.1978, ..., -1.6959, -0.6284, 0.5963]],
[[-0.3389, 0.2129, 1.2783, ..., 1.0402, 0.4531, 1.9179],
[ 0.4201, 0.6000, -0.6048, ..., -0.2739, 0.6021, -0.4467],
[-0.2578, -0.5420, -0.7303, ..., 0.9527, 1.8283, 0.5765],
...,
[-0.3595, -0.5524, 0.8008, ..., 0.5622, 0.3685, -0.5666],
[ 1.4712, -0.8457, 0.3197, ..., 0.3807, 0.0580, 0.1099],
[-0.5920, -3.1865, -1.2504, ..., 1.4477, 1.4455, 0.4195]],
[[-1.3461, 0.9136, -0.1402, ..., -0.0558, -0.9873, 1.2209],
[-0.9963, 0.1460, 0.5685, ..., 1.1063, 0.4424, 1.2003],
[-1.2245, -0.2666, 1.2451, ..., -0.5278, 1.5485, -0.2553],
...,
[ 0.2660, -0.3592, -1.9605, ..., -1.2485, -0.8174, -0.2745],
[-0.9404, 1.3743, 0.4145, ..., -1.7326, 0.6057, -0.7291],
[ 0.3479, 0.3329, 0.4403, ..., -1.5186, 0.2652, 3.2966]]],
[[[ 0.7794, 0.1165, -0.0097, ..., 0.0154, -0.1022, 1.0874],
[ 0.8231, 0.2086, 0.8247, ..., 1.3846, 0.4115, -0.7875],
[-0.5599, -0.8777, 0.0860, ..., -0.4499, 0.8062, -0.8251],
...,
[ 1.0082, -0.0869, 0.1664, ..., 0.7052, -0.5506, -1.0844],
[ 1.1836, 0.7602, 2.2287, ..., 0.3032, -0.7607, -0.8534],
[ 0.4466, 1.3760, -1.9631, ..., -0.7653, -0.3874, 0.1126]],
[[ 1.4206, 0.9637, 0.6838, ..., 0.6099, -0.7173, -0.9166],
[-0.5624, -0.6536, -1.2994, ..., 1.3861, -2.2174, 1.6432],
[-0.5166, -0.9876, 0.3641, ..., 0.0136, 0.3891, -0.7152],
...,
[-1.9587, -0.0290, 0.7256, ..., -0.8681, 1.2109, 1.4550],
[-0.7089, 0.0853, 0.9076, ..., -1.9827, 0.4440, 0.2140],
[ 0.9694, -0.3007, 0.5784, ..., 0.1984, -0.4576, 0.2178]],
[[ 0.0707, -0.7766, 0.3134, ..., -1.1565, 0.8534, -0.1165],
[-0.7874, -0.7807, 1.5859, ..., 0.3828, 0.9948, 0.1778],
[-0.2240, 0.8667, 1.3384, ..., 1.1469, 1.5945, -1.0732],
...,
[-0.6839, -0.9666, -0.2751, ..., -0.6123, -2.7403, 0.2976],
[ 0.7267, -0.4425, -0.5677, ..., -1.3017, 0.4829, -0.3434],
[-0.1188, 0.3201, -0.8754, ..., -0.2463, 1.2385, 0.4045]]]])
print ( a. shape)
print ( a. size( ) )
torch.Size([2, 3, 28, 28])
torch.Size([2, 3, 28, 28])
print ( a. shape[ 0 ] )
print ( a. size( 0 ) )
print ( a. shape[ 1 ] )
print ( a. size( 1 ) )
print ( a. shape[ 2 ] )
print ( a. size( 2 ) )
print ( a. shape[ 3 ] )
print ( a. size( 3 ) )
2
2
3
3
28
28
28
28
Mixed
a = torch. randn( 2 , 3 , 28 , 28 )
a. shape
torch.Size([2, 3, 28, 28])
a. numel( )
4704
print ( a. dim( ) )
4
a = torch. tensor( 1 )
a. dim( )
0
a = torch. tensor( [ 1 , 2 ] )
a. dim( )
1
a = torch. tensor( [ 2 , 3 ] )
a. dim( )
1
a = torch. tensor( 2 , 3 )
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-125-31ef262ceaf8> in <module>
----> 1 a = torch.tensor(2,3)
TypeError: tensor() takes 1 positional argument but 2 were given
a = torch. randn( 1 , 2 , 3 )
a
tensor([[[-0.4289, 0.4551, 0.4237],
[-0.6139, 1.2619, 0.8672]]])
a. dim( )
3
print ( a. shape[ 0 ] )
print ( a. size( 0 ) )
1
1