Having studied Martian glyphs and analyzed geochemical indices of samples, Kozak Vus's team finally gained access to the main archive of the bunker. This archive contains records of command sequences (represented by classes from to c-1) for controlling Martian computers.
The team wants to train an AI model to predict these sequences. For each index in a sequence, the model outputs scores (or logits) p_{i, j, k} for each possible class (from to c-1), where is the index of the sequence in the batch, as the model processes multiple sequences at once for greater efficiency. To effectively train the model, it is necessary to use an error evaluation method analogous to that described in recovered Martian texts. This complex evaluation method is implemented using a specific loss function. Your task is to implement this function, 'calculateLoss' which accurately reproduces the Martian evaluation algorithm.
Let be the batch size, be the maximum sequence length, and be the number of classes.
Let p_{i, j, k} denote the score (logit) determined by the model for the -th class, -th element, -th sequence in the batch. Let t_{i, j} denote the true class for the -th element, -th sequence in the batch. Let m_{i, j} ( or ) indicate whether the -th position is valid in the -th sequence in the batch; this is necessary to allow sequences of different lengths in the same batch. Let y_{i,j} = p_{i,j,t_{i,j}} be the logit of the true class.
The function consists of three parts:
Let z_{i,j} = \max_{k \ne y_{i,j}} p_{i,j,k} — the maximum logit of an incorrect class. The loss is defined as:
where m_1 is a parameter of the function. Note that multiplying by the mask simply takes values only from valid positions, and dividing by the sum of all masks is simply dividing by the number of valid positions.
Let d^{(1)}_{i,j} = |y_{i,j+1} - y_{i,j}|, i.e., the absolute difference of logits of true classes between adjacent elements of the sequence.
The loss is defined as:
where m_2 is a parameter of the function. Note that multiplying by the mask has the same functionality as in the first part, but now it is required that both adjacent elements are valid.
Let d^{(2)}_{i,j} = |y_{i,j+2} - 2y_{i,j+1} + y_{i,j}|.
The loss is defined as:
where m_3 is a parameter of the function. Note that multiplying by the mask has the same functionality as in the first and second parts, but now it is required that three adjacent elements are valid.
The total result is defined as:
where \alpha, \beta, \gamma are parameters of the function.
This loss function encourages correct ranking of true classes, as well as temporal smoothness for logits corresponding to correct labels.
Please note that you do not need to read or output data. You only need to implement this function.
Example code that you can use for local testing, as well as input and output data for the sample test, are provided in the 'sample.zip' attachment.
The function receives 9 arguments:
PyTorch tensor 'predictions' of dimension (b, l, c) with scores determined by the model.
PyTorch tensor 'targets' of dimension with true classes.
PyTorch tensor 'mask' of dimension with boolean values, indicating whether a position is valid.
float variables 'm1', 'm2', 'm3' - parameters m_1, m_2, m_3 from the function description.
float variables 'alpha', 'beta', 'gamma' - parameters \alpha, \beta, \gamma from the function description.
The problem uses a multi-test. In a single test, your function may be called up to times with different input data.
It is guaranteed that 1 \leq t \leq 100, 1 \leq b \leq 500, 3 \leq l \leq 200, 2 \leq c \leq 100, 0.1 \leq m_1, m_2, m_3 \leq 5, 0 \leq \alpha, \beta, \gamma \leq 1, -10 \leq p_{i, j, k} \leq 10.
Return a 0-dimensional tensor whose single element is the loss value. Let your answer be , and the true answer be . Your answer will be considered correct if \left|\frac{a-b}{b}\right| \leq 10^{-6}.
Example 1
The final score for the problem is the sum of the maximum points obtained for each subtask across all your attempts (submissions).
(5 points): b = 1, l \leq 100. In this subtask, the input tensors will not have a batch dimension. That is, the 'predictions' tensor will have dimension , and the 'targets' and 'mask' tensors will have dimension .
(10 points): b \leq 10, l \leq 100, c \leq 50, \beta = \gamma = 0.
(15 points): b \leq 10, l \leq 100, c \leq 50, \gamma = 0.
(15 points): b \leq 10, l \leq 100, c \leq 50.
(25 points): t \leq 10, \beta = \gamma = 0.
(20 points): t \leq 10, \gamma = 0.
(10 points): t \leq 10.