Last active
December 13, 2018 03:41
-
-
Save elcolie/0958cb96f1153f1b44dd3ac6e714ca74 to your computer and use it in GitHub Desktop.
Snippet record of my best accuracy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 69% | |
# 1. Design model | |
class Net(torch.nn.Module): | |
def __init__(self): | |
super().__init__() | |
self.fc1 = torch.nn.Linear(11, 40) | |
self.fc2 = torch.nn.Linear(40, 40) | |
self.fc3 = torch.nn.Linear(40, 40) | |
self.fc4 = torch.nn.Linear(40, 15) | |
self.fc5 = torch.nn.Linear(15, 1) | |
self.dropout = torch.nn.Dropout(p=0.5) | |
self.tanh = torch.nn.Tanh() | |
# self.sigmoid = torch.nn.Sigmoid() | |
self.relu = torch.nn.ReLU() | |
def forward(self, x): | |
x = self.tanh(self.fc1(x)) | |
x = self.dropout(x) | |
x = self.tanh(self.fc2(x)) | |
x = self.dropout(x) | |
x = self.tanh(self.fc3(x)) | |
x = self.dropout(x) | |
x = self.tanh(self.fc4(x)) | |
x = self.dropout(x) | |
x = self.tanh(self.fc5(x)) | |
x = self.relu(x) | |
return x | |
# 2. Define criterion and optimizer | |
model = Net() | |
criterion = torch.nn.BCEWithLogitsLoss() | |
# optimizer = torch.optim.Adadelta(model.parameters(), lr=0.01) | |
# optimizer = torch.optim.Adam(model.parameters(), lr=0.01) | |
optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9) | |
def train(model, device, train_loader, optimizer, epoch): | |
model.train() | |
for batch_idx, (data, target) in enumerate(train_loader): | |
optimizer.zero_grad() | |
output = model(data) | |
from pprint import pprint | |
# import ipdb; ipdb.set_trace() | |
loss = criterion(output, target) | |
loss.backward() | |
optimizer.step() | |
if batch_idx % 10 == 0: | |
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( | |
epoch, batch_idx * len(data), len(train_loader.dataset), | |
100. * batch_idx / len(train_loader), loss.item())) | |
def test(model, device, test_loader): | |
model.eval() | |
test_loss = 0 | |
correct = 0 | |
with torch.no_grad(): | |
for data, target in test_loader: | |
output = model(data) | |
test_loss += criterion(output, target).item() # sum up batch loss | |
pred = output.max(1, keepdim=True)[1] # get the index of the max log-probability | |
# import ipdb; ipdb.set_trace() | |
correct += pred.eq(target.long().view_as(pred)).sum().item() | |
test_loss /= len(test_loader.dataset) | |
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( | |
test_loss, correct, len(test_loader.dataset), | |
100. * correct / len(test_loader.dataset))) | |
# 3. Train | |
for epoch in range(1, 30): | |
train(model, device, train_loader, optimizer, epoch) | |
test(model, device, test_loader) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment