deeplotx 0.4.12b2__py3-none-any.whl → 0.4.12b3__py3-none-any.whl
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.
- deeplotx/encoder/bert_encoder.py +2 -4
- deeplotx/encoder/longformer_encoder.py +2 -4
- deeplotx/nn/auto_regression.py +5 -2
- deeplotx/nn/base_neural_network.py +12 -1
- deeplotx/nn/linear_regression.py +16 -14
- deeplotx/nn/logistic_regression.py +4 -2
- deeplotx/nn/recursive_sequential.py +11 -5
- deeplotx/nn/softmax_regression.py +5 -3
- deeplotx/trainer/text_binary_classification_trainer.py +2 -2
- {deeplotx-0.4.12b2.dist-info → deeplotx-0.4.12b3.dist-info}/METADATA +1 -1
- {deeplotx-0.4.12b2.dist-info → deeplotx-0.4.12b3.dist-info}/RECORD +14 -14
- {deeplotx-0.4.12b2.dist-info → deeplotx-0.4.12b3.dist-info}/WHEEL +0 -0
- {deeplotx-0.4.12b2.dist-info → deeplotx-0.4.12b3.dist-info}/licenses/LICENSE +0 -0
- {deeplotx-0.4.12b2.dist-info → deeplotx-0.4.12b3.dist-info}/top_level.txt +0 -0
deeplotx/encoder/bert_encoder.py
CHANGED
@@ -29,8 +29,6 @@ class BertEncoder(nn.Module):
|
|
29
29
|
def _encoder(_input_tup: tuple[torch.Tensor, torch.Tensor]) -> torch.Tensor:
|
30
30
|
return self.bert.forward(_input_tup[0], attention_mask=_input_tup[1]).last_hidden_state[:, 0, :]
|
31
31
|
|
32
|
-
input_ids = input_ids.to(self.device)
|
33
|
-
attention_mask = attention_mask.to(self.device)
|
34
32
|
num_chunks = math.ceil(input_ids.shape[-1] / self.embed_dim)
|
35
33
|
chunks = chunk_results = []
|
36
34
|
for i in range(num_chunks):
|
@@ -45,6 +43,6 @@ class BertEncoder(nn.Module):
|
|
45
43
|
return torch.cat(chunk_results, dim=-1)
|
46
44
|
|
47
45
|
def encode(self, text: str) -> torch.Tensor:
|
48
|
-
_input_ids = torch.tensor([self.tokenizer.encode(text)], dtype=torch.long)
|
49
|
-
_att_mask = torch.tensor([[1] * _input_ids.shape[-1]], dtype=torch.int)
|
46
|
+
_input_ids = torch.tensor([self.tokenizer.encode(text)], dtype=torch.long, device=self.device)
|
47
|
+
_att_mask = torch.tensor([[1] * _input_ids.shape[-1]], dtype=torch.int, device=self.device)
|
50
48
|
return self.forward(_input_ids, _att_mask).squeeze()
|
@@ -24,8 +24,6 @@ class LongformerEncoder(nn.Module):
|
|
24
24
|
logger.debug(f'{LongformerEncoder.__name__} initialized on device: {self.device}.')
|
25
25
|
|
26
26
|
def forward(self, input_ids: torch.Tensor, attention_mask: torch.Tensor) -> torch.Tensor:
|
27
|
-
input_ids = input_ids.to(self.device)
|
28
|
-
attention_mask = attention_mask.to(self.device)
|
29
27
|
ori_mode = self.bert.training
|
30
28
|
self.bert.eval()
|
31
29
|
with torch.no_grad():
|
@@ -34,6 +32,6 @@ class LongformerEncoder(nn.Module):
|
|
34
32
|
return res
|
35
33
|
|
36
34
|
def encode(self, text: str) -> torch.Tensor:
|
37
|
-
_input_ids = torch.tensor([self.tokenizer.encode(text)], dtype=torch.long)
|
38
|
-
_att_mask = torch.tensor([[1] * _input_ids.shape[-1]], dtype=torch.int)
|
35
|
+
_input_ids = torch.tensor([self.tokenizer.encode(text)], dtype=torch.long, device=self.device)
|
36
|
+
_att_mask = torch.tensor([[1] * _input_ids.shape[-1]], dtype=torch.int, device=self.device)
|
39
37
|
return self.forward(_input_ids, _att_mask).squeeze()
|
deeplotx/nn/auto_regression.py
CHANGED
@@ -1,9 +1,12 @@
|
|
1
|
+
import torch
|
2
|
+
|
1
3
|
from deeplotx.nn import RecursiveSequential
|
2
4
|
|
3
5
|
|
4
6
|
class AutoRegression(RecursiveSequential):
|
5
7
|
def __init__(self, feature_dim: int, hidden_dim: int | None = None,
|
6
|
-
recursive_layers: int = 2, model_name: str | None = None
|
8
|
+
recursive_layers: int = 2, model_name: str | None = None,
|
9
|
+
device: str | None = None, dtype: torch.dtype | None = None):
|
7
10
|
super().__init__(input_dim=feature_dim, output_dim=feature_dim,
|
8
11
|
hidden_dim=hidden_dim, recursive_layers=recursive_layers,
|
9
|
-
model_name=model_name)
|
12
|
+
model_name=model_name, device=device, dtype=dtype)
|
@@ -5,11 +5,22 @@ from torch import nn
|
|
5
5
|
|
6
6
|
|
7
7
|
class BaseNeuralNetwork(nn.Module):
|
8
|
-
def __init__(self, model_name: str | None = None):
|
8
|
+
def __init__(self, model_name: str | None = None, device: str | None = None, dtype: torch.dtype | None = None):
|
9
9
|
super().__init__()
|
10
10
|
self._model_name = model_name \
|
11
11
|
if model_name is not None \
|
12
12
|
else self.__class__.__name__
|
13
|
+
self.device = torch.device(device) if device is not None \
|
14
|
+
else torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
15
|
+
self.dtype = dtype if dtype is not None else torch.float32
|
16
|
+
|
17
|
+
@staticmethod
|
18
|
+
def ensure_device_and_dtype(x: torch.Tensor, device: torch.device, dtype: torch.dtype) -> torch.Tensor:
|
19
|
+
if x.device != device:
|
20
|
+
x = x.to(device)
|
21
|
+
if x.dtype != dtype:
|
22
|
+
x = x.to(dtype)
|
23
|
+
return x
|
13
24
|
|
14
25
|
def l1(self, _lambda: float = 1e-4) -> torch.Tensor:
|
15
26
|
def _l1() -> torch.Tensor:
|
deeplotx/nn/linear_regression.py
CHANGED
@@ -7,26 +7,28 @@ from deeplotx.nn.base_neural_network import BaseNeuralNetwork
|
|
7
7
|
|
8
8
|
|
9
9
|
class LinearRegression(BaseNeuralNetwork):
|
10
|
-
def __init__(self, input_dim: int, output_dim: int, model_name: str | None = None
|
11
|
-
|
12
|
-
|
13
|
-
self.
|
14
|
-
self.
|
15
|
-
self.
|
16
|
-
self.
|
17
|
-
self.
|
18
|
-
self.
|
19
|
-
self.
|
20
|
-
self.
|
21
|
-
self.
|
10
|
+
def __init__(self, input_dim: int, output_dim: int, model_name: str | None = None,
|
11
|
+
device: str | None = None, dtype: torch.dtype | None = None):
|
12
|
+
super().__init__(model_name=model_name, device=device, dtype=dtype)
|
13
|
+
self.fc1 = nn.Linear(input_dim, 1024, device=self.device, dtype=self.dtype)
|
14
|
+
self.fc1_to_fc4_res = nn.Linear(1024, 64, device=self.device, dtype=self.dtype)
|
15
|
+
self.fc2 = nn.Linear(1024, 768, device=self.device, dtype=self.dtype)
|
16
|
+
self.fc3 = nn.Linear(768, 128, device=self.device, dtype=self.dtype)
|
17
|
+
self.fc4 = nn.Linear(128, 64, device=self.device, dtype=self.dtype)
|
18
|
+
self.fc5 = nn.Linear(64, output_dim, device=self.device, dtype=self.dtype)
|
19
|
+
self.parametric_relu_1 = nn.PReLU(num_parameters=1, init=5e-3, device=self.device, dtype=self.dtype)
|
20
|
+
self.parametric_relu_2 = nn.PReLU(num_parameters=1, init=5e-3, device=self.device, dtype=self.dtype)
|
21
|
+
self.parametric_relu_3 = nn.PReLU(num_parameters=1, init=5e-3, device=self.device, dtype=self.dtype)
|
22
|
+
self.parametric_relu_4 = nn.PReLU(num_parameters=1, init=5e-3, device=self.device, dtype=self.dtype)
|
22
23
|
|
23
24
|
@override
|
24
25
|
def forward(self, x) -> torch.Tensor:
|
26
|
+
x = self.ensure_device_and_dtype(x, device=self.device, dtype=self.dtype)
|
25
27
|
fc1_out = self.parametric_relu_1(self.fc1(x))
|
26
|
-
x = nn.LayerNorm(normalized_shape=1024, eps=1e-9)(fc1_out)
|
28
|
+
x = nn.LayerNorm(normalized_shape=1024, eps=1e-9, device=self.device, dtype=self.dtype)(fc1_out)
|
27
29
|
x = torch.dropout(x, p=0.2, train=self.training)
|
28
30
|
x = self.parametric_relu_2(self.fc2(x))
|
29
|
-
x = nn.LayerNorm(normalized_shape=768, eps=1e-9)(x)
|
31
|
+
x = nn.LayerNorm(normalized_shape=768, eps=1e-9, device=self.device, dtype=self.dtype)(x)
|
30
32
|
x = torch.dropout(x, p=0.2, train=self.training)
|
31
33
|
x = self.parametric_relu_3(self.fc3(x))
|
32
34
|
x = torch.dropout(x, p=0.2, train=self.training)
|
@@ -6,9 +6,11 @@ from deeplotx.nn.linear_regression import LinearRegression
|
|
6
6
|
|
7
7
|
|
8
8
|
class LogisticRegression(LinearRegression):
|
9
|
-
def __init__(self, input_dim: int, output_dim: int = 1, model_name: str | None = None
|
10
|
-
|
9
|
+
def __init__(self, input_dim: int, output_dim: int = 1, model_name: str | None = None,
|
10
|
+
device: str | None = None, dtype: torch.dtype | None = None):
|
11
|
+
super().__init__(input_dim=input_dim, output_dim=output_dim, model_name=model_name, device=device, dtype=dtype)
|
11
12
|
|
12
13
|
@override
|
13
14
|
def forward(self, x) -> torch.Tensor:
|
15
|
+
x = self.ensure_device_and_dtype(x, device=self.device, dtype=self.dtype)
|
14
16
|
return torch.sigmoid(super().forward(x))
|
@@ -10,21 +10,27 @@ from deeplotx.nn import LinearRegression
|
|
10
10
|
class RecursiveSequential(BaseNeuralNetwork):
|
11
11
|
def __init__(self, input_dim: int, output_dim: int,
|
12
12
|
hidden_dim: int | None = None, recursive_layers: int = 2,
|
13
|
-
model_name: str | None = None
|
14
|
-
|
13
|
+
model_name: str | None = None, device: str | None = None,
|
14
|
+
dtype: torch.dtype | None = None):
|
15
|
+
super().__init__(model_name=model_name, device=device, dtype=dtype)
|
15
16
|
if hidden_dim is None:
|
16
17
|
hidden_dim = input_dim
|
17
18
|
self.lstm = nn.LSTM(input_size=input_dim, hidden_size=hidden_dim,
|
18
19
|
num_layers=recursive_layers, batch_first=True,
|
19
|
-
bias=True, bidirectional=True
|
20
|
-
|
20
|
+
bias=True, bidirectional=True, device=self.device,
|
21
|
+
dtype=self.dtype)
|
22
|
+
self.regressive_head = LinearRegression(input_dim=hidden_dim * 2, output_dim=output_dim,
|
23
|
+
device=self.device, dtype=self.dtype)
|
21
24
|
|
22
25
|
def initial_state(self, batch_size: int = 1) -> tuple[torch.Tensor, torch.Tensor]:
|
23
|
-
zeros = torch.zeros(self.lstm.num_layers * 2, batch_size, self.lstm.hidden_size
|
26
|
+
zeros = torch.zeros(self.lstm.num_layers * 2, batch_size, self.lstm.hidden_size, device=self.device, dtype=self.dtype)
|
24
27
|
return zeros, zeros
|
25
28
|
|
26
29
|
@override
|
27
30
|
def forward(self, x, state: tuple[torch.Tensor, torch.Tensor]) -> tuple[torch.Tensor, tuple[torch.Tensor, torch.Tensor]]:
|
31
|
+
x = self.ensure_device_and_dtype(x, device=self.device, dtype=self.dtype)
|
32
|
+
state = (self.ensure_device_and_dtype(state[0], device=self.device, dtype=self.dtype),
|
33
|
+
self.ensure_device_and_dtype(state[1], device=self.device, dtype=self.dtype))
|
28
34
|
x, (hidden_state, cell_state) = self.lstm(x, state)
|
29
35
|
x = self.regressive_head(x[:, -1, :])
|
30
36
|
return x, (hidden_state, cell_state)
|
@@ -6,9 +6,11 @@ from deeplotx.nn.linear_regression import LinearRegression
|
|
6
6
|
|
7
7
|
|
8
8
|
class SoftmaxRegression(LinearRegression):
|
9
|
-
def __init__(self, input_dim: int, output_dim: int, model_name: str | None = None
|
10
|
-
|
9
|
+
def __init__(self, input_dim: int, output_dim: int, model_name: str | None = None,
|
10
|
+
device: str | None = None, dtype: torch.dtype | None = None):
|
11
|
+
super().__init__(input_dim=input_dim, output_dim=output_dim, model_name=model_name, device=device, dtype=dtype)
|
11
12
|
|
12
13
|
@override
|
13
14
|
def forward(self, x) -> torch.Tensor:
|
14
|
-
|
15
|
+
x = self.ensure_device_and_dtype(x, device=self.device, dtype=self.dtype)
|
16
|
+
return torch.softmax(super().forward(x), dim=-1, dtype=self.dtype)
|
@@ -48,8 +48,8 @@ class TextBinaryClassifierTrainer(BaseTrainer):
|
|
48
48
|
if self.model is None:
|
49
49
|
self.model = RecursiveSequential(input_dim=feature_dim, output_dim=1,
|
50
50
|
hidden_dim=hidden_dim,
|
51
|
-
recursive_layers=recursive_layers
|
52
|
-
|
51
|
+
recursive_layers=recursive_layers,
|
52
|
+
device=self.device, dtype=dtype)
|
53
53
|
loss_function = nn.BCELoss()
|
54
54
|
optimizer = optim.Adamax(self.model.parameters(), lr=learning_rate)
|
55
55
|
for epoch in range(num_epochs):
|
@@ -1,27 +1,27 @@
|
|
1
1
|
deeplotx/__init__.py,sha256=wMN_AI14V-0BPbQghYpvd2y7eUGfhr7jKTTuur-5Upg,1002
|
2
2
|
deeplotx/encoder/__init__.py,sha256=EM-xrTsHoGaiiFpj-iFAxilMHXC_sQKWYrcq1qCnI3U,138
|
3
|
-
deeplotx/encoder/bert_encoder.py,sha256=
|
3
|
+
deeplotx/encoder/bert_encoder.py,sha256=uLqGcXH6AGY6CcjjbYbh09VWYqSpsg-y-jHYB6Fmp3w,2377
|
4
4
|
deeplotx/encoder/long_text_encoder.py,sha256=hl_O8kR9o1kcII9YfSx2rf_Pk0l_Rv7LNbsS9UsTU0c,3373
|
5
|
-
deeplotx/encoder/longformer_encoder.py,sha256=
|
5
|
+
deeplotx/encoder/longformer_encoder.py,sha256=A8FXqd4mdHxSn_o_R689XtpT73ISDT788EgMQRGLC2g,1822
|
6
6
|
deeplotx/nn/__init__.py,sha256=oQ-vYXyuaGelfCOs2im_gZXAiiBlCCVXh1uw9yjvRMs,253
|
7
|
-
deeplotx/nn/auto_regression.py,sha256=
|
8
|
-
deeplotx/nn/base_neural_network.py,sha256=
|
9
|
-
deeplotx/nn/linear_regression.py,sha256=
|
10
|
-
deeplotx/nn/logistic_regression.py,sha256=
|
11
|
-
deeplotx/nn/recursive_sequential.py,sha256=
|
12
|
-
deeplotx/nn/softmax_regression.py,sha256=
|
7
|
+
deeplotx/nn/auto_regression.py,sha256=7P63opWCWMqE2DigwbsL6kfXtFtJPz00Yo1RqflBz4A,572
|
8
|
+
deeplotx/nn/base_neural_network.py,sha256=yEyF5C-Z3bp4Ddx6GbvqpBxXyFdbSChmP6SgyTzjQmM,2180
|
9
|
+
deeplotx/nn/linear_regression.py,sha256=_LQFrOKBbQxvuNzb_B8Mr6PAQJUg-pFeu3h7_jQz04o,2166
|
10
|
+
deeplotx/nn/logistic_regression.py,sha256=j8QGe0e7In97RMOXApJRID85qf1rOUCOk3V368CBfqs,653
|
11
|
+
deeplotx/nn/recursive_sequential.py,sha256=YCQUUcTBsZUeyO7CLjUO1EISYX1SXPnW6asR6ZBQAb4,1926
|
12
|
+
deeplotx/nn/softmax_regression.py,sha256=SlhvHho-Oufp7adAjm1t1ygidu-FrnHQ9aleMXyS_s8,674
|
13
13
|
deeplotx/similarity/__init__.py,sha256=s3u-KSgxjnMcWpIItKgXNltFMPQ7YY3CqsqHI-5F1c8,724
|
14
14
|
deeplotx/similarity/distribution.py,sha256=wQGouuuW531pZeBRKBujXsdsoz4fDnPw7_GW81jwepc,1066
|
15
15
|
deeplotx/similarity/set.py,sha256=zhGFxtSIXlWqvipBYzoiPahp4g0boAIoUiMfG0wl07A,686
|
16
16
|
deeplotx/similarity/vector.py,sha256=WVbDHqykt-fvuILVrhUCtIFAOEjY_zvttrXGM9eylG0,1125
|
17
17
|
deeplotx/trainer/__init__.py,sha256=Fl5DR9UecQc5VtBcczU9sx_HtPNoFohpuELOh-Jrsks,77
|
18
18
|
deeplotx/trainer/base_trainer.py,sha256=z0MeAT-rRYmjeBXt0ckt7J1itYArR0Cx02wHesXUoZE,385
|
19
|
-
deeplotx/trainer/text_binary_classification_trainer.py,sha256=
|
19
|
+
deeplotx/trainer/text_binary_classification_trainer.py,sha256=Wq_pGO78zgdXxFeBjam4yp__-dTvsuwl4H81HSl_kjE,4691
|
20
20
|
deeplotx/util/__init__.py,sha256=JxqAK_WOOHcYVSTHBT1-WuBwWrPEVDTV3titeVWvNUM,74
|
21
21
|
deeplotx/util/hash.py,sha256=wwsC6kOQvbpuvwKsNQOARd78_wePmW9i3oaUuXRUnpc,352
|
22
22
|
deeplotx/util/read_file.py,sha256=ptzouvEQeeW8KU5BrWNJlXw-vFXVrpS9SkAUxsu6A8A,612
|
23
|
-
deeplotx-0.4.
|
24
|
-
deeplotx-0.4.
|
25
|
-
deeplotx-0.4.
|
26
|
-
deeplotx-0.4.
|
27
|
-
deeplotx-0.4.
|
23
|
+
deeplotx-0.4.12b3.dist-info/licenses/LICENSE,sha256=IwGE9guuL-ryRPEKi6wFPI_zOhg7zDZbTYuHbSt_SAk,35823
|
24
|
+
deeplotx-0.4.12b3.dist-info/METADATA,sha256=HZY8s697peX3onvVr5bjdHqwKJk2YyMOadMLGk-QvSc,6287
|
25
|
+
deeplotx-0.4.12b3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
26
|
+
deeplotx-0.4.12b3.dist-info/top_level.txt,sha256=hKg4pVDXZ-WWxkRfJFczRIll1Sv7VyfKCmzHLXbuh1U,9
|
27
|
+
deeplotx-0.4.12b3.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|