Skip to content

Commit 29af65e

Browse files
committed
Changed Dropout probability. With High probability takes too long to converge.
1 parent fdca28b commit 29af65e

26 files changed

+70
-22
lines changed
1.32 MB
Binary file not shown.
288 KB
Binary file not shown.
32.1 KB
Binary file not shown.
8.08 KB
Binary file not shown.

data/classifier/bn1_beta.npy

336 Bytes
Binary file not shown.

data/classifier/bn1_gamma.npy

336 Bytes
Binary file not shown.

data/classifier/bn2_beta.npy

336 Bytes
Binary file not shown.

data/classifier/bn2_gamma.npy

336 Bytes
Binary file not shown.

data/classifier/bn3_beta.npy

336 Bytes
Binary file not shown.

data/classifier/bn3_gamma.npy

336 Bytes
Binary file not shown.

data/classifier/bn4_beta.npy

336 Bytes
Binary file not shown.

data/classifier/bn4_gamma.npy

336 Bytes
Binary file not shown.

data/classifier/conv1_bias.npy

336 Bytes
Binary file not shown.
5.28 MB
Binary file not shown.

data/classifier/conv1_weigths.npy

2.33 KB
Binary file not shown.

data/classifier/conv2_bias.npy

336 Bytes
Binary file not shown.

data/classifier/conv2_weigths.npy

144 KB
Binary file not shown.

data/classifier/conv3_bias.npy

336 Bytes
Binary file not shown.

data/classifier/conv3_weigths.npy

144 KB
Binary file not shown.

data/classifier/conv4_bias.npy

336 Bytes
Binary file not shown.

data/classifier/conv4_weigths.npy

64.1 KB
Binary file not shown.

data/classifier/image_input.npy

98.1 KB
Binary file not shown.

