text:
from pyNN.random import RandomDistribution
from spynnaker.pyNN.models.neural_projections.connectors.abstract_connector \
    import AbstractConnector

import numpy
import logging

logger = logging.getLogger(__file__)

class FixedNumberPostConnector(AbstractConnector):

    def __init__(
            self, n, weights=0.0, delays=1, allow_self_connections=True,
            space=None, safe=True, verbose=False):
        AbstractConnector.__init__(self, safe, space, verbose)
        self._post_n = n
        self._weights = weights
        self._delays = delays
        self._allow_self_connections = allow_self_connections
        self._post_neurons = None

        self._check_parameters(weights, delays, allow_lists=False)
        if isinstance(n, RandomDistribution):
            raise NotImplementedError(
                "RandomDistribution is not supported for n in the"
                " implementation of FixedNumberPostConnector on this platform")

    def get_delay_maximum(self):
        return self._get_delay_maximum(
            self._delays, self._n_pre_neurons * self._post_n)

    def _get_post_neurons(self):
        if self._post_neurons is None:
            n = 0
            while (n < self._post_n):
                permutation = numpy.arange(self._n_post_neurons)
                for i in range(0, self._n_post_neurons - 1):
                    j = self._rng.next(
                        n=1, distribution="uniform",
                        parameters=[0, self._n_post_neurons])
                    (permutation[i], permutation[j]) = (
                        permutation[j], permutation[i])
                n += self._n_post_neurons
                if self._post_neurons is None:
                    self._post_neurons = permutation
                else:
                    self._post_neurons = numpy.append(
                        self._post_neurons, permutation)
            self._post_neurons = self._post_neurons[:self._post_n]
            self._post_neurons.sort()
        return self._post_neurons

    def _post_neurons_in_slice(self, post_vertex_slice):
        post_neurons = self._get_post_neurons()
        return post_neurons[
            (post_neurons >= post_vertex_slice.lo_atom) &
            (post_neurons <= post_vertex_slice.hi_atom)]

    def _is_connected(self, post_vertex_slice):
        return self._post_neurons_in_slice(post_vertex_slice).size > 0

    def get_n_connections_from_pre_vertex_maximum(
            self, pre_slices, pre_slice_index, post_slices,
            post_slice_index, pre_vertex_slice, post_vertex_slice,
            min_delay=None, max_delay=None):

        if not self._is_connected(post_vertex_slice):
            return 0

        post_neurons = self._post_neurons_in_slice(post_vertex_slice)
        if min_delay is None or max_delay is None:
            return len(post_neurons)

        return self._get_n_connections_from_pre_vertex_with_delay_maximum(
            self._delays, self._n_post * self._n_post_neurons,
            pre_vertex_slice.n_atoms * len(post_neurons), None,
            min_delay, max_delay)

    def get_n_connections_to_post_vertex_maximum(
            self, pre_slices, pre_slice_index, post_slices,
            post_slice_index, pre_vertex_slice, post_vertex_slice):
        if not self._is_connected(post_vertex_slice):
            return 0
        return pre_vertex_slice.n_atoms

    def get_weight_mean(
            self, pre_slices, pre_slice_index, post_slices,
            post_slice_index, pre_vertex_slice, post_vertex_slice):
        if not self._is_connected(post_vertex_slice):
            return 0.0
        post_neurons = self._post_neurons_in_slice(post_vertex_slice)
        n_connections = pre_vertex_slice.n_atoms * len(post_neurons)
        return self._get_weight_mean(self._weights, None)

    def get_weight_maximum(
            self, pre_slices, pre_slice_index, post_slices,
            post_slice_index, pre_vertex_slice, post_vertex_slice):
        if not self._is_connected(post_vertex_slice):
            return 0.0
        post_neurons = self._post_neurons_in_slice(post_vertex_slice)
        n_connections = pre_vertex_slice.n_atoms * len(post_neurons)
        return self._get_weight_maximum(
            self._weights, n_connections, None)

    def get_weight_variance(
            self, pre_slices, pre_slice_index, post_slices,
            post_slice_index, pre_vertex_slice, post_vertex_slice):
        if not self._is_connected(post_vertex_slice):
            return 0.0
        return self._get_weight_variance(self._weights, None)

    def generate_on_machine(self):
        return (
            not self._generate_lists_on_host(self._weights) and
            not self._generate_lists_on_host(self._delays))

    def create_synaptic_block(
            self, pre_slices, pre_slice_index, post_slices,
            post_slice_index, pre_vertex_slice, post_vertex_slice,
            synapse_type):
        if not self._is_connected(post_vertex_slice):
            return numpy.zeros(0, dtype=AbstractConnector.NUMPY_SYNAPSES_DTYPE)
        post_neurons_in_slice = self._post_neurons_in_slice(post_vertex_slice)
        n_connections = pre_vertex_slice.n_atoms * len(post_neurons_in_slice)
        if (not self._allow_self_connections and
                pre_vertex_slice is post_vertex_slice):
            n_connections -= len(post_neurons_in_slice)

        block = numpy.zeros(
            n_connections, dtype=AbstractConnector.NUMPY_SYNAPSES_DTYPE)

        if (not self._allow_self_connections and
                pre_vertex_slice is post_vertex_slice):
            block["source"] = [
                pre_index for pre_index in range(
                    pre_vertex_slice.lo_atom, post_vertex_slice.hi_atom + 1)
                for post_index in post_neurons_in_slice
                if pre_index != post_index]
            block["target"] = [
                post_index for pre_index in range(
                    pre_vertex_slice.lo_atom, post_vertex_slice.hi_atom + 1)
                for post_index in post_neurons_in_slice
                if pre_index != post_index]
        else:
            block["source"] = numpy.repeat(numpy.arange(
                pre_vertex_slice.lo_atom, pre_vertex_slice.hi_atom + 1),
                len(post_neurons_in_slice))
            block["target"] = numpy.tile(
                post_neurons_in_slice, pre_vertex_slice.n_atoms)
        block["weight"] = self._generate_weights(
            self._weights, n_connections, None)
        block["delay"] = self._generate_delays(
            self._delays, n_connections, None)
        block["synapse_type"] = synapse_type
        return block


text:
#! /usr/bin/python
# -*- coding: utf-8 -*-

__author__ = "Osman Baskaya"

from sklearn import grid_search
from sklearn.svm import SVC
from sklearn.naive_bayes import MultinomialNB, BernoulliNB
from sklearn.tree import DecisionTreeClassifier
#from statsmodels.discrete.discrete_model import MNLogit
import numpy as np
import sys

