def test_anchors(self):
        x = Input((2, ))
        z1 = Dense(2)(x)
        z2 = Activation('relu')(z1)
        y = Dense(1)(z2)

        k_model = Model(x, y)
        k_model.set_weights([
            np.array([[1., 0.], [0., -1.]]),
            np.array([0., 0.]),
            np.array([[1.], [1.]]),
            np.array([0.])
        ])

        model = ModelWrapper(k_model)

        infl_out = InternalInfluence(model,
                                     Cut(2, anchor='out'),
                                     ClassQoI(0),
                                     PointDoi(),
                                     multiply_activation=False)

        infl_in = InternalInfluence(model,
                                    Cut(2, anchor='in'),
                                    ClassQoI(0),
                                    PointDoi(),
                                    multiply_activation=False)

        res_out = infl_out.attributions(np.array([[1., 1.]]))
        res_in = infl_in.attributions(np.array([[1., 1.]]))

        self.assertEqual(res_out.shape, (1, 2))
        self.assertEqual(res_in.shape, (1, 2))
        self.assertTrue(np.allclose(res_out, np.array([[1., 1.]])))
        self.assertTrue(np.allclose(res_in, np.array([[1., 0.]])))
Exemple #2
0
    def test_internal_multiple_inputs(self):

        class ConcatenateLayer(Module):

            def forward(this, x1, x2):
                return cat((x1, x2), 1)

        class M(Module):

            def __init__(this):
                super(M, this).__init__()
                this.z1 = Linear(5, 6)
                this.concat = ConcatenateLayer()
                this.z3 = Linear(7, 7)
                this.y = Linear(7, 3)

            def forward(this, x1, x2):
                x1 = this.z1(x1)
                z = this.concat(x1, x2)
                z = this.z3(z)
                return this.y(z)

        model = ModelWrapper(M(), [(5,), (1,)])

        infl = InternalInfluence(
            model, Cut('concat', anchor='in'), ClassQoI(1), PointDoi())

        res = infl.attributions(
            np.array([[1., 2., 3., 4., 5.]]).astype('float32'),
            np.array([[1.]]).astype('float32'))

        self.assertEqual(len(res), 2)
        self.assertEqual(res[0].shape, (1, 6))
        self.assertEqual(res[1].shape, (1, 1))
Exemple #3
0
    def test_internal_slice_multiple_layers(self):

        class M(Module):

            def __init__(this):
                super(M, this).__init__()
                this.cut_layer1 = Linear(5, 6)
                this.cut_layer2 = Linear(1, 2)
                this.z3 = Linear(2, 4)
                this.z5 = Linear(10, 7)
                this.y = Linear(7, 3)

            def forward(this, x1, x2):
                z1 = this.cut_layer1(x1)
                z2 = this.cut_layer2(x2)
                z3 = this.z3(z2)
                z4 = cat((z1, z3), 1)
                z5 = this.z5(z4)
                return this.y(z5)

        model = ModelWrapper(M(), [(5,), (1,)])

        infl = InternalInfluence(
            model, Cut(['cut_layer1', 'cut_layer2']), ClassQoI(1), PointDoi())

        res = infl.attributions(
            np.array([[1., 2., 3., 4., 5.]]).astype('float32'),
            np.array([[1.]]).astype('float32'))

        self.assertEqual(len(res), 2)
        self.assertEqual(res[0].shape, (1, 6))
        self.assertEqual(res[1].shape, (1, 2))
    def test_internal_slice_multiple_layers(self):
        graph = Graph()

        with graph.as_default():
            x1 = tf.placeholder('float32', (None, 5))
            z1 = x1 @ tf.random.normal((5, 6))
            x2 = tf.placeholder('float32', (None, 1))
            z2 = x2 @ tf.random.normal((1, 2))
            z3 = z2 @ tf.random.normal((2, 4))
            z4 = tf.concat([z1, z3], axis=1)
            z5 = z4 @ tf.random.normal((10, 7))
            y = z5 @ tf.random.normal((7, 3))

        model = ModelWrapper(
            graph, [x1, x2], y, dict(cut_layer1=z1, cut_layer2=z2))

        infl = InternalInfluence(
            model, Cut(['cut_layer1', 'cut_layer2']), ClassQoI(1), PointDoi())

        res = infl.attributions(
            [np.array([[1., 2., 3., 4., 5.]]),
             np.array([[1.]])])

        self.assertEqual(len(res), 2)
        self.assertEqual(res[0].shape, (1, 6))
        self.assertEqual(res[1].shape, (1, 2))
