Initial commit
This commit is contained in:
0
models/__init__.py
Normal file
0
models/__init__.py
Normal file
BIN
models/__pycache__/__init__.cpython-314.pyc
Normal file
BIN
models/__pycache__/__init__.cpython-314.pyc
Normal file
Binary file not shown.
BIN
models/__pycache__/cnn.cpython-314.pyc
Normal file
BIN
models/__pycache__/cnn.cpython-314.pyc
Normal file
Binary file not shown.
BIN
models/__pycache__/mlp.cpython-314.pyc
Normal file
BIN
models/__pycache__/mlp.cpython-314.pyc
Normal file
Binary file not shown.
80
models/cnn.py
Normal file
80
models/cnn.py
Normal file
@@ -0,0 +1,80 @@
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import torch.optim as optim
|
||||
from torch.utils.data import DataLoader, TensorDataset
|
||||
|
||||
|
||||
class CNN(nn.Module):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.conv = nn.Sequential(
|
||||
nn.Conv2d(1, 32, kernel_size=3), # 28x28 -> 26x26
|
||||
nn.ReLU(),
|
||||
nn.MaxPool2d(2), # 26x26 -> 13x13
|
||||
nn.Conv2d(32, 64, kernel_size=3), # 13x13 -> 11x11
|
||||
nn.ReLU(),
|
||||
nn.MaxPool2d(2), # 11x11 -> 5x5
|
||||
)
|
||||
self.fc = nn.Sequential(
|
||||
nn.Flatten(),
|
||||
nn.Linear(64 * 5 * 5, 128),
|
||||
nn.ReLU(),
|
||||
nn.Linear(128, 10),
|
||||
)
|
||||
|
||||
def forward(self, x):
|
||||
x = x.view(-1, 1, 28, 28)
|
||||
return self.fc(self.conv(x))
|
||||
|
||||
|
||||
class CNNClassifier:
|
||||
def __init__(self, epochs=5):
|
||||
self.epochs = epochs
|
||||
|
||||
def fit(self, X, y):
|
||||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
||||
self._device = device
|
||||
|
||||
X_tr = torch.tensor(X, dtype=torch.float32)
|
||||
y_tr = torch.tensor(y, dtype=torch.long)
|
||||
|
||||
# Hold out 10% of the training data to track progress each epoch
|
||||
n_val = len(X_tr) // 10
|
||||
X_val, X_tr = X_tr[:n_val], X_tr[n_val:]
|
||||
y_val, y_tr = y_tr[:n_val], y_tr[n_val:]
|
||||
|
||||
loader = DataLoader(TensorDataset(X_tr, y_tr), batch_size=64, shuffle=True)
|
||||
|
||||
model = CNN().to(device)
|
||||
optimizer = optim.Adam(model.parameters(), lr=1e-3)
|
||||
loss_fn = nn.CrossEntropyLoss()
|
||||
|
||||
print(f"\nTraining CNN (epochs={self.epochs})")
|
||||
for epoch in range(1, self.epochs + 1):
|
||||
model.train()
|
||||
total_loss = 0
|
||||
for xb, yb in loader:
|
||||
xb, yb = xb.to(device), yb.to(device)
|
||||
optimizer.zero_grad()
|
||||
loss = loss_fn(model(xb), yb)
|
||||
loss.backward()
|
||||
optimizer.step()
|
||||
total_loss += loss.item()
|
||||
|
||||
model.eval()
|
||||
with torch.no_grad():
|
||||
val_pred = model(X_val.to(device)).argmax(dim=1).cpu()
|
||||
val_accuracy = (val_pred == y_val).float().mean().item()
|
||||
|
||||
print(f" epoch {epoch:2d}/{self.epochs} loss={total_loss / len(loader):.3f} val_accuracy={val_accuracy:.3f}")
|
||||
print()
|
||||
|
||||
self._model = model
|
||||
return self
|
||||
|
||||
def predict(self, X):
|
||||
X_te = torch.tensor(X, dtype=torch.float32)
|
||||
self._model.eval()
|
||||
with torch.no_grad():
|
||||
predictions = self._model(X_te.to(self._device)).argmax(dim=1).cpu().numpy()
|
||||
return predictions
|
||||
48
models/handpicked.py
Normal file
48
models/handpicked.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from sklearn.feature_extraction import DictVectorizer
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
from sklearn.pipeline import Pipeline
|
||||
|
||||
|
||||
class FeatureExtractor:
|
||||
def fit(self, X, y=None):
|
||||
return self
|
||||
|
||||
def transform(self, X):
|
||||
return [self.extract_features(pixels) for pixels in X]
|
||||
|
||||
def extract_features(self, pixels):
|
||||
"""Extract hand-designed features from a 784-pixel image.
|
||||
|
||||
Add at least two features of your own. Each feature should be a
|
||||
number computed from the pixel array.
|
||||
|
||||
Arguments:
|
||||
pixels: numpy array of 784 float values in [0, 1]
|
||||
|
||||
Returns:
|
||||
dict: feature name -> numerical value
|
||||
"""
|
||||
img = pixels.reshape(28, 28)
|
||||
return {
|
||||
"mean_brightness": float(pixels.mean()),
|
||||
"top_half_brightness": float(img[:14, :].mean()),
|
||||
"bottom_half_brightness": float(img[14:, :].mean()),
|
||||
# ---- Add your features here ----
|
||||
# "left_half_brightness": float(img[:, :14].mean()),
|
||||
# "right_half_brightness": float(img[:, 14:].mean()),
|
||||
# "num_bright_pixels": float((pixels > 0.5).sum()),
|
||||
}
|
||||
|
||||
|
||||
class HandPickedClassifier:
|
||||
def fit(self, X, y):
|
||||
self._pipeline = Pipeline([
|
||||
("features", FeatureExtractor()),
|
||||
("vectorizer", DictVectorizer()),
|
||||
("classifier", LogisticRegression(max_iter=1000)),
|
||||
])
|
||||
self._pipeline.fit(X, y)
|
||||
return self
|
||||
|
||||
def predict(self, X):
|
||||
return self._pipeline.predict(X)
|
||||
74
models/mlp.py
Normal file
74
models/mlp.py
Normal file
@@ -0,0 +1,74 @@
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import torch.optim as optim
|
||||
from torch.utils.data import DataLoader, TensorDataset
|
||||
|
||||
|
||||
class MLP(nn.Module):
|
||||
def __init__(self, hidden_sizes=(128, 64)):
|
||||
super().__init__()
|
||||
layers = []
|
||||
in_size = 784
|
||||
for h in hidden_sizes:
|
||||
layers.append(nn.Linear(in_size, h))
|
||||
layers.append(nn.ReLU())
|
||||
in_size = h
|
||||
layers.append(nn.Linear(in_size, 10))
|
||||
self.net = nn.Sequential(*layers)
|
||||
|
||||
def forward(self, x):
|
||||
return self.net(x)
|
||||
|
||||
|
||||
class MLPClassifier:
|
||||
def __init__(self, hidden_sizes=(128, 64), epochs=10):
|
||||
self.hidden_sizes = tuple(hidden_sizes)
|
||||
self.epochs = epochs
|
||||
|
||||
def fit(self, X, y):
|
||||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
||||
self._device = device
|
||||
|
||||
X_tr = torch.tensor(X, dtype=torch.float32)
|
||||
y_tr = torch.tensor(y, dtype=torch.long)
|
||||
|
||||
# Hold out 10% of the training data to track progress each epoch
|
||||
n_val = len(X_tr) // 10
|
||||
X_val, X_tr = X_tr[:n_val], X_tr[n_val:]
|
||||
y_val, y_tr = y_tr[:n_val], y_tr[n_val:]
|
||||
|
||||
loader = DataLoader(TensorDataset(X_tr, y_tr), batch_size=64, shuffle=True)
|
||||
|
||||
model = MLP(hidden_sizes=self.hidden_sizes).to(device)
|
||||
optimizer = optim.Adam(model.parameters(), lr=1e-3)
|
||||
loss_fn = nn.CrossEntropyLoss()
|
||||
|
||||
print(f"\nTraining MLP (hidden_sizes={self.hidden_sizes}, epochs={self.epochs})")
|
||||
for epoch in range(1, self.epochs + 1):
|
||||
model.train()
|
||||
total_loss = 0
|
||||
for xb, yb in loader:
|
||||
xb, yb = xb.to(device), yb.to(device)
|
||||
optimizer.zero_grad()
|
||||
loss = loss_fn(model(xb), yb)
|
||||
loss.backward()
|
||||
optimizer.step()
|
||||
total_loss += loss.item()
|
||||
|
||||
model.eval()
|
||||
with torch.no_grad():
|
||||
val_pred = model(X_val.to(device)).argmax(dim=1).cpu()
|
||||
val_accuracy = (val_pred == y_val).float().mean().item()
|
||||
|
||||
print(f" epoch {epoch:2d}/{self.epochs} loss={total_loss / len(loader):.3f} val_accuracy={val_accuracy:.3f}")
|
||||
print()
|
||||
|
||||
self._model = model
|
||||
return self
|
||||
|
||||
def predict(self, X):
|
||||
X_te = torch.tensor(X, dtype=torch.float32)
|
||||
self._model.eval()
|
||||
with torch.no_grad():
|
||||
predictions = self._model(X_te.to(self._device)).argmax(dim=1).cpu().numpy()
|
||||
return predictions
|
||||
11
models/pixels.py
Normal file
11
models/pixels.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
|
||||
|
||||
class PixelClassifier:
|
||||
def fit(self, X, y):
|
||||
self._classifier = LogisticRegression(max_iter=1000)
|
||||
self._classifier.fit(X, y)
|
||||
return self
|
||||
|
||||
def predict(self, X):
|
||||
return self._classifier.predict(X)
|
||||
Reference in New Issue
Block a user