class ClassifierWrapper(object):

    def __init__(self, name, classifier):
        self.name = name
        self.classifier = classifier
        self.is_optimized = False

    def optimize(self, X, y, cv, parameters=None):
        """Hyperparameter optimization"""
        best_params = best_estimator = None
        if parameters is None:
            parameters = self.parameters
        print >> sys.stderr, "Parameter space:{}".format(parameters)
        try:
            clf = grid_search.GridSearchCV(self.classifier, parameters, cv=cv, verbose=1,
                                          n_jobs=20)
            clf.fit(X, y)
            print >> sys.stderr, clf.grid_scores_
            best_params = clf.best_params_
            best_estimator = clf.best_estimator_
        except ValueError, e: # all instances belong to the same class
            print >> sys.stderr, e
        #FIXME: Why do we get index errors for book.v
        except IndexError, e:
            print >> sys.stderr, e
        #try:
            #msg = "Best_params:{}, Best Score:{}".format(best_params, clf.best_score_)
            #print >> sys.stderr, msg
        #except AttributeError:
            #pass
        return best_params, best_estimator, clf.best_score_

class SVCWrapper(ClassifierWrapper):

    def __init__(self, name, **kwargs):
        super(SVCWrapper, self).__init__(name, SVC(**kwargs))
        gamma = 10.0 ** np.arange(-5, 4)
        C = 10.0 ** np.arange(-2, 6)
        #C = [0.003, 0.03, 0.3, 1, 9, 27, 81, 243, 729, 2000, 6000]
        if self.classifier.kernel == "rbf":
            self.parameters = {'C': C, 'gamma': gamma}
        elif self.classifier.kernel == "linear":
            self.parameters = {'C': C}

class MultinomialNBWrapper(ClassifierWrapper):

    def __init__(self, name="MultinomialNB"):
        super(MultinomialNBWrapper, self).__init__(name, MultinomialNB())
        self.is_optimized = True
        self.parameters = {'alpha': np.linspace(0,1,11)}

class BernoulliNBWrapper(ClassifierWrapper):

    def __init__(self, name="BernoulliNBWrapper"):
        super(BernoulliNBWrapper, self).__init__(name, BernoulliNB())
        self.parameters = {'alpha': np.linspace(0,1,11)}
        self.is_optimized = True

class DecisionTreeWrapper(ClassifierWrapper):

    def __init__(self, name, criterion):
        super(DecisionTreeWrapper, self).__init__(name,
                                    DecisionTreeClassifier(criterion=criterion))
        self.is_optimized = True

#class MNLogitWrapper(ClassifierWrapper):

    #def __init__(self):
        #raise NotImplementedError, "soon"
        #super(MNLogitWrapper, self).__init__("MNLogitWrapper", MNLogitW())
        #self.parameters = {}
        #print self.name

def main():
    pass

if __name__ == '__main__':
    main()

ext: py

max_stars_repo_name: osmanbaskaya/mapping-impact

max_issues_repo_name: osmanbaskaya/mapping-impact

max_forks_repo_name: osmanbaskaya/mapping-impact

reason: import numpy,from statsmodels


text:
# -*- coding: utf-8 -*-
# <nbformat>3</nbformat>

# <markdowncell>

# # Exercise 1
# 1. Assign variables with the values 1, 5, and "ten" with the types int, float, and string respectively.
# <codecell>

# <markdowncell>
# 2. Confirm that the types are int, float, and string
# <codecell>

# <markdowncell>
# 3. Determine which for which pairs of the set of int, float, and string the + operation gives an error.
# > int-int :
# > int-float :
# > int-string :
# > float-float  :
# > float-string :
# > string-string :
# Any surprises?
# <codecell>

# <markdowncell>
# 4. Determine which for which pairs of the set of int, float, and string) the * operation gives an error.  Any surprises?
# <codecell>

# <markdowncell>
# 5. Assign a string the value of "1, 5, and ten" from these three variables.
# <codecell>

# <markdowncell>
# # Exercise 2
# Here you will use **math.log10()** and **math.floor()**, which require the line **import math** for you to access these funcitons.
# 1. Determine the return type of log10()
# <codecell>
import math

# <markdowncell>
# 2. What is the value and type of log10(42) ?
# <codecell>

# <markdowncell>
# 3. What is the value and type of log10(-0.32) ?
# <codecell>

# <markdowncell>
# 4. What about 1.0 / 0 ?
# <codecell>

# <markdowncell>
# 4. What is the return type of floor acting on an int?  Acting on a float?  **floor** is in the math namespace.  It will only work after **import math** and it is invoked as **math.floor()**
# <codecell>

# <markdowncell>
# # Exercise 3
#  len() is a builtin function to count the length of things.  For which of the basic datatypes so far does len() return a value?  Does it return the length or the length+1 ?
# <codecell>

# <markdowncell>
# # Example 1
# Python lists are agnostic to the type of the data contained within them.  You can generate arrays with different elements of different types:
# <codecell>
pythonlist =[2.43, 1, 0.92, 0, "0.38"]
print pythonlist
# <codecell>
print type(pythonlist[0]), type(pythonlist[1]), type(pythonlist[2]), type(pythonlist[3])
# <markdowncell>
# # Exercise 4
# numpy is an extremely useful library that has its own data types; you will frequently need to specify the types as float or possibly higher-precision float at the time you create them.  The numpy data types are only sometimes compatible with the native data types.
# <codecell>
import numpy as np
numpyarray = np.array(pythonlist)
# <markdowncell>
# What is the default data type of a after the conversion?
# <codecell>

# <markdowncell>
# Ack! The results of type() do not betray the representation.  For that we need
# <codecell>
print numpyarray.dtype
# <markdowncell>
# Which is not a numeric data type.  We can cause it to be stored as numpy floats if we specify float when we convert it to numpy:
# <codecell>
numpyfloatarray = np.array(python_array, dtype="float")

# <markdowncell>
# # Exercise 5
# 5A. Write an expression to determine the number of digits in a non-negative integer.  Hint:  maybe **len()** or **math.log()** might be useful here.
# <codecell>

# <markdowncell>
# 5B. Test your expression on 45, 2, and 2.0.  Does it work for all three?
# <codecell>

max_stars_repo_name: kbroman/UW-Madison-swc-boot-camps

max_issues_repo_name: kbroman/UW-Madison-swc-boot-camps

max_forks_repo_name: kbroman/UW-Madison-swc-boot-camps

reason: import numpy


text:
# AUTOGENERATED! DO NOT EDIT! File to edit: 11_synchro.extracting.ipynb (unless otherwise specified).

__all__ = ['get_QDSpy_logs', 'QDSpy_log', 'Stimulus', 'unpack_stim_npy', 'extract_spyking_circus_results',
           'extract_best_pupil', 'stack_len_extraction']

# Cell
import numpy as np
import datetime
import os, glob
import csv
import re

from .io import *
from ..utils import *

def get_QDSpy_logs(log_dir):
    """Factory function to generate QDSpy_log objects from all the QDSpy logs of the folder `log_dir`"""
    log_names = glob.glob(os.path.join(log_dir,'[0-9]*.log'))
    qdspy_logs = [QDSpy_log(log_name) for log_name in log_names]
    for qdspy_log in qdspy_logs:
        qdspy_log.find_stimuli()
    return qdspy_logs

