deeplotx 0.4.14__tar.gz → 0.5.1__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.
Files changed (35) hide show
  1. {deeplotx-0.4.14 → deeplotx-0.5.1}/PKG-INFO +21 -19
  2. {deeplotx-0.4.14 → deeplotx-0.5.1}/README.md +20 -18
  3. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/__init__.py +4 -1
  4. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/nn/__init__.py +3 -0
  5. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/nn/base_neural_network.py +11 -5
  6. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/nn/linear_regression.py +1 -1
  7. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/nn/logistic_regression.py +1 -1
  8. deeplotx-0.5.1/deeplotx/nn/long_context_auto_regression.py +12 -0
  9. deeplotx-0.5.1/deeplotx/nn/long_context_recursive_sequential.py +28 -0
  10. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/nn/recursive_sequential.py +2 -2
  11. deeplotx-0.5.1/deeplotx/nn/self_attention.py +34 -0
  12. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/nn/softmax_regression.py +1 -1
  13. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/trainer/text_binary_classification_trainer.py +8 -7
  14. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx.egg-info/PKG-INFO +21 -19
  15. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx.egg-info/SOURCES.txt +3 -0
  16. {deeplotx-0.4.14 → deeplotx-0.5.1}/pyproject.toml +1 -1
  17. {deeplotx-0.4.14 → deeplotx-0.5.1}/LICENSE +0 -0
  18. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/encoder/__init__.py +0 -0
  19. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/encoder/bert_encoder.py +0 -0
  20. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/encoder/long_text_encoder.py +0 -0
  21. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/encoder/longformer_encoder.py +0 -0
  22. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/nn/auto_regression.py +0 -0
  23. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/similarity/__init__.py +0 -0
  24. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/similarity/distribution.py +0 -0
  25. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/similarity/set.py +0 -0
  26. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/similarity/vector.py +0 -0
  27. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/trainer/__init__.py +0 -0
  28. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/trainer/base_trainer.py +0 -0
  29. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/util/__init__.py +0 -0
  30. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/util/hash.py +0 -0
  31. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx/util/read_file.py +0 -0
  32. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx.egg-info/dependency_links.txt +0 -0
  33. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx.egg-info/requires.txt +0 -0
  34. {deeplotx-0.4.14 → deeplotx-0.5.1}/deeplotx.egg-info/top_level.txt +0 -0
  35. {deeplotx-0.4.14 → deeplotx-0.5.1}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: deeplotx
3
- Version: 0.4.14
3
+ Version: 0.5.1
4
4
  Summary: Easy-2-use long text NLP toolkit.
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
@@ -177,31 +177,33 @@ Dynamic: license-file
177
177
 
178
178
  import torch
179
179
  from torch import nn
180
-
180
+
181
181
  from deeplotx.nn.base_neural_network import BaseNeuralNetwork
182
-
183
-
182
+
183
+
184
184
  class LinearRegression(BaseNeuralNetwork):
