deeplotx 0.4.11__tar.gz → 0.4.12b1__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 (33) hide show
  1. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/PKG-INFO +1 -1
  2. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/encoder/long_text_encoder.py +3 -3
  3. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/trainer/text_binary_classification_trainer.py +17 -11
  4. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx.egg-info/PKG-INFO +1 -1
  5. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx.egg-info/SOURCES.txt +2 -1
  6. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/pyproject.toml +1 -1
  7. deeplotx-0.4.12b1/test/test.py +21 -0
  8. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/LICENSE +0 -0
  9. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/README.md +0 -0
  10. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/__init__.py +0 -0
  11. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/encoder/__init__.py +0 -0
  12. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/encoder/bert_encoder.py +0 -0
  13. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/encoder/longformer_encoder.py +0 -0
  14. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/nn/__init__.py +0 -0
  15. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/nn/auto_regression.py +0 -0
  16. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/nn/base_neural_network.py +0 -0
  17. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/nn/linear_regression.py +0 -0
  18. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/nn/logistic_regression.py +0 -0
  19. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/nn/recursive_sequential.py +0 -0
  20. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/nn/softmax_regression.py +0 -0
  21. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/similarity/__init__.py +0 -0
  22. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/similarity/distribution.py +0 -0
  23. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/similarity/set.py +0 -0
  24. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/similarity/vector.py +0 -0
  25. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/trainer/__init__.py +0 -0
  26. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/trainer/base_trainer.py +0 -0
  27. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/util/__init__.py +0 -0
  28. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/util/hash.py +0 -0
  29. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx/util/read_file.py +0 -0
  30. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx.egg-info/dependency_links.txt +0 -0
  31. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx.egg-info/requires.txt +0 -0
  32. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/deeplotx.egg-info/top_level.txt +0 -0
  33. {deeplotx-0.4.11 → deeplotx-0.4.12b1}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: deeplotx
3
- Version: 0.4.11
3
+ Version: 0.4.12b1
4
4
  Summary: Easy-2-use long text NLP toolkit.
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
@@ -28,7 +28,7 @@ class LongTextEncoder(BertEncoder):
28
28
  def postprocess(tensors: list[torch.Tensor], _flatten: bool) -> torch.Tensor:
29
29
  if not _flatten:
30
30
  return torch.stack(tensors, dim=0).squeeze()
31
- _fin_emb_tensor = torch.tensor([], dtype=tensors[0].dtype)
31
+ _fin_emb_tensor = torch.tensor([], dtype=tensors[0].dtype, device=self.device)
32
32
  for _emb in tensors:
33
33
  _fin_emb_tensor = torch.cat((_fin_emb_tensor.detach().clone(), _emb.detach().clone()), dim=-1)
34
34
  return _fin_emb_tensor.squeeze()
@@ -55,8 +55,8 @@ class LongTextEncoder(BertEncoder):
55
55
  for i in range(num_chunks):
56
56
  _tmp_left = max(i * self._chunk_size - self._overlapping, 0)
57
57
  _tmp_right = (i + 1) * self._chunk_size + self._overlapping
58
- chunks.append((i, torch.tensor([_text_to_input_ids[_tmp_left: _tmp_right]], dtype=torch.int),
59
- torch.tensor([_text_to_input_ids_att_mask[_tmp_left: _tmp_right]], dtype=torch.int)))
58
+ chunks.append((i, torch.tensor([_text_to_input_ids[_tmp_left: _tmp_right]], dtype=torch.int, device=self.device),
59
+ torch.tensor([_text_to_input_ids_att_mask[_tmp_left: _tmp_right]], dtype=torch.int, device=self.device)))
60
60
  with ThreadPoolExecutor(max_workers=min(num_chunks + 1, 3)) as executor:
61
61
  embeddings = list(executor.map(self.__chunk_embedding, chunks))
62
62
  embeddings.sort(key=lambda x: x[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.logistic_regression import LogisticRegression
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')
@@ -16,23 +16,26 @@ class TextBinaryClassifierTrainer(BaseTrainer):
16
16
  def __init__(self, long_text_encoder: LongTextEncoder, batch_size: int = 2, train_ratio: float = 0.8):
17
17
  super().__init__(batch_size=batch_size, train_ratio=train_ratio)
18
18
  self._long_text_encoder = long_text_encoder
19
+ self.device = self._long_text_encoder.device
19
20
 
20
21
  @override
21
22
  def train(self, positive_texts: list[str], negative_texts: list[str],
22
- num_epochs: int, learning_rate: float = 2e-5, balancing_dataset: bool = True,
23
+ num_epochs: int, learning_rate: float = 2e-6, balancing_dataset: bool = True,
23
24
  train_loss_threshold: float = 0.0, valid_loss_threshold: float = 0.0,
24
- alpha: float = 1e-4, rho: float = 0.2) -> LogisticRegression:
25
+ alpha: float = 1e-4, rho: float = 0.2,
26
+ hidden_dim: int = 256, recursive_layers: int = 2) -> RecursiveSequential:
25
27
  if balancing_dataset:
26
28
  min_length = min(len(positive_texts), len(negative_texts))
27
29
  positive_texts = positive_texts[:min_length]
28
30
  negative_texts = negative_texts[:min_length]
29
31
  all_texts = positive_texts + negative_texts
30
- labels = ([torch.tensor([1.0], dtype=torch.float32) for _ in range(len(positive_texts))]
31
- + [torch.tensor([0.0], dtype=torch.float32) for _ in range(len(negative_texts))])
32
- 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]
33
33
  feature_dim = text_embeddings[0].shape[-1]