class QDSpy_log:
    """Class defining a QDSpy log.
    It reads the log it represent and extract the stimuli information from it:
      - Start and end time
      - Parameters like the md5 key
      - Frame delays
    """
    def __init__(self, log_path):
        self.log_path = log_path
        self.stimuli = []
        self.comments = []

    def _extract_data(self, data_line):
        data = data_line[data_line.find('{')+1:data_line.find('}')]
        data_splitted = data.split(',')
        data_dict = {}
        for data in data_splitted:
            ind = data.find("'")
            if type(data[data.find(":")+2:]) is str:
                data_dict[data[ind+1:data.find("'",ind+1)]] = data[data.find(":")+2:][1:-1]
            else:
                data_dict[data[ind+1:data.find("'",ind+1)]] = data[data.find(":")+2:]
        return data_dict

    def _extract_time(self,data_line):
        return datetime.datetime.strptime(data_line.split()[0], '%Y%m%d_%H%M%S')

    def _extract_delay(self,data_line):
        ind = data_line.find('#')
        index_frame = int(data_line[ind+1:data_line.find(' ',ind)])
        ind = data_line.find('was')
        delay = float(data_line[ind:].split(" ")[1])
        return (index_frame, delay)

    def __repr__(self):
        return "\n".join([str(stim) for stim in self.stimuli])

    @property
    def n_stim(self):
        return len(self.stimuli)

    @property
    def stim_names(self):
        return [stim.name for stim in self.stimuli]

    def find_stimuli(self):
        """Find the stimuli in the log file and return the list of the stimuli
        found by this object."""
        with open(self.log_path, 'r', encoding="ISO-8859-1") as log_file:
            for line in log_file:
                if "DATA" in line:
                    data_juice = self._extract_data(line)
                    if 'stimState' in data_juice.keys():
                        if data_juice['stimState'] == "STARTED" :
                            curr_stim = Stimulus(self._extract_time(line))
                            curr_stim.set_parameters(data_juice)
                            self.stimuli.append(curr_stim)
                            stimulus_ON = True
                        elif data_juice['stimState'] == "FINISHED" or data_juice['stimState'] == "ABORTED":
                            curr_stim.is_aborted = data_juice['stimState'] == "ABORTED"
                            curr_stim.stop_time = self._extract_time(line)
                            stimulus_ON = False

                    elif 'userComment' in data_juice.keys():
                        pass
                        #print("userComment, use it to bind logs to records")
                    elif stimulus_ON: #Information on stimulus parameters
                        curr_stim.set_parameters(data_juice)
    #                elif 'probeX' in data_juice.keys():
            #            print("Probe center not implemented yet")
                if "WARNING" in line and "dt of frame" and stimulus_ON:
                    curr_stim.frame_delay.append(self._extract_delay(line))
                    if curr_stim.frame_delay[-1][1] > 2000/60: #if longer than 2 frames could be bad
                        print(curr_stim.name, " ".join(line.split()[1:])[:-1])
        return self.stimuli

class Stimulus:
    """Stimulus object containing information about it's presentation.
        - start_time : a datetime object)
        - stop_time : a datetime object)
        - parameters : Parameters extracted from the QDSpy
        - md5 : The md5 hash of that compiled version of the stimulus
        - name : The name of the stimulus
    """
    def __init__(self,start):
        self.start_time = start
        self.stop_time = None
        self.parameters = {}
        self.md5 = None
        self.name = "NoName"

        self.frame_delay = []
        self.is_aborted = False

    def set_parameters(self, parameters):
        self.parameters.update(parameters)
        if "_sName" in parameters.keys():
            self.name = parameters["_sName"]
        if "stimMD5" in parameters.keys():
            self.md5 = parameters["stimMD5"]

    def __str__(self):
        return "%s %s at %s" %(self.name+" "*(24-len(self.name)),self.md5,self.start_time)

    def __repr__(self):
        return self.__str__()

# Cell
def unpack_stim_npy(npy_dir, md5_hash):
    """Find the stimuli of a given hash key in the npy stimulus folder. The stimuli are in a compressed version
    comprising three files. inten for the stimulus values on the screen, marker for the values of the marker
    read by a photodiode to get the stimulus timing during a record, and an optional shader that is used to
    specify informations about a shader when used, like for the moving gratings."""

    #Stimuli can be either npy or npz (useful when working remotely)
    def find_file(ftype):
        flist = glob.glob(os.path.join(npy_dir, "*_"+ftype+"_"+md5_hash+".npy"))
        if len(flist)==0:
            flist = glob.glob(os.path.join(npy_dir, "*_"+ftype+"_"+md5_hash+".npz"))
            res = np.load(flist[0])["arr_0"]
        else:
            res = np.load(flist[0])
        return res

    inten  = find_file("intensities")
    marker = find_file("marker")

    shader, unpack_shader = None, None
    if len(glob.glob(os.path.join(npy_dir, "*_shader_"+md5_hash+".np*")))>0:
        shader        = find_file("shader")
        unpack_shader = np.empty((np.sum(marker[:,0]), *shader.shape[1:]))

    #The latter unpacks the arrays
    unpack_inten  = np.empty((np.sum(marker[:,0]), *inten.shape[1:]))
    unpack_marker = np.empty(np.sum(marker[:,0]))

    cursor = 0
    for i, n_frame in enumerate(marker[:,0]):
        unpack_inten[cursor:cursor+n_frame] = inten[i]
        unpack_marker[cursor:cursor+n_frame] = marker[i, 1]
        if shader is not None:
            unpack_shader[cursor:cursor+n_frame] = shader[i]
        cursor += n_frame

    return unpack_inten, unpack_marker, unpack_shader

# Cell
def extract_spyking_circus_results(dir_, record_basename):
    """Extract the good cells of a record. Overlap with phy_results_dict."""
    phy_dir  = os.path.join(dir_,record_basename+"/"+record_basename+".GUI")
    phy_dict = phy_results_dict(phy_dir)

    good_clusters = []
    with open(os.path.join(phy_dir,'cluster_group.tsv'), 'r') as tsvfile:
        spamreader = csv.reader(tsvfile, delimiter='\t', quotechar='|')
        for i,row in enumerate(spamreader):
            if row[1] == "good":
                good_clusters.append(int(row[0]))
    good_clusters = np.array(good_clusters)

    phy_dict["good_clusters"] = good_clusters

    return phy_dict

# Cell
def extract_best_pupil(fn):
    """From results of MaskRCNN, go over all or None pupil detected and select the best pupil.
    Each pupil returned is (x,y,width,height,angle,probability)"""
    pupil = np.load(fn, allow_pickle=True)
    filtered_pupil = np.empty((len(pupil), 6))
    for i, detected in enumerate(pupil):
        if len(detected)>0:
            best = detected[0]
            for detect in detected[1:]:
                if detect[5]>best[5]:
                    best = detect
            filtered_pupil[i] = np.array(best)
        else:
            filtered_pupil[i] = np.array([0,0,0,0,0,0])
    return filtered_pupil