185
- def __init__(self, input_dim: int, output_dim: int, model_name: str | None = None):
186
- super().__init__(model_name=model_name)
187
- self.fc1 = nn.Linear(input_dim, 1024)
188
- self.fc1_to_fc4_res = nn.Linear(1024, 64)
189
- self.fc2 = nn.Linear(1024, 768)
190
- self.fc3 = nn.Linear(768, 128)
191
- self.fc4 = nn.Linear(128, 64)
192
- self.fc5 = nn.Linear(64, output_dim)
193
- self.parametric_relu_1 = nn.PReLU(num_parameters=1, init=5e-3)
194
- self.parametric_relu_2 = nn.PReLU(num_parameters=1, init=5e-3)
195
- self.parametric_relu_3 = nn.PReLU(num_parameters=1, init=5e-3)
196
- self.parametric_relu_4 = nn.PReLU(num_parameters=1, init=5e-3)
197
-
185
+ def __init__(self, input_dim: int, output_dim: int, model_name: str | None = None,
186
+ device: str | None = None, dtype: torch.dtype | None = None):
187
+ super().__init__(model_name=model_name, device=device, dtype=dtype)
188
+ self.fc1 = nn.Linear(input_dim, 1024, device=self.device, dtype=self.dtype)
189
+ self.fc1_to_fc4_res = nn.Linear(1024, 64, device=self.device, dtype=self.dtype)
190
+ self.fc2 = nn.Linear(1024, 768, device=self.device, dtype=self.dtype)
191
+ self.fc3 = nn.Linear(768, 128, device=self.device, dtype=self.dtype)
192
+ self.fc4 = nn.Linear(128, 64, device=self.device, dtype=self.dtype)
193
+ self.fc5 = nn.Linear(64, output_dim, device=self.device, dtype=self.dtype)
194
+ self.parametric_relu_1 = nn.PReLU(num_parameters=1, init=5e-3, device=self.device, dtype=self.dtype)
195
+ self.parametric_relu_2 = nn.PReLU(num_parameters=1, init=5e-3, device=self.device, dtype=self.dtype)
196
+ self.parametric_relu_3 = nn.PReLU(num_parameters=1, init=5e-3, device=self.device, dtype=self.dtype)
197
+ self.parametric_relu_4 = nn.PReLU(num_parameters=1, init=5e-3, device=self.device, dtype=self.dtype)
198
+
198
199
  @override
199
200
  def forward(self, x) -> torch.Tensor:
201
+ x = self.ensure_device_and_dtype(x, device=self.device, dtype=self.dtype)
200
202
  fc1_out = self.parametric_relu_1(self.fc1(x))
201
- x = nn.LayerNorm(normalized_shape=1024, eps=1e-9)(fc1_out)
203
+ x = nn.LayerNorm(normalized_shape=1024, eps=1e-9, device=self.device, dtype=self.dtype)(fc1_out)
202
204
  x = torch.dropout(x, p=0.2, train=self.training)
203
205
  x = self.parametric_relu_2(self.fc2(x))
204
- x = nn.LayerNorm(normalized_shape=768, eps=1e-9)(x)
206
+ x = nn.LayerNorm(normalized_shape=768, eps=1e-9, device=self.device, dtype=self.dtype)(x)
205
207
  x = torch.dropout(x, p=0.2, train=self.training)
206
208
  x = self.parametric_relu_3(self.fc3(x))
207
209
  x = torch.dropout(x, p=0.2, train=self.training)
@@ -160,31 +160,33 @@
160
160
 
161
161
  import torch
162
162
  from torch import nn
163
-
163
+
164
164
  from deeplotx.nn.base_neural_network import BaseNeuralNetwork
165
-
166
-
165
+
166
+
167
167
  class LinearRegression(BaseNeuralNetwork):
168
- def __init__(self, input_dim: int, output_dim: int, model_name: str | None = None):
169
- super().__init__(model_name=model_name)
170
- self.fc1 = nn.Linear(input_dim, 1024)
171
- self.fc1_to_fc4_res = nn.Linear(1024, 64)
172
- self.fc2 = nn.Linear(1024, 768)
173
- self.fc3 = nn.Linear(768, 128)
174
- self.fc4 = nn.Linear(128, 64)
175
- self.fc5 = nn.Linear(64, output_dim)
176
- self.parametric_relu_1 = nn.PReLU(num_parameters=1, init=5e-3)
177
- self.parametric_relu_2 = nn.PReLU(num_parameters=1, init=5e-3)
178
- self.parametric_relu_3 = nn.PReLU(num_parameters=1, init=5e-3)
179
- self.parametric_relu_4 = nn.PReLU(num_parameters=1, init=5e-3)
180
-
168
+ def __init__(self, input_dim: int, output_dim: int, model_name: str | None = None,
169
+ device: str | None = None, dtype: torch.dtype | None = None):
170
+ super().__init__(model_name=model_name, device=device, dtype=dtype)
171
+ self.fc1 = nn.Linear(input_dim, 1024, device=self.device, dtype=self.dtype)
172
+ self.fc1_to_fc4_res = nn.Linear(1024, 64, device=self.device, dtype=self.dtype)
173
+ self.fc2 = nn.Linear(1024, 768, device=self.device, dtype=self.dtype)
174
+ self.fc3 = nn.Linear(768, 128, device=self.device, dtype=self.dtype)
175
+ self.fc4 = nn.Linear(128, 64, device=self.device, dtype=self.dtype)
176
+ self.fc5 = nn.Linear(64, output_dim, device=self.device, dtype=self.dtype)
177
+ self.parametric_relu_1 = nn.PReLU(num_parameters=1, init=5e-3, device=self.device, dtype=self.dtype)
178
+ self.parametric_relu_2 = nn.PReLU(num_parameters=1, init=5e-3, device=self.device, dtype=self.dtype)
179
+ self.parametric_relu_3 = nn.PReLU(num_parameters=1, init=5e-3, device=self.device, dtype=self.dtype)
180
+ self.parametric_relu_4 = nn.PReLU(num_parameters=1, init=5e-3, device=self.device, dtype=self.dtype)
181
+
181
182
  @override
