Example #1
0
 def update(self):
     check_inputs_synchronized(self)
     
     y_dot = self.input.y_dot
     y = self.input.y
     u = self.input.u
     T = self.T
     
     check_multiple([
         ('shape(x),(array[HxW]|array[N])', y),
         ('shape(x),(array[HxW]|array[N])', y_dot),
         ('array[K]', u),
         ('array[Kx2xHxW]|array[Kx1xN]', T), # TODO: use references
     ])
     K = u.size
     
     # update covariance of gradients
     gy = generalized_gradient(y)
     
     Tgy = np.tensordot([1, 1], T * gy , axes=(0, 1)) 
             
     y_dot_pred = np.tensordot(u, Tgy, axes=(0, 0))
      
     assert y_dot_pred.ndim == 2
     
     error = np.maximum(0, -y_dot_pred * y_dot)
     # error = np.abs(np.sign(y_dot_pred) - np.sign(y_dot))
     
     self.output.y_dot_pred = y_dot_pred
     self.output.error = error
     self.output.error_sensel = np.sum(error, axis=0)
Example #2
0
def inner_product_embedding_slow(C, ndim):
    U, S, V = np.linalg.svd(C, full_matrices=0)
    check_multiple([('array[NxN]', U), ('array[N]', S), ('array[NxN]', V)])
    coords = V[:ndim, :]
    for i in range(ndim):
        coords[i, :] = coords[i, :] * np.sqrt(S[i])
    return coords
Example #3
0
    def test_check_multiple_1(self):

        data = [[1, 2, 3],
                [4, 5, 6]]
        row_labels = ['first season', 'second season']
        col_labels = ['Team1', 'Team2', 'Team3']

        spec = [('list[C](str),C>0', col_labels),
                ('list[R](str),R>0', row_labels),
                ('list[R](list[C])', data)]
        check_multiple(spec)

        # now with description 
        check_multiple(spec,
                        'I expect col_labels, row_labels, data to '
                        'have coherent dimensions.')

        data = [[1, 2, 3], [1, 2]]
        spec = [('list[C](str),C>0', col_labels),
                ('list[R](str),R>0', row_labels),
                ('list[R](list[C])', data)]

        self.assertRaises(ContractNotRespected, check_multiple, spec)
        self.assertRaises(ContractNotRespected, check_multiple, spec,
                          'my message')
Example #4
0
def inner_product_embedding_slow(C, ndim):
    U, S, V = np.linalg.svd(C, full_matrices=0)
    check_multiple([('array[NxN]', U),
                    ('array[N]', S),
                    ('array[NxN]', V)])
    coords = V[:ndim, :]
    for i in range(ndim):
        coords[i, :] = coords[i, :] * np.sqrt(S[i])
    return coords
Example #5
0
def inner_product_embedding_randomized(C, ndim):
    ''' 
        Best embedding of inner product matrix based on 
        randomized projections. 
    '''
    U, S, V = truncated_svd_randomized(C, ndim)  # @UnusedVariable.
    check_multiple([('K', ndim), ('array[KxN]', V), ('array[K]', S)])
    coords = V
    for i in range(ndim):
        coords[i, :] = coords[i, :] * np.sqrt(S[i])
    return coords
Example #6
0
def inner_product_embedding_randomized(C, ndim):
    ''' 
        Best embedding of inner product matrix based on 
        randomized projections. 
    '''
    U, S, V = truncated_svd_randomized(C, ndim)  # @UnusedVariable.
    check_multiple([('K', ndim),
                    ('array[KxN]', V),
                    ('array[K]', S)])
    coords = V
    for i in range(ndim):
        coords[i, :] = coords[i, :] * np.sqrt(S[i])
    return coords
Example #7
0
    def test_check_multiple_1(self):

        data = [[1, 2, 3], [4, 5, 6]]
        row_labels = ['first season', 'second season']
        col_labels = ['Team1', 'Team2', 'Team3']

        spec = [('list[C](str),C>0', col_labels),
                ('list[R](str),R>0', row_labels), ('list[R](list[C])', data)]
        check_multiple(spec)

        # now with description
        check_multiple(
            spec, 'I expect col_labels, row_labels, data to '
            'have coherent dimensions.')

        data = [[1, 2, 3], [1, 2]]
        spec = [('list[C](str),C>0', col_labels),
                ('list[R](str),R>0', row_labels), ('list[R](list[C])', data)]

        self.assertRaises(ContractNotRespected, check_multiple, spec)
        self.assertRaises(ContractNotRespected, check_multiple, spec,
                          'my message')
