pytinytensor 0.1.5__tar.gz → 0.1.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pytinytensor-0.1.5/pytinytensor.egg-info → pytinytensor-0.1.7}/PKG-INFO +3 -1
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/README.md +3 -1
- {pytinytensor-0.1.5 → pytinytensor-0.1.7/pytinytensor.egg-info}/PKG-INFO +3 -1
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/pytinytensor.egg-info/SOURCES.txt +7 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/setup.py +1 -1
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/core/tensor.py +14 -0
- pytinytensor-0.1.7/tinytensor/nn/__init__.py +19 -0
- pytinytensor-0.1.7/tinytensor/nn/activations.py +68 -0
- pytinytensor-0.1.7/tinytensor/nn/batchnorm.py +78 -0
- pytinytensor-0.1.7/tinytensor/nn/conv.py +84 -0
- pytinytensor-0.1.7/tinytensor/nn/embedding.py +33 -0
- pytinytensor-0.1.7/tinytensor/nn/flatten.py +9 -0
- pytinytensor-0.1.7/tinytensor/nn/functional.py +54 -0
- pytinytensor-0.1.7/tinytensor/nn/losses.py +58 -0
- pytinytensor-0.1.7/tinytensor/nn/pooling.py +113 -0
- pytinytensor-0.1.7/tinytensor/nn/rnn.py +140 -0
- pytinytensor-0.1.5/tinytensor/nn/__init__.py +0 -14
- pytinytensor-0.1.5/tinytensor/nn/activations.py +0 -37
- pytinytensor-0.1.5/tinytensor/nn/losses.py +0 -12
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/LICENSE +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/pyproject.toml +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/pytinytensor.egg-info/dependency_links.txt +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/pytinytensor.egg-info/requires.txt +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/pytinytensor.egg-info/top_level.txt +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/setup.cfg +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tests/test_autograd.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tests/test_data.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tests/test_losses.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tests/test_nn.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tests/test_optim.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tests/test_tensor_math.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/__init__.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/backends/__init__.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/backends/cpu_numpy.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/backends/cuda_binding.cpp +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/backends/cuda_gpu.cu +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/backends/cuda_gpu.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/config.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/core/__init__.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/core/autograd.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/core/ops.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/data/__init__.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/data/dataloader.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/data/dataset.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/nn/dropout.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/nn/linear.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/nn/modules.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/nn/utils.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/optim/__init__.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/optim/lr_scheduler.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/optim/optimizer.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/utils/__init__.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/utils/bar.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/utils/early_stopping.py +0 -0
- {pytinytensor-0.1.5 → pytinytensor-0.1.7}/tinytensor/utils/summary.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pytinytensor
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.7
|
|
4
4
|
Summary: мини ИИ фреймворк от IbrokimN ( github/IbrokhimN )
|
|
5
5
|
Home-page: https://github.com/IbrokhimN/tinytensor
|
|
6
6
|
Author: IbrokhimN
|
|
@@ -23,6 +23,8 @@ Dynamic: summary
|
|
|
23
23
|
|
|
24
24
|
# tinytensor
|
|
25
25
|
|
|
26
|
+
Новые доки напишу в след версии
|
|
27
|
+
|
|
26
28
|
Маленький самописный autograd на numpy. Тензоры, backprop, пара слоев,
|
|
27
29
|
лоссы, оптимизаторы и даталоадер. По сути свой мини-pytorch, только без
|
|
28
30
|
плюшек и без cuda (пока).
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# tinytensor
|
|
2
2
|
|
|
3
|
+
Новые доки напишу в след версии
|
|
4
|
+
|
|
3
5
|
Маленький самописный autograd на numpy. Тензоры, backprop, пара слоев,
|
|
4
6
|
лоссы, оптимизаторы и даталоадер. По сути свой мини-pytorch, только без
|
|
5
7
|
плюшек и без cuda (пока).
|
|
@@ -215,4 +217,4 @@ tinytensor/
|
|
|
215
217
|
- [Loshchilov & Hutter - AdamW](https://arxiv.org/abs/1711.05101)
|
|
216
218
|
- [Hendrycks & Gimpel - GELU](https://arxiv.org/abs/1606.08415)
|
|
217
219
|
- [PyTorch - torch.nn.Module](https://pytorch.org/docs/stable/generated/torch.nn.Module.html)
|
|
218
|
-
- [PyTorch - torch.utils.data.DataLoader](https://pytorch.org/docs/stable/data.html)
|
|
220
|
+
- [PyTorch - torch.utils.data.DataLoader](https://pytorch.org/docs/stable/data.html)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pytinytensor
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.7
|
|
4
4
|
Summary: мини ИИ фреймворк от IbrokimN ( github/IbrokhimN )
|
|
5
5
|
Home-page: https://github.com/IbrokhimN/tinytensor
|
|
6
6
|
Author: IbrokhimN
|
|
@@ -23,6 +23,8 @@ Dynamic: summary
|
|
|
23
23
|
|
|
24
24
|
# tinytensor
|
|
25
25
|
|
|
26
|
+
Новые доки напишу в след версии
|
|
27
|
+
|
|
26
28
|
Маленький самописный autograd на numpy. Тензоры, backprop, пара слоев,
|
|
27
29
|
лоссы, оптимизаторы и даталоадер. По сути свой мини-pytorch, только без
|
|
28
30
|
плюшек и без cuda (пока).
|
|
@@ -29,10 +29,17 @@ tinytensor/data/dataloader.py
|
|
|
29
29
|
tinytensor/data/dataset.py
|
|
30
30
|
tinytensor/nn/__init__.py
|
|
31
31
|
tinytensor/nn/activations.py
|
|
32
|
+
tinytensor/nn/batchnorm.py
|
|
33
|
+
tinytensor/nn/conv.py
|
|
32
34
|
tinytensor/nn/dropout.py
|
|
35
|
+
tinytensor/nn/embedding.py
|
|
36
|
+
tinytensor/nn/flatten.py
|
|
37
|
+
tinytensor/nn/functional.py
|
|
33
38
|
tinytensor/nn/linear.py
|
|
34
39
|
tinytensor/nn/losses.py
|
|
35
40
|
tinytensor/nn/modules.py
|
|
41
|
+
tinytensor/nn/pooling.py
|
|
42
|
+
tinytensor/nn/rnn.py
|
|
36
43
|
tinytensor/nn/utils.py
|
|
37
44
|
tinytensor/optim/__init__.py
|
|
38
45
|
tinytensor/optim/lr_scheduler.py
|
|
@@ -184,6 +184,20 @@ class Tensor:
|
|
|
184
184
|
out._backward = _backward
|
|
185
185
|
return out
|
|
186
186
|
|
|
187
|
+
def reshape(self, *shape):
|
|
188
|
+
out = Tensor(self.data.reshape(*shape), requires_grad=self.requires_grad, device=self.device)
|
|
189
|
+
if out.requires_grad:
|
|
190
|
+
out._prev = {self}
|
|
191
|
+
|
|
192
|
+
def _backward():
|
|
193
|
+
if self.requires_grad:
|
|
194
|
+
if self.grad is None:
|
|
195
|
+
self.grad = np.zeros_like(self.data, dtype=np.float32)
|
|
196
|
+
self.grad += out.grad.reshape(self.data.shape)
|
|
197
|
+
|
|
198
|
+
out._backward = _backward
|
|
199
|
+
return out
|
|
200
|
+
|
|
187
201
|
def relu(self):
|
|
188
202
|
out = Tensor(
|
|
189
203
|
np.maximum(0, self.data),
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from tinytensor.nn.modules import Module, Sequential
|
|
2
|
+
from tinytensor.nn.linear import Linear
|
|
3
|
+
from tinytensor.nn.conv import Conv2d
|
|
4
|
+
from tinytensor.nn.pooling import MaxPool2d, AvgPool2d
|
|
5
|
+
from tinytensor.nn.flatten import Flatten
|
|
6
|
+
from tinytensor.nn.batchnorm import BatchNorm2d
|
|
7
|
+
from tinytensor.nn.embedding import Embedding
|
|
8
|
+
from tinytensor.nn.rnn import RNNCell, RNN
|
|
9
|
+
from tinytensor.nn.activations import ReLU, LeReLU, Sigmod, Tanh, GELU, Softmax
|
|
10
|
+
from tinytensor.nn.dropout import Dropout
|
|
11
|
+
from tinytensor.nn.losses import MSELoss, CrossEntropyLoss
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"Module", "Sequential",
|
|
15
|
+
"Linear", "Conv2d", "MaxPool2d", "AvgPool2d", "Flatten", "BatchNorm2d",
|
|
16
|
+
"Embedding", "RNNCell", "RNN",
|
|
17
|
+
"ReLU", "LeReLU", "Sigmod", "Tanh", "GELU", "Softmax",
|
|
18
|
+
"Dropout", "MSELoss", "CrossEntropyLoss"
|
|
19
|
+
]
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from tinytensor.core.tensor import Tensor
|
|
4
|
+
from tinytensor.nn.modules import Module
|
|
5
|
+
|
|
6
|
+
class ReLU(Module):
|
|
7
|
+
def forward(self, x):
|
|
8
|
+
return x.relu()
|
|
9
|
+
|
|
10
|
+
class LeReLU(Module):
|
|
11
|
+
# у нас есть тут альфа
|
|
12
|
+
def __init__(self, alpha=0.01):
|
|
13
|
+
super().__init__()
|
|
14
|
+
self.alpha = alpha
|
|
15
|
+
|
|
16
|
+
def forward(self, x):
|
|
17
|
+
return x.leaky_relu(alpha=self.alpha)
|
|
18
|
+
|
|
19
|
+
class Sigmod(Module):
|
|
20
|
+
def forward(self, x):
|
|
21
|
+
return x.sigmoid()
|
|
22
|
+
|
|
23
|
+
class Tanh(Module):
|
|
24
|
+
def forward(self, x):
|
|
25
|
+
return x.tanh()
|
|
26
|
+
|
|
27
|
+
class GELU(Module):
|
|
28
|
+
def forward(self, x):
|
|
29
|
+
return x.gelu()
|
|
30
|
+
|
|
31
|
+
class Softmax(Module):
|
|
32
|
+
def __init__(self, dim=1):
|
|
33
|
+
super().__init__()
|
|
34
|
+
self.dim = dim
|
|
35
|
+
|
|
36
|
+
def forward(self, x):
|
|
37
|
+
max_x = np.max(x.data, axis=self.dim, keepdims=True)
|
|
38
|
+
exp_x = np.exp(x.data - max_x)
|
|
39
|
+
|
|
40
|
+
out_data = exp_x / np.sum(exp_x, axis=self.dim, keepdims=True)
|
|
41
|
+
|
|
42
|
+
out = Tensor(out_data, requires_grad=x.requires_grad, device=x.device)
|
|
43
|
+
|
|
44
|
+
if out.requires_grad:
|
|
45
|
+
out._prev = {x}
|
|
46
|
+
|
|
47
|
+
def _backward():
|
|
48
|
+
dy = out.grad
|
|
49
|
+
dot = np.sum(dy * out_data, axis=self.dim, keepdims=True)
|
|
50
|
+
dx = out_data * (dy - dot)
|
|
51
|
+
|
|
52
|
+
if x.grad is None:
|
|
53
|
+
x.grad = np.zeros_like(x.data, dtype=np.float32)
|
|
54
|
+
x.grad += dx
|
|
55
|
+
|
|
56
|
+
out._backward = _backward
|
|
57
|
+
|
|
58
|
+
return out
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
# ToDo:
|
|
63
|
+
# ReLU [x]
|
|
64
|
+
# LeReLU [x]
|
|
65
|
+
# Sigmoid [x]
|
|
66
|
+
# Tanh [x]
|
|
67
|
+
# GELU [x]
|
|
68
|
+
# kama soska
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from tinytensor.core.tensor import Tensor
|
|
3
|
+
from tinytensor.nn.modules import Module
|
|
4
|
+
|
|
5
|
+
class BatchNorm2d(Module):
|
|
6
|
+
def __init__(self, num_features, eps=1e-5, momentum=0.1):
|
|
7
|
+
super().__init__()
|
|
8
|
+
self.num_features = num_features
|
|
9
|
+
self.eps = eps
|
|
10
|
+
self.momentum = momentum
|
|
11
|
+
|
|
12
|
+
self.gamma = Tensor(np.ones((1, num_features, 1, 1), dtype=np.float32), requires_grad=True)
|
|
13
|
+
self.beta = Tensor(np.zeros((1, num_features, 1, 1), dtype=np.float32), requires_grad=True)
|
|
14
|
+
|
|
15
|
+
self.running_mean = np.zeros((1, num_features, 1, 1), dtype=np.float32)
|
|
16
|
+
self.running_var = np.ones((1, num_features, 1, 1), dtype=np.float32)
|
|
17
|
+
|
|
18
|
+
def forward(self, x):
|
|
19
|
+
training = self.training
|
|
20
|
+
if training:
|
|
21
|
+
mean = np.mean(x.data, axis=(0, 2, 3), keepdims=True)
|
|
22
|
+
var = np.var(x.data, axis=(0, 2, 3), keepdims=True)
|
|
23
|
+
|
|
24
|
+
self.running_mean = (1 - self.momentum) * self.running_mean + self.momentum * mean
|
|
25
|
+
self.running_var = (1 - self.momentum) * self.running_var + self.momentum * var
|
|
26
|
+
else:
|
|
27
|
+
mean = self.running_mean
|
|
28
|
+
var = self.running_var
|
|
29
|
+
|
|
30
|
+
std_inv = 1.0 / np.sqrt(var + self.eps)
|
|
31
|
+
x_centered = x.data - mean
|
|
32
|
+
x_hat = x_centered * std_inv
|
|
33
|
+
|
|
34
|
+
out_data = self.gamma.data * x_hat + self.beta.data
|
|
35
|
+
|
|
36
|
+
out = Tensor(
|
|
37
|
+
out_data,
|
|
38
|
+
requires_grad=x.requires_grad or self.gamma.requires_grad or self.beta.requires_grad,
|
|
39
|
+
device=x.device,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
if out.requires_grad:
|
|
43
|
+
out._prev = {x, self.gamma, self.beta}
|
|
44
|
+
|
|
45
|
+
def _backward():
|
|
46
|
+
dout = out.grad
|
|
47
|
+
N, C, H, W = x.data.shape
|
|
48
|
+
m = N * H * W
|
|
49
|
+
|
|
50
|
+
if self.gamma.requires_grad:
|
|
51
|
+
if self.gamma.grad is None:
|
|
52
|
+
self.gamma.grad = np.zeros_like(self.gamma.data, dtype=np.float32)
|
|
53
|
+
self.gamma.grad += np.sum(dout * x_hat, axis=(0, 2, 3), keepdims=True)
|
|
54
|
+
|
|
55
|
+
if self.beta.requires_grad:
|
|
56
|
+
if self.beta.grad is None:
|
|
57
|
+
self.beta.grad = np.zeros_like(self.beta.data, dtype=np.float32)
|
|
58
|
+
self.beta.grad += np.sum(dout, axis=(0, 2, 3), keepdims=True)
|
|
59
|
+
|
|
60
|
+
if x.requires_grad:
|
|
61
|
+
if x.grad is None:
|
|
62
|
+
x.grad = np.zeros_like(x.data, dtype=np.float32)
|
|
63
|
+
|
|
64
|
+
if training:
|
|
65
|
+
dx_hat = dout * self.gamma.data
|
|
66
|
+
dvar = np.sum(dx_hat * x_centered * -0.5 * std_inv ** 3, axis=(0, 2, 3), keepdims=True)
|
|
67
|
+
dmean = np.sum(dx_hat * -std_inv, axis=(0, 2, 3), keepdims=True) + \
|
|
68
|
+
dvar * np.mean(-2.0 * x_centered, axis=(0, 2, 3), keepdims=True)
|
|
69
|
+
dx = dx_hat * std_inv + dvar * 2.0 * x_centered / m + dmean / m
|
|
70
|
+
else:
|
|
71
|
+
dx = dout * self.gamma.data * std_inv
|
|
72
|
+
|
|
73
|
+
x.grad += dx
|
|
74
|
+
|
|
75
|
+
out._backward = _backward
|
|
76
|
+
|
|
77
|
+
return out
|
|
78
|
+
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from tinytensor.core.tensor import Tensor
|
|
4
|
+
from tinytensor.nn.modules import Module
|
|
5
|
+
from tinytensor.nn.functional import im2col_indices, col2_im_indices
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Conv2d(Module):
|
|
9
|
+
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0):
|
|
10
|
+
super().__init__()
|
|
11
|
+
|
|
12
|
+
self.in_channels = in_channels
|
|
13
|
+
self.out_channels = out_channels
|
|
14
|
+
|
|
15
|
+
# если как число
|
|
16
|
+
if isinstance(kernel_size, int):
|
|
17
|
+
self.kh = self.kw = kernel_size
|
|
18
|
+
#если кортеж
|
|
19
|
+
else:
|
|
20
|
+
self.kh, self.kw = kernel_size
|
|
21
|
+
|
|
22
|
+
self.stride = stride
|
|
23
|
+
self.padding = padding
|
|
24
|
+
|
|
25
|
+
n = in_channels * self.kh * self.kw
|
|
26
|
+
std = np.sqrt(2.0 / n)
|
|
27
|
+
|
|
28
|
+
w_data = np.random.randn(out_channels, in_channels, self.kh, self.kw).astype(np.float32) * std
|
|
29
|
+
self.weight = Tensor(w_data, requires_grad=True)
|
|
30
|
+
|
|
31
|
+
b_data = np.zeros((out_channels, 1), dtype=np.float32)
|
|
32
|
+
self.bias = Tensor(b_data, requires_grad=True)
|
|
33
|
+
|
|
34
|
+
def forward(self, x):
|
|
35
|
+
N, C, H, W = x.data.shape
|
|
36
|
+
|
|
37
|
+
x_col, out_h, out_w = im2col_indices(
|
|
38
|
+
x.data, self.kh, self.kw, padding=self.padding, stride=self.stride
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
w_row = self.weight.data.reshape(self.out_channels, -1)
|
|
42
|
+
|
|
43
|
+
out_data = w_row @ x_col + self.bias.data
|
|
44
|
+
|
|
45
|
+
out_data = out_data.reshape(self.out_channels, out_h, out_w, N)
|
|
46
|
+
out_data = out_data.transpose(3, 0, 1, 2)
|
|
47
|
+
|
|
48
|
+
out = Tensor(
|
|
49
|
+
out_data,
|
|
50
|
+
requires_grad=x.requires_grad or self.weight.requires_grad or self.bias.requires_grad,
|
|
51
|
+
device=x.device,
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
if out.requires_grad:
|
|
55
|
+
out._prev = {x, self.weight, self.bias}
|
|
56
|
+
|
|
57
|
+
def _backward():
|
|
58
|
+
dout = out.grad
|
|
59
|
+
dout_reshaped = dout.transpose(1, 2, 3, 0).reshape(self.out_channels, -1)
|
|
60
|
+
|
|
61
|
+
if self.weight.requires_grad:
|
|
62
|
+
if self.weight.grad is None:
|
|
63
|
+
self.weight.grad = np.zeros_like(self.weight.data, dtype=np.float32)
|
|
64
|
+
dW = dout_reshaped @ x_col.T
|
|
65
|
+
self.weight.grad += dW.reshape(self.weight.data.shape)
|
|
66
|
+
|
|
67
|
+
if self.bias.requires_grad:
|
|
68
|
+
if self.bias.grad is None:
|
|
69
|
+
self.bias.grad = np.zeros_like(self.bias.data, dtype=np.float32)
|
|
70
|
+
self.bias.grad += np.sum(dout_reshaped, axis=1, keepdims=True)
|
|
71
|
+
|
|
72
|
+
if x.requires_grad:
|
|
73
|
+
if x.grad is None:
|
|
74
|
+
x.grad = np.zeros_like(x.data, dtype=np.float32)
|
|
75
|
+
dx_col = w_row.T @ dout_reshaped
|
|
76
|
+
dx = col2_im_indices(
|
|
77
|
+
dx_col, x.data.shape, self.kh, self.kw,
|
|
78
|
+
padding=self.padding, stride=self.stride
|
|
79
|
+
)
|
|
80
|
+
x.grad += dx
|
|
81
|
+
|
|
82
|
+
out._backward = _backward
|
|
83
|
+
|
|
84
|
+
return out
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from tinytensor.nn.modules import Module
|
|
4
|
+
from tinytensor.core.tensor import Tensor
|
|
5
|
+
|
|
6
|
+
class Embedding(Module):
|
|
7
|
+
def __init__(self, num_embeddings: int, dim_embeddings:int ):
|
|
8
|
+
super().__init__()
|
|
9
|
+
self.num_embeddings = num_embeddings
|
|
10
|
+
self.dim_embeddings = dim_embeddings
|
|
11
|
+
|
|
12
|
+
weight_data = np.random.randn(num_embeddings, dim_embeddings).astype(np.float32) * 0.01
|
|
13
|
+
|
|
14
|
+
self.weight = Tensor(weight_data, requires_grad=True)
|
|
15
|
+
|
|
16
|
+
def forward(self, x):
|
|
17
|
+
#x эт тензор с целыми числами
|
|
18
|
+
indices = x.data.astype(np.int64)
|
|
19
|
+
out_data = self.weight.data[indices]
|
|
20
|
+
|
|
21
|
+
out = Tensor(out_data, requires_grad=self.weight.requires_grad, device=self.weight.device)
|
|
22
|
+
|
|
23
|
+
if out.requires_grad:
|
|
24
|
+
out._prev = {self.weight}
|
|
25
|
+
|
|
26
|
+
def _backward():
|
|
27
|
+
if self.weight.grad is None:
|
|
28
|
+
self.weight.grad = np.zeros_like(self.weight.data, dtype=np.float32)
|
|
29
|
+
np.add.at(self.weight.grad, indices, out.grad)
|
|
30
|
+
|
|
31
|
+
out._backward = _backward
|
|
32
|
+
|
|
33
|
+
return out
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
# вцелом идея im2col это то что изначально интуитивный метод перебора окна по изображению является просто написать 6 циклов для сдвигов и тд однако это ооочень медленный метод, и поэтому мы преобразовываем в 1 матрицу и умножаем и это все будет в разыыы быстрее чем наивный метод перебора
|
|
4
|
+
|
|
5
|
+
def im2col_indices(x, kh, kw, padding=1, stride=1):
|
|
6
|
+
p = padding
|
|
7
|
+
#делаем паддинг
|
|
8
|
+
x_padded = np.pad(x, ((0, 0), (0, 0), (p, p), (p, p)), mode='constant')
|
|
9
|
+
N, C, H, W = x.shape
|
|
10
|
+
out_h = int((H + 2 * p - kh) / stride + 1)
|
|
11
|
+
out_w = int((W + 2 * p - kw) / stride + 1)
|
|
12
|
+
|
|
13
|
+
i0 = np.repeat(np.arange(kh), kw)
|
|
14
|
+
i0 = np.tile(i0, C)
|
|
15
|
+
i1 = stride * np.repeat(np.arange(out_h), out_w)
|
|
16
|
+
j0 = np.tile(np.arange(kw), kh * C)
|
|
17
|
+
j1 = stride * np.tile(np.arange(out_w), out_h)
|
|
18
|
+
|
|
19
|
+
i = i0.reshape(-1, 1) + i1.reshape(1, -1)
|
|
20
|
+
j = j0.reshape(-1, 1) + j1.reshape(1, -1)
|
|
21
|
+
k = np.repeat(np.arange(C), kh * kw).reshape(-1, 1)
|
|
22
|
+
|
|
23
|
+
cols = x_padded[:, k, i, j]
|
|
24
|
+
cols = cols.transpose(1, 2, 0).reshape(kh * kw * C, -1)
|
|
25
|
+
return cols, out_h, out_w
|
|
26
|
+
|
|
27
|
+
def col2_im_indices(cols, x_shape, kh, kw, padding=1, stride=1):
|
|
28
|
+
N, C, H, W = x_shape
|
|
29
|
+
p = padding
|
|
30
|
+
H_padded, W_padded = H + 2 * p, W + 2 * p
|
|
31
|
+
x_padded = np.zeros((N, C, H_padded, W_padded), dtype=cols.dtype)
|
|
32
|
+
|
|
33
|
+
out_h = int((H + 2 * p - kh) / stride + 1)
|
|
34
|
+
out_w = int((W + 2 * p - kw) / stride + 1)
|
|
35
|
+
|
|
36
|
+
i0 = np.repeat(np.arange(kh), kw)
|
|
37
|
+
i0 = np.tile(i0, C)
|
|
38
|
+
i1 = stride * np.repeat(np.arange(out_h), out_w)
|
|
39
|
+
j0 = np.tile(np.arange(kw), kh * C)
|
|
40
|
+
j1 = stride * np.tile(np.arange(out_w), out_h)
|
|
41
|
+
|
|
42
|
+
i = i0.reshape(-1, 1) + i1.reshape(1, -1)
|
|
43
|
+
j = j0.reshape(-1, 1) + j1.reshape(1, -1)
|
|
44
|
+
k = np.repeat(np.arange(C), kh * kw).reshape(-1, 1)
|
|
45
|
+
|
|
46
|
+
cols_reshaped = cols.reshape(C * kh * kw, -1, N)
|
|
47
|
+
cols_reshaped = cols_reshaped.transpose(2, 0, 1)
|
|
48
|
+
|
|
49
|
+
np.add.at(x_padded, (slice(None), k, i, j), cols_reshaped)
|
|
50
|
+
|
|
51
|
+
if p == 0:
|
|
52
|
+
return x_padded
|
|
53
|
+
return x_padded[:, :, p:-p, p:-p]
|
|
54
|
+
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from tinytensor.core.tensor import Tensor
|
|
3
|
+
from tinytensor.nn.modules import Module
|
|
4
|
+
# 1/n * sum((n, i = 1), yi - y`i)^2
|
|
5
|
+
class MSELoss(Module):
|
|
6
|
+
def forward(self, y_pred, y_true):
|
|
7
|
+
diff = y_pred - y_true
|
|
8
|
+
sq_diff = diff ** 2
|
|
9
|
+
return sq_diff.sum() * (1.0/y_pred.data.size)
|
|
10
|
+
|
|
11
|
+
# -log((e-xtar - M)/(sum(exj-M)))=-(xt-M)+log sum(exj-M)
|
|
12
|
+
class CrossEntropyLoss(Module):
|
|
13
|
+
def __init__(self, eps=1e-12):
|
|
14
|
+
super().__init__()
|
|
15
|
+
self.eps = eps
|
|
16
|
+
|
|
17
|
+
def forward(self, logits, targets):
|
|
18
|
+
x = logits.data
|
|
19
|
+
batch_size = x.shape[0]
|
|
20
|
+
|
|
21
|
+
max_logits = np.max(x, axis=1, keepdims=True)
|
|
22
|
+
shifted_logits = x - max_logits
|
|
23
|
+
|
|
24
|
+
#знаменатель софтмакс
|
|
25
|
+
exp_shifted = np.exp(shifted_logits)
|
|
26
|
+
sums_exp = np.sum(exp_shifted, axis=1, keepdims=True)
|
|
27
|
+
softmax = exp_shifted / sums_exp
|
|
28
|
+
|
|
29
|
+
#подсчет лог
|
|
30
|
+
#эпсилон чтоб на 0 не поделилось случайно
|
|
31
|
+
log_softmax = shifted_logits - np.log(sums_exp + self.eps)
|
|
32
|
+
|
|
33
|
+
if targets.data.ndim == 1 or targets.data.shape[1] == 1:
|
|
34
|
+
target_indices = targets.data.astype(np.int64).ravel()
|
|
35
|
+
correct_log_probs = log_softmax[np.arange(batch_size), target_indices]
|
|
36
|
+
one_hot = np.zeros_like(x)
|
|
37
|
+
one_hot[np.arange(batch_size), target_indices] = 1.0
|
|
38
|
+
|
|
39
|
+
else:
|
|
40
|
+
correct_log_probs = np.sum(log_softmax * targets.data, axis=1)
|
|
41
|
+
one_hot = targets.data
|
|
42
|
+
|
|
43
|
+
loss = -np.mean(correct_log_probs)
|
|
44
|
+
|
|
45
|
+
out = Tensor(loss, requires_grad=logits.requires_grad, device=logits.device)
|
|
46
|
+
|
|
47
|
+
if out.requires_grad:
|
|
48
|
+
out._prev = {logits}
|
|
49
|
+
|
|
50
|
+
def _backward():
|
|
51
|
+
if logits.grad is None:
|
|
52
|
+
logits.grad = np.zeros_like(logits.data, dtype=np.float32)
|
|
53
|
+
logits.grad += (softmax - one_hot) / batch_size * out.grad
|
|
54
|
+
|
|
55
|
+
out._backward = _backward
|
|
56
|
+
|
|
57
|
+
return out
|
|
58
|
+
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from tinytensor.core.tensor import Tensor
|
|
3
|
+
from tinytensor.nn.modules import Module
|
|
4
|
+
from tinytensor.nn.functional import im2col_indices, col2_im_indices
|
|
5
|
+
|
|
6
|
+
class MaxPool2d(Module):
|
|
7
|
+
def __init__(self, kernel_size=2, stride=2, padding=0):
|
|
8
|
+
super().__init__()
|
|
9
|
+
if isinstance(kernel_size, int):
|
|
10
|
+
self.kh = self.kw = kernel_size
|
|
11
|
+
# если кортедж
|
|
12
|
+
else:
|
|
13
|
+
self.kh, self.kw = kernel_size
|
|
14
|
+
|
|
15
|
+
self.stride = stride
|
|
16
|
+
self.padding = padding
|
|
17
|
+
|
|
18
|
+
def forward(self,x):
|
|
19
|
+
N, C, H, W = x.data.shape
|
|
20
|
+
# меняем размерность
|
|
21
|
+
x_reshaped = x.data.reshape(N * C, 1, H, W)
|
|
22
|
+
x_col, out_h, out_w = im2col_indices(
|
|
23
|
+
x_reshaped, self.kh, self.kw, padding=self.padding, stride=self.stride
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
#находим максимумы
|
|
27
|
+
max_idx = np.argmax(x_col, axis=0)
|
|
28
|
+
out = x_col[max_idx, np.arange(x_col.shape[1])]
|
|
29
|
+
|
|
30
|
+
#сново меняем размеры
|
|
31
|
+
out = out.reshape(out_h, out_w, N, C)
|
|
32
|
+
out = out.transpose(2, 3, 0, 1)
|
|
33
|
+
|
|
34
|
+
out_t = Tensor(out, requires_grad=x.requires_grad, device=x.device)
|
|
35
|
+
|
|
36
|
+
if out_t.requires_grad:
|
|
37
|
+
out_t._prev = {x}
|
|
38
|
+
|
|
39
|
+
def _backward():
|
|
40
|
+
dout = out_t.grad
|
|
41
|
+
dout_flat = dout.transpose(2, 3, 0, 1).reshape(-1)
|
|
42
|
+
|
|
43
|
+
dx_col = np.zeros_like(x_col)
|
|
44
|
+
dx_col[max_idx, np.arange(max_idx.size)] = dout_flat
|
|
45
|
+
|
|
46
|
+
dx_reshaped = col2_im_indices(
|
|
47
|
+
dx_col, x_reshaped.shape, self.kh, self.kw,
|
|
48
|
+
padding=self.padding, stride=self.stride
|
|
49
|
+
)
|
|
50
|
+
dx = dx_reshaped.reshape(N, C, H, W)
|
|
51
|
+
|
|
52
|
+
if x.grad is None:
|
|
53
|
+
x.grad = np.zeros_like(x.data, dtype=np.float32)
|
|
54
|
+
x.grad += dx
|
|
55
|
+
|
|
56
|
+
out_t._backward = _backward
|
|
57
|
+
|
|
58
|
+
return out_t
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class AvgPool2d(Module):
|
|
62
|
+
def __init__(self, kernel_size=2, stride=2, padding=0):
|
|
63
|
+
super().__init__()
|
|
64
|
+
if isinstance(kernel_size, int):
|
|
65
|
+
self.kh = self.kw = kernel_size
|
|
66
|
+
# если кортедж
|
|
67
|
+
else:
|
|
68
|
+
self.kh, self.kw = kernel_size
|
|
69
|
+
|
|
70
|
+
self.stride = stride
|
|
71
|
+
self.padding = padding
|
|
72
|
+
|
|
73
|
+
def forward(self,x):
|
|
74
|
+
N, C, H, W = x.data.shape
|
|
75
|
+
# меняем размерность
|
|
76
|
+
x_reshaped = x.data.reshape(N * C, 1, H, W)
|
|
77
|
+
x_col, out_h, out_w = im2col_indices(
|
|
78
|
+
x_reshaped, self.kh, self.kw, padding=self.padding, stride=self.stride
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
#находим средние
|
|
82
|
+
out = np.mean(x_col, axis=0)
|
|
83
|
+
|
|
84
|
+
#сново меняем размеры
|
|
85
|
+
out = out.reshape(out_h, out_w, N, C)
|
|
86
|
+
out = out.transpose(2, 3, 0, 1)
|
|
87
|
+
|
|
88
|
+
out_t = Tensor(out, requires_grad=x.requires_grad, device=x.device)
|
|
89
|
+
|
|
90
|
+
if out_t.requires_grad:
|
|
91
|
+
out_t._prev = {x}
|
|
92
|
+
|
|
93
|
+
def _backward():
|
|
94
|
+
dout = out_t.grad
|
|
95
|
+
dout_flat = dout.transpose(2, 3, 0, 1).reshape(-1)
|
|
96
|
+
|
|
97
|
+
dx_col = np.repeat((dout_flat / (self.kh * self.kw))[None, :], self.kh * self.kw, axis=0)
|
|
98
|
+
|
|
99
|
+
dx_reshaped = col2_im_indices(
|
|
100
|
+
dx_col, x_reshaped.shape, self.kh, self.kw,
|
|
101
|
+
padding=self.padding, stride=self.stride
|
|
102
|
+
)
|
|
103
|
+
dx = dx_reshaped.reshape(N, C, H, W)
|
|
104
|
+
|
|
105
|
+
if x.grad is None:
|
|
106
|
+
x.grad = np.zeros_like(x.data, dtype=np.float32)
|
|
107
|
+
x.grad += dx
|
|
108
|
+
|
|
109
|
+
out_t._backward = _backward
|
|
110
|
+
|
|
111
|
+
return out_t
|
|
112
|
+
|
|
113
|
+
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from tinytensor.core.tensor import Tensor
|
|
3
|
+
from tinytensor.nn.modules import Module
|
|
4
|
+
|
|
5
|
+
class RNNCell(Module):
|
|
6
|
+
def __init__(self,input_size: int, hidden_size: int):
|
|
7
|
+
super().__init__()
|
|
8
|
+
self.input_size = input_size
|
|
9
|
+
self.hidden_size = hidden_size
|
|
10
|
+
|
|
11
|
+
std = 1.0 / np.sqrt(hidden_size)
|
|
12
|
+
w_ih = np.random.uniform(-std, std, (hidden_size, input_size)).astype(np.float32)
|
|
13
|
+
b_ih = np.zeros((hidden_size,), dtype=np.float32)
|
|
14
|
+
|
|
15
|
+
#веса скрытог состояния
|
|
16
|
+
w_hh = np.random.uniform(-std, std, (hidden_size, hidden_size)).astype(np.float32)
|
|
17
|
+
b_hh = np.zeros((hidden_size,), dtype=np.float32)
|
|
18
|
+
|
|
19
|
+
self.weight_ih = Tensor(w_ih, requires_grad=True)
|
|
20
|
+
self.bias_ih = Tensor(b_ih, requires_grad=True)
|
|
21
|
+
|
|
22
|
+
self.weight_hh = Tensor(w_hh, requires_grad=True)
|
|
23
|
+
self.bias_hh = Tensor(b_hh, requires_grad=True)
|
|
24
|
+
|
|
25
|
+
def forward(self, x, h=None):
|
|
26
|
+
batch_size = x.data.shape[0]
|
|
27
|
+
|
|
28
|
+
if h is None:
|
|
29
|
+
h = Tensor(np.zeros((batch_size, self.hidden_size), dtype=np.float32))
|
|
30
|
+
|
|
31
|
+
# h_n=tanh(x@W_ih.T+ b_ih+h@ W_hh.T+b_hh)
|
|
32
|
+
gate_x = x.data @ self.weight_ih.data.T + self.bias_ih.data
|
|
33
|
+
gate_h = h.data @ self.weight_hh.data.T + self.bias_hh.data
|
|
34
|
+
|
|
35
|
+
h_next_data = np.tanh(gate_x + gate_h)
|
|
36
|
+
|
|
37
|
+
out = Tensor(
|
|
38
|
+
h_next_data,
|
|
39
|
+
requires_grad=(
|
|
40
|
+
x.requires_grad or h.requires_grad
|
|
41
|
+
or self.weight_ih.requires_grad or self.bias_ih.requires_grad
|
|
42
|
+
or self.weight_hh.requires_grad or self.bias_hh.requires_grad
|
|
43
|
+
),
|
|
44
|
+
device=x.device,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
if out.requires_grad:
|
|
48
|
+
out._prev = {x, h, self.weight_ih, self.bias_ih, self.weight_hh, self.bias_hh}
|
|
49
|
+
|
|
50
|
+
def _backward():
|
|
51
|
+
dgate = out.grad * (1.0 - h_next_data ** 2)
|
|
52
|
+
|
|
53
|
+
if self.weight_ih.requires_grad:
|
|
54
|
+
if self.weight_ih.grad is None:
|
|
55
|
+
self.weight_ih.grad = np.zeros_like(self.weight_ih.data, dtype=np.float32)
|
|
56
|
+
self.weight_ih.grad += dgate.T @ x.data
|
|
57
|
+
|
|
58
|
+
if self.bias_ih.requires_grad:
|
|
59
|
+
if self.bias_ih.grad is None:
|
|
60
|
+
self.bias_ih.grad = np.zeros_like(self.bias_ih.data, dtype=np.float32)
|
|
61
|
+
self.bias_ih.grad += np.sum(dgate, axis=0)
|
|
62
|
+
|
|
63
|
+
if self.weight_hh.requires_grad:
|
|
64
|
+
if self.weight_hh.grad is None:
|
|
65
|
+
self.weight_hh.grad = np.zeros_like(self.weight_hh.data, dtype=np.float32)
|
|
66
|
+
self.weight_hh.grad += dgate.T @ h.data
|
|
67
|
+
|
|
68
|
+
if self.bias_hh.requires_grad:
|
|
69
|
+
if self.bias_hh.grad is None:
|
|
70
|
+
self.bias_hh.grad = np.zeros_like(self.bias_hh.data, dtype=np.float32)
|
|
71
|
+
self.bias_hh.grad += np.sum(dgate, axis=0)
|
|
72
|
+
|
|
73
|
+
if x.requires_grad:
|
|
74
|
+
if x.grad is None:
|
|
75
|
+
x.grad = np.zeros_like(x.data, dtype=np.float32)
|
|
76
|
+
x.grad += dgate @ self.weight_ih.data
|
|
77
|
+
|
|
78
|
+
if h.requires_grad:
|
|
79
|
+
if h.grad is None:
|
|
80
|
+
h.grad = np.zeros_like(h.data, dtype=np.float32)
|
|
81
|
+
h.grad += dgate @ self.weight_hh.data
|
|
82
|
+
|
|
83
|
+
out._backward = _backward
|
|
84
|
+
|
|
85
|
+
return out
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class RNN(Module):
|
|
89
|
+
def __init__(self, input_size: int, hidden_size: int):
|
|
90
|
+
super().__init__()
|
|
91
|
+
self.cell = RNNCell(input_size, hidden_size)
|
|
92
|
+
|
|
93
|
+
def forward(self, x, h_0=None):
|
|
94
|
+
#x.shape->(batch sSize, Seq len,input size)
|
|
95
|
+
batch_size, seq_len, _ = x.data.shape
|
|
96
|
+
|
|
97
|
+
h = h_0
|
|
98
|
+
outputs = []
|
|
99
|
+
|
|
100
|
+
#по циклу по времени т
|
|
101
|
+
for t in range(seq_len):
|
|
102
|
+
x_t = Tensor(x.data[:, t, :], requires_grad=x.requires_grad)
|
|
103
|
+
|
|
104
|
+
if x.requires_grad:
|
|
105
|
+
x_t._prev = {x}
|
|
106
|
+
|
|
107
|
+
def make_backward(t, x_t):
|
|
108
|
+
def _backward():
|
|
109
|
+
if x.grad is None:
|
|
110
|
+
x.grad = np.zeros_like(x.data, dtype=np.float32)
|
|
111
|
+
x.grad[:, t, :] += x_t.grad
|
|
112
|
+
return _backward
|
|
113
|
+
|
|
114
|
+
x_t._backward = make_backward(t, x_t)
|
|
115
|
+
|
|
116
|
+
h = self.cell(x_t, h)
|
|
117
|
+
outputs.append(h)
|
|
118
|
+
|
|
119
|
+
#сборка
|
|
120
|
+
out_data = np.stack([o.data for o in outputs], axis=1)
|
|
121
|
+
|
|
122
|
+
out = Tensor(
|
|
123
|
+
out_data,
|
|
124
|
+
requires_grad=any(o.requires_grad for o in outputs),
|
|
125
|
+
device=x.device,
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
if out.requires_grad:
|
|
129
|
+
out._prev = set(outputs)
|
|
130
|
+
|
|
131
|
+
def _backward():
|
|
132
|
+
for t, o in enumerate(outputs):
|
|
133
|
+
if o.requires_grad:
|
|
134
|
+
if o.grad is None:
|
|
135
|
+
o.grad = np.zeros_like(o.data, dtype=np.float32)
|
|
136
|
+
o.grad += out.grad[:, t, :]
|
|
137
|
+
|
|
138
|
+
out._backward = _backward
|
|
139
|
+
|
|
140
|
+
return out, h
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
from tinytensor.nn.modules import Module, Sequential
|
|
2
|
-
from tinytensor.nn.linear import Linear
|
|
3
|
-
from tinytensor.nn.activations import ReLU, LeReLU, Sigmod, Tanh, GELU
|
|
4
|
-
from tinytensor.nn.dropout import Dropout
|
|
5
|
-
from tinytensor.nn.losses import MSELoss
|
|
6
|
-
|
|
7
|
-
__all__ = [
|
|
8
|
-
"Module",
|
|
9
|
-
"Sequential",
|
|
10
|
-
"Linear",
|
|
11
|
-
"ReLU", "LeReLU", "Sigmod", "Tanh", "GELU",
|
|
12
|
-
"Dropout",
|
|
13
|
-
"MSELoss"
|
|
14
|
-
]
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
from tinytensor.nn.modules import Module
|
|
2
|
-
|
|
3
|
-
class ReLU(Module):
|
|
4
|
-
def forward(self, x):
|
|
5
|
-
return x.relu()
|
|
6
|
-
|
|
7
|
-
class LeReLU(Module):
|
|
8
|
-
# у нас есть тут альфа
|
|
9
|
-
def __init__(self, alpha=0.01):
|
|
10
|
-
super().__init__()
|
|
11
|
-
self.alpha = alpha
|
|
12
|
-
|
|
13
|
-
def forward(self, x):
|
|
14
|
-
return x.leaky_relu(alpha=self.alpha)
|
|
15
|
-
|
|
16
|
-
class Sigmod(Module):
|
|
17
|
-
def forward(self, x):
|
|
18
|
-
return x.sigmoid()
|
|
19
|
-
|
|
20
|
-
class Tanh(Module):
|
|
21
|
-
def forward(self, x):
|
|
22
|
-
return x.tanh()
|
|
23
|
-
|
|
24
|
-
class GELU(Module):
|
|
25
|
-
def forward(self, x):
|
|
26
|
-
return x.gelu()
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
# ToDo:
|
|
32
|
-
# ReLU [x]
|
|
33
|
-
# LeReLU [x]
|
|
34
|
-
# Sigmoid [x]
|
|
35
|
-
# Tanh [x]
|
|
36
|
-
# GELU [x]
|
|
37
|
-
# kama soska
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import numpy as np
|
|
2
|
-
from tinytensor.nn.modules import Module
|
|
3
|
-
# 1/n * sum((n, i = 1), yi - y`i)^2
|
|
4
|
-
class MSELoss(Module):
|
|
5
|
-
def forward(self, y_pred, y_true):
|
|
6
|
-
diff = y_pred - y_true
|
|
7
|
-
sq_diff = diff ** 2
|
|
8
|
-
return sq_diff.sum() * (1.0/y_pred.data.size)
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
# мб в будущем добавлю cross entropy loss
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|