tensornn.utils

This file contains useful variables that are used in TensorNN.

Functions

flatten

Flatten the inputs array.

normalize

Normalize the training data.

one_hot

Get the one-hot representation of an integer.

set_seed

Set the seed for TensorNN's randomness.

source

Get the source code of a TensorNN object.

Classes

TensorNNObject

Base class for all TensorNN objects.

tensornn.utils.atleast_2d(*arys)

View inputs as arrays with at least two dimensions.

Parameters

arys1, arys2, …array_like

One or more array-like sequences. Non-array inputs are converted to arrays. Arrays that already have two or more dimensions are preserved.

Returns

res, res2, …ndarray

An array, or tuple of arrays, each with a.ndim >= 2. Copies are avoided where possible, and views with two or more dimensions are returned.

See Also

atleast_1d, atleast_3d

Examples

>>> import numpy as np
>>> np.atleast_2d(3.0)
array([[3.]])
>>> x = np.arange(3.0)
>>> np.atleast_2d(x)
array([[0., 1., 2.]])
>>> np.atleast_2d(x).base is x
True
>>> np.atleast_2d(1, [1, 2], [[1, 2]])
(array([[1]]), array([[1, 2]]), array([[1, 2]]))
tensornn.utils.flatten(inputs: Tensor) Tensor

Flatten the inputs array. For example, a neural network cannot take in an image as input since it is 2D, so we can flatten it to make it 1D.

tensornn.utils.normalize(data)

Normalize the training data. This will never hurt your data, it will always help it, make sure to use this every time. This function will make it so that the largest value in the data is 1.

Parameters:

data – training data to the network

Returns:

normalized data, max is 1

tensornn.utils.one_hot(values: int | Iterable[int], classes: int) Tensor

Get the one-hot representation of an integer. One-hot representation is like the opposite of np.argmax. Let’s we want our network’s output to be [0, 1](first neuron on, second off), that would be the ‘one-hot vector’. If you were to run np.argmax([0, 1]), you would get the index of the 1(which is also the index of the max value).

Parameters:
  • values – to be converted to one-hot (max 1D), ex: one_hot(3, 5) -> [0, 0, 0, 1, 0]

  • classes – number of different places for the 1, len of one-hot

Returns:

one-hot vector from the given params

tensornn.utils.set_seed(seed)

Set the seed for TensorNN’s randomness. Useful for reproducibility while testing and debugging.

Parameters:

seed – the seed to set

tensornn.utils.source(obj: Any, output: TextIO | None = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>) str

Get the source code of a TensorNN object.

Parameters:

obj – the tensornn object, ex: tnn.nn.NeuralNetwork