def test_weight_minimization(self): base_network = reproducible_network_train() decay_network = reproducible_network_train( decay_rate=0.1, addons=[algorithms.WeightDecay]) iter_networks = zip(base_network.layers, decay_network.layers) for net_layer, decay_layer in iter_networks: self.assertGreater( np.linalg.norm(net_layer.weight.get_value()), np.linalg.norm(decay_layer.weight.get_value()), )
def test_weight_minimization(self): base_network = reproducible_network_train() decay_network = reproducible_network_train( decay_rate=0.1, addons=[algorithms.WeightDecay] ) iter_networks = zip(base_network.layers[1:-1], decay_network.layers[1:-1]) for net_layer, decay_layer in iter_networks: self.assertGreater( np.linalg.norm(net_layer.weight.get_value()), np.linalg.norm(decay_layer.weight.get_value()), )
def test_with_step_minimization_alg(self): default_step = 0.3 net1 = reproducible_network_train(step=default_step) net2 = reproducible_network_train( step=default_step, decay_rate=0.25, zero_weight=10, addons=[algorithms.WeightElimination] ) net3 = reproducible_network_train( step=default_step, decay_rate=0.25, zero_weight=10, addons=[algorithms.WeightElimination, algorithms.StepDecay] ) # Check that step is valid for each network StepCase = namedtuple('StepCase', 'network expected_step') step_test_cases = ( StepCase(network=net1, expected_step=default_step), StepCase(network=net2, expected_step=default_step), StepCase(network=net3, expected_step=default_step / 6.), ) for case in step_test_cases: step = case.network.variables.step self.assertAlmostEqual(step.get_value(), case.expected_step, places=5) # Compare weight norm between networks WeightNormCase = namedtuple('WeightNormCase', 'with_smaller_norm with_bigger_norm') norm_test_cases = ( WeightNormCase(with_smaller_norm=net2, with_bigger_norm=net1), WeightNormCase(with_smaller_norm=net3, with_bigger_norm=net1), ) for case in norm_test_cases: network_layers = zip(case.with_smaller_norm.layers[1:-1], case.with_bigger_norm.layers[1:-1]) for smaller_norm, bigger_norm in network_layers: weight_smaller_norm = smaller_norm.weight.get_value() weight_bigger_norm = bigger_norm.weight.get_value() self.assertGreater( np.linalg.norm(weight_bigger_norm), np.linalg.norm(weight_smaller_norm) )
def test_with_step_minimization_alg(self): default_step = 0.3 net1 = reproducible_network_train(step=default_step) net2 = reproducible_network_train( step=default_step, decay_rate=0.25, zero_weight=10, addons=[algorithms.WeightElimination] ) net3 = reproducible_network_train( step=default_step, decay_rate=0.25, zero_weight=10, addons=[algorithms.WeightElimination, algorithms.StepDecay] ) # Check that step is valid for each network StepCase = namedtuple('StepCase', 'network expected_step') step_test_cases = ( StepCase(network=net1, expected_step=default_step), StepCase(network=net2, expected_step=default_step), StepCase(network=net3, expected_step=default_step / 6.), ) for case in step_test_cases: step = case.network.variables.step self.assertAlmostEqual(step.get_value(), case.expected_step, places=2) # Compare weight norm between networks WeightNormCase = namedtuple('WeightNormCase', 'with_smaller_norm with_bigger_norm') norm_test_cases = ( WeightNormCase(with_smaller_norm=net2, with_bigger_norm=net1), WeightNormCase(with_smaller_norm=net3, with_bigger_norm=net1), ) for case in norm_test_cases: network_layers = zip(case.with_smaller_norm.layers[1:-1], case.with_bigger_norm.layers[1:-1]) for smaller_norm, bigger_norm in network_layers: weight_smaller_norm = smaller_norm.weight.get_value() weight_bigger_norm = bigger_norm.weight.get_value() self.assertGreater( np.linalg.norm(weight_bigger_norm), np.linalg.norm(weight_smaller_norm) )
def test_log_scale(self): original_image_name = format_image_name("log_scale.png") original_image = os.path.join(IMGDIR, original_image_name) with image_comparison(original_image) as fig: ax = fig.add_subplot(1, 1, 1) network = reproducible_network_train(step=0.3) network.plot_errors(logx=True, ax=ax, show=False)
def test_log_scale(self): original_image_name = format_image_name("log_scale.png") original_image = os.path.join(IMGDIR, original_image_name) with image_comparison(original_image) as fig: ax = fig.add_subplot(1, 1, 1) network = reproducible_network_train(step=0.3) plots.error_plot(network, logx=True, ax=ax, show=False)
def test_simple_plot(self): original_image_name = format_image_name("simple_plot.png") original_image = os.path.join(IMGDIR, original_image_name) with image_comparison(original_image) as fig: ax = fig.add_subplot(1, 1, 1) network = reproducible_network_train(step=0.3) plots.error_plot(network, ax=ax, show=False)
def test_simple_plot(self): original_image_name = format_image_name("simple_plot.png") original_image = os.path.join(IMGDIR, original_image_name) with image_comparison(original_image) as fig: ax = fig.add_subplot(1, 1, 1) network = reproducible_network_train(step=0.3) network.plot_errors(ax=ax, show=False)
def test_weight_minimization(self): base_network = reproducible_network_train() decay_network = reproducible_network_train( decay_rate=0.3, zero_weight=5, addons=[algorithms.WeightElimination]) iter_networks = zip( base_network.layers[1:-1], decay_network.layers[1:-1], ) for net_layer, decay_layer in iter_networks: self.assertGreater( np.linalg.norm(self.eval(net_layer.weight)), np.linalg.norm(self.eval(decay_layer.weight)), )
def test_with_step_minimization_alg(self): default_step = 0.3 net1 = reproducible_network_train(step=default_step) net2 = reproducible_network_train(step=default_step, addons=[algorithms.WeightDecay]) net3 = reproducible_network_train(step=default_step, addons=[ algorithms.WeightDecay, algorithms.StepDecay, ]) # Check that step is valid for each network StepCase = namedtuple('StepCase', 'network expected_step') step_test_cases = ( StepCase(network=net1, expected_step=default_step), StepCase(network=net2, expected_step=default_step), StepCase(network=net3, expected_step=default_step / 6.), ) for case_index, case in enumerate(step_test_cases, start=1): step = case.network.variables.step self.assertAlmostEqual(self.eval(step), case.expected_step, places=5, msg="Test case #{}".format(case_index)) # Compare weight norm between networks WeightNormCase = namedtuple('WeightNormCase', 'with_smaller_norm with_bigger_norm') norm_test_cases = ( WeightNormCase(with_smaller_norm=net2, with_bigger_norm=net1), WeightNormCase(with_smaller_norm=net3, with_bigger_norm=net1), ) for case in norm_test_cases: network_layers = zip( case.with_smaller_norm.layers[1:-1], case.with_bigger_norm.layers[1:-1], ) for smaller_norm, bigger_norm in network_layers: weight_smaller_norm = self.eval(smaller_norm.weight) weight_bigger_norm = self.eval(bigger_norm.weight) self.assertGreater(np.linalg.norm(weight_bigger_norm), np.linalg.norm(weight_smaller_norm))
def test_error_plot_show_image(self): def mock_plt_show(): pass # Test suppose not to fail real_plt_show = plt.show plt.show = mock_plt_show network = reproducible_network_train(step=0.3) plots.error_plot(network, show=True) plt.show = real_plt_show