MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / test_lr_finder

Method test_lr_finder

tests/optimizers/test_lr_finder.py:65–104  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

63 )
64
65 def test_lr_finder(self):
66 # 0.001 gives 54 examples
67 with skip_if_downloading_fails():
68 train_ds = MedNISTDataset(
69 root_dir=self.root_dir,
70 transform=self.transforms,
71 section="validation",
72 val_frac=0.001,
73 download=True,
74 num_workers=2,
75 )
76 train_loader = DataLoader(train_ds, batch_size=300, shuffle=True, num_workers=2)
77 num_classes = train_ds.get_num_classes()
78
79 model = DenseNet(
80 spatial_dims=2, in_channels=1, out_channels=num_classes, init_features=2, growth_rate=2, block_config=(2,)
81 )
82 loss_function = torch.nn.CrossEntropyLoss()
83 learning_rate = 1e-5
84 optimizer = torch.optim.Adam(model.parameters(), learning_rate)
85
86 lr_finder = LearningRateFinder(
87 model=model,
88 optimizer=optimizer,
89 criterion=loss_function,
90 device=device,
91 pickle_module=pickle,
92 pickle_protocol=4,
93 )
94 lr_finder.range_test(train_loader, val_loader=train_loader, end_lr=10.0, num_iter=5)
95 print(lr_finder.get_steepest_gradient(0, 0)[0])
96
97 if has_matplotlib:
98 ax = plt.subplot()
99 plt.show(block=False)
100 lr_finder.plot(0, 0, ax=ax) # to inspect the loss-learning rate graph
101 plt.pause(3)
102 plt.close()
103
104 lr_finder.reset() # to reset the model and optimizer to their initial state
105
106
107if __name__ == "__main__":

Callers

nothing calls this directly

Calls 11

get_num_classesMethod · 0.95
range_testMethod · 0.95
get_steepest_gradientMethod · 0.95
plotMethod · 0.95
resetMethod · 0.95
MedNISTDatasetClass · 0.90
DataLoaderClass · 0.90
DenseNetClass · 0.90
LearningRateFinderClass · 0.90
closeMethod · 0.45

Tested by

no test coverage detected