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.summarize
found 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
parameters
is one of ['trainable'
,'non-trainable'
,'all'
,True
].‘trainable’ parameters are the ones which require gradients and can be optimized by SGD.
Setting this to
True
will 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 showimages
can 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_range
default to the range in the image.titles
should only be given ifmerge
isTrue
.
Note
The merge shape is controlled by
shape
which can be either'square'
,'row'
,'column'
or atuple
which 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
sequences
is a tensor,lengths
needs to be provided.Note
The packed sequence that is returned has a convinient
unpack()
method as well asshape
andorder
attributes. Theorder
attribute stores the sorting order which should be used for unpacking.- Shapes:
sequences
should 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
PackedSequence
object.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
order
attribute 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