182
183
  def forward(self, x) -> torch.Tensor:
184
+ x = self.ensure_device_and_dtype(x, device=self.device, dtype=self.dtype)
183
185
  fc1_out = self.parametric_relu_1(self.fc1(x))
184
- x = nn.LayerNorm(normalized_shape=1024, eps=1e-9)(fc1_out)
186
+ x = nn.LayerNorm(normalized_shape=1024, eps=1e-9, device=self.device, dtype=self.dtype)(fc1_out)
185
187
  x = torch.dropout(x, p=0.2, train=self.training)
186
188
  x = self.parametric_relu_2(self.fc2(x))
187
- x = nn.LayerNorm(normalized_shape=768, eps=1e-9)(x)
189
+ x = nn.LayerNorm(normalized_shape=768, eps=1e-9, device=self.device, dtype=self.dtype)(x)
188
190
  x = torch.dropout(x, p=0.2, train=self.training)
189
191
  x = self.parametric_relu_3(self.fc3(x))
190
192
  x = torch.dropout(x, p=0.2, train=self.training)
@@ -9,7 +9,10 @@ from .nn import (
9
9
  LogisticRegression,
10
10
  SoftmaxRegression,
11
11
  RecursiveSequential,
12
- AutoRegression
12
+ LongContextRecursiveSequential,
13
+ SelfAttention,
14
+ AutoRegression,
15
+ LongContextAutoRegression
13
16
  )
14
17
  from .trainer import TextBinaryClassifierTrainer
15
18
 
@@ -2,4 +2,7 @@ from .linear_regression import LinearRegression
2
2
  from .logistic_regression import LogisticRegression
3
3
  from .softmax_regression import SoftmaxRegression
4
4
  from .recursive_sequential import RecursiveSequential
5
+ from .long_context_recursive_sequential import LongContextRecursiveSequential
6
+ from .self_attention import SelfAttention
5
7
  from .auto_regression import AutoRegression
8
+ from .long_context_auto_regression import LongContextAutoRegression
@@ -1,8 +1,11 @@
1
+ import os
1
2
  from abc import abstractmethod
2
3
 
3
4
  import torch
4
5
  from torch import nn
5
6
 
7
+ DEFAULT_SUFFIX = 'dlx'
8
+
6
9
 
7
10
  class BaseNeuralNetwork(nn.Module):
8
11
  def __init__(self, model_name: str | None = None, device: str | None = None, dtype: torch.dtype | None = None):
@@ -44,7 +47,7 @@ class BaseNeuralNetwork(nn.Module):
44
47
  @abstractmethod
45
48
  def forward(self, *args, **kwargs) -> torch.Tensor: ...
46
49
 
47
- def predict(self, x) -> torch.Tensor:
50
+ def predict(self, x: torch.Tensor) -> torch.Tensor:
48
51
  x = self.ensure_device_and_dtype(x, device=self.device, dtype=self.dtype)
