Пример #1
0
 def init(self):
     for mode in ['valid', 'full', 'same']:
         for dtype in util.TYPES.FLOAT:
             for cmd1, shape1 in util.gen_random_arrays(
                     "R",
                     3,
                     min_ndim=1,
                     max_dim=10,
                     samples_in_each_ndim=1,
                     dtype=dtype,
                     no_views=True):
                 if util.prod(shape1) <= 0:
                     continue
                 # Notice, the second shape must have the same number of dimension as the first one
                 # and the max dimension cannot be larger than in the first shape
                 # Finally, dimensions above 5 simple take too long
                 max_dim = min(min(shape1), 5)
                 for cmd2, shape2 in util.gen_random_arrays(
                         "R",
                         len(shape1),
                         min_ndim=len(shape1),
                         max_dim=max_dim,
                         samples_in_each_ndim=1,
                         dtype=dtype,
                         no_views=True):
                     if util.prod(shape2) > 0:
                         cmd = "R = bh.random.RandomState(42); a=%s; v=%s;" % (
                             cmd1, cmd2)
                         yield (cmd, mode)
Пример #2
0
    def __init__(self, argv, environ, shell_runner=None, service_json_loader=None, platform_config_loader=None):
        """Deployment constructor."""
        arguments = docopt(__doc__, argv=argv)
        self.shell_runner = shell_runner if shell_runner is not None else util.ShellRunner()
        self.environment = arguments.get('<environment>')
        self.version = arguments.get('<version>')
        self.component_name = util.get_component_name(arguments, environ, self.shell_runner)
        self.leg = arguments.get('--leg')
        self.plan = arguments.get('--plan')
        self.tfargs = arguments.get('<tfargs>')

        if not service_json_loader:
            service_json_loader = util.ServiceJsonLoader()
        self.config = None
        self.metadata = util.apply_metadata_defaults(
            service_json_loader.load(),
            self.component_name
        )
        if not platform_config_loader:
            platform_config_loader = util.PlatformConfigLoader()
        self.account_id = platform_config_loader.load(self.metadata['REGION'], self.metadata['ACCOUNT_PREFIX'],
                                                      util.prod(self.environment))['account_id']
        if util.prod(self.environment):
            dev_account_id = platform_config_loader.load(self.metadata['REGION'],
                                                         self.metadata['ACCOUNT_PREFIX'])['account_id']
        else:
            dev_account_id = self.account_id
        self.ecr_image_name = util.ecr_image_name(
            dev_account_id,
            self.metadata['REGION'],
            self.component_name,
            self.version
        )
        self.aws = None
Пример #3
0
    def prove_decrypt_batched(self, ciphertext_batch):
        """Batched version of `prove_decrypt()`

        Arguments:
            ciphertext_batch (list): the ciphertexts (PaillierCiphertext) to be
                decrypted in a verifiable manner

        Returns:
            tuple: `partial_decryption_batch`, `proof` where
            `partial_decryption_batch` is a list of the partial decryptions
            (int) of the ciphertexts, and `proof` is a proof (int) that they
            are indeed so
        """
        pk = self.public_key
        partial_decryption_batch = [
            self.decrypt(ciphertext) for ciphertext in ciphertext_batch
        ]

        # run protocol in the Fiat-Shamir heuristic

        # to aggregate ZKPs, the verifier provides λ_i *after* the plaintexts
        # have been provided; then combined_ciphertext = ∏ ciphertext^{λ_i}
        # and combined_plaintext = ∏ m^{λ_i} (not needed for prover)
        lambda_batch = [
            util.H([ciphertext.raw_value, partial_decryption])
            for ciphertext, partial_decryption in zip(
                ciphertext_batch, partial_decryption_batch)
        ]
        combined_plaintext = util.prod(
            util.powmod(plaintext, lambda_, pk.n) for plaintext, lambda_ in
            zip(partial_decryption_batch, lambda_batch))
        combined_ciphertext = util.prod(
            util.powmod(ciphertext.raw_value, lambda_, pk.n)
            for ciphertext, lambda_ in zip(ciphertext_batch, lambda_batch))

        try:
            # raises IndexError if not enough precomputations were forecast
            r, t1 = self.precomputed_values.pop()
        except AttributeError:
            # no pre-computations
            r = random.SystemRandom().randrange(pk.n <<
                                                (2 * pk.security_parameter))
            t1 = util.powmod(self.verification_base, r, pk.n)

        # prove knowledge of key_share such that:
        #   * v_i = v**key_share
        #   * combined_plaintext**2 = (combined_ciphertext**2)**(2*key_share)
        t2 = util.powmod(combined_ciphertext, _CP * _QR * r, pk.n)
        h = util.H([
            combined_ciphertext,
            combined_plaintext,
            t1,
            self.verification_base,
            self.verification,
            t2,
        ])
        w = r + h * self.key_share
        proof = t1, t2, w
        return partial_decryption_batch, proof
Пример #4
0
    def __init__(self, in_size, k,  gadditional=0, radditional=0,
                 region=None, sigma_scale=0.1,
                 num_values=-1, min_sigma=0.0, glimpses=1, pool=4, coord='none'):

        assert(len(in_size) == 3)

        self.in_size = in_size
        self.k = k
        self.sigma_scale = sigma_scale
        self.num_values = num_values
        self.min_sigma = min_sigma
        self.num_glimpses = glimpses

        ci, hi, wi = in_size
        co, ho, wo = ci , k, k
        out_size = glimpses, co, ho, wo

        self.out_size = out_size

        map = (glimpses, co, k, k)

        template = torch.LongTensor(list(np.ndindex( map))) # [:, (2, 0, 1)]

        assert template.size() == (prod(map), 4)

        pixel_indices = template[:, 2:].clone()

        template = torch.cat([template, template[:, 1:]], dim=1)

        assert template.size() == (prod(map), 7)

        self.lc = [5, 6] # learnable columns

        super().__init__(
            in_rank=3, out_size=(glimpses, co, ho, wo),
            temp_indices=template,
            learn_cols=self.lc,
            chunk_size=1,
            gadditional=gadditional, radditional=radditional, region=region,
            bias_type=util.Bias.NONE)

        # scale to [0,1] in each dim
        pixel_indices = pixel_indices.float() / torch.FloatTensor([[k, k]]).expand_as(pixel_indices)
        self.register_buffer('pixel_indices', pixel_indices)

        modules = prep(ci, hi, wi, pool, coord) + [nn.ReLU(), nn.Linear(HIDLIN, 4 * glimpses), util.Reshape((glimpses, 4))]
        self.preprocess = nn.Sequential(*modules)

        self.register_buffer('bbox_offset', torch.FloatTensor([-1, 1, -1, 1]))
        # -- added to the bounding box, to make sure there's a training signal
        #    from the initial weights (i.e. in case all outputs are close to zero)

        # One sigma per glimpse
        self.sigmas = Parameter(torch.randn( (glimpses, ) ))

        # All values 1, no bias. Glimpses extract only pixel information.
        self.register_buffer('one', torch.FloatTensor([1.0]))
