Multi

Description

A module that makes it possible to implement an ensemble of various error functions.

Initializing

def __init__(self):

Parameters

-

Explanations

-

Methods

def append(self, cost):
Functionality

Adds the passed error function for further calculations.

Parameters

Parameter Allowed types Description Default
cost Cost Subclass object Cost -

Explanations

-

Examples


Necessary imports:

import numpy as np
from PuzzleLib.Backend import gpuarray
from PuzzleLib.Cost import Multi, Abs, MSE

Info

gpuarray is required to properly place the tensor in the GPU.

Synthetic target and prediction tensors:

pred = gpuarray.to_gpu(np.random.randn(10, 10).astype(np.float32))
target = gpuarray.to_gpu(np.random.randn(10, 10).astype(np.float32))

Initializing the error function:

multi = Multi().append(MSE()).append(Abs())

Calculating the error and the gradient on the batch:

error, grad = multi([pred, pred], [target, target])
# error[0], grad[0] - error and gradient for MSE-Loss
# error[1], grad[1] - erroe and gradient for Abs-loss