Example #8
0
    def iteration(self, data):
        """ Records one iteration of the algorithm """

        for x in ['self']:
            if x in data:
                del data[x]

        S = data['S']
        check_multiple([('array[NxN]', self.R), ('array[*xN]', S)])

        if self.is_spherical():
            check('directions', S)

            # Observable errors
            C = cosines_from_directions(S)
            C_order = scale_score(C)
            data['spearman'] = correlation_coefficient(C_order, self.R_order)

            valid = self.R_order > self.R.size * 0.6
            data['spearman_robust'] = correlation_coefficient(C_order[valid],
                                                          self.R_order[valid])

            data['diameter'] = compute_diameter(S)
            data['diameter_deg'] = np.degrees(data['diameter'])

        if self.is_euclidean():
            D = euclidean_distances(S)
            D_order = scale_score(D)
            data['spearman'] = correlation_coefficient(-D_order, self.R_order)
            valid = self.R_order > self.R.size * 0.6
            data['spearman_robust'] = correlation_coefficient(-D_order[valid],
                                                          self.R_order[valid])

            data['diameter'] = 2 * euclidean_distribution_radius(S)
            data['diameter_deg'] = data['diameter']

        data['robust'] = data['spearman_robust']

        # These are unobservable statistics
        if self.is_spherical() and self.true_S is not None:
            # add more rows to S if necessary
            K = S.shape[0]
            if K != self.true_S.shape[0]:
                newS = np.zeros(self.true_S.shape)
                newS[:K, :] = S
                newS[K:, :] = 0
                S = newS
                check('directions', S)

            Rest = find_best_orthogonal_transform(S, self.true_S)
            data['S_aligned'] = np.dot(Rest, S)
            data['error'] = \
                overlap_error_after_orthogonal_transform(S, self.true_S)
            data['error_deg'] = np.degrees(data['error'])
            data['rel_error'] = compute_relative_error(self.true_S, S, 10)
            data['rel_error_deg'] = np.degrees(data['rel_error'])

            # only valid in 2d
            if K == 2:
                true_angles_deg = \
                    np.degrees(angles_from_directions(self.true_S))
                angles_deg = \
                    np.degrees(angles_from_directions(data['S_aligned']))
                angles_deg = \
                    find_closest_multiple(angles_deg, true_angles_deg, 360)
                data['angles_corr'] = \
                    correlation_coefficient(true_angles_deg, angles_deg)

            D = distances_from_directions(S)
            true_D = distances_from_directions(self.true_S)

            scaled_rel_error, scaled_scale = scaled_error(D, true_D,
                                                          also_scale=True)
            data['scaled_rel_error'] = scaled_rel_error
            data['scaled_scale'] = scaled_scale
            data['scaled_rel_error_deg'] = np.degrees(data['scaled_rel_error'])

        if self.is_euclidean() and self.true_S is not None:
            D = euclidean_distances(S)
            true_D = euclidean_distances(self.true_S)

            data['rel_error'] = np.abs(D - true_D).mean()
            data['scaled_rel_error'], scale = scaled_error(D, true_D,
                                                           also_scale=True)

            scaled_S = scale * S

            def remove_mean(x):
                k, n = x.shape
                m = np.tile(x.mean(axis=1).reshape((k, 1)), (1, n))
                assert m.shape == x.shape
                return x - m

            trans_scaled_S = remove_mean(scaled_S)
            trans_true_S = remove_mean(self.true_S)
            data['scaled_error'] = \
                mean_euclidean_distance_after_orthogonal_transform(
                            trans_scaled_S, trans_true_S)
            data['scaled_error_deg'] = np.rad2deg(data['scaled_error'])
            data['error'] = \
                mean_euclidean_distance_after_orthogonal_transform(
                            remove_mean(S), remove_mean(self.true_S))

        def varstat(x, format='%.3f', #@ReservedAssignment
                    label=None, sign=(+1)):
            if label is None:
                label = x[:5]
            if not x in data:
                return ' %s: /' % label
            current = data[x]
            s = ' %s: %s' % (label, format % current)

            if self.iterations:
                all_previous = np.array([it[x] for it in self.iterations])

                previous = all_previous[-1]
                is_best = sign * current >= max(sign * all_previous)
                if is_best:
                    mark = 'B'
                else:
                    mark = '+' if sign * current > sign * previous else '-'
                s += ' %s' % mark
            return s

        status = ('It: %2d' % len(self.iterations) +
                  varstat('diameter_deg', '%3d') +
                  varstat('spearman', '%.8f', label='spear') +
                  varstat('spearman_robust', '%.8f', label='sp_rob'))

        if self.is_spherical():
            status += (varstat('error_deg', '%5.3f', sign=(-1)) +
                      varstat('rel_error_deg', '%5.3f', sign=(-1)) +
                      varstat('scaled_rel_error_deg', '%5.3fd', sign=(-1),
                              label='s_r_err'))
        if self.is_euclidean():
            status += (
                varstat('scaled_error', '%5.3f', sign=(-1), label='s_err') +
                varstat('scaled_rel_error_deg', '%5.3fd',
                         sign=(-1), label='s_r_err'))

        print(status)

        self.iterations.append(data)