Пример #5
0
    def __init__(self, in_size, k,  gadditional=0, radditional=0,
                 region=None, sigma_scale=0.1,
                 num_values=-1, min_sigma=0.0, glimpses=1,
                 scale=0.001, pool=4, coord='none'):

        assert(len(in_size) == 3)

        self.in_size = in_size
        self.k = k
        self.sigma_scale = sigma_scale
        self.num_values = num_values
        self.min_sigma = min_sigma
        self.scale = scale

        ci, hi, wi = in_size
        co, ho, wo = ci , k, k
        out_size = glimpses, co, ho, wo

        self.out_size = out_size

        map = (glimpses, co, k, k)

        template = torch.LongTensor(list(np.ndindex( map)))

        assert template.size() == (prod(map), 4)

        template = torch.cat([template, template[:, 1:]], dim=1)

        assert template.size() == (prod(map), 7)

        self.lc = [5, 6] # learnable columns

        super().__init__(
            in_rank=3, out_size=(glimpses, co, ho, wo),
            temp_indices=template,
            learn_cols=self.lc,
            chunk_size=1,
            gadditional=gadditional, radditional=radditional, region=region,
            bias_type=util.Bias.NONE)

        self.num_glimpses = glimpses

        modules = prep(ci, hi, wi, pool, coord) + [nn.ReLU(), nn.Linear(HIDLIN, (2 * 3) * glimpses), util.Reshape((glimpses, 2, 3))]
        self.preprocess = nn.Sequential(*modules)

        self.register_buffer('grid', util.interpolation_grid((k, k)))

        self.register_buffer('identity', torch.FloatTensor([0.4, 0, 0, 0, 0.4, 0]).view(2, 3))

        self.register_buffer('corners', torch.FloatTensor([-2, 2, 2, 2, 2, -2, -2, -2]).view(4, 2))

        # One sigma per glimpse
        self.sigmas = Parameter(torch.randn( (glimpses, ) ))

        # All values 1, no bias. Glimpses extract only pixel information.
        self.register_buffer('one', torch.FloatTensor([1.0]))
Пример #6
0
    def verify_decrypt_batched(self, ciphertext_batch,
                               partial_decryption_batch, proof):
        """Batched version of `verify_decrypt()`

        Arguments:
            ciphertext_batch (list): the ciphertexts (PaillierCiphertext) to be
                decrypted in a verifiable manner
            partial_decryption_batch (list): the corresponding partial
                decryptions (int)
            proof: a proof that each element of `partial_decryption_batch` is
                indeed a partial decryption of the corresponding element in
                `ciphertext_batch` under the secret key corresponding to self
        """
        pk = self.public_key

        # run protocol in the Fiat-Shamir heuristic
        t1, t2, w = proof

        # generate random λ_i *after* decryption shares have been provided
        lambda_batch = [
            util.H([ciphertext.raw_value, partial_decryption])
            for ciphertext, partial_decryption in zip(
                ciphertext_batch, partial_decryption_batch)
        ]

        # compute combined plaintext and ciphertext for verification
        combined_plaintext = util.prod(
            util.powmod(plaintext, lambda_, pk.n) for plaintext, lambda_ in
            zip(partial_decryption_batch, lambda_batch))
        combined_ciphertext = util.prod(
            util.powmod(ciphertext.raw_value, lambda_, pk.n)
            for ciphertext, lambda_ in zip(ciphertext_batch, lambda_batch))
        h = util.H([
            combined_ciphertext,
            combined_plaintext,
            t1,
            self.verification_base,
            self.verification,
            t2,
        ])

        # verify proof
        # check that v^s = t_1 * v_i^c
        if util.powmod(self.verification_base, w, pk.n) != \
                t1 * util.powmod(self.verification, h, pk.n) % pk.n:
            raise InvalidProof
        # check that (x^2)^s = t_2 * (m^2)^c
        if util.powmod(combined_ciphertext, _CP*_QR*w, pk.n) != \
                t2 * util.powmod(combined_plaintext, _CP*h, pk.n) % pk.n:
            raise InvalidProof
Пример #7
0
    def _adjoint(self, input):
        outputs = []
        for linop in self.linops:
            i = tf.reshape(
                input[:util.prod(linop.oshape)], linop.oshape)
            with tf.device(linop.device):
                if self.parallel:
                    outputs.append(tf.reshape(linop._adjoint(i), [-1]))
                else:
                    with tf.control_dependencies(outputs):
                        outputs.append(tf.reshape(linop._adjoint(i), [-1]))
            input = input[util.prod(linop.oshape):]

        return tf.concat(outputs, 0)
Пример #8
0
 def init(self):
     for mode in ['valid', 'full', 'same']:
         for dtype in util.TYPES.FLOAT:
             for cmd1, shape1 in util.gen_random_arrays("R", 3, min_ndim=1, max_dim=10, samples_in_each_ndim=1,
                                                        dtype=dtype, no_views=True):
                 if util.prod(shape1) <= 0:
                     continue
                 # Notice, the second shape must have the same number of dimension as the first one
                 # and the max dimension cannot be larger than in the first shape
                 # Finally, dimensions above 5 simple take too long
                 max_dim = min(min(shape1), 5)
                 for cmd2, shape2 in util.gen_random_arrays("R", len(shape1), min_ndim=len(shape1), max_dim=max_dim,
                                                            samples_in_each_ndim=1, dtype=dtype, no_views=True):
                     if util.prod(shape2) > 0:
                         cmd = "R = bh.random.RandomState(42); a=%s; v=%s;" % (cmd1, cmd2)
                         yield (cmd, mode)
Пример #9
0
def sample_indices(batchsize, k, num, rng, use_cuda=False):
    """
    Sample 'num' integer indices within the dimensions indicated by the tuple, resulting in a LongTensor of size
        batchsize x k x num x len(rng).
    Ensures that for fixed values of the first and second dimension, no tuple is sample twice.

    :return:
    """

    total = util.prod(rng)
    num_means = batchsize * k

    # First, we'll sample some flat indices, and then compute their corresponding index-tuples
    flat_indices = LongTensor(num_means, num)
    for row in range(num_means):
        lst = random.sample(range(total), num)
        flat_indices[row, :] = LongTensor(lst)

    flat_indices = flat_indices.view(-1)
    full_indices = tup(flat_indices, rng, use_cuda)

    # Reshape
    full_indices = full_indices.unsqueeze(0).unsqueeze(0).view(
        batchsize, k, num, len(rng))

    if use_cuda:
        full_indices = full_indices.cuda()

    return full_indices
Пример #10
0
    def __init__(self, linops, parallel=False):
        oshape = []
        ishape = []

        ishape = [sum([util.prod(linop.ishape)
                       for linop in linops])]
        oshape = [sum([util.prod(linop.oshape)
                       for linop in linops])]

        dtype = linops[0].dtype
        for linop in linops:
            assert(linop.dtype == dtype)

        super().__init__(oshape, ishape, dtype)
        self.linops = linops
        self.parallel = parallel
