I am trying to understand a code, I am confused about the test cells. When i am printing the shape of the output it is hidden_output.shape =(num_test, 20, 4, 4), test_hidden_block_stride(hidden_output).shape) == (num_test, 20, 10, 10) and Gen_output.shape=(num_test, 1,28,28) for Mnist dataset. I am trying to understand how the sizes are being calculated here. Any help will be greatly appreciated!
class Generator(nn.Module):
def __init__(self, z_dim=10, im_chan=1, hidden_dim=64):
super(Generator, self).__init__()
self.z_dim = z_dim
# Build the neural network
self.gen = nn.Sequential(
self.make_gen_block(z_dim, hidden_dim * 4),
self.make_gen_block(hidden_dim * 4, hidden_dim * 2, kernel_size=4, stride=1),
self.make_gen_block(hidden_dim * 2, hidden_dim),
self.make_gen_block(hidden_dim, im_chan, kernel_size=4, final_layer=True),
)
def make_gen_block(self, input_channels, output_channels, kernel_size=3, stride=2, padding=0 ,final_layer=False):
# Build the neural block
layers = []
layers.append(nn.ConvTranspose2d(input_channels, output_channels, kernel_size, stride, padding, output_padding=padding))
if not final_layer:
layers.append(nn.BatchNorm2d(output_channels))
layers.append(nn.ReLU(True))
else:
layers.append(nn.Tanh())
return nn.Sequential(*layers)
# Testing
gen = Generator()
num_test = 100
# Test the hidden block
test_hidden_noise = get_noise(num_test, gen.z_dim)
test_hidden_block = gen.make_gen_block(10, 20, kernel_size=4, stride=1)
test_uns_noise = gen.unsqueeze_noise(test_hidden_noise)
hidden_output = test_hidden_block(test_uns_noise)
# Check that it works with other strides
test_hidden_block_stride = gen.make_gen_block(20, 20, kernel_size=4, stride=2)
test_final_noise = get_noise(num_test, gen.z_dim) * 20
test_final_block = gen.make_gen_block(10, 20, final_layer=True)
test_final_uns_noise = gen.unsqueeze_noise(test_final_noise)
final_output = test_final_block(test_final_uns_noise)
# Test the whole thing:
test_gen_noise = get_noise(num_test, gen.z_dim)
test_uns_gen_noise = gen.unsqueeze_noise(test_gen_noise)
gen_output = gen(test_uns_gen_noise)
I am trying to calculate the sizes from the formula by hand. I am just seeing different kernel sizes and strides and padding. not sure which values to use.
sifat karim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.