# Cell
def stack_len_extraction(stack_info_dir):
    """Extract from ImageJ macro directives the size of the stacks acquired."""
    ptrn_nFrame = r".*number=(\d*) .*"
    l_epochs = []
    for fn in glob.glob(os.path.join(stack_info_dir, "*.txt")):
        with open(fn) as f:
            line = f.readline()
            l_epochs.append(int(re.findall(ptrn_nFrame, line)[0]))
    return l_epochs

max_stars_repo_name: Ines-Filipa/theonerig

max_issues_repo_name: Ines-Filipa/theonerig

max_forks_repo_name: Ines-Filipa/theonerig


text:
from unittest.case import TestCase
import pandas as pd
import numpy as np
import pytest as test
from lambdata.helper_functions import *


text:
import numpy as np

import torch
from torch import nn
from torch import autograd

from networks.blocks import LinearBlock, Conv2dBlock, ResBlocks, ActFirstResBlock

def assign_adain_params(adain_params, model):
    # assign the adain_params to the AdaIN layers in model
    for m in model.modules():
        if m.__class__.__name__ == "AdaptiveInstanceNorm2d":
            mean = adain_params[:, :m.num_features]
            std = adain_params[:, m.num_features:2*m.num_features]
            m.bias = mean.contiguous().view(-1)
            m.weight = std.contiguous().view(-1)
            if adain_params.size(1) > 2*m.num_features:
                adain_params = adain_params[:, 2*m.num_features:]

def get_num_adain_params(model):
    # return the number of AdaIN parameters needed by the model
    num_adain_params = 0
    for m in model.modules():
        if m.__class__.__name__ == "AdaptiveInstanceNorm2d":
            num_adain_params += 2*m.num_features
    return num_adain_params

class Discriminator(nn.Module):
    def __init__(self, config):
        super(Discriminator, self).__init__()
        hp = config['gan']['dis']
        assert hp['n_res_blks'] % 2 == 0, 'n_res_blk must be multiples of 2'
        self.config = config
        self.hp = hp
        self.iter_num = 0
        self.n_layers = hp['n_res_blks'] // 2
        nf = hp['nf']
        cnn_f = [Conv2dBlock(3, nf, 7, 1, 3,
                             pad_type='reflect',
                             norm='none',
                             activation='none')]
        for i in range(self.n_layers - 1):
            nf_out = np.min([nf * 2, 1024])
            cnn_f += [ActFirstResBlock(nf, nf, None, 'lrelu', 'none')]
            cnn_f += [ActFirstResBlock(nf, nf_out, None, 'lrelu', 'none')]
            cnn_f += [nn.ReflectionPad2d(1)]
            cnn_f += [nn.AvgPool2d(kernel_size=3, stride=2)]
            nf = np.min([nf * 2, 1024])
        nf_out = np.min([nf * 2, 1024])
        cnn_f += [ActFirstResBlock(nf, nf, None, 'lrelu', 'none')]
        cnn_f += [ActFirstResBlock(nf, nf_out, None, 'lrelu', 'none')]
        cnn_c = [Conv2dBlock(nf_out, config['modalities']['k_source'] + config['modalities']['k_target'], 1, 1,
                             norm='none',
                             activation='lrelu',
                             activation_first=True)]
        cnn_classifier = [Conv2dBlock(nf_out, config['modalities']['k_source'] + config['modalities']['k_target'], 1, 1,
                             norm='none',
                             activation='lrelu',
                             activation_first=True)]
        self.cnn_f = nn.Sequential(*cnn_f)
        self.cnn_c = nn.Sequential(*cnn_c)
        self.cnn_classifier = nn.Sequential(*cnn_classifier)

    def forward(self, x, y):
        assert(x.size(0) == y.size(0))
        feat = self.cnn_f(x)
        out = self.cnn_c(feat)
        cls_out = self.cnn_classifier(feat)
        labels_score = out.mean(dim=[2,3])
        cls_labels_score = cls_out.mean(dim=[2, 3])
        dis_label = labels_score.argmax(dim=1)
        cls_label = cls_labels_score.argmax(dim=1)
        index = torch.LongTensor(range(out.size(0))).to(self.config['device'])
        out = out[index, y, :, :]
        return out, feat, dis_label, cls_out, cls_label

    def calc_dis_fake_loss(self, input_fake, input_label):
        resp_fake, gan_feat, dis_label, _, _ = self.forward(input_fake, input_label)
        total_count = torch.tensor(np.prod(resp_fake.size()),
                                   dtype=torch.float).to(self.config['device'])
        # hinge loss
        fake_loss = torch.nn.ReLU()(1.0 + resp_fake).mean()
        correct_count = (resp_fake < 0).sum()
        fake_accuracy = correct_count.type_as(fake_loss) / total_count
        return fake_loss, fake_accuracy, resp_fake

    def calc_dis_real_loss(self, input_real, input_label):
        resp_real, gan_feat, dis_label, _, _ = self.forward(input_real, input_label)
        total_count = torch.tensor(np.prod(resp_real.size()),
                                   dtype=torch.float).to(self.config['device'])
        real_loss = torch.nn.ReLU()(1.0 - resp_real).mean()
        correct_count = (resp_real >= 0).sum()
        real_accuracy = correct_count.type_as(real_loss) / total_count
        return real_loss, real_accuracy, resp_real

    def calc_gen_loss(self, input_fake, input_fake_label):
        resp_fake, gan_feat, dis_label, _, _ = self.forward(input_fake, input_fake_label)
        total_count = torch.tensor(np.prod(resp_fake.size()),
                                   dtype=torch.float).to(self.config['device'])
        loss = -resp_fake.mean()
        correct_count = (resp_fake >= 0).sum()
        accuracy = correct_count.type_as(loss) / total_count
        return loss, accuracy, gan_feat

    def calc_dis_cls_loss(self, input, label):
        _, _, _, resp_real, pred = self.forward(input, label)
        real_loss = nn.CrossEntropyLoss()(resp_real.mean(dim=[2,3]), label)
        return real_loss, resp_real

    def calc_grad2(self, d_out, x_in):
        batch_size = x_in.size(0)
        grad_dout = autograd.grad(outputs=d_out.mean(),
                                  inputs=x_in,
                                  create_graph=True,
                                  retain_graph=True,
                                  only_inputs=True)[0]
        grad_dout2 = grad_dout.pow(2)
        assert (grad_dout2.size() == x_in.size())
        reg = grad_dout2.sum()/batch_size
        return reg

