MCPcopy Create free account
hub / github.com/pytorch/tutorials / Net

Class Net

intermediate_source/custom_function_conv_bn_tutorial.py:249–290  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

247memory_allocated = [[],[]]
248
249class Net(nn.Module):
250 def __init__(self, fused=True):
251 super(Net, self).__init__()
252 self.fused = fused
253 if fused:
254 self.convbn1 = FusedConvBN(1, 32, 3)
255 self.convbn2 = FusedConvBN(32, 64, 3)
256 else:
257 self.conv1 = nn.Conv2d(1, 32, 3, 1, bias=False)
258 self.bn1 = nn.BatchNorm2d(32, affine=False, track_running_stats=False)
259 self.conv2 = nn.Conv2d(32, 64, 3, 1, bias=False)
260 self.bn2 = nn.BatchNorm2d(64, affine=False, track_running_stats=False)
261 self.fc1 = nn.Linear(9216, 128)
262 self.dropout = nn.Dropout(0.5)
263 self.fc2 = nn.Linear(128, 10)
264
265 def forward(self, x):
266 if self.fused:
267 x = self.convbn1(x)
268 else:
269 x = self.conv1(x)
270 x = self.bn1(x)
271 F.relu_(x)
272 if self.fused:
273 x = self.convbn2(x)
274 else:
275 x = self.conv2(x)
276 x = self.bn2(x)
277 F.relu_(x)
278 x = F.max_pool2d(x, 2)
279 F.relu_(x)
280 x = x.flatten(1)
281 x = self.fc1(x)
282 x = self.dropout(x)
283 F.relu_(x)
284 x = self.fc2(x)
285 output = F.log_softmax(x, dim=1)
286 if fused:
287 memory_allocated[0].append(torch.cuda.memory_allocated())
288 else:
289 memory_allocated[1].append(torch.cuda.memory_allocated())
290 return output
291
292def train(model, device, train_loader, optimizer, epoch):
293 model.train()

Calls

no outgoing calls

Tested by

no test coverage detected