-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_model.py
210 lines (168 loc) · 6.63 KB
/
train_model.py
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
import os
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers, models
import glob
import json
# Load both configuration files
with open('generation_config.json', 'r') as f:
GEN_CONFIG = json.load(f)
with open('config.json', 'r') as f:
CONFIG = json.load(f)
GRID_SIZE = CONFIG['game']['grid_size']
def load_game_data(file_path):
"""Load and parse a game file"""
states = []
actions = []
with open(file_path, 'r') as f:
lines = f.readlines()
current_board = []
reading_board = False
for line in lines:
line = line.strip()
if not line:
continue
if line.startswith("Action:"):
action = line.split(":")[1].strip()
actions.append(action)
# Verify board dimensions match config
if len(current_board) == GRID_SIZE[0] and all(len(row) == GRID_SIZE[1] for row in current_board):
states.append(np.array(current_board))
else:
print(f"Warning: Skipping move in {file_path} - incorrect board dimensions")
current_board = []
reading_board = False
elif line[0].isdigit() or line[0] == '0':
# Reading board line
board_line = [int(x) for x in line.split()]
current_board.append(board_line)
reading_board = True
return states, actions
def preprocess_data(states, actions):
"""Convert states and actions to neural network format"""
processed_states = []
for state in states:
# Convert to numpy array if not already
state = np.array(state)
# Verify state dimensions
if state.shape != tuple(GRID_SIZE):
print(f"Warning: Skipping state with incorrect dimensions: {state.shape}")
continue
# Resize state to 4x4 if necessary using max pooling
if GRID_SIZE != [4, 4]:
rows = np.array_split(state, 4, axis=0)
reduced_state = np.zeros((4, 4))
for i, row_group in enumerate(rows):
cols = np.array_split(row_group, 4, axis=1)
for j, block in enumerate(cols):
reduced_state[i, j] = np.max(block)
state = reduced_state
# Convert to log2 scale (with special handling for 0)
state = np.where(state > 0, np.log2(state), 0).astype(np.float32)
state = state / 11.0 # Normalize by log2(2048)
processed_states.append(state)
X = np.array(processed_states)
# Convert actions to one-hot encoding
action_map = {'up': 0, 'down': 1, 'left': 2, 'right': 3}
y = np.array([action_map[a] for a in actions])
y = tf.keras.utils.to_categorical(y, 4)
return X, y
def create_model():
"""Create the neural network model"""
model = models.Sequential([
layers.Input(shape=(4, 4, 1)),
layers.Conv2D(64, (2, 2), activation='relu', padding='same'),
layers.Conv2D(64, (2, 2), activation='relu', padding='same'),
layers.BatchNormalization(),
layers.Conv2D(128, (2, 2), activation='relu', padding='same'),
layers.Conv2D(128, (2, 2), activation='relu', padding='same'),
layers.BatchNormalization(),
layers.Flatten(),
layers.Dense(256, activation='relu'),
layers.Dropout(0.3),
layers.Dense(128, activation='relu'),
layers.Dropout(0.3),
layers.Dense(4, activation='softmax')
])
model.compile(
optimizer=tf.keras.optimizers.Adam(
learning_rate=GEN_CONFIG['model']['learning_rate']
),
loss='categorical_crossentropy',
metrics=['accuracy']
)
return model
def main():
os.makedirs('models', exist_ok=True)
print(f"Using grid size from config: {GRID_SIZE[0]}x{GRID_SIZE[1]}")
game_files = glob.glob('games/*.txt')
print(f"Found {len(game_files)} game files")
all_states = []
all_actions = []
# Load and combine all game data
for file_path in game_files:
try:
states, actions = load_game_data(file_path)
if len(states) == len(actions): # Verify data consistency
all_states.extend(states)
all_actions.extend(actions)
else:
print(f"Skipping {file_path} - inconsistent data")
except Exception as e:
print(f"Error loading {file_path}: {str(e)}")
print(f"Total number of moves: {len(all_states)}")
if len(all_states) == 0:
print("No valid training data found!")
return
# Preprocess data
print("Preprocessing data...")
X, y = preprocess_data(all_states, all_actions)
X = X.reshape(-1, 4, 4, 1) # Reshape for CNN
print(f"Input shape: {X.shape}")
print(f"Output shape: {y.shape}")
# Calculate split size based on actual data size
validation_split = GEN_CONFIG['model']['validation_split']
split_size = int(len(X) * (1 - validation_split))
# Generate indices only up to data size
indices = np.random.permutation(len(X))
train_idx, val_idx = indices[:split_size], indices[split_size:]
# Split the data
X_train, X_val = X[train_idx], X[val_idx]
y_train, y_val = y[train_idx], y[val_idx]
print(f"Training set size: {len(X_train)}")
print(f"Validation set size: {len(X_val)}")
# Create and train model
print("Creating model...")
model = create_model()
# Callbacks
callbacks = [
tf.keras.callbacks.EarlyStopping(
monitor='val_accuracy',
patience=GEN_CONFIG['model']['early_stopping_patience'],
restore_best_weights=True
)
]
# ModelCheckpoint only if we want to save intermediate models
if GEN_CONFIG['model']['save_frequency'] != 'last_only':
callbacks.append(
tf.keras.callbacks.ModelCheckpoint(
'models/2048_model_{epoch:02d}_{val_accuracy:.3f}.h5',
save_best_only=True,
monitor='val_accuracy'
)
)
# Train the model
print("\nStarting training...")
history = model.fit(
X_train, y_train,
validation_data=(X_val, y_val),
epochs=GEN_CONFIG['model']['epochs'],
batch_size=GEN_CONFIG['model']['batch_size'],
callbacks=callbacks
)
model.save('models/2048_model_final.h5')
print("\nTraining completed!")
print(f"Final training accuracy: {history.history['accuracy'][-1]:.3f}")
print(f"Final validation accuracy: {history.history['val_accuracy'][-1]:.3f}")
if __name__ == '__main__':
main()