datasets/omniglotNShot.py

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def __init__(self, dataroot, batch_size = 100, classes_per_set=10, samples_per_c
4141
np.save(os.path.join(dataroot,'data.npy'),self.x)
4242
else:
4343
self.x = np.load(os.path.join(dataroot,'data.npy'))
44+
# LOAD TENSORFLOW DATA IMPLEMENTATION
45+
#self.x = np.load('/home/aberenguel/TensorFlow/MatchingNetworks/data.npy')
46+
#self.x = np.reshape(self.x, [-1, 20, 28, 28, 1])
4447

4548
"""
4649
Constructs an N-Shot omniglot Dataset

datasets/omniglotNShot.pyc

1.42 KB
Binary file not shown.

main.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@
1515

1616
# Experiment Setup
1717
batch_size = 32
18-
fce = True
18+
fce = False
1919
classes_per_set = 20
2020
samples_per_class = 1
2121
channels = 1
2222
# Training setup
23-
total_epochs = 300
23+
total_epochs = 500
2424
total_train_batches = 1000
2525
total_val_batches = 100
2626
total_test_batches = 250
2727
# Parse other options
2828
args = Options().parse()
2929

30-
LOG_DIR = args.log_dir + '/run-batchSize_{}-fce_{}-classes_per_set{}-samples_per_class{}-channels{}' \
30+
LOG_DIR = args.log_dir + '/3_run-batchSize_{}-fce_{}-classes_per_set{}-samples_per_class{}-channels{}' \
3131
.format(batch_size,fce,classes_per_set,samples_per_class,channels)
3232

3333
# create logger

models/Classifier.py

+64-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import torch
22
import torch.nn as nn
3+
import torch.nn.init as init
34
from torch.autograd import Variable
45
import unittest
56
import numpy as np
@@ -11,11 +12,12 @@ def convLayer(in_planes, out_planes, stride=1, padding = 1, bias = True):
1112
return nn.Sequential(
1213
nn.Conv2d(in_planes, out_planes, kernel_size=3,
1314
stride=stride, padding=padding, bias=bias),
15+
#nn.LeakyReLU(0.2),
1416
nn.LeakyReLU(),
1517
nn.BatchNorm2d(out_planes),
16-
# nn.MaxPool2d(kernel_size=2, stride=2,ceil_mode=True),
18+
#nn.MaxPool2d(kernel_size=2, stride=2,ceil_mode=True),
1719
nn.MaxPool2d(kernel_size=2, stride=2),
18-
nn.Dropout()
20+
nn.Dropout(0.1)
1921
)
2022

2123

@@ -37,32 +39,47 @@ def __init__(self, layer_sizes, num_channels = 1, keep_prob = 0.5):
3739
self.layer3 = convLayer(layer_sizes[1], layer_sizes[2])
3840
self.layer4 = convLayer(layer_sizes[2], layer_sizes[3])
3941

42+
4043
self.weights_init(self.layer1)
4144
self.weights_init(self.layer2)
4245
self.weights_init(self.layer3)
4346
self.weights_init(self.layer4)
4447
'''
45-
# Module initialization
46-
for m in self.modules():
48+
self.weights_init_tensorflow(self.layer1,1)
49+
self.weights_init_tensorflow(self.layer2,2)
50+
self.weights_init_tensorflow(self.layer3,3)
51+
self.weights_init_tensorflow(self.layer4,4)
52+
'''
53+
54+
def weights_init(self,module):
55+
#for m in self.modules():
56+
for m in module.modules():
4757
if isinstance(m, nn.Conv2d):
48-
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
49-
m.weight.data.normal_(0, math.sqrt(2. / n))
58+
init.xavier_uniform(m.weight, gain=np.sqrt(2))
59+
init.constant(m.bias, 0)
60+
##m.weight.data.normal_(0.0, 0.02)
61+
#n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
62+
#m.weight.data.normal_(0, math.sqrt(2. / n))
5063
elif isinstance(m, nn.BatchNorm2d):
64+
#m.weight.data.normal_(1.0, 0.02)
65+
#m.bias.data.fill_(0)
5166
m.weight.data.fill_(1)
5267
m.bias.data.zero_()
53-
'''
5468

55-
def weights_init(self,m):
56-
for m in self.modules():
69+
def weights_init_tensorflow(self,module,layer):
70+
#for m in self.modules():
71+
for m in module.modules():
5772
if isinstance(m, nn.Conv2d):
58-
m.weight.data.normal_(0.0, 0.02)
59-
#n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
60-
#m.weight.data.normal_(0, math.sqrt(2. / n))
73+
m.weight.data = torch.from_numpy(np.load('/home/aberenguel/pytorch/examples/MatchingNetworks/data/classifier/conv' + str(layer) + '_weigths.npy').transpose((3,2,1,0)))
74+
m.weight.data = m.weight.data.contiguous()
75+
m.bias.data = torch.from_numpy(np.load('/home/aberenguel/pytorch/examples/MatchingNetworks/data/classifier/conv' + str(layer) + '_bias.npy'))
6176
elif isinstance(m, nn.BatchNorm2d):
62-
m.weight.data.normal_(1.0, 0.02)
63-
m.bias.data.fill_(0)
64-
#m.weight.data.fill_(1)
65-
#m.bias.data.zero_()
77+
m.weight.data = torch.from_numpy(np.load(
78+
'/home/aberenguel/pytorch/examples/MatchingNetworks/data/classifier/bn' + str(
79+
layer) + '_gamma.npy'))
80+
m.bias.data = torch.from_numpy(np.load(
81+
'/home/aberenguel/pytorch/examples/MatchingNetworks/data/classifier/bn' + str(
82+
layer) + '_beta.npy'))
6683

6784

6885
def forward(self, image_input):
@@ -71,19 +88,20 @@ def forward(self, image_input):
7188
:param image_input: Image input to produce embeddings for. [batch_size, 28, 28, 1]
7289
:return: Embeddings of size [batch_size, 64]
7390
"""
74-
# TODO: What is better 2 padding at first conv2d or 1 padding in the last layer of conv2d??
75-
#x = nn.Conv2d(1, 64, kernel_size=3, stride=1, bias=True).cuda()(image_input)
91+
92+
#x = nn.Conv2d(1, 64, kernel_size=3, stride=1, padding=0, bias=True).cuda()(image_input)
7693
#x = nn.LeakyReLU().cuda()(x)
7794
#x = nn.BatchNorm2d(64).cuda()(x)
7895
#x = nn.MaxPool2d(kernel_size=2, stride=2).cuda()(x)
7996
#x = nn.Dropout().cuda()(x)
8097

98+
#check = np.sum(self.layer1[0](image_input).data.cpu().numpy())
99+
81100
x = self.layer1(image_input)
82101
x = self.layer2(x)
83102
x = self.layer3(x)
84103
x = self.layer4(x)
85104
x = torch.squeeze(x)
86-
# TODO: flat output
87105
return x
88106

89107

@@ -94,11 +112,38 @@ def setUp(self):
94112
self.outputs = np.load('../data/gen_encode.npy')
95113
self.layer_sizes = [64,64,64,64]
96114

115+
self.after_conv1 = np.load(
116+
'/home/aberenguel/pytorch/examples/MatchingNetworks/data/classifier/after_conv1_encoder.npy')
117+
self.after_conv2 = np.load(
118+
'/home/aberenguel/pytorch/examples/MatchingNetworks/data/classifier/after_conv2_encoder.npy')
119+
self.after_conv3 = np.load(
120+
'/home/aberenguel/pytorch/examples/MatchingNetworks/data/classifier/after_conv3_encoder.npy')
121+
self.after_conv4 = np.load(
122+
'/home/aberenguel/pytorch/examples/MatchingNetworks/data/classifier/after_conv4_encoder.npy')
123+
self.image_input = np.load(
124+
'/home/aberenguel/pytorch/examples/MatchingNetworks/data/classifier/image_input.npy')
125+
97126
def tearDown(self):
98127
pass
99128

100129
def test_forward(self):
101130
classifier = Classifier(layer_sizes=self.layer_sizes).cuda()
131+
132+
print("sum conv1 tf: %f" % np.sum(self.after_conv1))
133+
print("sum conv2 tf: %f" % np.sum(self.after_conv2))
134+
print("sum conv3 tf: %f" % np.sum(self.after_conv3))
135+
print("sum conv4 tf: %f" % np.sum(self.after_conv4))
136+
input = Variable(torch.from_numpy(self.image_input.transpose((0,3,1,2))).cuda(), requires_grad=True)
137+
138+
x1 = classifier.layer1(input)
139+
print("sum conv1 pytorch: %f" % torch.sum(x1).data[0])
140+
x2 = classifier.layer2(x1)
141+
print("sum conv2 pytorch: %f" % torch.sum(x2).data[0])
142+
x3 = classifier.layer3(x2)
143+
print("sum conv2 pytorch: %f" % torch.sum(x3).data[0])
144+
x4 = classifier.layer4(x3)
145+
print("sum conv2 pytorch: %f" % torch.sum(x4).data[0])
146+
102147
input = Variable(torch.from_numpy(self.inputs).cuda(), requires_grad=True)
103148
output = classifier(input)
104149
# TODO: why the output contains so many 0? The self.outputs d

0 commit comments

Comments
 (0)