Thanks for the code.
I cannot reproduce the issue and get the same outputs after calling model.eval()
:
class CharacterLevelCNN(nn.Module):
def __init__(self, number_of_classes):
super(CharacterLevelCNN, self).__init__()
# define conv layers
# RLJ changed to 1d due to runtime warnings
self.dropout_input = nn.Dropout1d(0.5)
#RLJ introduced the large feature size via arg modelfeaturesize
convlayerfeaturesize = 256
fclayerfeaturesize=1024
modelfeaturesize = "small"
if modelfeaturesize=="large":
convlayerfeaturesize=1024
fclayerfeaturesize=2048
print("Conv Layer Feature Size {}".format(convlayerfeaturesize))
print("FC Layer Feature Size {}".format(fclayerfeaturesize))
number_of_characters = 69
self.conv1 = nn.Sequential(
nn.Conv1d(
number_of_characters + len(""),
convlayerfeaturesize,
kernel_size=7,
padding=0,
),
nn.ReLU(),
nn.MaxPool1d(3),
)
self.conv2 = nn.Sequential(
nn.Conv1d(convlayerfeaturesize, convlayerfeaturesize, kernel_size=7, padding=0), nn.ReLU(), nn.MaxPool1d(3)
)
self.conv3 = nn.Sequential(
nn.Conv1d(convlayerfeaturesize, convlayerfeaturesize, kernel_size=3, padding=0), nn.ReLU()
)
self.conv4 = nn.Sequential(
nn.Conv1d(convlayerfeaturesize, convlayerfeaturesize, kernel_size=3, padding=0), nn.ReLU()
)
self.conv5 = nn.Sequential(
nn.Conv1d(convlayerfeaturesize, convlayerfeaturesize, kernel_size=3, padding=0), nn.ReLU()
)
self.conv6 = nn.Sequential(
nn.Conv1d(convlayerfeaturesize, convlayerfeaturesize, kernel_size=3, padding=0), nn.ReLU(), nn.MaxPool1d(3)
)
# compute the output shape after forwarding an input to the conv layers
input_shape = (
16,
10000,
number_of_characters + len(""),
)
print("FC Input Shape {}".format(input_shape))
self.output_dimension = self._get_conv_output(input_shape)
print("FC Input Size {}".format(self.output_dimension))
# define linear layers
self.fc1 = nn.Sequential(
nn.Linear(self.output_dimension, fclayerfeaturesize), nn.ReLU(), nn.Dropout(0.5)
)
self.fc2 = nn.Sequential(nn.Linear(fclayerfeaturesize, fclayerfeaturesize), nn.ReLU(), nn.Dropout(0.5))
self.fc3 = nn.Linear(fclayerfeaturesize, number_of_classes)
# initialize weights
#RLJ changed from default values of 0.0 and 0.05 to the mean and std per the Yan Lecun paper based on whether it is a large or small model
#self._create_weights()
if modelfeaturesize=="small":
self._create_weights(0, 0.05)
else:
self._create_weights(0, 0.02)
# utility private functions
def _create_weights(self, mean=0.0, std=0.05):
for module in self.modules():
if isinstance(module, nn.Conv1d) or isinstance(module, nn.Linear):
module.weight.data.normal_(mean, std)
def _get_conv_output(self, shape):
x = torch.rand(shape)
x = x.transpose(1, 2)
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.conv5(x)
x = self.conv6(x)
x = x.view(x.size(0), -1)
output_dimension = x.size(1)
return output_dimension
# forward
def forward(self, x):
x = self.dropout_input(x)
x = x.transpose(1, 2)
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.conv5(x)
x = self.conv6(x)
x = x.view(x.size(0), -1)
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
return x
model = CharacterLevelCNN(10)
x = torch.randn(16, 10000, 69)
# all are different since dropout is still active
for _ in range(10):
out = model(x)
print(out.double().abs().sum())
# tensor(19179.5103, dtype=torch.float64, grad_fn=<SumBackward0>)
# tensor(19980.1256, dtype=torch.float64, grad_fn=<SumBackward0>)
# tensor(19489.9541, dtype=torch.float64, grad_fn=<SumBackward0>)
# tensor(21702.8869, dtype=torch.float64, grad_fn=<SumBackward0>)
# tensor(19419.2389, dtype=torch.float64, grad_fn=<SumBackward0>)
# tensor(19746.8942, dtype=torch.float64, grad_fn=<SumBackward0>)
# tensor(17694.0387, dtype=torch.float64, grad_fn=<SumBackward0>)
# tensor(19148.0841, dtype=torch.float64, grad_fn=<SumBackward0>)
# tensor(18543.3931, dtype=torch.float64, grad_fn=<SumBackward0>)
# tensor(18682.2872, dtype=torch.float64, grad_fn=<SumBackward0>)
# now all outputs are equal
model.eval()
for _ in range(10):
out = model(x)
print(out.double().abs().sum())
# tensor(5934.1699, dtype=torch.float64, grad_fn=<SumBackward0>)
# tensor(5934.1699, dtype=torch.float64, grad_fn=<SumBackward0>)
# tensor(5934.1699, dtype=torch.float64, grad_fn=<SumBackward0>)
# tensor(5934.1699, dtype=torch.float64, grad_fn=<SumBackward0>)
# tensor(5934.1699, dtype=torch.float64, grad_fn=<SumBackward0>)
# tensor(5934.1699, dtype=torch.float64, grad_fn=<SumBackward0>)
# tensor(5934.1699, dtype=torch.float64, grad_fn=<SumBackward0>)
# tensor(5934.1699, dtype=torch.float64, grad_fn=<SumBackward0>)
# tensor(5934.1699, dtype=torch.float64, grad_fn=<SumBackward0>)
# tensor(5934.1699, dtype=torch.float64, grad_fn=<SumBackward0>)