tortreinador.utils package#

Submodules#

tortreinador.utils.Recorder module#

class tortreinador.utils.Recorder.Recorder(device)[source]#

Bases: object

avg()[source]#
reset()[source]#
update(val)[source]#
class tortreinador.utils.Recorder.RecorderForEpoch(device)[source]#

Bases: object

avg()[source]#
reset()[source]#
update(val)[source]#

tortreinador.utils.View module#

tortreinador.utils.View.get_lastlayer_params(net)[source]#

get last trainable layer of a net :param network architectur:

Returns:

last layer weights and last layer bias

tortreinador.utils.View.init_weights(net)[source]#

the weights of conv layer and fully connected layers are both initilized with Xavier algorithm, In particular, we set the parameters to random values uniformly drawn from [-a, a] where a = sqrt(6 * (din + dout)), for batch normalization layers, y=1, b=0, all bias initialized to 0.

tortreinador.utils.View.split_weights(net)[source]#

split network weights into to categlories, one are weights in conv layer and linear layer, others are other learnable paramters(conv bias, bn weights, bn bias, linear bias) :param net: network architecture

Returns:

a dictionary of params splite into to categlories

tortreinador.utils.View.visualize_lastlayer(writer, net, n_iter)[source]#

visualize last layer grads

tortreinador.utils.View.visualize_learning_rate(writer, lr, epoch)[source]#

visualize learning rate

tortreinador.utils.View.visualize_network(writer, net)[source]#

visualize network architecture

tortreinador.utils.View.visualize_param_hist(writer, net, epoch)[source]#

visualize histogram of params

tortreinador.utils.View.visualize_test_acc(writer, acc, epoch)[source]#

visualize test acc

tortreinador.utils.View.visualize_test_loss(writer, loss, epoch)[source]#

visualize test loss

tortreinador.utils.View.visualize_train_loss(writer, loss, n_iter)[source]#

visualize training loss

tortreinador.utils.WarmUpLR module#

class tortreinador.utils.WarmUpLR.WarmUpLR(optimizer, total_iters, last_epoch=-1)[source]#

Bases: _LRScheduler

warmup_training learning rate scheduler :param optimizer: optimzier(e.g. SGD) :param total_iters: totoal_iters of warmup phase

get_lr()[source]#

we will use the first m batches, and set the learning rate to base_lr * m / total_iters

tortreinador.utils.metrics module#

tortreinador.utils.metrics.mixture(pi, mu, sigma, sample_for='train')[source]#
tortreinador.utils.metrics.r2_score(y_ture, y_pred)[source]#

r2 score function for the data in GPU

Args:
  • y_ture: true

  • y_pred: predicted

tortreinador.utils.plot module#

tortreinador.utils.plot.calculate_GMM(p, m, s, y_label)[source]#

Calculate the probability density function of the Gaussian Mixture Model

Args:
  • param p: pi

  • param m: mean

  • param s: standard deviation

  • param y_label: e.g. np.arange(0, 1, 0.001)

tortreinador.utils.plot.plot_line_2(y_1: str, y_2: str, df: DataFrame, output_path: str, fig_size: tuple = (10, 6), dpi: int = 300)[source]#

Plot Merge Line (2 Lines) using Seaborn

Args:

  • param y_1: Name of Line 1

  • param y_2: Name of Line 2

  • param df: Dataframe

  • param fig_size:

  • param output_path:

  • param dpi:

return: Show Line picture and save to the specific location

tortreinador.utils.preprocessing module#

tortreinador.utils.preprocessing.load_data(data: DataFrame, input_parameters: list, output_parameters: list, feature_range=None, train_size: float = 0.8, val_size: float = 0.1, if_normal: bool = True, if_shuffle: bool = True, n_workers: int = 8, batch_size: int = 256, random_state=42, if_double: bool = False)[source]#

Load Data and Normalize for Regression Tasks: This function preprocesses data specifically for regression tasks by handling data splitting, optional shuffling, normalization, and DataLoader creation.

Parameters:
  • data (pd.DataFrame) – The complete dataset in a Pandas DataFrame.

  • input_parameters (list of str or int) – Column names or indices representing the input features.

  • output_parameters (list of str or int) – Column names or indices representing the target variables.

  • feature_range (tuple of (float, float), optional) – The range (min, max) used by the MinMaxScaler for scaling data. Defaults to (0, 1).

  • train_size (float) – The proportion of the dataset to include in the train split (0 to 1).

  • val_size (float) – The proportion of the training data to use as validation data (0 to 1).

  • if_normal (bool) – Flag to determine whether to normalize the data using MinMaxScaler.

  • if_shuffle (bool) – Flag to determine whether to shuffle the data before splitting into training, validation, and test sets.

  • n_workers (int) – The number of subprocesses to use for data loading. More workers can increase the loading speed but consume more CPU cores.

  • batch_size (int) – Number of samples per batch to load.

  • random_state (int, optional) – A seed used by the random number generator for reproducibility. Defaults to None.

  • if_double (bool) – Flag to determine whether to convert data to double precision (float64) format.

Returns:

Contains Train DataLoader, Validation DataLoader, Test X, Test Y, Scaler for X, and Scaler for Y. - Train DataLoader (torch.utils.data.DataLoader): DataLoader containing the training data. - Validation DataLoader (torch.utils.data.DataLoader): DataLoader containing the validation data. - Test X (np.array): Features of the test dataset. - Test Y (np.array): Targets of the test dataset. - Scaler X (sklearn.preprocessing.MinMaxScaler): Scaler object used for the input features. - Scaler Y (sklearn.preprocessing.MinMaxScaler): Scaler object used for the output targets.

Return type:

tuple

tortreinador.utils.tools module#

tortreinador.utils.tools.check_outlier(validation_set, stride, windows_size, z_score_threshold)[source]#
tortreinador.utils.tools.xavier_init(net: Module)[source]#

Module contents#