class Generator(nn.Module):
    def __init__(self, config):
        super(Generator, self).__init__()
        hp = config['gan']['gen']
        nf = hp['nf']
        nf_mlp = hp['nf_mlp']
        down_class = hp['n_downs_class']
        down_content = hp['n_downs_content']
        n_mlp_blks = hp['n_mlp_blks']
        n_res_blks = hp['n_res_blks']
        latent_dim = hp['latent_dim']
        self.enc_class_model = TargetEncoder(down_class,
                                             3,
                                             nf,
                                             latent_dim,
                                             norm='none',
                                             activ='relu',
                                             pad_type='reflect')

        self.enc_content = SourceEncoder(down_content,
                                         n_res_blks,
                                         3,
                                         nf,
                                          'in',
                                         activ='relu',
                                         pad_type='reflect')

        self.dec = Decoder(down_content,
                           n_res_blks,
                           self.enc_content.output_dim,
                           3,
                           res_norm='adain',
                           activ='relu',
                           pad_type='reflect')

        self.mlp = MLP(latent_dim,
                       get_num_adain_params(self.dec),
                       nf_mlp,
                       n_mlp_blks,
                       norm='none',
                       activ='relu')

    def forward(self, one_image, model_set):
        # reconstruct an image
        content, model_codes = self.encode(one_image, model_set)
        model_code = torch.mean(model_codes, dim=0).unsqueeze(0)
        images_trans = self.decode(content, model_code)
        return images_trans

    def encode(self, one_image, model_set):
        # extract content code from the input image
        content = self.enc_content(one_image)
        # extract model code from the images in the model set
        class_codes = self.enc_class_model(model_set)
        class_code = torch.mean(class_codes, dim=0).unsqueeze(0)
        return content, class_code

    def decode(self, content, model_code):
        # decode content and style codes to an image
        adain_params = self.mlp(model_code)
        assign_adain_params(adain_params, self.dec)
        images = self.dec(content)
        return images

class TargetEncoder(nn.Module):
    def __init__(self, downs, ind_im, dim, latent_dim, norm, activ, pad_type):
        super(TargetEncoder, self).__init__()
        self.model = []
        self.model += [Conv2dBlock(ind_im, dim, 7, 1, 3,
                                   norm=norm,
                                   activation=activ,
                                   pad_type=pad_type)]
        for i in range(2):
            self.model += [Conv2dBlock(dim, 2 * dim, 4, 2, 1,
                                       norm=norm,
                                       activation=activ,
                                       pad_type=pad_type)]
            dim *= 2
        for i in range(downs - 2):
            self.model += [Conv2dBlock(dim, dim, 4, 2, 1,
                                       norm=norm,
                                       activation=activ,
                                       pad_type=pad_type)]
        self.model += [nn.AdaptiveAvgPool2d(1)]
        self.model += [nn.Conv2d(dim, latent_dim, 1, 1, 0)]
        self.model = nn.Sequential(*self.model)
        self.output_dim = dim

    def forward(self, x):
        return self.model(x)

class SourceEncoder(nn.Module):
    def __init__(self, downs, n_res, input_dim, dim, norm, activ, pad_type):
        super(SourceEncoder, self).__init__()
        self.model = []
        self.model += [Conv2dBlock(input_dim, dim, 7, 1, 3,
                                   norm=norm,
                                   activation=activ,
                                   pad_type=pad_type)]
        for i in range(downs):
            self.model += [Conv2dBlock(dim, 2 * dim, 4, 2, 1,
                                       norm=norm,
                                       activation=activ,
                                       pad_type=pad_type)]
            dim *= 2
        self.model += [ResBlocks(n_res, dim,
                                 norm=norm,
                                 activation=activ,
                                 pad_type=pad_type)]
        self.model = nn.Sequential(*self.model)
        self.output_dim = dim

    def forward(self, x):
        return self.model(x)

class Decoder(nn.Module):
    def __init__(self, ups, n_res, dim, out_dim, res_norm, activ, pad_type):
        super(Decoder, self).__init__()

        self.model = []
        self.model += [ResBlocks(n_res, dim, res_norm,
                                 activ, pad_type=pad_type)]
        for i in range(ups):
            self.model += [nn.Upsample(scale_factor=2),
                           Conv2dBlock(dim, dim // 2, 5, 1, 2,
                                       norm='in',
                                       activation=activ,
                                       pad_type=pad_type)]
            dim //= 2
        self.model += [Conv2dBlock(dim, out_dim, 7, 1, 3,
                                   norm='none',
                                   activation='tanh',
                                   pad_type=pad_type)]
        self.model = nn.Sequential(*self.model)

    def forward(self, x):
        return self.model(x)

class MLP(nn.Module):
    def __init__(self, in_dim, out_dim, dim, n_blk, norm, activ):

        super(MLP, self).__init__()
        self.model = []
        self.model += [LinearBlock(in_dim, dim, norm=norm, activation=activ)]
        for i in range(n_blk - 2):
            self.model += [LinearBlock(dim, dim, norm=norm, activation=activ)]
        self.model += [LinearBlock(dim, out_dim,
                                   norm='none', activation='none')]
        self.model = nn.Sequential(*self.model)

    def forward(self, x):
        return self.model(x.view(x.size(0), -1))

max_stars_repo_name: orpatashnik/BalaGAN

max_issues_repo_name: orpatashnik/BalaGAN

max_forks_repo_name: orpatashnik/BalaGAN


text:
# Copyright (c) 2018-present, Royal Bank of Canada and other authors.
# See the AUTHORS.txt file for a list of contributors.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F

from torch.distributions import laplace
from torch.distributions import uniform
from torch.nn.modules.loss import _Loss

from abc import ABCMeta
# from py_attacks.base import Attack, LabelMixin

#------------------------------------------------------------------------------------------

class Attack(object):
    """
    Abstract base class for all attack classes.

    :param predict: forward pass function.
    :param loss_fn: loss function that takes .
    :param clip_min: mininum value per input dimension.
    :param clip_max: maximum value per input dimension.

    """

    __metaclass__ = ABCMeta

    def __init__(self, predict, loss_fn, clip_min, clip_max):
        """Create an Attack instance."""
        self.predict = predict
        self.loss_fn = loss_fn
        self.clip_min = clip_min
        self.clip_max = clip_max

    def perturb(self, x, **kwargs):
        """Virtual method for generating the adversarial examples.

        :param x: the model's input tensor.
        :param **kwargs: optional parameters used by child classes.
        :return: adversarial examples.
        """
        error = "Sub-classes must implement perturb."
        raise NotImplementedError(error)

    def __call__(self, *args, **kwargs):
        return self.perturb(*args, **kwargs)

class LabelMixin(object):
    def _get_predicted_label(self, x):
        """
        Compute predicted labels given x. Used to prevent label leaking
        during adversarial training.

        :param x: the model's input tensor.
        :return: tensor containing predicted labels.
        """
        with torch.no_grad():
            outputs = self.predict(x)
        _, y = torch.max(outputs, dim=1)
        return y

    def _verify_and_process_inputs(self, x, y):
        if self.targeted:
            assert y is not None

        if not self.targeted:
            if y is None:
                y = self._get_predicted_label(x)

        x = replicate_input(x)
        y = replicate_input(y)
        return x, y

