deeplotx 0.4.12b0__py3-none-any.whl → 0.4.12b1__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/trainer/text_binary_classification_trainer.py +15 -11
- {deeplotx-0.4.12b0.dist-info → deeplotx-0.4.12b1.dist-info}/METADATA +1 -1
- {deeplotx-0.4.12b0.dist-info → deeplotx-0.4.12b1.dist-info}/RECORD +6 -6
- {deeplotx-0.4.12b0.dist-info → deeplotx-0.4.12b1.dist-info}/WHEEL +0 -0
- {deeplotx-0.4.12b0.dist-info → deeplotx-0.4.12b1.dist-info}/licenses/LICENSE +0 -0
- {deeplotx-0.4.12b0.dist-info → deeplotx-0.4.12b1.dist-info}/top_level.txt +0 -0
@@ -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.
|
9
|
+
from deeplotx.nn.recursive_sequential import RecursiveSequential
|
10
10
|
from deeplotx.trainer.base_trainer import BaseTrainer
|
11
11
|
|
12
12
|
logger = logging.getLogger('deeplotx.trainer')
|
@@ -20,20 +20,22 @@ class TextBinaryClassifierTrainer(BaseTrainer):
|
|
20
20
|
|
21
21
|
@override
|
22
22
|
def train(self, positive_texts: list[str], negative_texts: list[str],
|
23
|
-
num_epochs: int, learning_rate: float = 2e-
|
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
|
-
alpha: float = 1e-4, rho: float = 0.2
|
25
|
+
alpha: float = 1e-4, rho: float = 0.2,
|
26
|
+
hidden_dim: int = 256, recursive_layers: int = 2) -> RecursiveSequential:
|
26
27
|
if balancing_dataset:
|
27
28
|
min_length = min(len(positive_texts), len(negative_texts))
|
28
29
|
positive_texts = positive_texts[:min_length]
|
29
30
|
negative_texts = negative_texts[:min_length]
|
30
31
|
all_texts = positive_texts + negative_texts
|
31
|
-
|
32
|
-
+ [torch.tensor([0.0], dtype=torch.float32, device=self.device) for _ in range(len(negative_texts))])
|
33
|
-
text_embeddings = [self._long_text_encoder.encode(x) for x in all_texts]
|
32
|
+
text_embeddings = [self._long_text_encoder.encode(x, flatten=False, use_cache=True) for x in all_texts]
|
34
33
|
feature_dim = text_embeddings[0].shape[-1]
|
35
|
-
|
36
|
-
labels = torch.
|
34
|
+
dtype = text_embeddings[0].dtype
|
35
|
+
labels = ([torch.tensor([1.], dtype=dtype, device=self.device) for _ in range(len(positive_texts))]
|
36
|
+
+ [torch.tensor([.0], dtype=dtype, device=self.device) for _ in range(len(negative_texts))])
|
37
|
+
inputs = torch.stack(text_embeddings).to(self.device)
|
38
|
+
labels = torch.stack(labels).to(self.device)
|
37
39
|
dataset_size = len(labels)
|
38
40
|
train_size = int(self._train_ratio * dataset_size)
|
39
41
|
train_dataset = TensorDataset(inputs[:train_size], labels[:train_size])
|
@@ -44,7 +46,9 @@ class TextBinaryClassifierTrainer(BaseTrainer):
|
|
44
46
|
logger.warning("The dimension of features doesn't match. A new model instance will be created.")
|
45
47
|
self.model = None
|
46
48
|
if self.model is None:
|
47
|
-
self.model =
|
49
|
+
self.model = RecursiveSequential(input_dim=feature_dim, output_dim=1,
|
50
|
+
hidden_dim=hidden_dim,
|
51
|
+
recursive_layers=recursive_layers)
|
48
52
|
self.model.to(self.device)
|
49
53
|
loss_function = nn.BCELoss()
|
50
54
|
optimizer = optim.Adamax(self.model.parameters(), lr=learning_rate)
|
@@ -52,7 +56,7 @@ class TextBinaryClassifierTrainer(BaseTrainer):
|
|
52
56
|
self.model.train()
|
53
57
|
total_loss = 0.0
|
54
58
|
for batch_texts, batch_labels in train_loader:
|
55
|
-
outputs = self.model.forward(batch_texts)
|
59
|
+
outputs = torch.sigmoid(self.model.forward(batch_texts, self.model.initial_state(batch_texts.shape[0]))[0])
|
56
60
|
loss = loss_function(outputs, batch_labels) + self.model.elastic_net(alpha=alpha, rho=rho)
|
57
61
|
optimizer.zero_grad()
|
58
62
|
loss.backward()
|
@@ -63,7 +67,7 @@ class TextBinaryClassifierTrainer(BaseTrainer):
|
|
63
67
|
for batch_texts, batch_labels in valid_loader:
|
64
68
|
with torch.no_grad():
|
65
69
|
self.model.eval()
|
66
|
-
outputs = self.model.forward(batch_texts)
|
70
|
+
outputs = torch.sigmoid(self.model.forward(batch_texts, self.model.initial_state(batch_texts.shape[0]))[0])
|
67
71
|
loss = loss_function(outputs, batch_labels) + self.model.elastic_net(alpha=alpha, rho=rho)
|
68
72
|
total_valid_loss += loss.item()
|
69
73
|
self.model.train()
|
@@ -16,12 +16,12 @@ deeplotx/similarity/set.py,sha256=zhGFxtSIXlWqvipBYzoiPahp4g0boAIoUiMfG0wl07A,68
|
|
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=CRu7YM5sbox1GsCyWwsZtxD68TSnzLV91QaHLHm9tdU,4648
|
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.12b1.dist-info/licenses/LICENSE,sha256=IwGE9guuL-ryRPEKi6wFPI_zOhg7zDZbTYuHbSt_SAk,35823
|
24
|
+
deeplotx-0.4.12b1.dist-info/METADATA,sha256=qCyRk3nz-Jy7_1UMvJ34AhvWfvqWfrISvx8T5-ieXZw,6287
|
25
|
+
deeplotx-0.4.12b1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
26
|
+
deeplotx-0.4.12b1.dist-info/top_level.txt,sha256=hKg4pVDXZ-WWxkRfJFczRIll1Sv7VyfKCmzHLXbuh1U,9
|
27
|
+
deeplotx-0.4.12b1.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|