49
52
  __train = self.training
50
53
  self.training = False
@@ -53,10 +56,13 @@ class BaseNeuralNetwork(nn.Module):
53
56
  self.training = __train
54
57
  return res
55
58
 
56
- def save(self):
57
- torch.save(self.state_dict(), f'{self._model_name}.deeplotx')
59
+ def save(self, model_name: str | None = None, model_dir: str = '.', _suffix: str = DEFAULT_SUFFIX):
60
+ os.makedirs(model_dir, exist_ok=True)
61
+ model_file_name = f'{model_name}.{_suffix}' if model_name is not None else f'{self._model_name}.{_suffix}'
62
+ torch.save(self.state_dict(), os.path.join(model_dir, model_file_name))
58
63
  return self
59
64
 
60
- def load(self):
61
- self.load_state_dict(torch.load(f'{self._model_name}.deeplotx', weights_only=True))
65
+ def load(self, model_name: str | None = None, model_dir: str = '.', _suffix: str = DEFAULT_SUFFIX):
66
+ model_file_name = f'{model_name}.{_suffix}' if model_name is not None else f'{self._model_name}.{_suffix}'
67
+ self.load_state_dict(torch.load(os.path.join(model_dir, model_file_name), map_location=self.device, weights_only=True))
62
68
  return self
@@ -22,7 +22,7 @@ class LinearRegression(BaseNeuralNetwork):
22
22
  self.parametric_relu_4 = nn.PReLU(num_parameters=1, init=5e-3, device=self.device, dtype=self.dtype)
23
23
 
24
24
  @override
25
- def forward(self, x) -> torch.Tensor:
25
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
26
26
  x = self.ensure_device_and_dtype(x, device=self.device, dtype=self.dtype)
27
27
  fc1_out = self.parametric_relu_1(self.fc1(x))
28
28
  x = nn.LayerNorm(normalized_shape=1024, eps=1e-9, device=self.device, dtype=self.dtype)(fc1_out)
@@ -11,6 +11,6 @@ class LogisticRegression(LinearRegression):
11
11
  super().__init__(input_dim=input_dim, output_dim=output_dim, model_name=model_name, device=device, dtype=dtype)
12
12
 
13
13
  @override
14
- def forward(self, x) -> torch.Tensor:
14
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
15
15
  x = self.ensure_device_and_dtype(x, device=self.device, dtype=self.dtype)
16
16
  return torch.sigmoid(super().forward(x))
@@ -0,0 +1,12 @@
1
+ import torch
2
+
3
+ from deeplotx.nn import LongContextRecursiveSequential
4
+
5
+
6
+ class LongContextAutoRegression(LongContextRecursiveSequential):
7
+ def __init__(self, feature_dim: int, hidden_dim: int | None = None,
8
+ recursive_layers: int = 2, model_name: str | None = None,
9
+ device: str | None = None, dtype: torch.dtype | None = None):
10
+ super().__init__(input_dim=feature_dim, output_dim=feature_dim,
11
+ hidden_dim=hidden_dim, recursive_layers=recursive_layers,
12
+ model_name=model_name, device=device, dtype=dtype)
@@ -0,0 +1,28 @@
1
+ from typing_extensions import override
2
+
3
+ import torch
4
+ from torch import nn
5
+
6
+ from deeplotx.nn.recursive_sequential import RecursiveSequential
7
+ from deeplotx.nn.self_attention import SelfAttention
8
+
9
+
10
+ class LongContextRecursiveSequential(RecursiveSequential):
11
+ def __init__(self, input_dim: int, output_dim: int,
12
+ hidden_dim: int | None = None, recursive_layers: int = 2,
13
+ model_name: str | None = None, device: str | None = None,
14
+ dtype: torch.dtype | None = None):
15
+ super().__init__(input_dim=input_dim, output_dim=output_dim,
16
+ hidden_dim=hidden_dim, recursive_layers=recursive_layers,
17
+ model_name=model_name, device=device, dtype=dtype)
18
+ self._feature_dim = input_dim
19
+ self.self_attention = SelfAttention(feature_dim=input_dim)
20
+ self.proj = nn.Linear(in_features=input_dim * 2, out_features=input_dim,
21
+ bias=True, device=self.device, dtype=self.dtype)
22
+
23
+ @override
24
+ def forward(self, x: torch.Tensor, state: tuple[torch.Tensor, torch.Tensor]) -> tuple[torch.Tensor, tuple[torch.Tensor, torch.Tensor]]:
25
+ x = self.ensure_device_and_dtype(x, device=self.device, dtype=self.dtype)
26
+ x = torch.cat([self.self_attention(x), x], dim=-1)
27
+ x = nn.LayerNorm(normalized_shape=x.shape[-1], eps=1e-9, device=self.device, dtype=self.dtype)(x)
28
+ return super().forward(self.proj(x), state)
@@ -27,7 +27,7 @@ class RecursiveSequential(BaseNeuralNetwork):
27
27
  return zeros, zeros
