Skip to content

Instantly share code, notes, and snippets.

View mohcinemadkour's full-sized avatar

Mohcine Madkour mohcinemadkour

View GitHub Profile
labels = ['MLP no dropout',
'MLP 50% dropout in hidden layers',
'MLP 50% dropout in hidden layers + 20% in input layer',
'LeNet no dropout',
'LeNet 50% dropout']
plt.figure(figsize=(8, 7))
for i, r in enumerate(mlp1_test_errors.tolist() + lenet1_test_errors.tolist()):
plt.plot(range(1, len(r)+1), r, '.-', label=labels[i], alpha=0.6);
plt.ylim([50, 250]);
### Below is training code, uncomment to train your own model... ###
### Note: You need GPU and CUDA to run this section ###
'''
# Define networks
lenet1 = [LeNetClassifier(droprate=0, max_epoch=1500),
LeNetClassifier(droprate=0.5, max_epoch=1500)]
# Training, set verbose=True to see loss after each epoch.
[lenet.fit(trainset, testset,verbose=False) for lenet in lenet1]
class LeNetClassifier:
def __init__(self, droprate=0.5, batch_size=128, max_epoch=300, lr=0.01):
self.batch_size = batch_size
self.max_epoch = max_epoch
self.lr = lr
self.model = LeNet(droprate)
self.model.cuda()
self.criterion = nn.CrossEntropyLoss().cuda()
self.optimizer = optim.SGD(self.model.parameters(), lr=lr)
self.loss_ = []
class Flatten(nn.Module):
def __init__(self):
super(Flatten, self).__init__()
def forward(self, x):
x = x.view(x.size(0), -1)
return x
class LeNet(nn.Module):
def __init__(self, droprate=0.5):
def caloutdim(hin, kernel_size, stride=1, padding=0, dilation=1):
return int(np.floor((hin+2*padding-dilation*(kernel_size-1)-1)/stride+1))
d = [28]
d.append(caloutdim(d[-1], 5, padding=2))
d.append(caloutdim(d[-1], 2, 2))
d.append(caloutdim(d[-1], 5, padding=2))
d.append(caloutdim(d[-1], 2, 2))
print(d)
class MLPClassifier:
def __init__(self, hidden_layers=[800, 800], droprates=[0, 0], batch_size=128, max_epoch=10, \
lr=0.1, momentum=0):
# Wrap MLP model
self.hidden_layers = hidden_layers
self.droprates = droprates
self.batch_size = batch_size
self.max_epoch = max_epoch
self.model = MLP(hidden_layers=hidden_layers, droprates=droprates)
self.model.cuda()
labels = ['MLP no dropout',
'MLP 50% dropout in hidden layers',
'MLP 50% dropout in hidden layers + 20% in input layer']
plt.figure(figsize=(8, 7))
for i, r in enumerate(mlp1_test_errors):
plt.plot(range(1, len(r)+1), r, '.-', label=labels[i], alpha=0.6);
plt.ylim([50, 250]);
plt.legend(loc=1);
plt.xlabel('Epochs');
hidden_layers = [800, 800]
### Below is training code, uncomment to train your own model... ###
### Note: You need GPU to run this section ###
'''
# Define networks
mlp1 = [MLPClassifier(hidden_layers, droprates=[0, 0], max_epoch=1500),
MLPClassifier(hidden_layers, droprates=[0, 0.5], max_epoch=1500),
MLPClassifier(hidden_layers, droprates=[0.2, 0.5], max_epoch=1500)]
class MLP(nn.Module):
def __init__(self, hidden_layers=[800, 800], droprates=[0, 0]):
super(MLP, self).__init__()
self.model = nn.Sequential()
self.model.add_module("dropout0",MyDropout(p=droprates[0]))
self.model.add_module("input", nn.Linear(28*28, hidden_layers[0]))
self.model.add_module("tanh", nn.Tanh())
# Add hidden layers
for i,d in enumerate(hidden_layers[:-1]):
class MyDropout(nn.Module):
def __init__(self, p=0.5):
super(MyDropout, self).__init__()
self.p = p
# multiplier is 1/(1-p). Set multiplier to 0 when p=1 to avoid error...
if self.p < 1:
self.multiplier_ = 1.0 / (1.0-p)
else:
self.multiplier_ = 0.0
def forward(self, input):