-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp10.py
More file actions
327 lines (264 loc) · 12.2 KB
/
app10.py
File metadata and controls
327 lines (264 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
import numpy as np
from typing import Dict, List, Optional, Tuple
class SimpleTransformerLayer(nn.Module):
"""
Basic transformer layer with self-attention and feed-forward network
"""
def __init__(self, embed_dim, num_heads, ff_dim, dropout=0.1):
super().__init__()
self.self_attn = nn.MultiheadAttention(embed_dim, num_heads, dropout=dropout, batch_first=True)
self.layer_norm1 = nn.LayerNorm(embed_dim)
self.layer_norm2 = nn.LayerNorm(embed_dim)
# Feed-forward network
self.ff_network = nn.Sequential(
nn.Linear(embed_dim, ff_dim),
nn.ReLU(),
nn.Dropout(dropout),
nn.Linear(ff_dim, embed_dim),
nn.Dropout(dropout)
)
def forward(self, x, attention_mask=None):
# Self-attention block with residual connection
residual = x
x = self.layer_norm1(x)
attn_output, attn_weights = self.self_attn(x, x, x, attn_mask=attention_mask)
x = residual + attn_output
# Feed-forward block with residual connection
residual = x
x = self.layer_norm2(x)
ff_output = self.ff_network(x)
x = residual + ff_output
return x, attn_weights
class SimpleKnowledgeGraph:
"""
Simple knowledge graph implementation with basic reasoning
"""
def __init__(self):
# Initialize empty graph structure
self.entities = {} # entity_id -> name
self.relations = [] # (source_id, relation_type, target_id, weight)
self.rules = [] # (premise_ids, conclusion_id, confidence)
def add_entity(self, entity_id, name):
"""Add an entity to the knowledge graph"""
self.entities[entity_id] = name
return self
def add_relation(self, source_id, relation_type, target_id, weight=1.0):
"""Add a relation between two entities"""
self.relations.append((source_id, relation_type, target_id, weight))
return self
def add_rule(self, premise_ids, conclusion_id, confidence=1.0):
"""Add a logical rule to the knowledge graph"""
self.rules.append((premise_ids, conclusion_id, confidence))
return self
def reason(self, active_entities):
"""
Apply simple forward reasoning to derive new knowledge
Args:
active_entities: Set of currently active entity IDs
Returns:
Set of inferred entity IDs and reasoning steps
"""
inferred = set(active_entities)
reasoning_steps = {}
# Apply relation-based inference
for source_id, relation_type, target_id, weight in self.relations:
if source_id in inferred and target_id not in inferred:
inferred.add(target_id)
step = f"{self.entities[source_id]} --{relation_type}--> {self.entities[target_id]}"
reasoning_steps[target_id] = step
# Apply rule-based inference
for premise_ids, conclusion_id, confidence in self.rules:
if all(p_id in inferred for p_id in premise_ids) and conclusion_id not in inferred:
inferred.add(conclusion_id)
premises = [self.entities[p_id] for p_id in premise_ids]
step = f"Rule: IF {' AND '.join(premises)} THEN {self.entities[conclusion_id]}"
reasoning_steps[conclusion_id] = step
return inferred, reasoning_steps
class NeuralSymbolicInterface:
"""
Simple interface between neural and symbolic components
"""
def __init__(self, input_dim, num_symbols):
self.neural_to_symbol_matrix = torch.randn(num_symbols, input_dim)
self.symbol_to_neural_matrix = torch.randn(input_dim, num_symbols)
def neural_to_symbolic(self, neural_repr, threshold=0.5):
"""Convert neural representations to symbolic activations"""
# Compute similarity between neural representations and symbol embeddings
similarity = torch.matmul(neural_repr, self.neural_to_symbol_matrix.T)
# Apply threshold to get binary activations
activations = (similarity > threshold).float()
return activations, similarity
def symbolic_to_neural(self, symbolic_activations):
"""Convert symbolic activations to neural representations"""
# Compute weighted sum of symbol embeddings
neural_repr = torch.matmul(symbolic_activations, self.symbol_to_neural_matrix.T)
return neural_repr
class NeuralSymbolicInterface:
"""
Simple interface between neural and symbolic components
"""
def __init__(self, input_dim, num_symbols):
self.neural_to_symbol_matrix = torch.randn(num_symbols, input_dim)
self.symbol_to_neural_matrix = torch.randn(num_symbols, input_dim)
def neural_to_symbolic(self, neural_repr, threshold=0.5):
"""Convert neural representations to symbolic activations"""
# Compute similarity between neural representations and symbol embeddings
similarity = torch.matmul(neural_repr, self.neural_to_symbol_matrix.T)
# Apply threshold to get binary activations
activations = (similarity > threshold).float()
return activations, similarity
# ADD THIS METHOD - it's missing in your code
def symbolic_to_neural(self, symbolic_activations):
"""Convert symbolic activations to neural representations"""
# Compute weighted sum of symbol embeddings
neural_repr = torch.matmul(symbolic_activations, self.symbol_to_neural_matrix)
return neural_repr
class NexusToyModel:
"""
Simplified NEXUS architecture with basic neural and symbolic components
"""
def __init__(self, embed_dim, num_heads, ff_dim, num_layers, num_symbols, symbol_names, dropout=0.1):
# Neural component
self.transformer_layers = [SimpleTransformerLayer(embed_dim, num_heads, ff_dim, dropout)
for _ in range(num_layers)]
# Symbolic component
self.knowledge_graph = SimpleKnowledgeGraph()
# Neural-symbolic interface
self.interface = NeuralSymbolicInterface(embed_dim, num_symbols)
# Store symbol names for explanation
self.symbol_names = symbol_names
self.symbol_to_id = {name: i for i, name in enumerate(symbol_names)}
def forward(self, neural_input):
"""
Process input through neural and symbolic components
Args:
neural_input: Tensor of shape [batch_size, seq_len, embed_dim]
Returns:
Dictionary with neural and symbolic outputs
"""
# Process through transformer layers
x = neural_input
all_attentions = []
for layer in self.transformer_layers:
x, attn_weights = layer(x)
all_attentions.append(attn_weights)
# Take final representation from first token (CLS)
cls_representation = x[:, 0, :]
# Convert to symbolic representation
symbolic_activations, symbolic_scores = self.interface.neural_to_symbolic(cls_representation)
# Apply reasoning
batch_size = neural_input.size(0)
all_inferred = []
all_reasoning_steps = []
for i in range(batch_size):
# Get active symbols for this instance
active_symbols = torch.nonzero(symbolic_activations[i]).squeeze(-1).tolist()
if not isinstance(active_symbols, list):
active_symbols = [active_symbols]
# Apply reasoning
inferred, reasoning_steps = self.knowledge_graph.reason(active_symbols)
# Store results
all_inferred.append(inferred)
all_reasoning_steps.append(reasoning_steps)
# Convert inferred symbols back to neural representation
inferred_activations = torch.zeros_like(symbolic_activations)
for i, inferred in enumerate(all_inferred):
for symbol_id in inferred:
if symbol_id < inferred_activations.size(1):
inferred_activations[i, symbol_id] = 1.0
enhanced_representation = self.interface.symbolic_to_neural(inferred_activations)
# Combine neural and symbolic representations
combined_representation = cls_representation + enhanced_representation
return {
'neural_representation': cls_representation,
'symbolic_activations': symbolic_activations,
'symbolic_scores': symbolic_scores,
'inferred_symbols': all_inferred,
'reasoning_steps': all_reasoning_steps,
'enhanced_representation': enhanced_representation,
'combined_representation': combined_representation
}
def explain(self, inferred_symbols, reasoning_steps):
"""Generate explanation for the inference process"""
active_names = [self.symbol_names[i] for i in inferred_symbols if i < len(self.symbol_names)]
explanation = [f"Identified concepts: {', '.join(active_names)}"]
# Add reasoning steps
if reasoning_steps:
explanation.append("Reasoning steps:")
for symbol_id, step in reasoning_steps.items():
if symbol_id < len(self.symbol_names):
symbol_name = self.symbol_names[symbol_id]
explanation.append(f" - {symbol_name}: {step}")
return "\n".join(explanation)
# Example usage for a simple medical diagnosis model
def create_toy_medical_model():
# Model parameters
embed_dim = 64
num_heads = 4
ff_dim = 128
num_layers = 2
# Define medical symbols
symbols = [
'fever', 'cough', 'fatigue', 'shortness_of_breath',
'common_cold', 'influenza', 'covid19', 'pneumonia',
'rest', 'medication', 'hospitalization'
]
num_symbols = len(symbols)
# Create model
model = NexusToyModel(
embed_dim=embed_dim,
num_heads=num_heads,
ff_dim=ff_dim,
num_layers=num_layers,
num_symbols=num_symbols,
symbol_names=symbols
)
# Initialize knowledge graph with medical knowledge
kg = model.knowledge_graph
# Add entities
for i, symbol in enumerate(symbols):
kg.add_entity(i, symbol)
# Add relations
kg.add_relation(0, "symptom_of", 5) # fever -> influenza
kg.add_relation(0, "symptom_of", 6) # fever -> covid19
kg.add_relation(1, "symptom_of", 4) # cough -> common_cold
kg.add_relation(1, "symptom_of", 5) # cough -> influenza
kg.add_relation(1, "symptom_of", 6) # cough -> covid19
kg.add_relation(1, "symptom_of", 7) # cough -> pneumonia
kg.add_relation(2, "symptom_of", 5) # fatigue -> influenza
kg.add_relation(2, "symptom_of", 6) # fatigue -> covid19
kg.add_relation(3, "symptom_of", 6) # shortness_of_breath -> covid19
kg.add_relation(3, "symptom_of", 7) # shortness_of_breath -> pneumonia
# Add logical rules
kg.add_rule([0, 1, 2], 5) # fever + cough + fatigue -> influenza
kg.add_rule([0, 1, 3], 6) # fever + cough + shortness_of_breath -> covid19
kg.add_rule([4], 8) # common_cold -> rest
kg.add_rule([5], 9) # influenza -> medication
kg.add_rule([6], 9) # covid19 -> medication
kg.add_rule([7], 10) # pneumonia -> hospitalization
kg.add_rule([6, 3], 10) # covid19 + shortness_of_breath -> hospitalization
return model
# Example of using the toy model
def test_toy_model():
# Create model
model = create_toy_medical_model()
# Create a dummy input (batch_size=1, seq_len=5, embed_dim=64)
batch_size = 1
seq_len = 5
embed_dim = 64
dummy_input = torch.randn(batch_size, seq_len, embed_dim)
# Process input
output = model.forward(dummy_input)
# Create explanation
explanation = model.explain(
output['inferred_symbols'][0],
output['reasoning_steps'][0]
)
print(explanation)
return output, explanation
if __name__ == "__main__":
test_toy_model()