28
28
 
29
29
  @override
30
- def forward(self, x, state: tuple[torch.Tensor, torch.Tensor]) -> tuple[torch.Tensor, tuple[torch.Tensor, torch.Tensor]]:
30
+ def forward(self, x: torch.Tensor, state: tuple[torch.Tensor, torch.Tensor]) -> tuple[torch.Tensor, tuple[torch.Tensor, torch.Tensor]]:
31
31
  x = self.ensure_device_and_dtype(x, device=self.device, dtype=self.dtype)
32
32
  state = (self.ensure_device_and_dtype(state[0], device=self.device, dtype=self.dtype),
33
33
  self.ensure_device_and_dtype(state[1], device=self.device, dtype=self.dtype))
@@ -36,7 +36,7 @@ class RecursiveSequential(BaseNeuralNetwork):
36
36
  return x, (hidden_state, cell_state)
37
37
 
38
38
  @override
39
- def predict(self, x) -> torch.Tensor:
39
+ def predict(self, x: torch.Tensor) -> torch.Tensor:
40
40
  __train = self.training
41
41
  self.training = False
42
42
  with torch.no_grad():
@@ -0,0 +1,34 @@
1
+ from typing_extensions import override
2
+
3
+ import torch
4
+ from torch import nn, softmax
5
+
6
+ from deeplotx.nn.base_neural_network import BaseNeuralNetwork
7
+
8
+
9
+ class SelfAttention(BaseNeuralNetwork):
10
+ def __init__(self, feature_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._feature_dim = feature_dim
14
+ self.q_proj = nn.Linear(in_features=self._feature_dim, out_features=self._feature_dim,
15
+ bias=True, device=self.device, dtype=self.dtype)
16
+ self.k_proj = nn.Linear(in_features=self._feature_dim, out_features=self._feature_dim,
17
+ bias=True, device=self.device, dtype=self.dtype)
18
+ self.v_proj = nn.Linear(in_features=self._feature_dim, out_features=self._feature_dim,
19
+ bias=True, device=self.device, dtype=self.dtype)
20
+
21
+ def _attention(self, x: torch.Tensor, mask: torch.Tensor | None = None) -> torch.Tensor:
22
+ q, k = self.q_proj(x), self.k_proj(x)
23
+ attn = torch.matmul(q, k.transpose(-2, -1))
24
+ attn = attn / (self._feature_dim ** 0.5)
25
+ attn = attn.masked_fill(mask == 0, -1e9) if mask is not None else attn
26
+ return softmax(attn, dim=-1)
27
+
28
+ @override
29
+ def forward(self, x: torch.Tensor, mask: torch.Tensor | None = None) -> torch.Tensor:
30
+ x = self.ensure_device_and_dtype(x, device=self.device, dtype=self.dtype)
31
+ if mask is not None:
32
+ mask = self.ensure_device_and_dtype(mask, device=self.device, dtype=self.dtype)
33
+ v = self.v_proj(x)
34
+ return torch.matmul(self._attention(x, mask), v)
@@ -11,6 +11,6 @@ class SoftmaxRegression(LinearRegression):
11
11
  super().__init__(input_dim=input_dim, output_dim=output_dim, model_name=model_name, device=device, dtype=dtype)
12
12
 
13
13
  @override
14
- def forward(self, x) -> torch.Tensor:
14
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
15
15
  x = self.ensure_device_and_dtype(x, device=self.device, dtype=self.dtype)
16
16
  return torch.softmax(super().forward(x), dim=-1, dtype=self.dtype)
@@ -6,7 +6,7 @@ from torch import nn, optim
6
6
  from torch.utils.data import DataLoader, TensorDataset
7
7
 
8
8
  from deeplotx.encoder.long_text_encoder import LongTextEncoder
9
- from deeplotx.nn.recursive_sequential import RecursiveSequential
9
+ from deeplotx.nn.long_context_recursive_sequential import LongContextRecursiveSequential
10
10
  from deeplotx.trainer.base_trainer import BaseTrainer
11
11
 
12
12
  logger = logging.getLogger('deeplotx.trainer')
@@ -23,7 +23,7 @@ class TextBinaryClassifierTrainer(BaseTrainer):
23
23
  num_epochs: int, learning_rate: float = 2e-6, balancing_dataset: bool = True,
24
24
  train_loss_threshold: float = 0.0, valid_loss_threshold: float = 0.0,
25
25
  alpha: float = 1e-4, rho: float = 0.2,
26
- hidden_dim: int = 256, recursive_layers: int = 2) -> RecursiveSequential:
26
+ hidden_dim: int = 256, recursive_layers: int = 2) -> LongContextRecursiveSequential:
27
27
  if balancing_dataset:
