39
$\begingroup$

I've recently become interested in LSTMs and I was surprised to learn that the weights are shared across time.

  • I know that if you share the weights across time, then your input time sequences can be a variable length.

  • With shared weights you have many fewer parameters to train.

From my understanding, the reason one would turn to an LSTM vs. some other learning method is because you believe there is some sort of temporal/sequential structure/dependence in your data that you would like to learn. If you sacrifice the variable length ‘luxury’, and accept long computation time, wouldn’t an RNN/LSTM without shared weights (i.e. for every time step you have different weights) perform way better or is there something I’m missing?

$\endgroup$

5 Answers 5

43
$\begingroup$

The accepted answer focuses on the practical side of the question: it would require a lot of resources, if there parameters are not shared. However, the decision to share parameters in an RNN has been made when any serious computation was a problem (1980s according to wiki), so I believe it wasn't the main argument (though still valid).

There are pure theoretical reasons for parameter sharing:

  • It helps in applying the model to examples of different lengths. While reading a sequence, if RNN model uses different parameters for each step during training, it won't generalize to unseen sequences of different lengths.

  • Oftentimes, the sequences operate according to the same rules across the sequence. For instance, in NLP:

                                                     "On Monday it was snowing"

                                                     "It was snowing on Monday"

...these two sentences mean the same thing, though the details are in different parts of the sequence. Parameter sharing reflects the fact that we are performing the same task at each step, as a result, we don't have to relearn the rules at each point in the sentence.

LSTM is no different in this sense, hence it uses shared parameters as well.

$\endgroup$
3
  • 5
    $\begingroup$ This is by far the more important reason than the accepted answer! $\endgroup$
    – jlh
    Commented Jan 4, 2019 at 9:17
  • 2
    $\begingroup$ I believe my answer has been mischaracterized here. I did say that more computational resources would be required without weight sharing, but this wasn't intended as the main point. In particular, I also wrote that a model without shared weights would be far more flexible and thus more prone to overfitting. Sharing weights over time is a way to overcome this. As rightly pointed out here, this strategy corresponds to the 'prior' that the same rules apply at each timestep. So, the two answers are not in disagreement. $\endgroup$
    – user20160
    Commented Jul 2, 2019 at 23:15
  • $\begingroup$ On Monday it was snowing . But how about " I like Ice cream with hot chocolate on it " justify that we are learning the same thing ? Similar kind of sentences with same syntax and semantics can have shared weights (agreeable ) but how can sentences that differ in semantics and syntax have same weights ? Wont one sentence(drastically different from the previous sentence) adversely alter the weights learned for the previous sentence ? $\endgroup$ Commented Jan 16, 2023 at 9:33
16
$\begingroup$

The 'shared weights' perspective comes from thinking about RNNs as feedforward networks unrolled across time. If the weights were different at each moment in time, this would just be a feedforward network. But, I suppose another way to think about it would be as an RNN whose weights are a time-varying function (and that could let you keep the ability to process variable length sequences).

If you did this, the number of parameters would grow linearly with the number of time steps. That would be a big explosion of parameters for sequences of any appreciable length. It would indeed make the network more powerful, if you had the massive computational resources to run it and the massive data to constrain it. For long sequences, it would probably be computationally infeasible and you'd get overfitting. In fact, people usually go in the opposite direction by running truncated backpropagation through time, which only unrolls the network for some short period of time, rather than over the entire sequence. This is done for computational feasibility. Interestingly, RNNs can still learn temporal structure that extends beyond the truncation length, because the recurrent units can store memory from before.

$\endgroup$
1
  • 1
    $\begingroup$ If you don't share weights, you still have the cell state that persists across time. An unrolled LSTM with unique time weights would look like a feedforward net where each 'layer' would represent a time slice, but each 'layer' would have incoming cell state information. It would resemble a feedforward, but with the addition of cell state. $\endgroup$
    – beeCwright
    Commented Feb 22, 2017 at 17:51
1
$\begingroup$

I think since the RNNs with hidden-to-hidden recurrences (and time shared weights) are equivalent to Universal Turing Machines, letting them have different weights for different time steps does not make them more powerful.

$\endgroup$
4
  • $\begingroup$ Could you elaborate on what you mean by "powerful"? The reference to Turing Machines suggests what you have in mind may be completely different than what is meant in statistics. $\endgroup$
    – whuber
    Commented Feb 4, 2017 at 17:27
  • 1
    $\begingroup$ The RNNs are used to process sequence of data. One of their commonest types gets a sequence as input and produces another sequence as output (such as language translation systems). I say that an RNN model family M1 is more powerful than another RNN model family M2, if for a problem (such as mapping a set of input sequences to a set of output sequences) there is some model m1 in M1 where can solve this problem but there is no model in M2 where can solve that problem. $\endgroup$
    – Hossein
    Commented Feb 4, 2017 at 20:21
  • $\begingroup$ This is a fascinating comment, and it's nicely complementary to the other explanations on this thread. Do you have a reference, or could you explain how this works? $\endgroup$ Commented Dec 27, 2022 at 16:13
  • $\begingroup$ It would be really nice to see even a length-3 example where I give you an arbitrary RNN with different weights for 0 to 1 and 1 to 2 and you give me back an RNN that imitates it but using the same weights for 0 to 1 and 1 to 2. I suspect the imitator would need to be deeper or wider per-layer than the original, which makes this less of a free lunch and more of a tradeoff about adding complexity within vs between layers. $\endgroup$ Commented Dec 27, 2022 at 16:25
0
$\begingroup$

I am trying hard to visualize how weight sharing combined with recurrence and combined with word embeddings behaves in a high-dimensional space.

Taking the example from @Maxim and visualizing a network that suggests the next word in the sequence: "On Monday it was" when accumulated using recurrence will be a point in a high dimensional space, and thanks to word embeddings, "On Tuesday it was" will be in the same manifold. Given this accumulated weight as an input to a downstream fully connected layer with high memory capacity, it will learn to map to things like cold, snowing, etc. There may be other stored mappings like hectic, slow, obvious, etc. This may be learnt by one unit of the layer. Another unit may have learnt to map the high dimensional vector formed from the accumulated weight of "It was snowing on" to vectors like christmas, Monday, the, etc. This is about hidden-hidden weights and hidden-output weights. About input-hidden weights, although the weights are shared, the units of the layer that they lead to will be activated for different aspects of a sentence (people and places, stop words, etc), making them position (time) agnostic.

$\endgroup$
0
$\begingroup$

RNN is a time based neural network.. at the end of time steps ( length of the input) it forms a vector which represents a thought preserving sequence information across the time. Thinking of thought vector like some sort of figure or object might help, which gets it's proper shape (depending on the input sequence) through time steps depending on the inputs it see's each time. The weight matrices are initialized randomly first, If we take example as predicting the next letter using RNN, when we send the first letter and the network predicts the next letter by assigning probabilities to each possible letters. we can update the weights using the gradients in that timestep. same goes for all the letters until the word ends. At the end the weights gets updated in such a way that, it would increase the confidence ( probability ) of finding the right word which can be achieved through backpropagation. This Training process continues for large number of data, thus tuning the weight parameters in such a way that given the sequence seen so far and this is the current state then this particular letter/ word ( in case of machine translations ) has the high probability of occurring. Weight Sharing across the time stamps thus helps in understanding the sequence as well as in applied point of view reduces the training time.

$\endgroup$

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.