FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pytorch-tutorial/src/tensor_initialization.py at master · kozer/pytorch-tutorial · GitHub
kozer
/
pytorch-tutorial
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
3
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pytorch-tutorial
/
src
/
tensor_initialization.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
67 lines (56 loc) · 1.55 KB
Breadcrumbs
pytorch-tutorial
/
src
/
tensor_initialization.py
Copy path
File metadata and controls
67 lines (56 loc) · 1.55 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import
torch
# ----------------------------------------- #
# Tensor initialization #
# ----------------------------------------- #
my_tensor
=
torch
.
tensor
([[
1
,
2
,
3
], [
4
,
5
,
6
]])
my_tensor2
=
torch
.
tensor
([[
1
,
2
,
3
], [
4
,
5
,
6
]],
dtype
=
torch
.
float32
)
my_tensor3
=
torch
.
tensor
([[
1
,
2
,
3
], [
4
,
5
,
6
]],
dtype
=
torch
.
float32
,
device
=
"cuda"
)
my_tensor4
=
torch
.
tensor
([[
1
,
2
,
3
], [
4
,
5
,
6
]],
dtype
=
torch
.
float32
,
device
=
"cpu"
)
my_tensor5
=
torch
.
tensor
([[
1
,
2
,
3
], [
4
,
5
,
6
]],
dtype
=
torch
.
float32
,
device
=
"cuda"
,
requires_grad
=
True
)
print
(
my_tensor
)
print
(
my_tensor2
)
print
(
my_tensor3
)
print
(
my_tensor4
)
print
(
my_tensor5
)
# Other initialization methods
x
=
torch
.
empty
(
size
=
(
3
,
3
))
print
(
x
)
x
=
torch
.
zeros
((
3
,
3
))
print
(
x
)
x
=
torch
.
rand
((
3
,
3
))
print
(
x
)
x
=
torch
.
ones
((
3
,
3
))
print
(
x
)
x
=
torch
.
eye
(
5
,
5
)
print
(
x
)
x
=
torch
.
arange
(
start
=
0
,
end
=
5
,
step
=
1
)
print
(
x
)
x
=
torch
.
linspace
(
start
=
0.1
,
end
=
1
,
steps
=
10
)
print
(
x
)
x
=
torch
.
empty
(
size
=
(
1
,
5
)).
normal_
(
mean
=
0
,
std
=
1
)
print
(
x
)
x
=
torch
.
empty
(
size
=
(
1
,
5
)).
uniform_
(
0
,
1
)
print
(
x
)
x
=
torch
.
diag
(
torch
.
ones
(
3
))
print
(
x
)
# How to initialise and convert tensors to other types (int, float, double)
x
=
torch
.
arange
(
4
)
print
(
x
)
print
(
x
.
bool
())
print
(
x
.
short
())
print
(
x
.
long
())
print
(
x
.
half
())
print
(
x
.
float
())
print
(
x
.
double
())
#Array to tensor conversion
import
numpy
as
np
np_arr
=
np
.
zeros
((
5
,
5
))
tensor
=
torch
.
from_numpy
(
np_arr
)
print
(
x
)
print
(
tensor
.
numpy
())
Back
|
FazBrowse Home
|
New Git URL