34
- inputs = torch.stack(text_embeddings)
35
- labels = torch.stack(labels)
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)
36
39
  dataset_size = len(labels)
37
40
  train_size = int(self._train_ratio * dataset_size)
38
41
  train_dataset = TensorDataset(inputs[:train_size], labels[:train_size])
@@ -43,14 +46,17 @@ class TextBinaryClassifierTrainer(BaseTrainer):
43
46
  logger.warning("The dimension of features doesn't match. A new model instance will be created.")
44
47
  self.model = None
45
48
  if self.model is None:
46
- self.model = LogisticRegression(input_dim=feature_dim, output_dim=1)
49
+ self.model = RecursiveSequential(input_dim=feature_dim, output_dim=1,
50
+ hidden_dim=hidden_dim,
51
+ recursive_layers=recursive_layers)
52
+ self.model.to(self.device)
47
53
  loss_function = nn.BCELoss()
48
54
  optimizer = optim.Adamax(self.model.parameters(), lr=learning_rate)
49
55
  for epoch in range(num_epochs):
50
56
  self.model.train()
51
57
  total_loss = 0.0
52
58
  for batch_texts, batch_labels in train_loader:
53
- outputs = self.model.forward(batch_texts)
59
+ outputs = torch.sigmoid(self.model.forward(batch_texts, self.model.initial_state(batch_texts.shape[0]))[0])
54
60
  loss = loss_function(outputs, batch_labels) + self.model.elastic_net(alpha=alpha, rho=rho)
55
61
  optimizer.zero_grad()
56
62
  loss.backward()
@@ -61,7 +67,7 @@ class TextBinaryClassifierTrainer(BaseTrainer):
61
67
  for batch_texts, batch_labels in valid_loader:
62
68
  with torch.no_grad():
63
69
  self.model.eval()
64
- outputs = self.model.forward(batch_texts)
70
+ outputs = torch.sigmoid(self.model.forward(batch_texts, self.model.initial_state(batch_texts.shape[0]))[0])
65
71
  loss = loss_function(outputs, batch_labels) + self.model.elastic_net(alpha=alpha, rho=rho)
66
72
  total_valid_loss += loss.item()
67
73
  self.model.train()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: deeplotx
3
- Version: 0.4.11
3
+ Version: 0.4.12b1
4
4
  Summary: Easy-2-use long text NLP toolkit.
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
@@ -27,4 +27,5 @@ deeplotx/trainer/base_trainer.py
27
27
  deeplotx/trainer/text_binary_classification_trainer.py
28
28
  deeplotx/util/__init__.py
29
29
  deeplotx/util/hash.py
30
- deeplotx/util/read_file.py
30
+ deeplotx/util/read_file.py
31
+ test/test.py
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "deeplotx"
3
- version = "0.4.11"
3
+ version = "0.4.12.beta1"
4
4
  description = "Easy-2-use long text NLP toolkit."
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.10"
@@ -0,0 +1,21 @@
1
+ from deeplotx import TextBinaryClassifierTrainer, LongTextEncoder
2
+ from deeplotx.util import get_files, read_file
3
+
4
+ long_text_encoder = LongTextEncoder(
5
+ max_length=2048,
6
+ chunk_size=512,
7
+ overlapping=128
8
+ )
9
+
10
+ trainer = TextBinaryClassifierTrainer(
11
+ long_text_encoder=long_text_encoder,
12
+ batch_size=4,
13
+ train_ratio=0.9
14
+ )
15
+
16
+ pos_data_path = './data/pos'
17
+ neg_data_path = './data/neg'
18
+ pos_data = [read_file(x) for x in get_files(pos_data_path)]
19
+ neg_data = [read_file(x) for x in get_files(neg_data_path)]
20
+ model = trainer.train(pos_data, neg_data, num_epochs=20, learning_rate=2e-5, train_loss_threshold=1)
21
+ model.predict(long_text_encoder.encode('这是一个测试文本.').squeeze())
File without changes
File without changes
File without changes