Skip to content

Cost

Description

Info

This module is intended primarily for developers who want to better understand the structure of the library, as well as those who are going to implement their own modules.

Costs (learning criteria, errors, loss functions) are functions that calculate the network error.

Cost is the parent class from which all loss functions are inherited.

Initializing

def __init__(self):

Parameters

-

Explanations

-

Methods

resetAccumulator

def resetAccumulator(self):
Functionality
Resets the global parameters of the class, that is, those that take part in the calculation at a particular epoch.

Resets the accumulated error, resets the number of batches and samples. It is a wrapper around the resetDeviceAccumulator function.

It is used at the beginning of calculations at a new epoch.

Parameters

-

Explanations

-

updateState

def updateState(self, samples):
Functionality
Updates the size of the batch and the number of samples.

Parameters

Parameter Allowed types Description Default
samples int Batch size -

Explanations

-

resetDeviceAccumulator

def resetDeviceAccumulator(self):
Functionality
Resets the accumulated error.

Parameters

-

Explanations

-

getError

def getError(self):
Functionality
Gets from the GPU memory and returns the batch error.

Parameters

-

Explanations

-

getMeanError

def getMeanError(self):
Functionality
Gets from the GPU memory and returns the average sample error.

Parameters

-

Explanations

-

getValError

def getValError(self):
Functionality
ВReturns a validation error. The method must be called after the validate method is called.

Parameters

-

Explanations

-

__call__

def __call__(self, pred, target, queryError=True):
Functionality
Counts the gradient and/or the error for two vectors.

Parameters

Parameter Allowed types Description Default
pred tensor Predicted labels -
target tensor Target labels -
queryError bool Whether to load the error value from the GPU True

Explanations

queryError - If False, then the method will run a little faster, since the error value will not be loaded from the GPU. Also, if set to False, the method will return self.grad only, in the opposite case self.error and self.grad are returned

calcError

def calcError(self, pred, target):
Functionality
An abstract method that has to be implemented in derived classes. After implementation, it should update the error value accumulated during this epoch.

Optional calculation - calculation of the error per batch (it can also be performed in calcGrad, it depends on the error function).

Parameters

Parameter Allowed types Description Default
pred tensor Predicted labels -
target tensor Target lables -

Explanations

-

calcGrad

def calcGrad(self, pred, target):
Functionality
An abstract method that has to be implemented in derived classes. After implementation, it should calculate the gradient of the network error.

Optional calculation - calculation of the error per batch (it can also be performed in calcError, it depends on the error function).

Parameters

Параметр Возможные типы Описание По умолчанию
pred tensor Predicted labels -
target tensor Target labels -

Explanations

calcVal

def calcVal(self, pred, target):
Functionality
An abstract method that has to be implemented in derived classes. After implementation, it should calculate and return the error for the given vectors without affecting the internal state of the class

Parameters

Parameter Allowed types Description Default
pred tensor Predicted labels -
target tensor Target labels -

Explanations

-

validate

def validate(self, pred, target):
Functionality
A wrapper around the calcVal function with validation test.

Parameters

Parameter Allowed types Description Default
pred tensor Predicted labels -
target tensor Target labels -

Explanations

-

reset

def reset(self):
Functionality
Resets the batch parameters of the class, that is, those that take part in the calculation of a specific batch.

It is used when starting calculations on a new batch.

Parameters

-

Explanations

-

checkDataShape

def checkDataShape(self, pred, target):
Functionality
Checks the shape of input tensors. By default it does nothing.

The method can be meaningfully redefined in the derived classes: for this, it must raise an exception when the shape is incorrect.

Parameters

Parameter Allowed types Description Default
pred tensor Predicted labels -
target tensor Target labels -

Explanations

-

checkValDataShape

def checkValDataShape(self, pred, target):
Functionality
Checks the shape of input tensors for the validate. By default it does nothing.

The method can be meaningfully redefined in the derived classes: for this, it must raise an exception when the shape is incorrect.

Parameters

Parameter Allowed types Description Default
pred tensor Predicted labels -
target tensor Target labels -

Explanations

-

getBatchsize

def getBatchsize(self, pred):
Functionality
Counts the batch size for a given tensor.

Parameters

Parameter Allowed types Description Default
pred tensor Label tensors -

Explanations

-