def eval(self,q:Queue,inner_q:Queue):
        running_mean = RunningMeanWelford()
        # activation_sum=0
        n=0
        for transformation in self.queue_as_generator(q):
            for activations in self.queue_as_generator(inner_q):
                if self.sign != 1:
                    activations *= self.sign
                activated = (activations > self.threshold) * 1
                running_mean.update_all(activated)

        self.l=running_mean.mean()
Example #2
0
    def distance(self,batch:np.ndarray,batch_inverted:np.ndarray,mean_running:RunningMeanWelford):
        n_shape=len(batch.shape)
        assert n_shape>=2
        n,f=batch.shape[0],batch.shape[1]

        if n_shape>2 and self.normalize:
            # normalize all extra dimensions
            for i in range(n):
                for j in range(f):
                    batch[i,j,:]/=np.linalg.norm(batch[i,j,:])
                    batch_inverted[i,j,:]/=np.linalg.norm(batch_inverted[i,j,:])

        # ssd of all values
        distances = (batch-batch_inverted)**2
        n_shape=len(batch.shape)
        if n_shape>2:
            # aggregate extra dims to keep only the feature dim
            distances= distances.mean(axis=tuple(range(2,n_shape)))
        distances = np.sqrt(distances)
        assert len(distances.shape)==2
        mean_running.update_all(distances)