7
$\begingroup$

I have a custom situation for which I am trying to design a cost function.

The idea is that you have a stack of LSTMs doing something slightly unconventional. Each LSTM$_l$ computes a linear transformation of its hidden layer $V_{l-1}h^t_l$ to predict the hidden layer vector $h^{t+1}_{l-1}$ at the level below; at the same time, it should not stray far from the value it was predicted to have from the layer above (i.e., we want $h^t_l \approx V_lh^{t-1}_{l+1}$). That is, the goal at each layer is that:

$V_{l-1}h^t_l \approx h^{t+1}_{l-1}$

and

$h^t_l \approx V_lh^{t-1}_{l+1}$

I would like to design a cost function that will bring about this situation in a system of $L$ stacked LSTMs. I introduced an error variable at each layer $E^{t}_{l} = V_lh^{t-1}_{l+1} - h^{t}_{l}$, where $l$ ranges from $0$ to $L-1$, such that $h^t_0 \equiv x^t$, and the goal is to model a sequence $x^{1:T}$. The loss function is then:

$\Large \mathcal{L}_{\text{subtract}} = \sum_{t=1}^T\sum_{i=0}^{L-1}|E^t_i|$

And the sequence of steps is the following:

enter image description here

However, this simple subtractive loss function is not very good, in the sense that the model could not even overfit a minibatch (I did this as a test). I tried a divisive loss:

$\Large E^t_l = \frac{V_{l}h_{l+1}^{t-1}}{\max(\epsilon, h_{l}^t)} + \frac{h_{l}^t}{\max(\epsilon,V_{l}h_{l+1}^{t-1})}$

But the model also did not learn anything (probably because division led to numerical instability). How do I go about designing a stable loss function that I can use at all layers?

NB My goal for this project is to build a language model based on predictive coding principles, to test a hypothesis about brain activity (i.e., the goal is not to beat SoTA).

$\endgroup$
2
  • 1
    $\begingroup$ I'll ask the same question I asked when you brought up this 'divisive loss', because it seems this may be an XY problem—why do you think it makes sense at all as a function worth computing? $\endgroup$ Commented Jun 29, 2021 at 20:35
  • $\begingroup$ The divisive loss works if the goal is to reconstruct a one-hot vector: imgur.com/a/u3EboOp ; when the entries corresponding to $0$ have a value greater than $\epsilon$, the loss will be large; and when the entries corresponding to $1$ are small, the loss will also be large. And when the reconstruction vector entries corresponding to $0$ are $< \epsilon$ and those corresponding to $1$ are $\approx 1$, the loss will be small. So its minimum is achieved when the "correct" vector is reconstructed; that's the rationale. But now I realize that it will not work for arbitrary embeddings. $\endgroup$
    – Sam
    Commented Jun 29, 2021 at 23:07

1 Answer 1

10
$\begingroup$

To answer the titular question, a key characteristic of a loss function is that the loss is minimized at the target values $y$. In other words, if you're estimating a quantity, the least loss should be assigned to the estimates that are exactly correct.

Using $\mathcal{L}_\text{subtract}$ and divisive losses are not loss functions because they do not involve the target variables. In other words, you can achieve the absolute minimum value of 0 without learning anything about what you want to model.

Consider this loss function $L(\hat{y}) = (\hat{y} - c)^2$ for model outputs $\hat{y}$, a constant $c$ and model targets $y$. This function is clearly convex in $\hat{y},c$, but it has nothing to do with the model targets $y$. If $c$ is a fixed target and our model outputs $\hat{y}$, then we achieve a minimum at $\hat{y}=c$, and this is true no matter what the targets $y$ happen to be.

My suggestion is to use $\mathcal{L}_\text{subtract}$ as a to augment a typical loss function. So if you're using a binomial cross entropy loss, you could write down a total loss as

$$ \text{total loss} = \text{BCE}(y,\hat{y}) + \lambda \mathcal{L}_\text{subtract}$$

for $\lambda >0$ some tuning parameter controlling how much deviation can occur from one layer to the next. Likewise, you can use an alternative loss in place of $\text{BCE}$ to model other types of data.


This seems related to, but not exactly the same as "Regularizing RNNs by Stabilizing Activations" by David Krueger & Roland Memisevic. Perhaps their work and bibliography is of interest.

$\endgroup$
9
  • $\begingroup$ Note that the LSTM at the bottom of the stack is directly predicting the target input sequence; although the rest of the loss is concerned with predicting the hidden state of the LSTM below, it is all eventually coupled to the target sequence. Maybe I misunderstood your answer - am I missing something? $\endgroup$
    – Sam
    Commented Jun 29, 2021 at 18:11
  • $\begingroup$ Where does your model compare the target $y$ to the model predictions $\hat{y}$? Is $h$ a hidden sequence or the target vector? $\endgroup$
    – Sycorax
    Commented Jun 29, 2021 at 19:28
  • 2
    $\begingroup$ Ah, so it's not that you're computing the error only using the hidden layers, but using all layers, hidden, input or output. I still think you should use a loss function of the type that I describe at the end: apply the regularization to the hidden layers, but compute the model loss using an appropriate loss. MAE for binary targets isn't a good loss because it penalizes all errors proportionally to misfit, instead of assigning much larger loss the further you are from the target. $\endgroup$
    – Sycorax
    Commented Jun 29, 2021 at 19:41
  • 1
    $\begingroup$ Yep, that's a standard language model. Given $k$ words, predict the $(k+1)$th word. Use a cross-entropy loss. $\endgroup$
    – Sycorax
    Commented Jun 29, 2021 at 19:50
  • 4
    $\begingroup$ (+1) It has not been mentioned but making a loss-function differentiable (preferably twice) will help the optimisers quite a bit. Smoother surface to explore / easier convergence. $\endgroup$
    – usεr11852
    Commented Jun 30, 2021 at 2:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.