#------------------------------------------------------------------------------------------

def torch_allclose(x, y, rtol=1.e-5, atol=1.e-8):
    """
    Wrap on numpy's allclose. Input x and y are both tensors of equal shape

    Original numpy documentation:
    https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.allclose.html

    Notes:
    If the following equation is element-wise True, then allclose returns
    True.

     absolute(`a` - `b`) <= (`atol` + `rtol` * absolute(`b`))

    :param x: (torch tensor)
    :param y: (torch tensor)
    :param rtol: (float) the relative tolerance parameter
    :param atol: (float) the absolute tolerance parameter
    :return: (bool) if x and y are all close
    """
    import numpy as np
    return np.allclose(x.detach().cpu().numpy(), y.detach().cpu().numpy(),
                       rtol=rtol, atol=atol)

def single_dim_flip(x, dim):
    dim = x.dim() + dim if dim < 0 else dim
    indices = torch.arange(
        x.size(dim) - 1, -1, -1,
        dtype=torch.long, device=x.device, requires_grad=x.requires_grad)
    # TODO: do we need requires_grad???
    return x.index_select(dim, indices)

def torch_flip(x, dims):
    for dim in dims:
        x = single_dim_flip(x, dim)
    return x

def replicate_input(x):
    return x.detach().clone()

def replicate_input_withgrad(x):
    return x.detach().clone().requires_grad_()

def calc_l2distsq(x, y):
    d = (x - y)**2
    return d.view(d.shape[0], -1).sum(dim=1)

def calc_l1dist(x, y):
    d = torch.abs(x - y)
    return d.view(d.shape[0], -1).sum(dim=1)

def tanh_rescale(x, x_min=-1., x_max=1.):
    return (torch.tanh(x)) * 0.5 * (x_max - x_min) + (x_max + x_min) * 0.5

def torch_arctanh(x, eps=1e-6):
    return (torch.log((1 + x) / (1 - x))) * 0.5

def clamp(input, min=None, max=None):
    ndim = input.ndimension()
    if min is None:
        pass
    elif isinstance(min, (float, int)):
        input = torch.clamp(input, min=min)
    elif isinstance(min, torch.Tensor):
        if min.ndimension() == ndim - 1 and min.shape == input.shape[1:]:
            input = torch.max(input, min.view(1, *min.shape))
        else:
            assert min.shape == input.shape
            input = torch.max(input, min)
    else:
        raise ValueError("min can only be None | float | torch.Tensor")

    if max is None:
        pass
    elif isinstance(max, (float, int)):
        input = torch.clamp(input, max=max)
    elif isinstance(max, torch.Tensor):
        if max.ndimension() == ndim - 1 and max.shape == input.shape[1:]:
            input = torch.min(input, max.view(1, *max.shape))
        else:
            assert max.shape == input.shape
            input = torch.min(input, max)
    else:
        raise ValueError("max can only be None | float | torch.Tensor")
    return input

def to_one_hot(y, num_classes=10):
    """
    Take a batch of label y with n dims and convert it to
    1-hot representation with n+1 dims.
    Link: https://discuss.pytorch.org/t/convert-int-into-one-hot-format/507/24
    """
    y = replicate_input(y).view(-1, 1)
    y_one_hot = y.new_zeros((y.size()[0], num_classes)).scatter_(1, y, 1)
    return y_one_hot

class CarliniWagnerLoss(nn.Module):
    """
    Carlini-Wagner Loss: objective function #6.
    Paper: https://arxiv.org/pdf/1608.04644.pdf
    """

    def __init__(self):
        super(CarliniWagnerLoss, self).__init__()

    def forward(self, input, target):
        """
        :param input: pre-softmax/logits.
        :param target: true labels.
        :return: CW loss value.
        """
        num_classes = input.size(1)
        label_mask = to_one_hot(target, num_classes=num_classes).float()
        correct_logit = torch.sum(label_mask * input, dim=1)
        wrong_logit = torch.max((1. - label_mask) * input, dim=1)[0]
        loss = -F.relu(correct_logit - wrong_logit + 50.).sum()
        return loss

def _batch_multiply_tensor_by_vector(vector, batch_tensor):
    """Equivalent to the following
    for ii in range(len(vector)):
        batch_tensor.data[ii] *= vector[ii]
    return batch_tensor
    """
    return (
        batch_tensor.transpose(0, -1) * vector).transpose(0, -1).contiguous()

def _batch_clamp_tensor_by_vector(vector, batch_tensor):
    """Equivalent to the following
    for ii in range(len(vector)):
        batch_tensor[ii] = clamp(
            batch_tensor[ii], -vector[ii], vector[ii])
    """
    return torch.min(
        torch.max(batch_tensor.transpose(0, -1), -vector), vector
    ).transpose(0, -1).contiguous()

def batch_multiply(float_or_vector, tensor):
    if isinstance(float_or_vector, torch.Tensor):
        assert len(float_or_vector) == len(tensor)
        tensor = _batch_multiply_tensor_by_vector(float_or_vector, tensor)
    elif isinstance(float_or_vector, float):
        tensor *= float_or_vector
    else:
        raise TypeError("Value has to be float or torch.Tensor")
    return tensor

def batch_clamp(float_or_vector, tensor):
    if isinstance(float_or_vector, torch.Tensor):
        assert len(float_or_vector) == len(tensor)
        tensor = _batch_clamp_tensor_by_vector(float_or_vector, tensor)
        return tensor
    elif isinstance(float_or_vector, float):
        tensor = clamp(tensor, -float_or_vector, float_or_vector)
    else:
        raise TypeError("Value has to be float or torch.Tensor")
    return tensor

def _get_norm_batch(x, p):
    batch_size = x.size(0)
    return x.abs().pow(p).view(batch_size, -1).sum(dim=1).pow(1. / p)

def _thresh_by_magnitude(theta, x):
    return torch.relu(torch.abs(x) - theta) * x.sign()