Exemple #5
0
    def test_anchors(self):

        class M(Module):

            def __init__(this):
                super(M, this).__init__()
                this.z1 = Linear(2, 2)
                this.z2 = ReLU()
                this.y = Linear(2, 1)

                this.z1.weight.data = B.as_tensor(
                    np.array([[1., 0.], [0., -1.]]).T)
                this.z1.bias.data = B.as_tensor(np.array([0., 0.]))
                this.y.weight.data = B.as_tensor(np.array([[1.], [1.]]).T)
                this.y.bias.data = B.as_tensor(np.array([0.]))

            def forward(this, x):
                z1 = this.z1(x)
                z2 = this.z2(z1)
                return this.y(z2)

        model = ModelWrapper(M(), (2,))

        infl_out = InternalInfluence(
            model,
            Cut('z2', anchor='out'),
            ClassQoI(0),
            PointDoi(),
            multiply_activation=False)

        infl_in = InternalInfluence(
            model,
            Cut('z2', anchor='in'),
            ClassQoI(0),
            PointDoi(),
            multiply_activation=False)

        res_out = infl_out.attributions(np.array([[1., 1.]]))
        res_in = infl_in.attributions(np.array([[1., 1.]]))

        self.assertEqual(res_out.shape, (1, 2))
        self.assertEqual(res_in.shape, (1, 2))
        self.assertTrue(np.allclose(res_out, np.array([[1., 1.]])))
        self.assertTrue(np.allclose(res_in, np.array([[1., 0.]])))
    def test_catch_cut_index_error(self):
        x = Input((2, ))
        z1 = Dense(2)(x)
        z2 = Activation('relu')(z1)
        y = Dense(1)(z2)

        model = ModelWrapper(Model(x, y))

        with self.assertRaises(ValueError):
            infl = InternalInfluence(model, Cut(4), ClassQoI(0), PointDoi())

            infl.attributions(np.array([[1., 1.]]))
    def test_linear_agreement_multiply_activation(self):
        c = 1
        infl = InternalInfluence(
            self.model_lin,
            InputCut(),
            ClassQoI(c),
            PointDoi(),
            multiply_activation=True)

        res = infl.attributions(self.x)

        self.assertEqual(res.shape, (2, self.input_size))

        self.assertTrue(np.allclose(res, self.model_lin_weights[:, c] * self.x))
    def test_distributional_linearity_internal_influence(self):
        x1, x2 = self.x[0:1], self.x[1:]
        p1, p2 = 0.25, 0.75

        class DistLinDoI(DoI):
            '''
            Represents the distribution of interest that weights `z` with
            probability 1/4 and `z + diff` with probability 3/4.
            '''

            def __init__(self, diff):
                super(DistLinDoI, self).__init__()
                self.diff = diff

            def __call__(self, z):
                return [z, z + self.diff, z + self.diff, z + self.diff]

        infl_pt = InternalInfluence(
            self.model_deep,
            Cut(self.layer2),
            ClassQoI(0),
            PointDoi(),
            multiply_activation=False)

        attr1 = infl_pt.attributions(x1)
        attr2 = infl_pt.attributions(x2)

        infl_dl = InternalInfluence(
            self.model_deep,
            Cut(self.layer2),
            ClassQoI(0),
            DistLinDoI(x2 - x1),
            multiply_activation=False)

        attr12 = infl_dl.attributions(x1)

        self.assertTrue(np.allclose(attr12, p1 * attr1 + p2 * attr2))
    def test_completeness_zero_baseline(self):
        c = 2
        infl = InternalInfluence(
            self.model_deep,
            InputCut(),
            ClassQoI(c),
            LinearDoi(resolution=100),
            multiply_activation=True)

        out_x = self.model_deep.fprop((self.x,))[0][:, c]
        out_baseline = self.model_deep.fprop((self.baseline * 0,))[0][:, c]

        res = infl.attributions(self.x)

        self.assertTrue(
            np.allclose(res.sum(axis=1), out_x - out_baseline, atol=5e-2))
    def test_catch_cut_name_error(self):
        graph = Graph()

        with graph.as_default():
            x = tf.placeholder('float32', (None, 2))
            z1 = x @ tf.random.normal((2, 2))
            z2 = relu(z1)
            y = z2 @ tf.random.normal((2, 1))

        model = ModelWrapper(graph, x, y)

        with self.assertRaises(ValueError):
            infl = InternalInfluence(
                model, Cut('not_a_real_layer'), ClassQoI(0), PointDoi())

            infl.attributions(np.array([[1., 1.]]))
    def test_sensitivity(self):
        c = 2
        infl = InternalInfluence(
            self.model_deep,
            InputCut(),
            ClassQoI(c),
            LinearDoi(self.baseline),
            multiply_activation=False)

        out_x = self.model_deep.fprop((self.x[0:1],))[0][:, c]
        out_baseline = self.model_deep.fprop((self.baseline,))[0][:, c]

        if not np.allclose(out_x, out_baseline):
            res = infl.attributions(self.x)

            self.assertEqual(res.shape, (2, self.input_size))

            self.assertNotEqual(res[0, 3], 0.)
    def test_multiple_inputs(self):
        x1 = Input((5, ))
        z1 = Dense(6)(x1)
        x2 = Input((1, ))
        z2 = Concatenate()([z1, x2])
        z3 = Dense(7)(z2)
        y = Dense(3)(z3)

        model = ModelWrapper(Model([x1, x2], y))

        infl = InternalInfluence(model, InputCut(), ClassQoI(1), PointDoi())

        res = infl.attributions(
            [np.array([[1., 2., 3., 4., 5.]]),
             np.array([[1.]])])

        self.assertEqual(len(res), 2)
        self.assertEqual(res[0].shape, (1, 5))
        self.assertEqual(res[1].shape, (1, 1))
    def test_completeness_internal_zero_baseline(self):
        c = 2

        infl = InternalInfluence(
            self.model_deep,
            Cut(self.layer2),
            ClassQoI(c),
            LinearDoi(resolution=100, cut=Cut(self.layer2)),
            multiply_activation=True)

        g = partial(
            self.model_deep.fprop,
            doi_cut=Cut(self.layer2),
            intervention=np.zeros((2, 10)))
        out_x = self.model_deep.fprop((self.x,))[0][:, c]
        out_baseline = g((self.x,))[0][:, c]

        res = infl.attributions(self.x)

        self.assertTrue(
            np.allclose(res.sum(axis=1), out_x - out_baseline, atol=5e-2))
    def test_internal_slice_multiple_layers(self):
        x1 = Input((5, ))
        z1 = Dense(6, name='cut_layer1')(x1)
        x2 = Input((1, ))
        z2 = Dense(2, name='cut_layer2')(x2)
        z3 = Dense(4)(z2)
        z4 = Concatenate()([z1, z3])
        z5 = Dense(7)(z4)
        y = Dense(3)(z5)

        model = ModelWrapper(Model([x1, x2], y))

        infl = InternalInfluence(model, Cut(['cut_layer1', 'cut_layer2']),
                                 ClassQoI(1), PointDoi())

        res = infl.attributions(
            [np.array([[1., 2., 3., 4., 5.]]),
             np.array([[1.]])])

        self.assertEqual(len(res), 2)
        self.assertEqual(res[0].shape, (1, 6))
        self.assertEqual(res[1].shape, (1, 2))
