cogentrl 0.2.0__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.
- cogentrl-0.2.0/LICENSE.txt +19 -0
- cogentrl-0.2.0/MANIFEST.in +4 -0
- cogentrl-0.2.0/PKG-INFO +75 -0
- cogentrl-0.2.0/README.rst +47 -0
- cogentrl-0.2.0/cogentrl/__init__.py +0 -0
- cogentrl-0.2.0/cogentrl/decision/__init__.py +6 -0
- cogentrl-0.2.0/cogentrl/decision/agent.py +14 -0
- cogentrl-0.2.0/cogentrl/decision/environment.py +8 -0
- cogentrl-0.2.0/cogentrl/decision/strategy.py +225 -0
- cogentrl-0.2.0/cogentrl/nlp/__init__.py +3 -0
- cogentrl-0.2.0/cogentrl/nlp/embedding.py +174 -0
- cogentrl-0.2.0/cogentrl/nlp/preprocessing.py +185 -0
- cogentrl-0.2.0/cogentrl/nlp/sentiment.py +8 -0
- cogentrl-0.2.0/cogentrl/nlp/tokenizer.py +8 -0
- cogentrl-0.2.0/cogentrl/nlp/vectorizer.py +224 -0
- cogentrl-0.2.0/cogentrl/utils/__init__.py +4 -0
- cogentrl-0.2.0/cogentrl/utils/helpers.py +11 -0
- cogentrl-0.2.0/cogentrl.egg-info/PKG-INFO +75 -0
- cogentrl-0.2.0/cogentrl.egg-info/SOURCES.txt +33 -0
- cogentrl-0.2.0/cogentrl.egg-info/dependency_links.txt +1 -0
- cogentrl-0.2.0/cogentrl.egg-info/requires.txt +10 -0
- cogentrl-0.2.0/cogentrl.egg-info/top_level.txt +1 -0
- cogentrl-0.2.0/pyproject.toml +37 -0
- cogentrl-0.2.0/requirements.txt +1 -0
- cogentrl-0.2.0/setup.cfg +4 -0
- cogentrl-0.2.0/setup.py +22 -0
- cogentrl-0.2.0/tests/test_agent.py +28 -0
- cogentrl-0.2.0/tests/test_embedding.py +40 -0
- cogentrl-0.2.0/tests/test_environment.py +49 -0
- cogentrl-0.2.0/tests/test_helpers.py +54 -0
- cogentrl-0.2.0/tests/test_preprocessing.py +50 -0
- cogentrl-0.2.0/tests/test_sentiment.py +50 -0
- cogentrl-0.2.0/tests/test_strategy.py +42 -0
- cogentrl-0.2.0/tests/test_tokenizer.py +49 -0
- cogentrl-0.2.0/tests/test_vectorizer.py +37 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
cogentrl-0.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cogentrl
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: star AI: Tools for decision-making and NLP.
|
|
5
|
+
Author: Mathis Daviau
|
|
6
|
+
Classifier: Development Status :: 4 - Beta
|
|
7
|
+
Classifier: Intended Audience :: Developers
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Requires-Python: >=3.7
|
|
15
|
+
Description-Content-Type: text/x-rst
|
|
16
|
+
License-File: LICENSE.txt
|
|
17
|
+
Requires-Dist: numpy>=1.21.0
|
|
18
|
+
Requires-Dist: matplotlib>=3.4.0
|
|
19
|
+
Requires-Dist: torch>=1.9.0
|
|
20
|
+
Requires-Dist: transformers>=4.0.0
|
|
21
|
+
Requires-Dist: scikit-learn>=0.24.0
|
|
22
|
+
Requires-Dist: pyyaml>=5.4.0
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: pytest>=6.2.0; extra == "dev"
|
|
25
|
+
Requires-Dist: flake8>=3.9.0; extra == "dev"
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
Dynamic: requires-python
|
|
28
|
+
|
|
29
|
+
star AI
|
|
30
|
+
=======
|
|
31
|
+
|
|
32
|
+
star AI is a Python package that bridges decision-making and natural language processing, inspired by intelligent behavior.
|
|
33
|
+
|
|
34
|
+
Features
|
|
35
|
+
--------
|
|
36
|
+
|
|
37
|
+
- **Decision-Making Models**
|
|
38
|
+
- Game-theoretic strategies (e.g., tit-for-tat).
|
|
39
|
+
- Multi-agent simulations.
|
|
40
|
+
- Reinforcement learning agents.
|
|
41
|
+
|
|
42
|
+
- **Natural Language Processing**
|
|
43
|
+
- Sentiment analysis.
|
|
44
|
+
- Tokenization and text similarity.
|
|
45
|
+
- Language evolution simulation.
|
|
46
|
+
|
|
47
|
+
Installation
|
|
48
|
+
------------
|
|
49
|
+
|
|
50
|
+
You can install the package via pip::
|
|
51
|
+
|
|
52
|
+
pip install star
|
|
53
|
+
|
|
54
|
+
Quick Start
|
|
55
|
+
-----------
|
|
56
|
+
|
|
57
|
+
Example usage:
|
|
58
|
+
|
|
59
|
+
.. code-block:: python
|
|
60
|
+
|
|
61
|
+
# Decision Agent
|
|
62
|
+
from star.decision.agent import DecisionAgent
|
|
63
|
+
agent = DecisionAgent(strategy="tit_for_tat")
|
|
64
|
+
decision = agent.decide(opponent_action="cooperate")
|
|
65
|
+
print(f"Agent decided to: {decision}")
|
|
66
|
+
|
|
67
|
+
# Sentiment Analysis
|
|
68
|
+
from star.nlp.sentiment import SentimentAnalyzer
|
|
69
|
+
analyzer = SentimentAnalyzer()
|
|
70
|
+
print(analyzer.analyze("I love bananas!")) # Output: "positive"
|
|
71
|
+
|
|
72
|
+
License
|
|
73
|
+
-------
|
|
74
|
+
|
|
75
|
+
This project is licensed under the MIT License.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
star AI
|
|
2
|
+
=======
|
|
3
|
+
|
|
4
|
+
star AI is a Python package that bridges decision-making and natural language processing, inspired by intelligent behavior.
|
|
5
|
+
|
|
6
|
+
Features
|
|
7
|
+
--------
|
|
8
|
+
|
|
9
|
+
- **Decision-Making Models**
|
|
10
|
+
- Game-theoretic strategies (e.g., tit-for-tat).
|
|
11
|
+
- Multi-agent simulations.
|
|
12
|
+
- Reinforcement learning agents.
|
|
13
|
+
|
|
14
|
+
- **Natural Language Processing**
|
|
15
|
+
- Sentiment analysis.
|
|
16
|
+
- Tokenization and text similarity.
|
|
17
|
+
- Language evolution simulation.
|
|
18
|
+
|
|
19
|
+
Installation
|
|
20
|
+
------------
|
|
21
|
+
|
|
22
|
+
You can install the package via pip::
|
|
23
|
+
|
|
24
|
+
pip install star
|
|
25
|
+
|
|
26
|
+
Quick Start
|
|
27
|
+
-----------
|
|
28
|
+
|
|
29
|
+
Example usage:
|
|
30
|
+
|
|
31
|
+
.. code-block:: python
|
|
32
|
+
|
|
33
|
+
# Decision Agent
|
|
34
|
+
from star.decision.agent import DecisionAgent
|
|
35
|
+
agent = DecisionAgent(strategy="tit_for_tat")
|
|
36
|
+
decision = agent.decide(opponent_action="cooperate")
|
|
37
|
+
print(f"Agent decided to: {decision}")
|
|
38
|
+
|
|
39
|
+
# Sentiment Analysis
|
|
40
|
+
from star.nlp.sentiment import SentimentAnalyzer
|
|
41
|
+
analyzer = SentimentAnalyzer()
|
|
42
|
+
print(analyzer.analyze("I love bananas!")) # Output: "positive"
|
|
43
|
+
|
|
44
|
+
License
|
|
45
|
+
-------
|
|
46
|
+
|
|
47
|
+
This project is licensed under the MIT License.
|
|
File without changes
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class DecisionAgent:
|
|
5
|
+
def __init__(self, strategy="random"):
|
|
6
|
+
self.strategy = strategy
|
|
7
|
+
|
|
8
|
+
def decide(self, opponent_action=None):
|
|
9
|
+
if self.strategy == "random":
|
|
10
|
+
return "cooperate" if np.random.rand() > 0.5 else "defect"
|
|
11
|
+
elif self.strategy == "tit_for_tat":
|
|
12
|
+
return opponent_action if opponent_action else "cooperate"
|
|
13
|
+
else:
|
|
14
|
+
raise ValueError("Unknown strategy!")
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import torch.nn as nn
|
|
3
|
+
import torch.optim as optim
|
|
4
|
+
import numpy as np
|
|
5
|
+
from collections import deque
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class StrategyBase(nn.Module):
|
|
9
|
+
"""
|
|
10
|
+
Base class for strategies using PyTorch.
|
|
11
|
+
|
|
12
|
+
This serves as a foundation for building various strategies, including neural networks
|
|
13
|
+
and reinforcement learning-based strategies.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
def __init__(self, input_dim, hidden_dim, output_dim):
|
|
17
|
+
super(StrategyBase, self).__init__()
|
|
18
|
+
self.fc1 = nn.Linear(input_dim, hidden_dim)
|
|
19
|
+
self.fc2 = nn.Linear(hidden_dim, output_dim)
|
|
20
|
+
|
|
21
|
+
def forward(self, x):
|
|
22
|
+
"""
|
|
23
|
+
Forward pass through the network.
|
|
24
|
+
|
|
25
|
+
Parameters
|
|
26
|
+
----------
|
|
27
|
+
x : torch.Tensor
|
|
28
|
+
Input tensor.
|
|
29
|
+
|
|
30
|
+
Returns
|
|
31
|
+
-------
|
|
32
|
+
torch.Tensor
|
|
33
|
+
Output logits.
|
|
34
|
+
"""
|
|
35
|
+
x = torch.relu(self.fc1(x))
|
|
36
|
+
return self.fc2(x)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class RandomStrategy:
|
|
40
|
+
"""
|
|
41
|
+
Random strategy that selects actions based on a fixed probability distribution.
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
def __init__(self, cooperation_prob=0.5):
|
|
45
|
+
self.cooperation_prob = cooperation_prob
|
|
46
|
+
|
|
47
|
+
def decide(self):
|
|
48
|
+
"""
|
|
49
|
+
Decide an action based on a fixed probability distribution.
|
|
50
|
+
|
|
51
|
+
Returns
|
|
52
|
+
-------
|
|
53
|
+
str
|
|
54
|
+
"cooperate" or "defect".
|
|
55
|
+
"""
|
|
56
|
+
return "cooperate" if np.random.rand() < self.cooperation_prob else "defect"
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class TitForTatStrategy:
|
|
60
|
+
"""
|
|
61
|
+
Tit-for-tat strategy that mimics the opponent's previous action.
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
def decide(self, opponent_action=None):
|
|
65
|
+
"""
|
|
66
|
+
Decide an action based on the opponent's previous action.
|
|
67
|
+
|
|
68
|
+
Parameters
|
|
69
|
+
----------
|
|
70
|
+
opponent_action : str, optional
|
|
71
|
+
The opponent's last action.
|
|
72
|
+
|
|
73
|
+
Returns
|
|
74
|
+
-------
|
|
75
|
+
str
|
|
76
|
+
"cooperate" or "defect".
|
|
77
|
+
"""
|
|
78
|
+
return opponent_action if opponent_action else "cooperate"
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class PolicyGradientStrategy(nn.Module):
|
|
82
|
+
"""
|
|
83
|
+
Policy Gradient strategy implemented using PyTorch.
|
|
84
|
+
|
|
85
|
+
This strategy learns a policy for choosing actions based on a reward signal.
|
|
86
|
+
"""
|
|
87
|
+
|
|
88
|
+
def __init__(self, input_dim, hidden_dim, action_space):
|
|
89
|
+
super(PolicyGradientStrategy, self).__init__()
|
|
90
|
+
self.fc1 = nn.Linear(input_dim, hidden_dim)
|
|
91
|
+
self.fc2 = nn.Linear(hidden_dim, action_space)
|
|
92
|
+
self.softmax = nn.Softmax(dim=-1)
|
|
93
|
+
|
|
94
|
+
# Reinforcement learning memory
|
|
95
|
+
self.memory = []
|
|
96
|
+
|
|
97
|
+
def forward(self, x):
|
|
98
|
+
"""
|
|
99
|
+
Forward pass through the policy network.
|
|
100
|
+
|
|
101
|
+
Parameters
|
|
102
|
+
----------
|
|
103
|
+
x : torch.Tensor
|
|
104
|
+
Input tensor.
|
|
105
|
+
|
|
106
|
+
Returns
|
|
107
|
+
-------
|
|
108
|
+
torch.Tensor
|
|
109
|
+
Probability distribution over actions.
|
|
110
|
+
"""
|
|
111
|
+
x = torch.relu(self.fc1(x))
|
|
112
|
+
x = self.softmax(self.fc2(x))
|
|
113
|
+
return x
|
|
114
|
+
|
|
115
|
+
def store_transition(self, state, action, reward):
|
|
116
|
+
"""
|
|
117
|
+
Store a transition in memory for policy gradient updates.
|
|
118
|
+
|
|
119
|
+
Parameters
|
|
120
|
+
----------
|
|
121
|
+
state : torch.Tensor
|
|
122
|
+
The state vector.
|
|
123
|
+
action : int
|
|
124
|
+
The action index taken.
|
|
125
|
+
reward : float
|
|
126
|
+
The reward received.
|
|
127
|
+
"""
|
|
128
|
+
self.memory.append((state, action, reward))
|
|
129
|
+
|
|
130
|
+
def compute_returns(self, gamma=0.99):
|
|
131
|
+
"""
|
|
132
|
+
Compute discounted rewards for the stored transitions.
|
|
133
|
+
|
|
134
|
+
Parameters
|
|
135
|
+
----------
|
|
136
|
+
gamma : float
|
|
137
|
+
Discount factor for future rewards.
|
|
138
|
+
|
|
139
|
+
Returns
|
|
140
|
+
-------
|
|
141
|
+
list
|
|
142
|
+
List of discounted rewards.
|
|
143
|
+
"""
|
|
144
|
+
discounted_rewards = []
|
|
145
|
+
cumulative = 0
|
|
146
|
+
for _, _, reward in reversed(self.memory):
|
|
147
|
+
cumulative = reward + gamma * cumulative
|
|
148
|
+
discounted_rewards.insert(0, cumulative)
|
|
149
|
+
return discounted_rewards
|
|
150
|
+
|
|
151
|
+
def train_policy(self, optimizer, gamma=0.99):
|
|
152
|
+
"""
|
|
153
|
+
Train the policy using stored transitions and policy gradient.
|
|
154
|
+
|
|
155
|
+
Parameters
|
|
156
|
+
----------
|
|
157
|
+
optimizer : torch.optim.Optimizer
|
|
158
|
+
Optimizer for updating the policy network.
|
|
159
|
+
gamma : float
|
|
160
|
+
Discount factor for future rewards.
|
|
161
|
+
"""
|
|
162
|
+
if not self.memory:
|
|
163
|
+
return
|
|
164
|
+
|
|
165
|
+
states, actions, rewards = zip(*self.memory)
|
|
166
|
+
states = torch.stack(states)
|
|
167
|
+
actions = torch.tensor(actions)
|
|
168
|
+
returns = torch.tensor(self.compute_returns(gamma))
|
|
169
|
+
|
|
170
|
+
# Normalize returns for better gradient stability
|
|
171
|
+
returns = (returns - returns.mean()) / (returns.std() + 1e-5)
|
|
172
|
+
|
|
173
|
+
optimizer.zero_grad()
|
|
174
|
+
probs = self.forward(states)
|
|
175
|
+
action_probs = probs.gather(1, actions.unsqueeze(1)).squeeze()
|
|
176
|
+
loss = -torch.sum(torch.log(action_probs) * returns)
|
|
177
|
+
loss.backward()
|
|
178
|
+
optimizer.step()
|
|
179
|
+
|
|
180
|
+
# Clear memory
|
|
181
|
+
self.memory = []
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
class AdaptiveStrategy:
|
|
185
|
+
"""
|
|
186
|
+
Adaptive strategy that adjusts its decision logic based on performance.
|
|
187
|
+
"""
|
|
188
|
+
|
|
189
|
+
def __init__(self, cooperation_threshold=0.7):
|
|
190
|
+
self.cooperation_threshold = cooperation_threshold
|
|
191
|
+
self.history = deque(maxlen=10)
|
|
192
|
+
|
|
193
|
+
def decide(self, opponent_action=None):
|
|
194
|
+
"""
|
|
195
|
+
Decide an action based on historical performance.
|
|
196
|
+
|
|
197
|
+
Parameters
|
|
198
|
+
----------
|
|
199
|
+
opponent_action : str, optional
|
|
200
|
+
The opponent's last action.
|
|
201
|
+
|
|
202
|
+
Returns
|
|
203
|
+
-------
|
|
204
|
+
str
|
|
205
|
+
"cooperate" or "defect".
|
|
206
|
+
"""
|
|
207
|
+
if len(self.history) < self.history.maxlen:
|
|
208
|
+
return "cooperate" # Default to cooperation initially
|
|
209
|
+
|
|
210
|
+
# Calculate cooperation ratio
|
|
211
|
+
cooperation_ratio = sum(1 for _, op_action in self.history if op_action == "cooperate") / len(self.history)
|
|
212
|
+
return "cooperate" if cooperation_ratio >= self.cooperation_threshold else "defect"
|
|
213
|
+
|
|
214
|
+
def update_history(self, action, opponent_action):
|
|
215
|
+
"""
|
|
216
|
+
Update the history with the latest action pair.
|
|
217
|
+
|
|
218
|
+
Parameters
|
|
219
|
+
----------
|
|
220
|
+
action : str
|
|
221
|
+
The agent's action.
|
|
222
|
+
opponent_action : str
|
|
223
|
+
The opponent's action.
|
|
224
|
+
"""
|
|
225
|
+
self.history.append((action, opponent_action))
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import torch.nn as nn
|
|
3
|
+
from transformers import AutoTokenizer, AutoModel
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class StaticEmbedding:
|
|
7
|
+
"""
|
|
8
|
+
Handles static embeddings like GloVe or FastText.
|
|
9
|
+
|
|
10
|
+
Parameters
|
|
11
|
+
----------
|
|
12
|
+
embedding_file : str
|
|
13
|
+
Path to the pre-trained embedding file.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
def __init__(self, embedding_file):
|
|
17
|
+
self.embedding_file = embedding_file
|
|
18
|
+
self.word_to_index = {}
|
|
19
|
+
self.index_to_vector = []
|
|
20
|
+
|
|
21
|
+
self._load_embeddings()
|
|
22
|
+
|
|
23
|
+
def _load_embeddings(self):
|
|
24
|
+
"""
|
|
25
|
+
Load static embeddings from a file.
|
|
26
|
+
"""
|
|
27
|
+
print(f"Loading embeddings from {self.embedding_file}...")
|
|
28
|
+
with open(self.embedding_file, "r", encoding="utf-8") as f:
|
|
29
|
+
for line in f:
|
|
30
|
+
values = line.split()
|
|
31
|
+
word = values[0]
|
|
32
|
+
vector = torch.tensor([float(x) for x in values[1:]], dtype=torch.float32)
|
|
33
|
+
self.word_to_index[word] = len(self.index_to_vector)
|
|
34
|
+
self.index_to_vector.append(vector)
|
|
35
|
+
self.index_to_vector = torch.stack(self.index_to_vector)
|
|
36
|
+
print(f"Loaded {len(self.word_to_index)} embeddings.")
|
|
37
|
+
|
|
38
|
+
def get_embedding(self, word):
|
|
39
|
+
"""
|
|
40
|
+
Get the embedding for a given word.
|
|
41
|
+
|
|
42
|
+
Parameters
|
|
43
|
+
----------
|
|
44
|
+
word : str
|
|
45
|
+
The word to retrieve the embedding for.
|
|
46
|
+
|
|
47
|
+
Returns
|
|
48
|
+
-------
|
|
49
|
+
torch.Tensor
|
|
50
|
+
The embedding vector.
|
|
51
|
+
"""
|
|
52
|
+
index = self.word_to_index.get(word, None)
|
|
53
|
+
if index is None:
|
|
54
|
+
raise ValueError(f"Word '{word}' not found in the embedding vocabulary.")
|
|
55
|
+
return self.index_to_vector[index]
|
|
56
|
+
|
|
57
|
+
def similarity(self, word1, word2):
|
|
58
|
+
"""
|
|
59
|
+
Compute cosine similarity between two word embeddings.
|
|
60
|
+
|
|
61
|
+
Parameters
|
|
62
|
+
----------
|
|
63
|
+
word1 : str
|
|
64
|
+
The first word.
|
|
65
|
+
word2 : str
|
|
66
|
+
The second word.
|
|
67
|
+
|
|
68
|
+
Returns
|
|
69
|
+
-------
|
|
70
|
+
float
|
|
71
|
+
Cosine similarity score.
|
|
72
|
+
"""
|
|
73
|
+
embedding1 = self.get_embedding(word1)
|
|
74
|
+
embedding2 = self.get_embedding(word2)
|
|
75
|
+
return torch.nn.functional.cosine_similarity(embedding1.unsqueeze(0), embedding2.unsqueeze(0)).item()
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class ContextualEmbedding:
|
|
79
|
+
"""
|
|
80
|
+
Handles contextual embeddings using transformer models like BERT.
|
|
81
|
+
|
|
82
|
+
Parameters
|
|
83
|
+
----------
|
|
84
|
+
model_name : str
|
|
85
|
+
Name of the transformer model to use.
|
|
86
|
+
"""
|
|
87
|
+
|
|
88
|
+
def __init__(self, model_name="bert-base-uncased"):
|
|
89
|
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
90
|
+
self.model = AutoModel.from_pretrained(model_name)
|
|
91
|
+
|
|
92
|
+
def get_embedding(self, sentence):
|
|
93
|
+
"""
|
|
94
|
+
Get contextual embeddings for a given sentence.
|
|
95
|
+
|
|
96
|
+
Parameters
|
|
97
|
+
----------
|
|
98
|
+
sentence : str
|
|
99
|
+
The input sentence.
|
|
100
|
+
|
|
101
|
+
Returns
|
|
102
|
+
-------
|
|
103
|
+
torch.Tensor
|
|
104
|
+
The embedding for the sentence.
|
|
105
|
+
"""
|
|
106
|
+
inputs = self.tokenizer(sentence, return_tensors="pt", padding=True, truncation=True)
|
|
107
|
+
outputs = self.model(**inputs)
|
|
108
|
+
# Return the CLS token's embedding
|
|
109
|
+
return outputs.last_hidden_state[:, 0, :]
|
|
110
|
+
|
|
111
|
+
def sentence_similarity(self, sentence1, sentence2):
|
|
112
|
+
"""
|
|
113
|
+
Compute cosine similarity between two sentence embeddings.
|
|
114
|
+
|
|
115
|
+
Parameters
|
|
116
|
+
----------
|
|
117
|
+
sentence1 : str
|
|
118
|
+
The first sentence.
|
|
119
|
+
sentence2 : str
|
|
120
|
+
The second sentence.
|
|
121
|
+
|
|
122
|
+
Returns
|
|
123
|
+
-------
|
|
124
|
+
float
|
|
125
|
+
Cosine similarity score.
|
|
126
|
+
"""
|
|
127
|
+
embedding1 = self.get_embedding(sentence1)
|
|
128
|
+
embedding2 = self.get_embedding(sentence2)
|
|
129
|
+
return torch.nn.functional.cosine_similarity(embedding1, embedding2).item()
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
class CustomEmbedding(nn.Module):
|
|
133
|
+
"""
|
|
134
|
+
Custom embedding layer for specific NLP tasks.
|
|
135
|
+
|
|
136
|
+
Parameters
|
|
137
|
+
----------
|
|
138
|
+
vocab_size : int
|
|
139
|
+
Size of the vocabulary.
|
|
140
|
+
embedding_dim : int
|
|
141
|
+
Dimension of the embeddings.
|
|
142
|
+
"""
|
|
143
|
+
|
|
144
|
+
def __init__(self, vocab_size, embedding_dim):
|
|
145
|
+
super(CustomEmbedding, self).__init__()
|
|
146
|
+
self.embedding = nn.Embedding(vocab_size, embedding_dim)
|
|
147
|
+
|
|
148
|
+
def forward(self, input_ids):
|
|
149
|
+
"""
|
|
150
|
+
Forward pass to retrieve embeddings for input IDs.
|
|
151
|
+
|
|
152
|
+
Parameters
|
|
153
|
+
----------
|
|
154
|
+
input_ids : torch.Tensor
|
|
155
|
+
Tensor of input token IDs.
|
|
156
|
+
|
|
157
|
+
Returns
|
|
158
|
+
-------
|
|
159
|
+
torch.Tensor
|
|
160
|
+
Embedding vectors.
|
|
161
|
+
"""
|
|
162
|
+
return self.embedding(input_ids)
|
|
163
|
+
|
|
164
|
+
def freeze_embeddings(self):
|
|
165
|
+
"""
|
|
166
|
+
Freeze the embedding weights (useful for fine-tuning).
|
|
167
|
+
"""
|
|
168
|
+
self.embedding.weight.requires_grad = False
|
|
169
|
+
|
|
170
|
+
def unfreeze_embeddings(self):
|
|
171
|
+
"""
|
|
172
|
+
Unfreeze the embedding weights.
|
|
173
|
+
"""
|
|
174
|
+
self.embedding.weight.requires_grad = True
|