Theory

## Highway Networks

This week I implemented highway networks to get an intuition for how they work. Highway networks, inspired by LSTMs, are a method of constructing networks with hundreds, even thousands, of layers. Let’s see how we construct them using TensorFlow.

## Implementation

For comparison, let’s start with a standard fully-connected (or “dense”) layer. We need a weight matrix and a bias vector then we’ll compute the following for the layer output:

Here’s what a dense layer looks like as a graph in TensorBoard:

For the highway layer what we want are two “gates” that control the flow of information. The “transform” gate controls how much of the activation we pass through and the “carry” gate controls how much of the unmodified input we pass through. Otherwise, the layer largely resembles a dense layer with a few additions:

• An extra set of weights and biases to be learned for the gates.
• The transform gate operation (T).
• The carry gate operation (C or just 1 - T).
• The layer output (y) with the new gates.

What happens is that when the transform gate is 1, we pass through our activation (H) and suppress the carry gate (since it will be 0). When the carry gate is 1, we pass through the unmodified input (x), while the activation is suppressed.

Here’s what the highway layer graph looks in TensorBoard:

Using a highway layer in a network is also straightforward. One detail to keep in mind is that consecutive highway layers must be the same size but you can use fully-connected layers to change dimensionality. This becomes especially complicated in convolutional layers where each layer can change the output dimensions. We can use padding (‘SAME’) to maintain each layers dimensionality.

Otherwise, by simply using hyperparameters from the TensorFlow docs (i.e. no hyperparameter search) the fully-connected highway network performed much better than a fully-connected network. Using MNIST as my simple trial:

• 20 fully-connected layers fail to achieve more than 15% accuracy.
• 18 highway layers (with two fully-connected layers to transform the input and output) achieves ~95% accuracy. Which is also much better than a shallow network which only reaches 91%.

Now that we have a highway network, I wanted to answer a few questions that came up for me while reading the paper. For instance, how deep will the network converge? The paper briefly mentions 1000 layers:

In pilot experiments, SGD did not stall for networks with more than 1000 layers. (2.2)

### Can we train with 1000 layers on MNIST?

Yes, also reaching around 95% accuracy. Try it out with a carry bias around -20.0 for MNIST (from the paper the network will only utilize ~15 layers anyway). The network can probably even go deeper since the it’s just learning to carry the last 980 layers or so. We can’t do much useful at or past 1000 layers so that seems sufficient for now.

### What happens if you set very low or very high carry biases?

In either extreme the network simply fails to converge in a reasonable amount of time. In the case of low biases (more positive), the network starts as if the carry gates aren’t present at all. In the case of high biases (more negative), we’re putting more emphasis on carrying and the network can take a long time to overcome that. Otherwise, the biases don’t seem to need to be exact, at least on this simple example. When in doubt start with high biases (more negative) since it’s easier to learn to overcome carrying than without carry gates (which is just a plain network).

## Conclusion

Overall I was happy with how easy highway networks were to implement. They’re fully differentiable with only a single additional hyperparameter for the initial carry bias. One downside is that highway layers do require additional parameters for the transform weights and biases. However, since we can go deeper, the layers do not need to be as wide which can compensate.

Here are the complete notebooks if you want to play with the code: fully-connected highway repo and convolutional highway repo.

#### TensorFlow on Kubernetes

While GPUs are a staple of deep learning, deploying on GPUs makes everything more complicated,…

#### TD-Gammon

Before DeepMind built AlphaGo there was TD-Gammon, the first algorithm to reach an expert level of…

#### An LSTM Odyssey

Discusses the paper LSTM: A Search Space Odyssey. We implemented each of the variants in TensorFlow…
{"ft_lang_label":"__label__en","ft_lang_prob":0.92520666,"math_prob":0.82628125,"size":4064,"snap":"2020-45-2020-50","text_gpt3_token_len":858,"char_repetition_ratio":0.13940887,"word_repetition_ratio":0.017569546,"special_character_ratio":0.21555118,"punctuation_ratio":0.0927835,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9539068,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-11-01T00:15:21Z\",\"WARC-Record-ID\":\"<urn:uuid:63bdc7b6-3fb6-4172-8f38-b44d93adb511>\",\"Content-Length\":\"18205\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ffd1fd5e-f9ff-417a-8573-875ce3f8b83f>\",\"WARC-Concurrent-To\":\"<urn:uuid:f3131eb1-2658-4dbe-9f52-054197b2c0f7>\",\"WARC-IP-Address\":\"104.27.174.68\",\"WARC-Target-URI\":\"https://fomoro.com/research/article/highway-networks-with-tensorflow\",\"WARC-Payload-Digest\":\"sha1:DKCNTYYLM4UMHPY7S4TTSC7RS3E4FGKK\",\"WARC-Block-Digest\":\"sha1:PNWBCJSVLXHWRS7HWSYCNDDBLYBAKG65\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107922746.99_warc_CC-MAIN-20201101001251-20201101031251-00223.warc.gz\"}"}