torchtree.core.utils#
Attributes#
Exceptions#
Common base class for all non-exit exceptions. |
|
Custom exception for debugging conflicts between @property and |
Classes#
Extensible JSON <http://json.org> encoder for Python data structures. |
|
Simple JSON <http://json.org> decoder |
|
Functions#
|
|
|
Create a tensor with the given dtype and shape and initialize it using a |
|
|
|
|
|
|
|
|
|
|
|
Remove comments in dictionary representation of objects. |
|
|
|
|
|
|
|
Recursively replace tensor in json_object with tensors present in |
|
Print computation graph. |
|
|
|
|
|
Module Contents#
- torchtree.core.utils.REGISTERED_CLASSES#
- class torchtree.core.utils.TensorEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]#
Bases:
json.JSONEncoder
Extensible JSON <http://json.org> encoder for Python data structures.
Supports the following objects and types by default:
Python
JSON
dict
object
list, tuple
array
str
string
int, float
number
True
true
False
false
None
null
To extend this to recognize other objects, subclass and implement a
.default()
method with another method that returns a serializable object foro
if possible, otherwise it should call the superclass implementation (to raiseTypeError
).- default(obj)[source]#
Implement this method in a subclass such that it returns a serializable object for
o
, or calls the base implementation (to raise aTypeError
).For example, to support arbitrary iterators, you could implement default like this:
def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o)
- class torchtree.core.utils.TensorDecoder(*args, **kwargs)[source]#
Bases:
json.JSONDecoder
Simple JSON <http://json.org> decoder
Performs the following translations in decoding by default:
JSON
Python
object
dict
array
list
string
str
number (int)
int
number (real)
float
true
True
false
False
null
None
It also understands
NaN
,Infinity
, and-Infinity
as their correspondingfloat
values, which is outside the JSON spec.
- torchtree.core.utils.tensor_rand(distribution, shape, dtype=None, device=None, requires_grad=False)[source]#
Create a tensor with the given dtype and shape and initialize it using a distribution.
Continuous distributions: normal, log_normal, uniform. Discrete distributions: random, bernoulli
- Parameters:
- Returns:
tensor
- Return type:
torch.Tensor
- Example:
>>> _ = torch.manual_seed(0) >>> t1 = tensor_rand('normal(1.0, 2.0)', (1,2), dtype=torch.float64) >>> t1 tensor([[4.0820, 0.4131]], dtype=torch.float64) >>> _ = torch.manual_seed(0) >>> t2 = tensor_rand('normal(0.0, 1.0)', (1,2), dtype=torch.float64) >>> _ = torch.manual_seed(0) >>> t3 = tensor_rand('normal()', (1,2), dtype=torch.float64) >>> t2 == t3 tensor([[True, True]])
- exception torchtree.core.utils.JSONParseError[source]#
Bases:
Exception
Common base class for all non-exit exceptions.
- torchtree.core.utils.remove_comments(obj)[source]#
Remove comments in dictionary representation of objects.
A key starting with an underscore results in the key/value pair to be removed.
A dictionary with key equal to ignore and value set to True results in its removal.
- torchtree.core.utils.update_parameters(json_object, parameters) None [source]#
Recursively replace tensor in json_object with tensors present in parameters.
- torchtree.core.utils.print_graph(g: torch.Tensor, level: int = 0) None [source]#
Print computation graph.
- Parameters:
g (torch.Tensor) – a tensor
level – indentation level