Example #9
0
File: base.py Project: afcarl/cbc
    def iteration(self, data):
        """ Records one iteration of the algorithm """

        for x in ['self']:
            if x in data:
                del data[x]

        S = data['S']
        check_multiple([('array[NxN]', self.R), ('array[*xN]', S)])

        if self.is_spherical():
            check('directions', S)

            # Observable errors
            C = cosines_from_directions(S)
            C_order = scale_score(C)
            data['spearman'] = correlation_coefficient(C_order, self.R_order)

            valid = self.R_order > self.R.size * 0.6
            data['spearman_robust'] = correlation_coefficient(
                C_order[valid], self.R_order[valid])

            data['diameter'] = compute_diameter(S)
            data['diameter_deg'] = np.degrees(data['diameter'])

        if self.is_euclidean():
            D = euclidean_distances(S)
            D_order = scale_score(D)
            data['spearman'] = correlation_coefficient(-D_order, self.R_order)
            valid = self.R_order > self.R.size * 0.6
            data['spearman_robust'] = correlation_coefficient(
                -D_order[valid], self.R_order[valid])

            data['diameter'] = 2 * euclidean_distribution_radius(S)
            data['diameter_deg'] = data['diameter']

        data['robust'] = data['spearman_robust']

        # These are unobservable statistics
        if self.is_spherical() and self.true_S is not None:
            # add more rows to S if necessary
            K = S.shape[0]
            if K != self.true_S.shape[0]:
                newS = np.zeros(self.true_S.shape)
                newS[:K, :] = S
                newS[K:, :] = 0
                S = newS
                check('directions', S)

            Rest = find_best_orthogonal_transform(S, self.true_S)
            data['S_aligned'] = np.dot(Rest, S)
            data['error'] = \
                overlap_error_after_orthogonal_transform(S, self.true_S)
            data['error_deg'] = np.degrees(data['error'])
            data['rel_error'] = compute_relative_error(self.true_S, S, 10)
            data['rel_error_deg'] = np.degrees(data['rel_error'])

            # only valid in 2d
            if K == 2:
                true_angles_deg = \
                    np.degrees(angles_from_directions(self.true_S))
                angles_deg = \
                    np.degrees(angles_from_directions(data['S_aligned']))
                angles_deg = \
                    find_closest_multiple(angles_deg, true_angles_deg, 360)
                data['angles_corr'] = \
                    correlation_coefficient(true_angles_deg, angles_deg)

            D = distances_from_directions(S)
            true_D = distances_from_directions(self.true_S)

            scaled_rel_error, scaled_scale = scaled_error(D,
                                                          true_D,
                                                          also_scale=True)
            data['scaled_rel_error'] = scaled_rel_error
            data['scaled_scale'] = scaled_scale
            data['scaled_rel_error_deg'] = np.degrees(data['scaled_rel_error'])

        if self.is_euclidean() and self.true_S is not None:
            D = euclidean_distances(S)
            true_D = euclidean_distances(self.true_S)

            data['rel_error'] = np.abs(D - true_D).mean()
            data['scaled_rel_error'], scale = scaled_error(D,
                                                           true_D,
                                                           also_scale=True)

            scaled_S = scale * S

            def remove_mean(x):
                k, n = x.shape
                m = np.tile(x.mean(axis=1).reshape((k, 1)), (1, n))
                assert m.shape == x.shape
                return x - m

            trans_scaled_S = remove_mean(scaled_S)
            trans_true_S = remove_mean(self.true_S)
            data['scaled_error'] = \
                mean_euclidean_distance_after_orthogonal_transform(
                            trans_scaled_S, trans_true_S)
            data['scaled_error_deg'] = np.rad2deg(data['scaled_error'])
            data['error'] = \
                mean_euclidean_distance_after_orthogonal_transform(
                            remove_mean(S), remove_mean(self.true_S))

        def varstat(
            x,
            format='%.3f',  #@ReservedAssignment
            label=None,
            sign=(+1)):
            if label is None:
                label = x[:5]
            if not x in data:
                return ' %s: /' % label
            current = data[x]
            s = ' %s: %s' % (label, format % current)

            if self.iterations:
                all_previous = np.array([it[x] for it in self.iterations])

                previous = all_previous[-1]
                is_best = sign * current >= max(sign * all_previous)
                if is_best:
                    mark = 'B'
                else:
                    mark = '+' if sign * current > sign * previous else '-'
                s += ' %s' % mark
            return s

        status = ('It: %2d' % len(self.iterations) +
                  varstat('diameter_deg', '%3d') +
                  varstat('spearman', '%.8f', label='spear') +
                  varstat('spearman_robust', '%.8f', label='sp_rob'))

        if self.is_spherical():
            status += (varstat('error_deg', '%5.3f', sign=(-1)) +
                       varstat('rel_error_deg', '%5.3f', sign=(-1)) +
                       varstat('scaled_rel_error_deg',
                               '%5.3fd',
                               sign=(-1),
                               label='s_r_err'))
        if self.is_euclidean():
            status += (
                varstat('scaled_error', '%5.3f', sign=(-1), label='s_err') +
                varstat('scaled_rel_error_deg',
                        '%5.3fd',
                        sign=(-1),
                        label='s_r_err'))

        print(status)

        self.iterations.append(data)