def batch_l1_proj_flat(x, z=1):
    """
    Implementation of L1 ball projection from:

    https://stanford.edu/~jduchi/projects/DuchiShSiCh08.pdf

    inspired from:

    https://gist.github.com/daien/1272551/edd95a6154106f8e28209a1c7964623ef8397246

    :param x: input data
    :param eps: l1 radius

    :return: tensor containing the projection.
    """

    # Computing the l1 norm of v
    v = torch.abs(x)
    v = v.sum(dim=1)

    # Getting the elements to project in the batch
    indexes_b = torch.nonzero(v > z).view(-1)
    if isinstance(z, torch.Tensor):
        z = z[indexes_b][:, None]
    x_b = x[indexes_b]
    batch_size_b = x_b.size(0)

    # If all elements are in the l1-ball, return x
    if batch_size_b == 0:
        return x

    # make the projection on l1 ball for elements outside the ball
    view = x_b
    view_size = view.size(1)
    mu = view.abs().sort(1, descending=True)[0]
    vv = torch.arange(view_size).float().to(x.device)
    st = (mu.cumsum(1) - z) / (vv + 1)
    u = (mu - st) > 0
    if u.dtype.__str__() == "torch.bool":  # after and including torch 1.2
        rho = (~u).cumsum(dim=1).eq(0).sum(1) - 1
    else:  # before and including torch 1.1
        rho = (1 - u).cumsum(dim=1).eq(0).sum(1) - 1
    theta = st.gather(1, rho.unsqueeze(1))
    proj_x_b = _thresh_by_magnitude(theta, x_b)

    # gather all the projected batch
    proj_x = x.detach().clone()
    proj_x[indexes_b] = proj_x_b
    return proj_x

def batch_l1_proj(x, eps):
    batch_size = x.size(0)
    view = x.view(batch_size, -1)
    proj_flat = batch_l1_proj_flat(view, z=eps)
    return proj_flat.view_as(x)

def clamp_by_pnorm(x, p, r):
    assert isinstance(p, float) or isinstance(p, int)
    norm = _get_norm_batch(x, p)
    if isinstance(r, torch.Tensor):
        assert norm.size() == r.size()
    else:
        assert isinstance(r, float)
    factor = torch.min(r / norm, torch.ones_like(norm))
    return batch_multiply(factor, x)

def is_float_or_torch_tensor(x):
    return isinstance(x, torch.Tensor) or isinstance(x, float)

def normalize_by_pnorm(x, p=2, small_constant=1e-6):
    """
    Normalize gradients for gradient (not gradient sign) attacks.
    # TODO: move this function to utils

    :param x: tensor containing the gradients on the input.
    :param p: (optional) order of the norm for the normalization (1 or 2).
    :param small_constant: (optional float) to avoid dividing by zero.
    :return: normalized gradients.
    """
    # loss is averaged over the batch so need to multiply the batch
    # size to find the actual gradient of each input sample

    assert isinstance(p, float) or isinstance(p, int)
    norm = _get_norm_batch(x, p)
    norm = torch.max(norm, torch.ones_like(norm) * small_constant)
    return batch_multiply(1. / norm, x)

def jacobian(model, x, output_class):
    """
    Compute the output_class'th row of a Jacobian matrix. In other words,
    compute the gradient wrt to the output_class.

    :param model: forward pass function.
    :param x: input tensor.
    :param output_class: the output class we want to compute the gradients.
    :return: output_class'th row of the Jacobian matrix wrt x.
    """
    xvar = replicate_input_withgrad(x)
    scores = model(xvar)

    # compute gradients for the class output_class wrt the input x
    # using backpropagation
    torch.sum(scores[:, output_class]).backward()

    return xvar.grad.detach().clone()

MNIST_MEAN = (0.1307,)
MNIST_STD = (0.3081,)

CIFAR10_MEAN = (0.4914, 0.4822, 0.4465)
CIFAR10_STD = (0.2023, 0.1994, 0.2010)

class NormalizeByChannelMeanStd(nn.Module):
    def __init__(self, mean, std):
        super(NormalizeByChannelMeanStd, self).__init__()
        if not isinstance(mean, torch.Tensor):
            mean = torch.tensor(mean)
        if not isinstance(std, torch.Tensor):
            std = torch.tensor(std)
        self.register_buffer("mean", mean)
        self.register_buffer("std", std)

    def forward(self, tensor):
        return normalize_fn(tensor, self.mean, self.std)

    def extra_repr(self):
        return 'mean={}, std={}'.format(self.mean, self.std)

def normalize_fn(tensor, mean, std):
    """Differentiable version of torchvision.functional.normalize"""
    # here we assume the color channel is in at dim=1
    mean = mean[None, :, None, None]
    std = std[None, :, None, None]
    return tensor.sub(mean).div(std)

def batch_per_image_standardization(imgs):
    # replicate tf.image.per_image_standardization, but in batch
    assert imgs.ndimension() == 4
    mean = imgs.view(imgs.shape[0], -1).mean(dim=1).view(
        imgs.shape[0], 1, 1, 1)
    return (imgs - mean) / batch_adjusted_stddev(imgs)

def batch_adjusted_stddev(imgs):
    # for batch_per_image_standardization
    std = imgs.view(imgs.shape[0], -1).std(dim=1).view(imgs.shape[0], 1, 1, 1)
    std_min = 1. / imgs.new_tensor(imgs.shape[1:]).prod().float().sqrt()
    return torch.max(std, std_min)

class PerImageStandardize(nn.Module):
    def __init__(self):
        super(PerImageStandardize, self).__init__()

    def forward(self, tensor):
        return batch_per_image_standardization(tensor)

def predict_from_logits(logits, dim=1):
    return logits.max(dim=dim, keepdim=False)[1]

def get_accuracy(pred, target):
    return pred.eq(target).float().mean().item()

def set_torch_deterministic():
    import torch.backends.cudnn as cudnn
    cudnn.benchmark = False
    cudnn.deterministic = True

def set_seed(seed=None):
    import torch
    import numpy as np
    import random
    if seed is not None:
        torch.manual_seed(seed)
    np.random.seed(seed)
    random.seed(seed)

#------------------------------------------------------------------------------------------

def rand_init_delta(delta, x, ord, eps, clip_min, clip_max):
    # TODO: Currently only considered one way of "uniform" sampling
    # for Linf, there are 3 ways:
    #   1) true uniform sampling by first calculate the rectangle then sample
    #   2) uniform in eps box then truncate using data domain (implemented)
    #   3) uniform sample in data domain then truncate with eps box
    # for L2, true uniform sampling is hard, since it requires uniform sampling
    #   inside a intersection of cube and ball, so there are 2 ways:
    #   1) uniform sample in the data domain, then truncate using the L2 ball
    #       (implemented)
    #   2) uniform sample in the L2 ball, then truncate using the data domain
    # for L1: uniform l1 ball init, then truncate using the data domain

    if isinstance(eps, torch.Tensor):
        assert len(eps) == len(delta)

    if ord == np.inf:
        delta.data.uniform_(-1, 1)
        delta.data = batch_multiply(eps, delta.data)
    elif ord == 2:
        delta.data.uniform_(clip_min, clip_max)
        delta.data = delta.data - x
        delta.data = clamp_by_pnorm(delta.data, ord, eps)
    elif ord == 1:
        ini = laplace.Laplace(
            loc=delta.new_tensor(0), scale=delta.new_tensor(1))
        delta.data = ini.sample(delta.data.shape)
        delta.data = normalize_by_pnorm(delta.data, p=1)
        ray = uniform.Uniform(0, eps).sample()
        delta.data *= ray
        delta.data = clamp(x.data + delta.data, clip_min, clip_max) - x.data
    else:
        error = "Only ord = inf, ord = 1 and ord = 2 have been implemented"
        raise NotImplementedError(error)

    delta.data = clamp(
        x + delta.data, min=clip_min, max=clip_max) - x
    return delta.data

