-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01Py-basics.py
More file actions
75 lines (52 loc) · 1.19 KB
/
01Py-basics.py
File metadata and controls
75 lines (52 loc) · 1.19 KB
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
67
68
69
70
71
72
73
74
75
# -*- coding: utf-8 -*-
"""
Automatically generated by Colaboratory.
# **PyTorch (Basics)**
**PyTorch** is an optimized tensor library for deep learning using GPUs and CPUs.
**Torch** is an open-source machine learning library, a scientific computing framework, and a script language based on the Lua programming language.
"""
import torch
# define a number
t = torch.tensor(5.0)
t
# type variable
t.dtype
# vector
v = torch.tensor([1,2,3,4])
v
# matrix
m = torch.tensor([[1,2,3,4],[1,2,3,4]])
m
# 3 dimentional array
t1 = torch.tensor([[[1,2,3,4],[1,2,3,4]],[[1,2,3,4],[1,2,3,4]]])
t1
# shapes
print('number: ', t.shape)
print('vector: ', v.shape)
print('matrix: ', m.shape)
print('tensor: ', t1.shape)
# create tensor
x = torch.tensor(3.)
b = torch.tensor(4., requires_grad=True)
w = torch.tensor(5., requires_grad=True)
# arithmetic operation
y = x * w + b
y
"""$y = 3x + 4$"""
# compute derivative
y.backward()
# gradiants
print('dy/dx =', x.grad)
print('dy/dw =', w.grad)
print('dy/db =', b.grad)
"""Using Numpy """
import numpy as np
# array
v = np.array([1,2,3,4])
v
# convert numpy array to torch tersor
y = torch.from_numpy(v)
y
# convert torch tersor to numpy array
x = y.numpy()
x