-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
136 lines (107 loc) · 5 KB
/
model.py
File metadata and controls
136 lines (107 loc) · 5 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
"""
Deep Q-Network (DQN) Model and Trainer for Snake AI
This module contains the neural network architecture and training logic
for the Snake AI agent using Deep Q-Learning.
"""
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import os
from typing import Union, Tuple
from pathlib import Path
class Linear_QNet(nn.Module):
"""
Simple Deep Q-Network with one hidden layer
Architecture: Input -> Hidden (ReLU) -> Output
- Input: 11 features about game state
- Hidden: 256 neurons with ReLU activation
- Output: 3 Q-values (one for each action)
"""
def __init__(self, input_size: int, hidden_size: int, output_size: int):
super().__init__()
self.input_layer = nn.Linear(input_size, hidden_size)
self.output_layer = nn.Linear(hidden_size, output_size)
# Initialize weights for better training stability
self._initialize_weights()
def _initialize_weights(self):
"""Initialize network weights using Xavier initialization"""
nn.init.xavier_uniform_(self.input_layer.weight)
nn.init.xavier_uniform_(self.output_layer.weight)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Forward pass through the network"""
x = F.relu(self.input_layer(x))
x = self.output_layer(x) # No activation on output (raw Q-values)
return x
def save(self, filename: str = 'best_model.pth') -> None:
"""Save the trained model to disk"""
save_dir = Path('./saved_models')
save_dir.mkdir(exist_ok=True) # Create directory if it doesn't exist
filepath = save_dir / filename
torch.save(self.state_dict(), filepath)
print(f"Model saved to {filepath}")
def load(self, filename: str = 'best_model.pth') -> bool:
"""Load a previously trained model from disk"""
filepath = Path('./saved_models') / filename
if not filepath.exists():
print(f"Model file {filepath} not found")
return False
try:
self.load_state_dict(torch.load(filepath))
print(f"Model loaded from {filepath}")
return True
except Exception as e:
print(f"Error loading model: {e}")
return False
class QTrainer:
"""
Trainer for the Deep Q-Network using the Bellman equation
Implements the core DQN learning algorithm:
Q(s,a) = r + γ * max(Q(s',a')) for non-terminal states
Q(s,a) = r for terminal states
"""
def __init__(self, model: Linear_QNet, learning_rate: float, discount_factor: float):
self.learning_rate = learning_rate
self.discount_factor = discount_factor # How much we value future rewards
self.model = model
self.optimizer = optim.Adam(model.parameters(), lr=learning_rate)
self.loss_function = nn.MSELoss()
def train_step(self, state: Union[torch.Tensor, tuple], action: Union[torch.Tensor, tuple],
reward: Union[torch.Tensor, tuple], next_state: Union[torch.Tensor, tuple],
game_over: Union[bool, tuple]) -> float:
"""
Perform one training step using the DQN algorithm
Returns the loss value for monitoring training progress
"""
# Convert everything to tensors
state = torch.tensor(state, dtype=torch.float)
next_state = torch.tensor(next_state, dtype=torch.float)
action = torch.tensor(action, dtype=torch.long)
reward = torch.tensor(reward, dtype=torch.float)
# Handle both single samples and batches
if len(state.shape) == 1:
# Single sample - add batch dimension
state = torch.unsqueeze(state, 0)
next_state = torch.unsqueeze(next_state, 0)
action = torch.unsqueeze(action, 0)
reward = torch.unsqueeze(reward, 0)
game_over = (game_over, ) # Convert to tuple for consistent handling
# Get current Q-values for all actions
current_q_values = self.model(state)
# Calculate target Q-values using Bellman equation
target_q_values = current_q_values.clone()
for batch_idx in range(len(game_over)):
new_q_value = reward[batch_idx]
if not game_over[batch_idx]:
# If game continues, add discounted future reward
future_q_values = self.model(next_state[batch_idx])
new_q_value = reward[batch_idx] + self.discount_factor * torch.max(future_q_values)
# Update Q-value for the action that was taken
action_idx = torch.argmax(action[batch_idx]).item()
target_q_values[batch_idx][action_idx] = new_q_value
# Train the network
self.optimizer.zero_grad()
loss = self.loss_function(target_q_values, current_q_values)
loss.backward()
self.optimizer.step()
return loss.item()