def is_successful(y1, y2, targeted):
    if targeted is True:
        return y1 == y2
    else:
        return y1 != y2

class AttackConfig(object):
    # a convenient class for generate an attack/adversary instance

    def __init__(self):
        self.kwargs = {}

        for mro in reversed(self.__class__.__mro__):
            if mro in (AttackConfig, object):
                continue
            for kwarg in mro.__dict__:
                if kwarg in self.AttackClass.__init__.__code__.co_varnames:
                    self.kwargs[kwarg] = mro.__dict__[kwarg]
                else:
                    # make sure we don't specify wrong kwargs
                    assert kwarg in ["__module__", "AttackClass", "__doc__"]

    def __call__(self, *args):
        adversary = self.AttackClass(*args, **self.kwargs)
        print(self.AttackClass, args, self.kwargs)
        return adversary

def multiple_mini_batch_attack(
        adversary, loader, device="cuda", save_adv=False,
        norm=None, num_batch=None):
    lst_label = []
    lst_pred = []
    lst_advpred = []
    lst_dist = []

    _norm_convert_dict = {"Linf": "inf", "L2": 2, "L1": 1}
    if norm in _norm_convert_dict:
        norm = _norm_convert_dict[norm]

    if norm == "inf":
        def dist_func(x, y):
            return (x - y).view(x.size(0), -1).max(dim=1)[0]
    elif norm == 1 or norm == 2:
        from advertorch.utils import _get_norm_batch

        def dist_func(x, y):
            return _get_norm_batch(x - y, norm)
    else:
        assert norm is None

    idx_batch = 0

    for data, label in loader:
        data, label = data.to(device), label.to(device)
        adv = adversary.perturb(data, label)
        advpred = predict_from_logits(adversary.predict(adv))
        pred = predict_from_logits(adversary.predict(data))
        lst_label.append(label)
        lst_pred.append(pred)
        lst_advpred.append(advpred)
        if norm is not None:
            lst_dist.append(dist_func(data, adv))

        idx_batch += 1
        if idx_batch == num_batch:
            break

    return torch.cat(lst_label), torch.cat(lst_pred), torch.cat(lst_advpred), \
        torch.cat(lst_dist) if norm is not None else None

class MarginalLoss(_Loss):
    # TODO: move this to advertorch.loss

    def forward(self, logits, targets):  # pylint: disable=arguments-differ
        assert logits.shape[-1] >= 2
        top_logits, top_classes = torch.topk(logits, 2, dim=-1)
        target_logits = logits[torch.arange(logits.shape[0]), targets]
        max_nontarget_logits = torch.where(
            top_classes[..., 0] == targets,
            top_logits[..., 1],
            top_logits[..., 0],
        )

        loss = max_nontarget_logits - target_logits
        if self.reduction == "none":
            pass
        elif self.reduction == "sum":
            loss = loss.sum()
        elif self.reduction == "mean":
            loss = loss.mean()
        else:
            raise ValueError("unknown reduction: '%s'" % (self.recution,))

        return loss

class ChooseBestAttack(Attack, LabelMixin):
    def __init__(self, predict, base_adversaries, loss_fn=None,
                 targeted=False):
        self.predict = predict
        self.base_adversaries = base_adversaries
        self.loss_fn = loss_fn
        self.targeted = targeted

        if self.loss_fn is None:
            self.loss_fn = ZeroOneLoss(reduction="none")
        else:
            assert self.loss_fn.reduction == "none"

        for adversary in self.base_adversaries:
            assert self.targeted == adversary.targeted

    def perturb(self, x, y=None):
        # TODO: might want to also retain the list of all attacks

        x, y = self._verify_and_process_inputs(x, y)

        with torch.no_grad():
            maxloss = self.loss_fn(self.predict(x), y)
        final_adv = torch.zeros_like(x)
        for adversary in self.base_adversaries:
            adv = adversary.perturb(x, y)
            loss = self.loss_fn(self.predict(adv), y)
            to_replace = maxloss < loss
            final_adv[to_replace] = adv[to_replace]
            maxloss[to_replace] = loss[to_replace]

        return final_adv

def attack_whole_dataset(adversary, loader, device="cuda"):
    lst_adv = []
    lst_label = []
    lst_pred = []
    lst_advpred = []
    for data, label in loader:
        data, label = data.to(device), label.to(device)
        pred = predict_from_logits(adversary.predict(data))
        adv = adversary.perturb(data, label)
        advpred = predict_from_logits(adversary.predict(adv))
        lst_label.append(label)
        lst_pred.append(pred)
        lst_advpred.append(advpred)
        lst_adv.append(adv)
    return torch.cat(lst_adv), torch.cat(lst_label), torch.cat(lst_pred), \
        torch.cat(lst_advpred)

max_stars_repo_name: eashanadhikarla/MemoryDef

max_issues_repo_name: eashanadhikarla/MemoryDef

max_forks_repo_name: eashanadhikarla/MemoryDef


text:
#!/usr/local/bin/python3
# -*- coding:utf-8 -*-

import __future__
import numpy as np
import sys
import os
sys.path.insert(1, os.path.join(sys.path[0], '..'))
from script.rio import read_quantity, read_converter
from timeit import default_timer as timer

def compare_data(input_path1, input_path2, quantityNameList, precision):
    uid_to_coords_1 = dict()
    uid_to_coords_2 = dict()
    floatType = np.float64 if precision == 'double' else np.float32

    Nx1, Ny1, dx1, dy1 = read_converter(input_path1, uid_to_coords_1)
    Nx2, Ny2, dx2, dy2 = read_converter(input_path2, uid_to_coords_2)

    if not dx1 == dx2 or not dy1 == dy2 or not Nx1 == Nx2 or not Ny1 == Ny2:
        return False, "domain", (dx1, dy1, None)

    data1 = np.zeros((Nx1, Ny1), dtype = floatType)
    data2 = np.zeros((Nx2, Ny2), dtype = floatType)

    for q in quantityNameList:
        start = timer()
        read_quantity(data1, input_path1, uid_to_coords_1, q)
        read_quantity(data2, input_path2, uid_to_coords_2, q)
        end = timer()
        print("Time to load quantity", q, "%s second(s)" % int((end - start)))

        if not np.array_equal(data1, data2):
            print(q, input_path1, input_path2)
            print(dx1, dy1, dx2, dy2)
            print(data1 - data2)
            comparedata = data2 - data1
            return False, q, (dx1, dy1, comparedata)

    return True, None, (None, None, None)

max_stars_repo_name: will-iam/Variant

max_issues_repo_name: will-iam/Variant

max_forks_repo_name: will-iam/Variant