28
28
  min_length = min(len(positive_texts), len(negative_texts))
29
29
  positive_texts = positive_texts[:min_length]
@@ -46,10 +46,10 @@ class TextBinaryClassifierTrainer(BaseTrainer):
46
46
  logger.warning("The dimension of features doesn't match. A new model instance will be created.")
47
47
  self.model = None
48
48
  if self.model is None:
49
- self.model = RecursiveSequential(input_dim=feature_dim, output_dim=1,
50
- hidden_dim=hidden_dim,
51
- recursive_layers=recursive_layers,
52
- device=self.device, dtype=dtype)
49
+ self.model = LongContextRecursiveSequential(input_dim=feature_dim, output_dim=1,
50
+ hidden_dim=hidden_dim,
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):
@@ -76,7 +76,8 @@ class TextBinaryClassifierTrainer(BaseTrainer):
76
76
  f"Valid Loss: {total_valid_loss:.4f}")
77
77
  if total_valid_loss < valid_loss_threshold:
78
78
  break
79
- logger.debug(f"Epoch {epoch + 1}/{num_epochs} | Train Loss: {total_loss:.4f}")
79
+ else:
80
+ logger.debug(f"Epoch {epoch + 1}/{num_epochs} | Train Loss: {total_loss:.4f}")
80
81
  if total_loss < train_loss_threshold:
81
82
  break
82
83
  return self.model
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: deeplotx
3
- Version: 0.4.14
3
+ Version: 0.5.1
4
4
  Summary: Easy-2-use long text NLP toolkit.
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
@@ -177,31 +177,33 @@ Dynamic: license-file
177
177
 
178
178
  import torch
179
179
  from torch import nn
180
-
180
+
181
181
  from deeplotx.nn.base_neural_network import BaseNeuralNetwork
182
-
183
-
182
+
183
+
184
184
  class LinearRegression(BaseNeuralNetwork):
