Exemplo n.º 1
0
def test_gradient_calculator_multiply_target():
    model = DummyModel()
    x = numpy.array([[1, 5, 8]], dtype=numpy.float32)
    calculator = GradientCalculator(model, multiply_target=True)
    saliency = calculator.compute(x)
    # gradient * input
    assert numpy.allclose(saliency, numpy.array([[1, 15, 16]]))
Exemplo n.º 2
0
def test_gradient_calculator():
    model = DummyModel()
    x = numpy.array([[1, 5, 8]], dtype=numpy.float32)
    calculator = GradientCalculator(model)
    saliency = calculator.compute(x)
    # Gradient is equal to `initialW` of DummyModel.
    assert numpy.allclose(saliency, numpy.array([[1, 3, 2]]))
def test_gradient_calculator_multiply_target():
    model = DummyModel()
    x = numpy.array([[1, 5, 8]], dtype=numpy.float32)
    calculator = GradientCalculator(model, multiply_target=True)
    saliency = calculator.compute(x)
    # gradient * input
    assert numpy.allclose(saliency, numpy.array([[1, 15, 16]]))
def test_gradient_calculator():
    model = DummyModel()
    x = numpy.array([[1, 5, 8]], dtype=numpy.float32)
    calculator = GradientCalculator(model)
    saliency = calculator.compute(x)
    # Gradient is equal to `initialW` of DummyModel.
    assert numpy.allclose(saliency, numpy.array([[1, 3, 2]]))
Exemplo n.º 5
0
def test_gradient_calculator_multiple_output():
    model = DummyModel()
    x = numpy.array([[1, 5, 8], [2, 3, 4]], dtype=numpy.float32)
    calculator = GradientCalculator(model)
    # even batchsize=2 sum is applied automatically inside `compute`,
    # so gradient can be calculated.
    saliency = calculator.compute(x)
    # Gradient is equal to `initialW` of DummyModel.
    assert numpy.allclose(saliency, numpy.array([[1, 3, 2]]))
Exemplo n.º 6
0
def test_gradient_calculator_target_extractor():
    model = DummyModel()
    x = numpy.array([[1, 5, 8]], dtype=numpy.float32)
    calculator = GradientCalculator(
        model,
        target_extractor=VariableMonitorLinkHook(model.l1, timing='pre'))
    saliency = calculator.compute(x)
    # Gradient is equal to `initialW` of DummyModel.
    assert numpy.allclose(saliency, numpy.array([[[1, 3, 2]]]))
    assert saliency.shape == (1, 1, 3)
def test_gradient_calculator_target_extractor():
    model = DummyModel()
    x = numpy.array([[1, 5, 8]], dtype=numpy.float32)
    calculator = GradientCalculator(
        model,
        target_extractor=VariableMonitorLinkHook(model.l1, timing='pre'))
    saliency = calculator.compute(x)
    # Gradient is equal to `initialW` of DummyModel.
    assert numpy.allclose(saliency, numpy.array([[[1, 3, 2]]]))
    assert saliency.shape == (1, 1, 3)