Пример #11
0
def enum_qps(ipolys, max_degree, max_qps):
    """
    Enumerates sufficient information to generate (cyclic) sum-of-squares (SOS)
    problems.

    Args:
    - ipolys (dict): output of `enum_ipolys`
    - max_degree: max degree of the _expanded_ polynomial
    - max_qps: max number of (q, ps) pairs to generate

    Returns: list of (q, ps) pairs, where:
    - `util.csum(xs, q**2 * prod(ps))` is the input to an SOS problem
    - a representation of `q**2 * prod(ps)` is a sufficient "witness" to solve
    """
    n_qps = 0
    for q_degree, p_degrees in enum_qp_degrees(max_degree):
        for q in ipolys[(q_degree, False)]:
            seen_p = set()
            for ps in itertools.product(
                    *[ipolys[(p_degree, True)] for p_degree in p_degrees]):
                if len(set(ps)) < len(ps): continue
                p = util.prod(
                    ps)  # we use this as a convenient way to sort factors
                if p not in seen_p:
                    yield q, list(ps)
                    seen_p.add(p)
                    n_qps += 1
                    if max_qps is not None and n_qps >= max_qps: return None
Пример #12
0
def day16b(lines):
    rules, my_ticket, nearby_tickets = day16a.parse(lines)
    candidates = find_candidates(my_ticket, nearby_tickets, rules)
    unique = next(unique_product(candidates))
    return util.prod(
        my_ticket[i] for i, field in enumerate(unique) if field.startswith("departure")
    )
Пример #13
0
    def _forward(self, input):

        outputs = []

        for linop in self.linops:

            i = input[:util.prod(linop.ishape)]
            with tf.device(linop.device):
                if self.parallel:
                    outputs.append(linop._forward(tf.reshape(i, linop.ishape)))
                else:
                    with tf.control_dependencies(outputs):
                        outputs.append(linop._forward(
                            tf.reshape(i, linop.ishape)))

            input = input[util.prod(linop.ishape):]

        return sum(outputs)
Пример #14
0
    def forward(self, inputs, return_activations=False):
        # inputs specifies one float for each neuron in the first layer
        # if return_activations is true, we return a list of activations at each layer
        #  otherwise, we only return the output layer
        values = numpy.array([inputs], float).T  # values in current layer
        activations = [{'activations': values}]

        for layer in self.layers[1:]:
            if layer['type'] == 'sigmoid':
                # compute the weighted sum of neuron inputs, plus bias term (for each neuron in current layer)
                z_vector = numpy.dot(layer['weights'], values) + layer['bias']

                # apply sigmoid activation function
                values = util.sigmoid(z_vector)
                if return_activations:
                    activations.append({'activations': values[:, 0]})
            elif layer['type'] == 'softmax':
                # compute the weighted sum of neuron inputs, plus bias term (for each neuron in current layer)
                z_vector = numpy.dot(layer['weights'], values) + layer['bias']

                # apply softmax
                values = numpy.exp(z_vector - numpy.max(z_vector))
                values = values / numpy.sum(values)
                if return_activations:
                    activations.append({'activations': values[:, 0]})
            elif layer['type'] == 'convsample':
                # carry out convolution to get convolved values
                convolved_out = numpy.zeros(
                    (layer['k'], layer['conv_count'], layer['conv_count']))
                for k in xrange(len(layer['weights'])):
                    convolved_out[k] = scipy.signal.convolve2d(
                        values.reshape(layer['m'], layer['m']),
                        numpy.rot90(layer['weights'][k], 2), 'valid')
                    convolved_out[k] = util.sigmoid(convolved_out[k] +
                                                    layer['bias'][k])

                # pool the convolved features
                pooled = numpy.zeros((layer['k'], layer['o'], layer['o']))
                for k in xrange(layer['k']):
                    for i in xrange(layer['o']):
                        for j in xrange(layer['o']):
                            pooled[k][i][j] = numpy.average(
                                convolved_out[k, (i * layer['p']):((i + 1) *
                                                                   layer['p']),
                                              (j * layer['p']):((j + 1) *
                                                                layer['p'])])
                values = pooled.reshape(util.prod(pooled.shape), 1)
                if return_activations:
                    activations.append({
                        'activations': values[:, 0],
                        'extra': convolved_out
                    })

        if return_activations: return activations
        else: return values[:, 0]