185
- def __init__(self, input_dim: int, output_dim: int, model_name: str | None = None):
186
- super().__init__(model_name=model_name)
187
- self.fc1 = nn.Linear(input_dim, 1024)
188
- self.fc1_to_fc4_res = nn.Linear(1024, 64)
189
- self.fc2 = nn.Linear(1024, 768)
190
- self.fc3 = nn.Linear(768, 128)
191
- self.fc4 = nn.Linear(128, 64)
192
- self.fc5 = nn.Linear(64, output_dim)
193
- self.parametric_relu_1 = nn.PReLU(num_parameters=1, init=5e-3)
194
- self.parametric_relu_2 = nn.PReLU(num_parameters=1, init=5e-3)
195
- self.parametric_relu_3 = nn.PReLU(num_parameters=1, init=5e-3)
196
- self.parametric_relu_4 = nn.PReLU(num_parameters=1, init=5e-3)
197
-
185
+ def __init__(self, input_dim: int, output_dim: int, model_name: str | None = None,
186
+ device: str | None = None, dtype: torch.dtype | None = None):
187
+ super().__init__(model_name=model_name, device=device, dtype=dtype)
188
+ self.fc1 = nn.Linear(input_dim, 1024, device=self.device, dtype=self.dtype)
189
+ self.fc1_to_fc4_res = nn.Linear(1024, 64, device=self.device, dtype=self.dtype)
190
+ self.fc2 = nn.Linear(1024, 768, device=self.device, dtype=self.dtype)
191
+ self.fc3 = nn.Linear(768, 128, device=self.device, dtype=self.dtype)
192
+ self.fc4 = nn.Linear(128, 64, device=self.device, dtype=self.dtype)
193
+ self.fc5 = nn.Linear(64, output_dim, device=self.device, dtype=self.dtype)
194
+ self.parametric_relu_1 = nn.PReLU(num_parameters=1, init=5e-3, device=self.device, dtype=self.dtype)
195
+ self.parametric_relu_2 = nn.PReLU(num_parameters=1, init=5e-3, device=self.device, dtype=self.dtype)
196
+ self.parametric_relu_3 = nn.PReLU(num_parameters=1, init=5e-3, device=self.device, dtype=self.dtype)
197
+ self.parametric_relu_4 = nn.PReLU(num_parameters=1, init=5e-3, device=self.device, dtype=self.dtype)
198
+
198
199
  @override
199
200
  def forward(self, x) -> torch.Tensor:
201
+ x = self.ensure_device_and_dtype(x, device=self.device, dtype=self.dtype)
200
202
  fc1_out = self.parametric_relu_1(self.fc1(x))
201
- x = nn.LayerNorm(normalized_shape=1024, eps=1e-9)(fc1_out)
203
+ x = nn.LayerNorm(normalized_shape=1024, eps=1e-9, device=self.device, dtype=self.dtype)(fc1_out)
202
204
  x = torch.dropout(x, p=0.2, train=self.training)
203
205
  x = self.parametric_relu_2(self.fc2(x))
204
- x = nn.LayerNorm(normalized_shape=768, eps=1e-9)(x)
206
+ x = nn.LayerNorm(normalized_shape=768, eps=1e-9, device=self.device, dtype=self.dtype)(x)
205
207
  x = torch.dropout(x, p=0.2, train=self.training)
206
208
  x = self.parametric_relu_3(self.fc3(x))
207
209
  x = torch.dropout(x, p=0.2, train=self.training)
@@ -16,7 +16,10 @@ deeplotx/nn/auto_regression.py
16
16
  deeplotx/nn/base_neural_network.py
17
17
  deeplotx/nn/linear_regression.py
18
18
  deeplotx/nn/logistic_regression.py
19
+ deeplotx/nn/long_context_auto_regression.py
20
+ deeplotx/nn/long_context_recursive_sequential.py
19
21
  deeplotx/nn/recursive_sequential.py
22
+ deeplotx/nn/self_attention.py
20
23
  deeplotx/nn/softmax_regression.py
21
24
  deeplotx/similarity/__init__.py
22
25
  deeplotx/similarity/distribution.py
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "deeplotx"
3
- version = "0.4.14"
3
+ version = "0.5.1"
4
4
  description = "Easy-2-use long text NLP toolkit."
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.10"
File without changes
File without changes