magnet.utils¶
-
magnet.utils.summarize(module, x, parameters='trainable', arguments=False, batch=False, max_width=120)[source]¶ Prints a pretty picture of how a one-input one output sequential model works.
Similar to
Model.summarizefound in Keras.Parameters: - module (
nn.Module) – The module to summarize - x (
torch.Tensor) – A sample tensor sent as input to themodule. - parameters (str or True) – Which kind of parameters to enumerate.
Default:
'trainable' - arguments (bool) – Whether to show the arguments to a node.
Default:
False - batch (bool) – Whether to show the batch dimension in the shape.
Default:
False - max_width (int) – The maximum width of the table. Default:
120
parametersis one of ['trainable','non-trainable','all',True].‘trainable’ parameters are the ones which require gradients and can be optimized by SGD.
Setting this to
Truewill print both types as a tuple.
- module (
magnet.utils.images¶
-
magnet.utils.images.show_images(images, **kwargs)[source]¶ A nifty helper function to show images represented by tensors. :param images: The images
to showimagescan be anything which from you could conceivable harvest an image. If it’s atorch.Tensor, it is converted to anumpy.ndarray. The first dimension of the tensor is treated as a batch dimension. If it’s astr, it is treated as a glob path from which all images are extracted. More commonly, a list of numpy arrays can be given.
Keyword Arguments: - pixel_range (tuple or
'auto') – The range of pixel values to be expected. Default:'auto' - cmap (str or None) – The color map for the plots. Default:
'gray' - merge (bool) – If
True, all images are merged into one giant image. Default:True - titles (list or None) – The titles for each image. Default:
None - shape (str) – The shape of the merge tile.
Default:
'square' - resize (str) – The common shape to which images are resized.
Default:
'smean' - retain (bool) – If
True, the plot is retained. Default:False - savepath (str or None) – If given, the image is saved to this path.
Default:
None
pixel_rangedefault to the range in the image.titlesshould only be given ifmergeisTrue.
Note
The merge shape is controlled by
shapewhich can be either'square','row','column'or atuplewhich explicitly specifies this shape.'square'automatically finds a shape with least difference between the number of rows and columns. This is aesthetically pleasing. In the explicit case, the product of the tuple needs to equal the number of images.
magnet.utils.plot¶
-
magnet.utils.plot.smooth_plot(*args, **kwargs)[source]¶ Same as the plot function from matplotlib… only smoother!
This function plots a modified, smoothened version of the data. Useful when data is jagged and one is interested in the average trends.
Keyword Arguments: - window_fraction (float) – The fraction of the data to use as window to the smoothener. Default: \(0.3\)
- gain (float) – The amount of artificial datapoints inserted per raw datapoint. Default: \(10\)
- replace_outliers (bool) – If
True, replaces outlier datapoints by a sensible value. Default:True - ax (Pyplot axes object) – The axis to plot onto. Default:
None
Note
Uses a Savitzky Golay filter to smoothen out the data.
magnet.utils.varseq¶
-
magnet.utils.varseq.pack(sequences, lengths=None)[source]¶ Packs a list of variable length Tensors
Parameters: - sequences (list or torch.Tensor) – The list of Tensors to pack
- lengths (list) – list of lengths of each tensor. Default:
None
Note
If
sequencesis a tensor,lengthsneeds to be provided.Note
The packed sequence that is returned has a convinient
unpack()method as well asshapeandorderattributes. Theorderattribute stores the sorting order which should be used for unpacking.- Shapes:
sequencesshould be a list of Tensors of size L x *, where L is the length of a sequence and * is any number of trailing dimensions, including zero.
-
magnet.utils.varseq.unpack(sequence, as_list=False)[source]¶ Unpacks a
PackedSequenceobject.Parameters: - sequence (
PackedSequence) – The tensor to unpack. - as_list (bool) – If
True, returns a list of tensors. Default:False
Note
The sequence should have an
orderattribute that stores the sorting order.- sequence (
-
magnet.utils.varseq.sort(sequences, order, dim=0)[source]¶ Sorts a tensor in a certain order along a certain dimension.
Parameters: - sequences (torch.Tensor) – The tensor to sort
- order (numpy.ndarray) – The sorting order
- dim (int) – The dimension to sort. Default
0
-
magnet.utils.varseq.unsort(sequences, order, dim=0)[source]¶ Unsorts a tensor in a certain order along a certain dimension.
Parameters: - sequences (torch.Tensor) – The tensor to unsort
- order (numpy.ndarray) – The sorting order
- dim (int) – The dimension to unsort. Default
0