Exemple #15
0
    def test_catch_cut_name_error(self):

        class M(Module):

            def __init__(this):
                super(M, this).__init__()
                this.z1 = Linear(2, 2)
                this.z2 = ReLU()
                this.y = Linear(2, 1)

            def forward(this, x):
                z1 = this.z1(x)
                z2 = this.z2(z1)
                return this.y(z2)

        model = ModelWrapper(M(), (2,))

        with self.assertRaises(ValueError):
            infl = InternalInfluence(
                model, Cut('not_a_real_layer'), ClassQoI(0), PointDoi())

            infl.attributions(np.array([[1., 1.]]).astype('float32'))
    def test_multiple_inputs(self):
        graph = Graph()

        with graph.as_default():
            x1 = tf.placeholder('float32', (None, 5))
            z1 = x1 @ tf.random.normal((5, 6))
            x2 = tf.placeholder('float32', (None, 1))
            z2 = tf.concat([z1, x2], axis=1)
            z3 = z2 @ tf.random.normal((7, 7))
            y = z3 @ tf.random.normal((7, 3))

        model = ModelWrapper(graph, [x1, x2], y)

        infl = InternalInfluence(model, InputCut(), ClassQoI(1), PointDoi())

        res = infl.attributions(
            [np.array([[1., 2., 3., 4., 5.]]),
             np.array([[1.]])])

        self.assertEqual(len(res), 2)
        self.assertEqual(res[0].shape, (1, 5))
        self.assertEqual(res[1].shape, (1, 1))