Пример #15
0
def qs(n, b, I):
    prod_mod = make_prod_mod(n)
    xs, ys, fb = find_smooth(n, b, I)
    if len(xs) <= len(fb):
        raise Exception(f'''\
With size {len(fb)} factor base found {len(xs)} (x,y) smooth pairs;
at least {len(fb)+1} are required to guarantee linear dependence.
Try increasing the factor base or the sieving interval.
''')
    for indices in combinations(range(len(xs)), len(fb) + 1):
        xs_ = [xs[i] for i in indices]
        ys_ = [ys[i] for i in indices]
        emat, bmat = construct_exponent_mats(ys_, fb)
        # The augmented binary matrix is for solving for the left nullspace,
        # and thus all the subsets of linearly dependent exponent vector indices.
        for indices in ld_index_iter(bmat):
            x = prod(xs_[i] for i in indices)
            l = [sum(emat[i][j] for i in indices) // 2 for j in range(len(fb))]
            y = prod(p**e for (p, e) in zip(fb, l))
            if not x % n in [y % n, -y % n]:
                return (gcd(x + y, n), gcd(x - y, n))
Пример #16
0
    def verify_private_multiply_batched(self, cx, cy_list, cz_list, proof):
        """Check the proof of multiplication of a ciphertext with a plaintext

        Arguments:
            cx (PaillierCiphertext): the encrypted left operand
            cy_list (list): the encrypted right operands (PaillierCiphertext)
            cz_list (list): the encrypted products (PaillierCiphertext) of x*y
                for each y in `cy_list`
            proof (int): a proof that z = x*y for each y, z in cy_list, cz_list
        """
        n2 = self.nsquare

        # generate random λ_i *after* ciphertexts have been provided
        cu, cyu, w, rs, rys = proof
        lambda_list = [
            util.H([cx, cy, cz]) for cy, cz in zip(cy_list, cz_list)
        ]

        # compute combined ciphertexts
        cy = util.prod(
            util.powmod(cy, lambda_, self.nsquare)
            for cy, lambda_ in zip(cy_list, lambda_list))
        cz = util.prod(
            util.powmod(cz, lambda_, self.nsquare)
            for cz, lambda_ in zip(cz_list, lambda_list))

        # run private multiply protocol in the Fiat-Shamir heuristic
        h = util.H([cx, cy, cz, cu, cyu])

        # verify proofs
        cs, _ = self.raw_multiply(self.g, w, rs)  # ⟦s⟧ = ⟦u + xe⟧
        cys, _ = self.raw_multiply(cy, w, rys)  # ⟦ys⟧ = ⟦y(u + xe)⟧
        # ⟦u⟧ * ⟦x⟧**e = ⟦u + xe⟧ = ⟦s⟧
        if cs != cu * util.powmod(cx, h, n2) % n2:
            raise InvalidProof
        # ⟦yu⟧ * ⟦z⟧**e = ⟦yu + yxe⟧ = ⟦ys⟧
        if cys != cyu * util.powmod(cz, h, n2) % n2:
            raise InvalidProof
    def __init__(self, layers, embed_size, traj_len, action_space):
        super().__init__()
        self.traj_len = traj_len
        self.action_space = action_space
        self.embed_dim = embed_size
        self.max_embedding = None

        traj_size = util.prod(self.action_space.shape) * self.traj_len

        self.layers = nn.ModuleList([
            nn.Linear(embed_size, 400),
            *[nn.Linear(400, 400) for _ in range(layers)]
        ])
        self.out_layer = nn.Linear(400, traj_size)
Пример #18
0
def part_2(data):
    header, owned_ticket, context = parse(data)

    valid_tickets = [
        ticket for ticket in [owned_ticket, *context]
        if is_valid_ticket(header, ticket)
    ]

    ticket_data = dict(zip(field_order(header, valid_tickets), owned_ticket))

    return prod([
        value for field, value in ticket_data.items()
        if field.startswith("departure")
    ])
Пример #19
0
    def __init__(self,
                 out_size,
                 channels,
                 zchannels,
                 zs=256,
                 k=3,
                 mapping=3,
                 batch_norm=False,
                 dropouts=None):
        super().__init__()

        self.out_size = out_size

        c, h, w = self.out_size
        self.channels = channels
        c1, c2, c3, c4, c5 = self.channels
        z0, z1, z2, z3, z4, z5 = zchannels

        # resnet blocks
        self.block5 = util.Block(c5, c4, kernel_size=k, batch_norm=batch_norm)
        self.block4 = util.Block(c4, c3, kernel_size=k, batch_norm=batch_norm)
        self.block3 = util.Block(c3, c2, kernel_size=k, batch_norm=batch_norm)
        self.block2 = util.Block(c2, c1, kernel_size=k, batch_norm=batch_norm)
        self.block1 = util.Block(c1, c, kernel_size=k, batch_norm=batch_norm)

        # affine mappings from latent space sample
        self.affine5 = nn.Linear(zs, 2 * util.prod((c5, h // 32, w // 32)))
        self.affine4 = nn.Linear(zs, 2 * util.prod((c4, h // 16, w // 16)))
        self.affine3 = nn.Linear(zs, 2 * util.prod((c3, h // 8, w // 8)))
        self.affine2 = nn.Linear(zs, 2 * util.prod((c2, h // 4, w // 4)))
        self.affine1 = nn.Linear(zs, 2 * util.prod((c1, h // 2, w // 2)))
        self.affine0 = nn.Linear(zs, 2 * util.prod(out_size))

        # 1x1 convolution from "noise space" sample
        self.tonoise5 = nn.Conv2d(z5, c5, kernel_size=1, padding=0)
        self.tonoise4 = nn.Conv2d(z4, c4, kernel_size=1, padding=0)
        self.tonoise3 = nn.Conv2d(z3, c3, kernel_size=1, padding=0)
        self.tonoise2 = nn.Conv2d(z2, c2, kernel_size=1, padding=0)
        self.tonoise1 = nn.Conv2d(z1, c1, kernel_size=1, padding=0)
        self.tonoise0 = nn.Conv2d(z0, c, kernel_size=1, padding=0)

        self.conv0 = nn.Conv2d(c, c, kernel_size=1)

        m = []
        for _ in range(mapping):
            m.append(nn.Linear(zs, zs))
            m.append(nn.ReLU())
        self.mapping = nn.Sequential(*m)

        self.dropouts = dropouts

        # constant, learnable input
        self.x5 = nn.Parameter(torch.randn(1, c5, h // 32, w // 32))
        self.x4 = nn.Parameter(torch.randn(1, c4, h // 16, w // 16))
        self.x3 = nn.Parameter(torch.randn(1, c3, h // 8, w // 8))
        self.x2 = nn.Parameter(torch.randn(1, c2, h // 4, w // 4))
        self.x1 = nn.Parameter(torch.randn(1, c1, h // 2, w // 2))
Пример #20
0
    def __init__(self,
                 in_shape,
                 out_shape,
                 k,
                 additional=0,
                 poolsize=4,
                 subsample=None):
        super().__init__(in_rank=len(in_shape),
                         out_shape=out_shape,
                         additional=additional,
                         bias_type=Bias.DENSE,
                         subsample=subsample)

        self.k = k
        self.in_shape = in_shape
        self.out_shape = out_shape

        rep = 4 * 4 * 4 * 2

        self.w_rank = len(in_shape) + len(out_shape)

        c, x, y = in_shape
        flat_size = int(x / poolsize) * int(y / poolsize) * c

        # hypernetwork
        self.tohidden = nn.Sequential(
            nn.MaxPool2d(kernel_size=poolsize, stride=poolsize), Flatten(),
            nn.Linear(flat_size, int(k / rep)), nn.ReLU())

        self.conv1da = nn.ConvTranspose1d(in_channels=1,
                                          out_channels=1,
                                          kernel_size=4,
                                          stride=4)
        self.conv1db = nn.ConvTranspose1d(in_channels=1,
                                          out_channels=1,
                                          kernel_size=4,
                                          stride=4)
        self.conv1dc = nn.ConvTranspose1d(in_channels=1,
                                          out_channels=1,
                                          kernel_size=4,
                                          stride=4)

        self.conv2d = nn.ConvTranspose2d(in_channels=1,
                                         out_channels=1,
                                         kernel_size=(2, self.w_rank + 2),
                                         stride=2)

        self.bias = nn.Sequential(
            nn.Linear(int(k / rep), util.prod(out_shape)), )
Пример #21
0
def get_coefficient_term(R, f):
    """
    
    """
    terms = f.terms(R.order)
    syms = R.symbols
    LM, _ = terms[0]

    # get the first non-zero index - this is the "leading variable".
    leading_idx = first(nonzeros(LM))
    # Then, scan the rest of the terms for any thing other than this,
    # and add it to the polynomial.
    u = 0
    for monom, coeff in terms:
        if monom[leading_idx] != LM[leading_idx]: continue
        monom = monom[:leading_idx] + (0,) + monom[leading_idx+1:]
        u += coeff * prod(syms[var]**exp for (var, exp) in zip(xrange(len(monom)), monom))
    return R(u)
Пример #22
0
def get_coefficient_term(R, f):
    """
    
    """
    terms = f.terms(R.order)
    syms = R.symbols
    LM, _ = terms[0]

    # get the first non-zero index - this is the "leading variable".
    leading_idx = first(nonzeros(LM))
    # Then, scan the rest of the terms for any thing other than this,
    # and add it to the polynomial.
    u = 0
    for monom, coeff in terms:
        if monom[leading_idx] != LM[leading_idx]: continue
        monom = monom[:leading_idx] + (0, ) + monom[leading_idx + 1:]
        u += coeff * prod(syms[var]**exp
                          for (var, exp) in zip(xrange(len(monom)), monom))
    return R(u)
Пример #23
0
    def __init__(self, out_size, channels, zs=256, k=3, dist='gaussian', mapping=3, batch_norm=False, dropouts=None):
        super().__init__()

        self.out_size = out_size

        c, h, w = self.out_size
        self.channels = channels
        c1, c2, c3, c4, c5 = self.channels

        # resnet blocks
        self.block5 = util.Block(c5, c4, kernel_size=k, deconv=True, batch_norm=batch_norm)
        self.block4 = util.Block(c4, c3, kernel_size=k, deconv=True, batch_norm=batch_norm)
        self.block3 = util.Block(c3, c2, kernel_size=k, deconv=True, batch_norm=batch_norm)
        self.block2 = util.Block(c2, c1, kernel_size=k, deconv=True, batch_norm=batch_norm)
        self.block1 = util.Block(c1, c,  kernel_size=k, deconv=True, batch_norm=batch_norm)

        # affine mappings from latent space sample
        self.affine5 = nn.Linear(zs, 2 * util.prod((c5, h//32, w//32)))
        self.affine4 = nn.Linear(zs, 2 * util.prod((c4, h//16, w//16)))
        self.affine3 = nn.Linear(zs, 2 * util.prod((c3, h//8, w//8)))
        self.affine2 = nn.Linear(zs, 2 * util.prod((c2, h//4, w//4)))
        self.affine1 = nn.Linear(zs, 2 * util.prod((c1, h//2, w//2)))
        self.affine0 = nn.Linear(zs, 2 * util.prod(out_size))

        # 1x1 convolution from "noise space" sample
        self.tonoise5 = nn.Conv2d(1, c5, kernel_size=1, padding=0)
        self.tonoise4 = nn.Conv2d(1, c4, kernel_size=1, padding=0)
        self.tonoise3 = nn.Conv2d(1, c3, kernel_size=1, padding=0)
        self.tonoise2 = nn.Conv2d(1, c2, kernel_size=1, padding=0)
        self.tonoise1 = nn.Conv2d(1, c1, kernel_size=1, padding=0)
        self.tonoise0 = nn.Conv2d(1, c,  kernel_size=1, padding=0)

        # mapping to distribution on image space
        if dist in ['gaussian','beta']:
            self.conv0 = nn.Conv2d(c, c*2, kernel_size=1)
        elif dist == 'bernoulli': # binary xent loss
            self.conv0 = nn.Conv2d(c, c, kernel_size=1)
        else:
            raise Exception('Output distribution {} not recognized'.format(dist))

        m = []
        for _ in range(mapping):
            m.append(nn.Linear(zs, zs))
            m.append(nn.ReLU())
        self.mapping = nn.Sequential(*m)

        self.dropouts = dropouts

        # constant, learnable input
        self.x5 = nn.Parameter(torch.randn(c5, h//32, w//32))
Пример #24
0
def diag_gather(x, grouping):
    """Extract diagonal subtensors from `x`.

  This is a generalization of `tf.linalg.diag_part` to multiple dimensions and
  multiple diagonals.

  `grouping` is a list of lists, each of which contains axes whose diagonal
  should be taken. Order of groups determines order of axes in the output.
  Groups may contain any nonzero number of axes for simultaneous
  diagonalization across multiple axes, and axes within groups need not be
  contiguous. The grouping must form a partition: all axes of `x` must appear
  exactly once. A `grouping` may be obtained from an einsum signature through
  `Signature.as_grouping`, e.g. `Signature("ii->i").as_grouping()`.

  Examples:
    `diag_gather(x, [[0, 1]]) <=> einsum("ii->i", x)`
    `diag_gather(x, [[0], [1, 2]]) <=> einsum("bii->bi", x)`
  """
    x_shape = get_shape(x)

    if not all(grouping):
        # TODO support this if a use case appears
        raise ValueError("empty groups are not allowed", grouping)
    shapes = [[x_shape[axis] for axis in group] for group in grouping]
    if sorted(axis for group in grouping
              for axis in group) != list(range(get_ndim(x))):
        # TODO consider supporting Ellipsis if it can be made predictable
        raise ValueError("grouping must reference all axes of x exactly once",
                         grouping)

    # transpose the argument so that the tied axes are adjacent
    z = tf.transpose(x, [axis for axes in grouping for axis in axes])
    # flatten the tied axes and use strided slicing to select the diagonal elements
    z = tf.reshape(
        z, [util.prod([x_shape[axis] for axis in axes]) for axes in grouping])
    strides = [
        tf.reduce_sum(
            tf.cumprod([x_shape[axis] for axis in axes], exclusive=True))
        for axes in grouping
    ]
    y = z[tuple(slice(None, None, stride) for stride in strides)]
    return y
Пример #25
0
	def forward(self, inputs, return_activations = False):
		# inputs specifies one float for each neuron in the first layer
		# if return_activations is true, we return a list of activations at each layer
		#  otherwise, we only return the output layer
		values = numpy.array([inputs], float).T # values in current layer
		activations = [{'activations': values}]

		for layer in self.layers[1:]:
			if layer['type'] == 'sigmoid':
				# compute the weighted sum of neuron inputs, plus bias term (for each neuron in current layer)
				z_vector = numpy.dot(layer['weights'], values) + layer['bias']

				# apply sigmoid activation function
				values = util.sigmoid(z_vector)
				if return_activations: activations.append({'activations': values[:, 0]})
			elif layer['type'] == 'softmax':
				# compute the weighted sum of neuron inputs, plus bias term (for each neuron in current layer)
				z_vector = numpy.dot(layer['weights'], values) + layer['bias']

				# apply softmax
				values = numpy.exp(z_vector - numpy.max(z_vector))
				values = values / numpy.sum(values)
				if return_activations: activations.append({'activations': values[:, 0]})
			elif layer['type'] == 'convsample':
				# carry out convolution to get convolved values
				convolved_out = numpy.zeros((layer['k'], layer['conv_count'], layer['conv_count']))
				for k in xrange(len(layer['weights'])):
					convolved_out[k] = scipy.signal.convolve2d(values.reshape(layer['m'], layer['m']), numpy.rot90(layer['weights'][k], 2), 'valid')
					convolved_out[k] = util.sigmoid(convolved_out[k] + layer['bias'][k])

				# pool the convolved features
				pooled = numpy.zeros((layer['k'], layer['o'], layer['o']))
				for k in xrange(layer['k']):
					for i in xrange(layer['o']):
						for j in xrange(layer['o']):
							pooled[k][i][j] = numpy.average(convolved_out[k, (i * layer['p']):((i + 1) * layer['p']), (j * layer['p']):((j + 1) * layer['p'])])
				values = pooled.reshape(util.prod(pooled.shape), 1)
				if return_activations: activations.append({'activations': values[:, 0], 'extra': convolved_out})

		if return_activations: return activations
		else: return values[:, 0]
Пример #26
0
def tup(index, shape, use_cuda=False):
    """
    Returns the tuple indicated by a given single integer (reverse of fi(...)).
    :param indices:
    :param shape:
    :param use_cuda:
    :return:
    """

    num, = index.size()

    result = torch.cuda.LongTensor(
        num, len(shape)) if use_cuda else LongTensor(num, len(shape))

    for dim in range(len(shape) - 1):
        per_inc = util.prod(shape[dim + 1:])
        result[:, dim] = index / per_inc
        index = index % per_inc
    result[:, -1] = index

    return result
Пример #27
0
    def forward(self, batch):

        scores = 0

        assert batch.size(-1) == 3

        n, r = self.n, self.r

        dims = batch.size()[:-1]
        batch = batch.reshape(-1, 3)

        for forward in [True, False] if self.reciprocal else [True]:

            nodes = self.entities
            relations = self.relations if forward else self.relations_backward

            if self.edo is not None:
                nodes = self.edo(nodes)
            if self.rdo is not None:
                relations = self.rdo(relations)

            if self.biases:
                if forward:
                    biases = (self.gbias, self.sbias, self.pbias, self.obias)
                else:
                    biases = (self.gbias, self.sbias, self.pbias_bw,
                              self.obias)
            else:
                biases = None

            scores = scores + self.decoder(
                batch, nodes, relations, biases=biases, forward=forward)

            assert scores.size() == (util.prod(dims), )

        if self.reciprocal:
            scores = scores / 2

        return scores.view(*dims)
Пример #28
0
    def __init__(self,
                 in_size,
                 channels,
                 zchannels,
                 zs=256,
                 k=3,
                 unmapping=3,
                 batch_norm=False):
        super().__init__()

        c, h, w = in_size
        c1, c2, c3, c4, c5 = channels
        z0, z1, z2, z3, z4, z5 = zchannels

        # resnet blocks
        self.block1 = util.Block(c, c1, kernel_size=k, batch_norm=batch_norm)
        self.block2 = util.Block(c1, c2, kernel_size=k, batch_norm=batch_norm)
        self.block3 = util.Block(c2, c3, kernel_size=k, batch_norm=batch_norm)
        self.block4 = util.Block(c3, c4, kernel_size=k, batch_norm=batch_norm)
        self.block5 = util.Block(c4, c5, kernel_size=k, batch_norm=batch_norm)

        # affine mappings to distribution on latent space
        self.affine0 = nn.Linear(util.prod(in_size), 2 * zs)
        self.affine1 = nn.Linear(util.prod((c1, h // 2, w // 2)), 2 * zs)
        self.affine2 = nn.Linear(util.prod((c2, h // 4, w // 4)), 2 * zs)
        self.affine3 = nn.Linear(util.prod((c3, h // 8, w // 8)), 2 * zs)
        self.affine4 = nn.Linear(util.prod((c4, h // 16, w // 16)), 2 * zs)
        self.affine5 = nn.Linear(util.prod((c5, h // 32, w // 32)), 2 * zs)

        self.affinez = nn.Linear(12 * zs, 2 * zs)

        # 1x1 convolution to distribution on "noise space"
        # (mean and sigma)
        self.tonoise0 = nn.Conv2d(c, z0 * 2, kernel_size=1, padding=0)
        self.tonoise1 = nn.Conv2d(c1, z1 * 2, kernel_size=1, padding=0)
        self.tonoise2 = nn.Conv2d(c2, z2 * 2, kernel_size=1, padding=0)
        self.tonoise3 = nn.Conv2d(c3, z3 * 2, kernel_size=1, padding=0)
        self.tonoise4 = nn.Conv2d(c4, z4 * 2, kernel_size=1, padding=0)
        self.tonoise5 = nn.Conv2d(c5, z5 * 2, kernel_size=1, padding=0)

        um = []
        for _ in range(unmapping):
            um.append(nn.ReLU())
            um.append(nn.Linear(zs * 2, zs * 2))
        self.unmapping = nn.Sequential(*um)
Пример #29
0
    def assemble_decryption_shares(ciphertext,
                                   shares,
                                   decryption_shares,
                                   relative=True):
        """Assemble decryptions share in a complete decryption

        Arguments:
            shares (list): the public key shares (PaillierPublicKeyShare)
                corresponding to the secret key shares used to decrypt the
                ciphertext
            decryption_shares (list): the decryption shares (int) resulting
                from the (partial) decryptions by the secret key shares
            relative (bool): whether the result should be interpreted as a
                relative integer (i.e. in [-n/2, n/2] rather than in [0, n])

        Returns:
            int: the message represented in the ciphertext

            If no transformation other than (re)randomization has been
            performed on the ciphertext, then the original message should be
            returned. If homomorphic operations have been performed, then the
            result of these operations on the original messages should be
            returned.

            If relative is set to `True`, then the returned value is a relative
            integer between `-n/2` and `n/2`. Otherwise, it is a non-negative
            integer lower than `n`.
        """
        pk = shares[0].public_key
        C = ciphertext.raw_value
        R = util.prod(decryption_shares, pk.n)
        plaintext = (
            (C * util.powmod(R, -pk.n, pk.nsquare) % pk.nsquare) - 1) // pk.n
        # TODO:  _QR
        if relative and plaintext >= pk.n // 2:
            plaintext -= pk.n
        return plaintext
Пример #30
0
    def forward(self, batch):

        assert batch.size(-1) == 3

        n, r = self.n, self.r

        dims = batch.size()[:-1]
        batch = batch.reshape(-1, 3)

        nodes = self.tohidden(self.embeddings)
        nodes = self.layers(nodes)  # -- message passing

        embeddings = self.embeddings + self.frhidden(nodes)

        if self.biases:
            biases = (self.gbias, self.sbias, self.pbias, self.obias)
        else:
            biases = None

        scores = self.decoder(batch, embeddings, self.relations, biases=biases)

        assert scores.size() == (util.prod(dims), )

        return scores.view(*dims)
Пример #31
0
                                       qpos_only=False,
                                       qpos_qvel=False,
                                       delta=False,
                                       whiten=False,
                                       pixels=True)

train_loader = torch.utils.data.DataLoader(dataset,
                                           batch_size=args.batch_size,
                                           shuffle=True,
                                           num_workers=0,
                                           pin_memory=True)

epoch_size = 10000
train_iterator = iter(train_loader)

input_nelement = util.prod(dataset.get_obs().shape)

model = RegularVAE(args.state_embed_size).to(device)
optimizer = optim.Adam(model.parameters(), lr=args.lr, amsgrad=True)


# Reconstruction + KL divergence losses summed over all elements and batch
def loss_function(recon_x, x, state_mu, state_logvar):
    # BCE tends to work better on images
    likelihood = F.binary_cross_entropy(recon_x, x, size_average=False)

    # see Appendix B from VAE paper: https://arxiv.org/abs/1312.6114
    # 0.5 * sum(1 + log(sigma^2) - mu^2 - sigma^2)
    state_KLD = -0.5 * torch.sum(1 + state_logvar - state_mu.pow(2) -
                                 state_logvar.exp())
Пример #32
0
	def backward(self, inputs, desired_outputs):
		# inputs is a list of input layer values, desired_outputs is expected output layer values
		inputs = numpy.array([inputs], float).T
		desired_outputs = numpy.array([desired_outputs], float).T

		# execute forward propogation and store activations of each layer
		activations = self.forward(inputs[:, 0], True)

		# compute deltas at each layer
		deltas_list = [None] * self.nlayers # deltas_list[0] is for input layer and remains None

		if self.layers[self.nlayers - 1]['type'] == 'sigmoid':
			cost = numpy.sum(abs(activations[self.nlayers - 1]['activations'] - desired_outputs[:, 0]))
			deltas_list[self.nlayers - 1] = numpy.multiply(activations[self.nlayers - 1]['activations'] - desired_outputs[:, 0], util.sigmoid_d2(activations[self.nlayers - 1]['activations']))
		elif self.layers[self.nlayers - 1]['type'] == 'softmax':
			cost = -numpy.sum(numpy.multiply(desired_outputs[:, 0], numpy.log(activations[self.nlayers - 1]['activations'])))
			deltas_list[self.nlayers - 1] = activations[self.nlayers - 1]['activations'] - desired_outputs[:, 0]
		else:
			raise Exception('invalid error function type')

		for l in xrange(self.nlayers - 2, 0, -1): # for each non-input layer
			previous_deltas = deltas_list[l + 1]
			target_layer = self.layers[l + 1]

			if target_layer['type'] == 'sigmoid' or target_layer['type'] == 'softmax':
				sums = numpy.dot(target_layer['weights'].T, previous_deltas.reshape(util.prod(previous_deltas.shape), 1))
				deltas_list[l] = numpy.multiply(sums.flatten(), util.sigmoid_d2(activations[l]['activations']))
			elif target_layer['type'] == 'convsample':
				#deltas_list[l] = numpy.zeros((target_layer['k'], activations[l].shape[0], activations[l].shape[1]))
				#previous_deltas = previous_deltas.reshape(target_layer['k'], target_layer['p'], target_layer['p'])

				#for k in xrange(target_layer['k']):
				#	pooled_size = target_layer['m'] / target_layer['p']
				#	sums = (1. / pooled_size / pooled_size) * ponumpy.kron(previous_deltas[k], numpy.ones((pooled_size, pooled_size))) # upsample over mean pooling
				#	deltas_list[l][k, :, :] = numpy.multiply(sums, util.sigmoid_d2(activations[l][k].flatten()))
				#	deltas_list[l] = None
				raise Exception('convsample bp')

		# compute squared-error partial derivatives with respect to weight and bias parameters
		# we do this in a list indexed by layer, and then in an array
		weight_derivatives = []
		bias_derivatives = []
		for l in xrange(self.nlayers - 1): # loop over each weights
			previous_deltas = deltas_list[l + 1]
			target_layer = self.layers[l + 1]

			if target_layer['type'] == 'sigmoid' or target_layer['type'] == 'softmax':
				weight_derivatives.append(numpy.dot(previous_deltas.reshape(len(previous_deltas), 1), activations[l]['activations'].reshape(1, len(activations[l]['activations']))))
				bias_derivatives.append(previous_deltas.reshape(len(deltas_list[l + 1]), 1))
			elif target_layer['type'] == 'convsample':
				previous_deltas = previous_deltas.reshape(target_layer['k'], target_layer['o'], target_layer['o'])
				delta_bp_pool = numpy.zeros((target_layer['k'], target_layer['conv_count'], target_layer['conv_count'])) # back-propogate error across pooling layer
				for k in xrange(target_layer['k']):
					delta_bp_pool[k] = numpy.kron(previous_deltas[k], numpy.ones((target_layer['p'], target_layer['p'])) / target_layer['p'] / target_layer['p'])
				delta_bp_conv = numpy.multiply(delta_bp_pool, util.sigmoid_d2(activations[l + 1]['extra']))

				# bias derivative from delta_bp_pool sum per filter
				bias_d = numpy.zeros(target_layer['k'])
				for k in xrange(target_layer['k']):
					bias_d[k] = numpy.sum(delta_bp_pool[k])
				bias_derivatives.append(bias_d)

				# weight derivative
				weight_d = numpy.zeros((target_layer['k'], target_layer['n'], target_layer['n']))
				for k in xrange(target_layer['k']):
					weight_d[k] = scipy.signal.convolve2d(activations[l]['activations'].reshape(target_layer['m'], target_layer['m']), numpy.rot90(delta_bp_conv[k], 2), 'valid')
				weight_derivatives.append(weight_d)

		return (weight_derivatives, bias_derivatives, cost)
Пример #33
0
from util import prod

input = open("11 - grid.txt")
grid = []

for line in input:
    grid.append([int(x) for x in line.split(' ')])

x = []
for i in range(len(grid)):
    for j in range(len(grid[i])):
        if i <= len(grid)-4:
            x.append(prod([grid[i+k][j] for k in range(0,4)]))
            if j <= len(grid[i])-4:
                x.append(prod([grid[i+k][j+k] for k in range(0,4)]))
            if j >= 4:
                x.append(prod([grid[i+k][j-k] for k in range(0,4)]))
        if j <= len(grid[i])-4:
            x.append(prod([grid[i][j+k] for k in range(0,4)]))

print max(x) # 70600674
from util import prod

num = """73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450""".replace("\n", "")

x = []
for i in range(len(num)-13):
    x.append(prod(int(x) for x in num[i:i+13]))

print max(x)
Пример #35
0
def _df_wb_proc_to_page(
    df_wb_proc,
    grid_shape,
    precedence,
    years,
    figsize=(20, 20),
    debug=False,
    ylabel=None,
    label_space=10,
    legend=False,
):
    """
    rule:
      group and sort by country, then knows status (1 page per value)
        (order doesn't matter)
      for each country and knows status, group and sort by domain,
      then covariate
        Men first, [agegrp, marital_status, bmigrp, pregnancy]
      for each page, domain, and covariate (i.e. for each row)
        insert any missing years as blanks then sort by year

    """
    assert len(df_wb_proc.groupby(["country", "knows_status"])) == 1
    fig, axes = plt.subplots(*grid_shape, figsize=figsize)
    axes = axes.reshape((1, prod(grid_shape)))
    df_wb_proc_idx = df_wb_proc.set_index(INDEX)

    grid_order = _get_supplemented_grid_order(df_wb_proc_idx.index, INDEX,
                                              years, precedence)
    axis_list = axes[0]
    s_done_axis_keys = df_wb_proc_idx.groupby(INDEX).apply(
        lambda df: pp_grid(df,
                           fig,
                           axis_list,
                           DS,
                           DS_COLORS,
                           INDEX,
                           grid_order,
                           debug=debug,
                           ylabel=ylabel,
                           label_space=label_space))
    done_axis_keys = set(s_done_axis_keys.unique())

    for i in range(len(axis_list)):
        if i not in done_axis_keys:
            fig.delaxes(axis_list[i])

    if legend:
        leg = fig.legend(handles=[
            Patch(facecolor=c, label=_ds) for _ds, c in zip(DS, DS_COLORS)
        ],
                         loc="lower right",
                         fontsize=24,
                         framealpha=1)
        for lh in leg.legendHandles:
            lh.set_alpha(1)

    fig.tight_layout(pad=1)

    page_cols = ["country", "knows_status"]
    country_knows = df_wb_proc_idx.reset_index()[page_cols].loc[0]
    page_metadata = dict(zip(page_cols, country_knows))
    return fig, page_metadata
Пример #36
0
def poly_for_zeros(R, x, Z):
    return prod(x - z for z in Z)
Пример #37
0
    def discretize(self,
                   means,
                   sigmas,
                   values,
                   rng=None,
                   use_cuda=False,
                   relative_range=None):
        """
        Takes the output of a hypernetwork (real-valued indices and corresponding values) and turns it into a list of
        integer indices, by "distributing" the values to the nearest neighboring integer indices.

        NB: the returned ints is not a Variable (just a plain LongTensor). autograd of the real valued indices passes
        through the values alone, not the integer indices used to instantiate the sparse matrix.

        :param ind: A Variable containing a matrix of N by K, where K is the number of indices.
        :param val: A Variable containing a vector of length N containing the values corresponding to the given indices
        :return: a triple (ints, props, vals). ints is an N*2^K by K matrix representing the N*2^K integer index-tuples that can
            be made by flooring or ceiling the indices in 'ind'. 'props' is a vector of length N*2^K, which indicates how
            much of the original value each integer index-tuple receives (based on the distance to the real-valued
            index-tuple). vals is vector of length N*2^K, containing the value of the corresponding real-valued index-tuple
            (ie. vals just repeats each value in the input 'val' 2^K times).
        """

        batchsize, n, rank = means.size()

        # ints is the same size as ind, but for every index-tuple in ind, we add an extra axis containing the 2^rank
        # integerized index-tuples we can make from that one real-valued index-tuple
        # ints = torch.cuda.FloatTensor(batchsize, n, 2 ** rank + additional, rank) if use_cuda else FloatTensor(batchsize, n, 2 ** rank, rank)
        t0 = time.time()

        if BATCH_NEIGHBORS:
            fm = self.floor_mask.unsqueeze(0).unsqueeze(0).expand(
                batchsize, n, 2**rank, rank)

            neighbor_ints = means.data.unsqueeze(2).expand(
                batchsize, n, 2**rank, rank).contiguous()

            neighbor_ints[fm] = neighbor_ints[fm].floor()
            neighbor_ints[~fm] = neighbor_ints[~fm].ceil()

            neighbor_ints = neighbor_ints.long()

        else:
            neighbor_ints = torch.LongTensor(batchsize, n, 2**rank, rank)

            # produce all integer index-tuples that neighbor the means
            for row in range(n):
                for t, bools in enumerate(
                        itertools.product([True, False], repeat=rank)):

                    for col, bool in enumerate(bools):
                        r = means[:, row, col].data
                        neighbor_ints[:, row, t, col] = torch.floor(
                            r) if bool else torch.ceil(r)

        logging.info('  neighbors: {} seconds'.format(time.time() - t0))

        # Sample additional points
        if rng is not None:
            t0 = time.time()
            total = util.prod(rng)

            if PROPER_SAMPLING:

                ints_flat = torch.LongTensor(batchsize, n,
                                             2**rank + self.gadditional)

                # flatten
                neighbor_ints = fi(neighbor_ints.view(-1, rank),
                                   rng,
                                   use_cuda=False)
                neighbor_ints = neighbor_ints.unsqueeze(0).view(
                    batchsize, n, 2**rank)

                for b in range(batchsize):
                    for m in range(n):
                        sample = util.sample(range(total),
                                             self.gadditional + 2**rank,
                                             list(neighbor_ints[b, m, :]))
                        ints_flat[b, m, :] = torch.LongTensor(sample)

                ints = tup(ints_flat.view(-1), rng, use_cuda=False)
                ints = ints.unsqueeze(0).unsqueeze(0).view(
                    batchsize, n, 2**rank + self.gadditional, rank)
                ints_fl = ints.float().cuda() if use_cuda else ints.float()

            else:

                sampled_ints = torch.cuda.FloatTensor(batchsize, n, self.gadditional, rank) if use_cuda \
                    else torch.FloatTensor(batchsize, n, self.gadditional, rank)

                sampled_ints.uniform_()
                sampled_ints *= (1.0 - EPSILON)

                rng = torch.cuda.FloatTensor(
                    rng) if use_cuda else torch.FloatTensor(rng)
                rngxp = rng.unsqueeze(0).unsqueeze(0).unsqueeze(0).expand_as(
                    sampled_ints)

                sampled_ints = torch.floor(sampled_ints * rngxp).long()

                if relative_range is not None:
                    """
                    Sample uniformly from a small range around the given index tuple
                    """
                    rr_ints = torch.cuda.FloatTensor(batchsize, n, self.radditional, rank) if use_cuda \
                        else torch.FloatTensor(batchsize, n, self.radditional, rank)

                    rr_ints.uniform_()
                    rr_ints *= (1.0 - EPSILON)

                    rngxp = rng.unsqueeze(0).unsqueeze(0).unsqueeze(
                        0).expand_as(rr_ints)  # bounds of the tensor
                    rrng = torch.cuda.FloatTensor(relative_range) if use_cuda \
                        else torch.FloatTensor(relative_range) # bounds of the range from which to sample

                    rrng = rrng.unsqueeze(0).unsqueeze(0).unsqueeze(
                        0).expand_as(rr_ints)

                    mns_expand = means.round().unsqueeze(2).expand_as(rr_ints)

                    # upper and lower bounds
                    lower = mns_expand - rrng * 0.5
                    upper = mns_expand + rrng * 0.5

                    # check for any ranges that are out of bounds
                    idxs = lower < 0.0
                    lower[idxs] = 0.0

                    idxs = upper > rngxp
                    lower[idxs] = rngxp[idxs] - rrng[idxs]

                    # print('means', means.round().long())
                    # print('lower', lower)

                    rr_ints = (rr_ints * rrng + lower).long()

                samples = [neighbor_ints, sampled_ints, rr_ints
                           ] if relative_range is not None else [
                               neighbor_ints, sampled_ints
                           ]

                ints = torch.cat(samples, dim=2)

                ints_fl = ints.float()

            logging.info('  sampling: {} seconds'.format(time.time() - t0))

        ints_fl = Variable(
            ints_fl
        )  # leaf node in the comp graph, gradients go through values

        t0 = time.time()
        # compute the proportion of the value each integer index tuple receives
        props = densities(ints_fl, means, sigmas)
        # props is batchsize x K x 2^rank+a, giving a weight to each neighboring or sampled integer-index-tuple

        # -- normalize the proportions of the neigh points and the
        sums = torch.sum(props + EPSILON, dim=2, keepdim=True).expand_as(props)
        props = props / sums

        logging.info('  densities: {} seconds'.format(time.time() - t0))
        t0 = time.time()

        # repeat each value 2^rank+A times, so it matches the new indices
        val = torch.unsqueeze(values, 2).expand_as(props).contiguous()

        # 'Unroll' the ints tensor into a long list of integer index tuples (ie. a matrix of n*2^rank by rank for each
        # instance in the batch) ...
        ints = ints.view(batchsize, -1, rank, 1).squeeze(3)

        # ... and reshape the props and vals the same way
        props = props.view(batchsize, -1)
        val = val.view(batchsize, -1)
        logging.info('  reshaping: {} seconds'.format(time.time() - t0))

        return ints, props, val
Пример #38
0
def expected_gaussian_moments(sigma, alphas):
    return {alpha : prod(gaussian_moments(sigma, a) for a in alpha) for alpha in alphas}