Python torch.Tensors() Examples

The following are 30 code examples of torch.Tensors(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module torch , or try the search function .
Example #1
Source File: show_result.py    From DenseMatchingBenchmark with MIT License 6 votes vote down vote up
def conf2hist(self, array, bins=100):
        error_msg = "Confidence must contain torch.Tensors or numpy.ndarray; found {}"
        if isinstance(array, torch.Tensor):
            array = array.clone().detach().cpu().numpy()
        elif isinstance(array, np.ndarray):
            array = array.copy()
        else:
            raise TypeError((error_msg.format(type(array))))

        length = len(array.shape)
        assert length >= 2
        if length == 4:
            array = array[0, 0, :, :]
        elif length == 3:
            array = array[0, :, :]

        # for interval [bin_edges[i], bin_edges[i+1]], it has counts[i] numbers.
        counts, bin_edges = np.histogram(array, bins=bins)
        return counts, bin_edges

    # return a plt.figure() 
Example #2
Source File: environment.py    From stog with MIT License 6 votes vote down vote up
def move_to_device(obj, device):
    """
    Given a structure (possibly) containing Tensors on the CPU,
    move all the Tensors to the specified GPU (or do nothing, if they should be on the CPU).
    """
    if not has_tensor(obj):
        return obj
    elif isinstance(obj, torch.Tensor):
        return obj.to(device)
    elif isinstance(obj, dict):
        return {key: move_to_device(value, device) for key, value in obj.items()}
    elif isinstance(obj, list):
        return [move_to_device(item, device) for item in obj]
    elif isinstance(obj, tuple):
        return tuple([move_to_device(item, device) for item in obj])
    else:
        return obj 
Example #3
Source File: base.py    From convis with GNU General Public License v3.0 6 votes vote down vote up
def _repr_html_(self):
        def _shape(t):
            try:
                shp = list(t.size())
                return 'x'.join([str(s) for s in shp])
            except:
                shp = list(t.shape)
                return 'x'.join([str(s) for s in shp])
        def _plot(t):
            utils.plot_tensor(t)
            return "<img src='data:image/png;base64," + variable_describe._plot_to_string() + "'>"
        if len(self) == 1:
            return "<b>Output</b> containing a "+_shape(self._outs[0])+" Tensor.<br/>"+_plot(self._outs[0])+""
        else:
            s = "<b>Output</b> containing "+str(len(self))+" Tensors."
            s += "<div style='background:#ff;padding:10px'>"
            for t in self._outs:
                s += "<div style='background:#fff; margin:10px;padding:10px; border-left: 4px solid #eee;'>"+_shape(t)+" Tensor<br/> "+_plot(t)+"</div>"
            s += "</div>"
            return s 
Example #4
Source File: loader.py    From rising with MIT License 6 votes vote down vote up
def __init__(self, collate_fn: Callable, transforms: Optional[Callable] = None,
                 auto_convert: bool = True,
                 transform_call: Callable[[Any, Callable], Any] = default_transform_call):
        """
        Args:
            collate_fn: merges a list of samples to form a
                mini-batch of Tensor(s).  Used when using batched loading from a
                map-style dataset.
            transforms: transforms which can be applied to a whole batch.
                Usually this accepts either mappings or sequences and returns the
                same type containing transformed elements
            auto_convert: if set to ``True``, the batches will always be transformed to
                torch.Tensors, if possible. (default: ``True``)
            transform_call: function which determines how transforms are called. By default
                Mappings and Sequences are unpacked during the transform.
        """

        self._collate_fn = collate_fn
        self._transforms = transforms
        self._auto_convert = auto_convert
        self._transform_call = transform_call 
Example #5
Source File: data.py    From nlp_classification with MIT License 6 votes vote down vote up
def batchify(
    data: List[Tuple[torch.Tensor, torch.Tensor, torch.Tensor]]
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
    """custom collate_fn for DataLoader

    Args:
        data (List[Tuple[torch.Tensor, torch.Tensor, torch.Tensor]]): list of tuples of torch.Tensors

    Returns:
        qpair (Tuple[torch.Tensor, torch.Tensor, torch.Tensor]): tuple of torch.Tensors

    """
    data = list(zip(*data))
    queries_a, queries_b, is_duplicates = data
    queries_a = pad_sequence(queries_a, batch_first=True, padding_value=1)
    queries_b = pad_sequence(queries_b, batch_first=True, padding_value=1)
    is_duplicates = torch.stack(is_duplicates, 0)

    return queries_a, queries_b, is_duplicates 
Example #6
Source File: data.py    From nlp_classification with MIT License 6 votes vote down vote up
def batchify(
    data: List[Tuple[torch.Tensor, torch.Tensor, torch.Tensor]]
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
    """custom collate_fn for DataLoader
    Args:
        data (List[Tuple[torch.Tensor, torch.Tensor, torch.Tensor]]): list of tuples of torch.Tensors
    Returns:
        qpair (Tuple[torch.Tensor, torch.Tensor, torch.Tensor]): tuple of torch.Tensors
    """
    data = list(zip(*data))
    queries_a, queries_b, is_duplicates = data
    queries_a = pad_sequence(queries_a, batch_first=True, padding_value=1)
    queries_b = pad_sequence(queries_b, batch_first=True, padding_value=1)
    is_duplicates = torch.stack(is_duplicates, 0)

    return queries_a, queries_b, is_duplicates 
Example #7
Source File: environment.py    From gtos with MIT License 6 votes vote down vote up
def move_to_device(obj, device):
    """
    Given a structure (possibly) containing Tensors on the CPU,
    move all the Tensors to the specified GPU (or do nothing, if they should be on the CPU).
    """
    if not has_tensor(obj):
        return obj
    elif isinstance(obj, torch.Tensor):
        return obj.to(device)
    elif isinstance(obj, dict):
        return {key: move_to_device(value, device) for key, value in obj.items()}
    elif isinstance(obj, list):
        return [move_to_device(item, device) for item in obj]
    elif isinstance(obj, tuple):
        return tuple([move_to_device(item, device) for item in obj])
    else:
        return obj 
Example #8
Source File: variables.py    From Brancher with MIT License 6 votes vote down vote up
def var2link(var):
    """
    Function. Constructs a PartialLink from variables, numbers, numpy arrays, tensors or a combination of variables and
    PartialLinks.

    Args:
        var: brancher.Variables, numbers, numpy.ndarrays, torch.Tensors, or List/Tuple of brancher.Variables and
        brancher.PartialLinks.

    Retuns: brancher.PartialLink
    """
    if isinstance(var, Variable):
        vars = {var}
        fn = lambda values: values[var]
    elif isinstance(var, (numbers.Number, np.ndarray, torch.Tensor)):
        vars = set()
        fn = lambda values: var
    elif isinstance(var, (tuple, list)) and all([isinstance(v, (Variable, PartialLink)) for v in var]):
        vars = join_sets_list([{v} if isinstance(v, Variable) else v.vars for v in var])
        fn = lambda values: tuple([values[v] if isinstance(v, Variable) else v.fn(values) for v in var])
    else:
        return var
    return PartialLink(vars=vars, fn=fn, links=set(), string=str(var)) 
Example #9
Source File: LSA_mnist.py    From novelty-detection with MIT License 6 votes vote down vote up
def forward(self, x):
        # type: (torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]
        """
        Forward propagation.

        :param x: the input batch of images.
        :return: a tuple of torch.Tensors holding reconstructions, latent vectors and CPD estimates.
        """
        h = x

        # Produce representations
        z = self.encoder(h)

        # Estimate CPDs with autoregression
        z_dist = self.estimator(z)

        # Reconstruct x
        x_r = self.decoder(z)
        x_r = x_r.view(-1, *self.input_shape)

        return x_r, z, z_dist 
Example #10
Source File: LSA_ucsd.py    From novelty-detection with MIT License 6 votes vote down vote up
def forward(self, x):
        # type: (torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]
        """
        Forward propagation.

        :param x: the input batch of patches.
        :return: a tuple of torch.Tensors holding reconstructions, latent vectors and CPD estimates.
        """
        h = x

        # Produce representations
        z = self.encoder(h)

        # Estimate CPDs with autoregression
        z_dist = self.estimator(z)

        # Reconstruct x
        x_r = self.decoder(z)
        x_r = x_r.view(-1, *self.input_shape)

        return x_r, z, z_dist 
Example #11
Source File: LSA_shanghaitech.py    From novelty-detection with MIT License 6 votes vote down vote up
def forward(self, x):
        # type: (torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]
        """
        Forward propagation.

        :param x: the input batch of patches.
        :return: a tuple of torch.Tensors holding reconstructions, latent vectors and CPD estimates.
        """
        h = x

        # Produce representations
        z = self.encoder(h)

        # Estimate CPDs with autoregression
        z_dist = self.estimator(z)

        # Reconstruct x
        x_r = self.decoder(z)
        x_r = x_r.view(-1, *self.input_shape)

        return x_r, z, z_dist 
Example #12
Source File: LSA_cifar10.py    From novelty-detection with MIT License 6 votes vote down vote up
def forward(self, x):
        # type: (torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]
        """
        Forward propagation.

        :param x: the input batch of images.
        :return: a tuple of torch.Tensors holding reconstructions, latent vectors and CPD estimates.
        """
        h = x

        # Produce representations
        z = self.encoder(h)

        # Estimate CPDs with autoregression
        z_dist = self.estimator(z)

        # Reconstruct x
        x_r = self.decoder(z)
        x_r = x_r.view(-1, *self.input_shape)

        return x_r, z, z_dist 
Example #13
Source File: model.py    From argus with MIT License 6 votes vote down vote up
def prepare_batch(self, batch, device):
        """Prepare batch data for training by performing device conversion.

        Args:
            batch (tuple of 2 torch.Tensors: (input, target)): The input and
                target tensors to move on the required device.
            device (str or torch.device): The target device for the tensors.

        Returns:
            tuple of 2 torch.Tensors: (input, target): The resulted tensors on
            the required device.

        """
        input, target = batch
        input = deep_to(input, device, non_blocking=True)
        target = deep_to(target, device, non_blocking=True)
        return input, target 
Example #14
Source File: util.py    From allennlp with Apache License 2.0 6 votes vote down vote up
def move_to_device(obj, cuda_device: Union[torch.device, int]):
    """
    Given a structure (possibly) containing Tensors on the CPU,
    move all the Tensors to the specified GPU (or do nothing, if they should be on the CPU).
    """
    from allennlp.common.util import int_to_device

    cuda_device = int_to_device(cuda_device)

    if cuda_device == torch.device("cpu") or not has_tensor(obj):
        return obj
    elif isinstance(obj, torch.Tensor):
        return obj.cuda(cuda_device)
    elif isinstance(obj, dict):
        return {key: move_to_device(value, cuda_device) for key, value in obj.items()}
    elif isinstance(obj, list):
        return [move_to_device(item, cuda_device) for item in obj]
    elif isinstance(obj, tuple) and hasattr(obj, "_fields"):
        # This is the best way to detect a NamedTuple, it turns out.
        return obj.__class__(*(move_to_device(item, cuda_device) for item in obj))
    elif isinstance(obj, tuple):
        return tuple(move_to_device(item, cuda_device) for item in obj)
    else:
        return obj 
Example #15
Source File: model.py    From funsor with Apache License 2.0 6 votes vote down vote up
def initialize_raggedness_masks(self):
        """
        Convert raw raggedness tensors into funsor.tensor.Tensors
        """
        batch_inputs = OrderedDict([
            ("i", bint(self.config["sizes"]["individual"])),
            ("g", bint(self.config["sizes"]["group"])),
            ("t", bint(self.config["sizes"]["timesteps"])),
        ])

        raggedness_masks = {}
        for name in ("individual", "timestep"):
            data = self.config[name]["mask"]
            if len(data.shape) < len(batch_inputs):
                while len(data.shape) < len(batch_inputs):
                    data = data.unsqueeze(-1)
                data = data.expand(tuple(v.dtype for v in batch_inputs.values()))
            data = data.to(self.config["observations"]["step"].dtype)
            raggedness_masks[name] = Tensor(data[..., :self.config["sizes"]["timesteps"]],
                                            batch_inputs)

        self.raggedness_masks = raggedness_masks
        return self.raggedness_masks 
Example #16
Source File: model.py    From funsor with Apache License 2.0 6 votes vote down vote up
def initialize_observations(self):
        """
        Convert raw observation tensors into funsor.tensor.Tensors
        """
        batch_inputs = OrderedDict([
            ("i", bint(self.config["sizes"]["individual"])),
            ("g", bint(self.config["sizes"]["group"])),
            ("t", bint(self.config["sizes"]["timesteps"])),
        ])

        observations = {}
        for name, data in self.config["observations"].items():
            observations[name] = Tensor(data[..., :self.config["sizes"]["timesteps"]], batch_inputs)

        self.observations = observations
        return self.observations 
Example #17
Source File: base.py    From torch-kalman with MIT License 6 votes vote down vote up
def set_ilink(self, ilink: Optional[Callable], overwrite: bool = False, **kwargs):
        """
        Set the inverse-link function that will translate value-assignments/adjustments for an element of the
        design-matrix into their final value.

        :param ilink: A callable that is appropriate for torch.Tensors (e.g. torch.exp). If None, then the identity
          link is assumed.
        :param overwrite: If False (default) then cannot re-assign if already assigned; if True will overwrite.
        :param kwargs: The names of the dimensions.
        """
        key = self._get_key(kwargs)
        if key not in self._assignments:
            raise ValueError(f"Tried to set ilink for {key} but must `assign` first.")
        if key in self._ilinks and not overwrite:
            raise ValueError(f"Already have ilink for {key}.")
        assert ilink is None or callable(ilink)
        self._ilinks[key] = ilink 
Example #18
Source File: tensor_utils.py    From sciwing with MIT License 6 votes vote down vote up
def move_to_device(obj, cuda_device: torch.device):
    """
    Given a structure (possibly) containing Tensors on the CPU,
    move all the Tensors to the specified GPU (or do nothing, if they should be on the CPU).
    From ``allenlp.nn.util``
    """
    # pylint: disable=too-many-return-statements
    # pargma: no cover
    # not tested relying on allennlp
    if cuda_device.type == "cpu" or not has_tensor(obj):
        return obj
    elif isinstance(obj, torch.Tensor):
        return obj.to(cuda_device)
    elif isinstance(obj, dict):
        return {key: move_to_device(value, cuda_device) for key, value in obj.items()}
    elif isinstance(obj, list):
        return [move_to_device(item, cuda_device) for item in obj]
    elif isinstance(obj, tuple) and hasattr(obj, "_fields"):
        # This is the best way to detect a NamedTuple, it turns out.
        return obj.__class__(*[move_to_device(item, cuda_device) for item in obj])
    elif isinstance(obj, tuple):
        return tuple([move_to_device(item, cuda_device) for item in obj])
    else:
        return obj 
Example #19
Source File: _functions.py    From garage with MIT License 6 votes vote down vote up
def global_device():
    """Returns the global device that torch.Tensors should be placed on.

    Note: The global device is set by using the function
        `garage.torch._functions.set_gpu_mode.`
        If this functions is never called
        `garage.torch._functions.device()` returns None.

    Returns:
        `torch.Device`: The global device that newly created torch.Tensors
            should be placed on.

    """
    # pylint: disable=global-statement
    global _DEVICE
    return _DEVICE 
Example #20
Source File: model.py    From funsor with Apache License 2.0 6 votes vote down vote up
def __call__(self):

        # calls pyro.param so that params are exposed and constraints applied
        # should not create any new torch.Tensors after __init__
        self.initialize_params()

        N_c = self.config["sizes"]["group"]
        N_s = self.config["sizes"]["individual"]

        log_prob = Tensor(torch.tensor(0.), OrderedDict())

        plate_g = Tensor(torch.zeros(N_c), OrderedDict([("g", bint(N_c))]))
        plate_i = Tensor(torch.zeros(N_s), OrderedDict([("i", bint(N_s))]))

        if self.config["group"]["random"] == "continuous":
            eps_g_dist = plate_g + dist.Normal(**self.params["eps_g"])(value="eps_g")
            log_prob += eps_g_dist

        # individual-level random effects
        if self.config["individual"]["random"] == "continuous":
            eps_i_dist = plate_g + plate_i + dist.Normal(**self.params["eps_i"])(value="eps_i")
            log_prob += eps_i_dist

        return log_prob 
Example #21
Source File: base.py    From torch-kalman with MIT License 6 votes vote down vote up
def adjust(self, value: DesignMatAdjustment, check_slow_grad: bool, **kwargs):
        """
        Adjust the value of an assignment. The final value for an element will be given by (a) taking the sum of the
        initial value and all adjustments, (b) applying the ilink function from `set_ilink()`.

        :param value: Either (a) a torch.Tensor or (b) a sequence of torch.Tensors (one for each timepoint). The tensor
          should be either scalar, or be 1D with length = self.num_groups.
        :param check_slow_grad: A natural way to create adjustments is to first create a tensor that `requires_grad`,
          then split it into a list of tensors, one for each time-point. This way of creating adjustments should be
          avoided because it leads to a very slow backwards pass. When check_slow_grad is True then a heuristic is used
          to check for this "gotcha". It can lead to false-alarms, so disabling is allowed with `check_slow_grad=False`.
        :param kwargs: The names of the dimensions.
        """
        key = self._get_key(kwargs)
        value = self._validate_adjustment(value, check_slow_grad=check_slow_grad)
        try:
            self._assignments[key].append(value)
        except KeyError:
            raise RuntimeError("Tried to adjust {} (in {}); but need to `assign()` it first.".format(key, self)) 
Example #22
Source File: environment.py    From gtos with MIT License 5 votes vote down vote up
def has_tensor(obj) -> bool:
    """
    Given a possibly complex data structure,
    check if it has any torch.Tensors in it.
    """
    if isinstance(obj, torch.Tensor):
        return True
    elif isinstance(obj, dict):
        return any(has_tensor(value) for value in obj.values())
    elif isinstance(obj, (list, tuple)):
        return any(has_tensor(item) for item in obj)
    else:
        return False 
Example #23
Source File: model.py    From allennlp with Apache License 2.0 5 votes vote down vote up
def forward_on_instance(self, instance: Instance) -> Dict[str, numpy.ndarray]:
        """
        Takes an [`Instance`](../data/instance.md), which typically has raw text in it, converts
        that text into arrays using this model's [`Vocabulary`](../data/vocabulary.md), passes those
        arrays through `self.forward()` and `self.make_output_human_readable()` (which by default
        does nothing) and returns the result.  Before returning the result, we convert any
        `torch.Tensors` into numpy arrays and remove the batch dimension.
        """
        return self.forward_on_instances([instance])[0] 
Example #24
Source File: model.py    From gtos with MIT License 5 votes vote down vote up
def forward(self, inputs) -> Dict[str, torch.Tensor]:  # pylint: disable=arguments-differ
        """
        Defines the forward pass of the model. In addition, to facilitate easy training,
        this method is designed to compute a loss function defined by a user.
        The input is comprised of everything required to perform a
        training update, `including` labels - you define the signature here!
        It is down to the user to ensure that inference can be performed
        without the presence of these labels. Hence, any inputs not available at
        inference time should only be used inside a conditional block.
        The intended sketch of this method is as follows::
            def forward(self, input1, input2, targets=None):
                ....
                ....
                output1 = self.layer1(input1)
                output2 = self.layer2(input2)
                output_dict = {"output1": output1, "output2": output2}
                if targets is not None:
                    # Function returning a scalar torch.Tensor, defined by the user.
                    loss = self._compute_loss(output1, output2, targets)
                    output_dict["loss"] = loss
                return output_dict
        Parameters
        ----------
        inputs:
            Tensors comprising everything needed to perform a training update, `including` labels,
            which should be optional (i.e have a default value of ``None``).  At inference time,
            simply pass the relevant inputs, not including the labels.
        Returns
        -------
        output_dict: ``Dict[str, torch.Tensor]``
            The outputs from the model. In order to train a model using the
            :class:`~allennlp.training.Trainer` api, you must provide a "loss" key pointing to a
            scalar ``torch.Tensor`` representing the loss to be optimized.
        """
        raise NotImplementedError 
Example #25
Source File: model.py    From gtos with MIT License 5 votes vote down vote up
def forward_on_instance(self, instance) -> Dict[str, numpy.ndarray]:
        """
        Takes an :class:`~allennlp.data.instance.Instance`, which typically has raw text in it,
        converts that text into arrays using this model's :class:`Vocabulary`, passes those arrays
        through :func:`self.forward()` and :func:`self.decode()` (which by default does nothing)
        and returns the result.  Before returning the result, we convert any
        ``torch.Tensors`` into numpy arrays and remove the batch dimension.
        """
        raise NotImplementedError 
Example #26
Source File: data.py    From nlp_classification with MIT License 5 votes vote down vote up
def batchify(data: List[Tuple[torch.Tensor, torch.Tensor]]) -> Tuple[torch.Tensor, torch.Tensor]:
    """custom collate_fn for DataLoader

    Args:
        data (list): list of torch.Tensors

    Returns:
        data (tuple): tuple of torch.Tensors
    """
    indices, labels = zip(*data)
    indices = pad_sequence(indices, batch_first=True, padding_value=1)
    labels = torch.stack(labels, 0)
    return indices, labels 
Example #27
Source File: data.py    From nlp_classification with MIT License 5 votes vote down vote up
def batchify(data: List[Tuple[torch.tensor, torch.tensor, torch.tensor]]) ->\
        Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
    """custom collate_fn for DataLoader

    Args:
        data (list): list of torch.Tensors

    Returns:
        data (tuple): tuple of torch.Tensors
    """
    indices, labels = zip(*data)
    indices = pad_sequence(indices, batch_first=True, padding_value=1, )
    labels = torch.stack(labels, 0)
    return indices, labels 
Example #28
Source File: train.py    From multimodal-vae-public with MIT License 5 votes vote down vote up
def elbo_loss(recon, data, mu, logvar, lambda_image=1.0, 
              lambda_attrs=1.0, annealing_factor=1.):
    """Compute the ELBO for an arbitrary number of data modalities.

    @param recon: list of torch.Tensors/Variables
                  Contains one for each modality.
    @param data: list of torch.Tensors/Variables
                 Size much agree with recon.
    @param mu: Torch.Tensor
               Mean of the variational distribution.
    @param logvar: Torch.Tensor
                   Log variance for variational distribution.
    @param lambda_image: float [default: 1.0]
                         weight for image BCE
    @param lambda_attr: float [default: 1.0]
                        weight for attribute BCE
    @param annealing_factor: float [default: 1]
                             Beta - how much to weight the KL regularizer.
    """
    assert len(recon) == len(data), "must supply ground truth for every modality."
    n_modalities = len(recon)
    batch_size   = mu.size(0)

    BCE  = 0  # reconstruction cost
    for ix in xrange(n_modalities):
        # dimensionality > 1 implies an image
        if len(recon[ix].size()) > 1:
            recon_ix = recon[ix].view(batch_size, -1)
            data_ix  = data[ix].view(batch_size, -1)
            BCE += lambda_image * torch.sum(binary_cross_entropy_with_logits(recon_ix, data_ix), dim=1)
        else:  # this is for an attribute
            BCE += lambda_attrs * binary_cross_entropy_with_logits(recon[ix], data[ix])
    KLD  = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp(), dim=1)
    ELBO = torch.mean(BCE + annealing_factor * KLD)
    return ELBO 
Example #29
Source File: show_result.py    From DenseMatchingBenchmark with MIT License 5 votes vote down vote up
def vis_per_conf_hist(self, Conf, bins=100, ):
        def conf2hist2vis(array, bins):
            counts, bin_edges = self.conf2hist(array, bins)
            fig = self.hist2vis(counts, bin_edges)
            return fig

        error_msg = "Confidence must contain torch.Tensors or numpy.ndarray, dicts or lists; found {}"
        if isinstance(Conf, (torch.Tensor, np.ndarray)):
            return conf2hist2vis(Conf, bins)
        elif isinstance(Conf, container_abcs.Mapping):
            return {key: self.vis_per_conf_hist(Conf[key]) for key in Conf}
        elif isinstance(Conf, container_abcs.Sequence):
            return [self.vis_per_conf_hist(samples) for samples in Conf]

        raise TypeError((error_msg.format(type(Conf)))) 
Example #30
Source File: monitors.py    From bindsnet with GNU Affero General Public License v3.0 5 votes vote down vote up
def reset_state_variables(self) -> None:
        # language=rst
        """
        Resets recordings to empty ``torch.Tensors``.
        """
        # Reset to empty recordings
        self.recording = {k: {} for k in self.layers + self.connections}

        if self.time is not None:
            self.i = 0

        # If no simulation time is specified, specify 0-dimensional recordings.
        if self.time is None:
            for v in self.state_vars:
                for l in self.layers:
                    if hasattr(self.network.layers[l], v):
                        self.recording[l][v] = torch.Tensor()

                for c in self.connections:
                    if hasattr(self.network.connections[c], v):
                        self.recording[c][v] = torch.Tensor()

        # If simulation time is specified,
        # pre-allocate recordings in memory for speed.
        else:
            for v in self.state_vars:
                for l in self.layers:
                    if hasattr(self.network.layers[l], v):
                        self.recording[l][v] = torch.zeros(
                            self.time,
                            *getattr(self.network.layers[l], v).size()
                        )

                for c in self.connections:
                    if hasattr(self.network.connections[c], v):
                        self.recording[c][v] = torch.zeros(
                            self.time,
                            *getattr(self.network.layers[c], v).size()
                        )