Update App.py
Browse files
App.py
CHANGED
@@ -1,48 +1,35 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
from
|
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 |
-
with gr.Column():
|
37 |
-
chat_input = gr.Textbox(lines=2, placeholder="Enter your message here...")
|
38 |
-
chat_output = gr.Textbox(lines=5, placeholder="Model response will appear here...")
|
39 |
-
chat_button = gr.Button("Send")
|
40 |
-
with gr.Column():
|
41 |
-
code_input = gr.Textbox(lines=10, placeholder="Enter your code here...")
|
42 |
-
code_output = gr.Textbox(lines=5, placeholder="Code output will appear here...")
|
43 |
-
code_button = gr.Button("Run Code")
|
44 |
-
|
45 |
-
chat_button.click(chat_response, inputs=chat_input, outputs=chat_output)
|
46 |
-
code_button.click(execute_code, inputs=code_input, outputs=code_output)
|
47 |
-
|
48 |
-
demo.launch()
|
|
|
1 |
+
import torch
|
2 |
+
import torch.optim as optim
|
3 |
+
from torch.utils.data import DataLoader
|
4 |
+
from models.moe_model import MoEModel
|
5 |
+
from utils.data_loader import load_data
|
6 |
+
|
7 |
+
# Load data
|
8 |
+
train_loader, test_loader = load_data()
|
9 |
+
|
10 |
+
# Initialize model, loss function, and optimizer
|
11 |
+
model = MoEModel(input_dim=512, num_experts=3)
|
12 |
+
criterion = nn.CrossEntropyLoss()
|
13 |
+
optimizer = optim.Adam(model.parameters(), lr=0.001)
|
14 |
+
|
15 |
+
# Training loop
|
16 |
+
for epoch in range(10):
|
17 |
+
model.train()
|
18 |
+
for vision_input, audio_input, sensor_input, labels in train_loader:
|
19 |
+
optimizer.zero_grad()
|
20 |
+
outputs = model(vision_input, audio_input, sensor_input)
|
21 |
+
loss = criterion(outputs, labels)
|
22 |
+
loss.backward()
|
23 |
+
optimizer.step()
|
24 |
+
print(f"Epoch {epoch+1}, Loss: {loss.item()}")
|
25 |
+
|
26 |
+
# Evaluation
|
27 |
+
model.eval()
|
28 |
+
correct, total = 0, 0
|
29 |
+
with torch.no_grad():
|
30 |
+
for vision_input, audio_input, sensor_input, labels in test_loader:
|
31 |
+
outputs = model(vision_input, audio_input, sensor_input)
|
32 |
+
_, predicted = torch.max(outputs.data, 1)
|
33 |
+
total += labels.size(0)
|
34 |
+
correct += (predicted == labels).sum().item()
|
35 |
+
print(f"Accuracy: {100 * correct / total}%")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|