# What is the fundamental difference between CNN and RNN?

What is the fundamental difference between convolutional neural networks and recurrent neural networks? Where are they applied?

• Better do not think about RNN/CNN as different networks, but as different network capabilities: a network can be stateless or stateful ( as RNN, LSTM, deep ); a network can/cannot have spatial operators (as 2D convolution, like CNN); ... – pasaba por aqui Jun 5 '19 at 14:44

Basically, a CNN saves a set of weights and applies them spatially. For example, in a layer, I could have 32 sets of weights (also called feature maps). Each set of weights is a 3x3 block, meaning I have 3x3x32=288 weights for that layer. If you gave me an input image, for each 3x3 map, I slide it across all the pixels in the image, multiplying the regions together. I repeat this for all 32 feature maps, and pass the outputs on. So, I am learning a few weights that I can apply at a lot of locations.

For an RNN, it is a set of weights applied temporally (through time). An input comes in, and is multiplied by the weight. The networks saves an internal state and puts out some sort of output. Then, the next piece of data comes in, and is multiplied by the weight. However, the internal state that was created from the last piece of data also comes in and is multiplied by a different weight. Those are added and the output comes from an activation applied to the sum, times another weight. The internal state is updated, and the process repeats.

CNN's work really well for computer vision. At the low levels, you often want to find things like vertical and horizontal lines. Those kinds of things are going to be all over the images, so it makes sense to have weights that you can apply anywhere in the images.

RNN's are really good for natural language processing. You can imagine that the next word in a sentence will be highly influenced by the ones that came before it, so it makes sense to carry that internal state forward and have a small set of weights that can apply to any input.

However, there are many more applications. In addition, CNN's have performed well on NLP tasks. There are also more advanced versions of RNN's called LSTM's that you could check out.

For an explanation of CNN's, go to the Stanford CS231n course. Especially check out lecture 5. There are full class videos on YouTube.

For an explanation of RNN's, go here.

• IMHO, this is a quite confusing explanation. – nbro May 13 '19 at 21:46

Recurrent neural networks (RNNs) are artificial neural networks (ANNs) that have one or more recurrent (or cyclic) connections, as opposed to just having feed-forward connections, like a feed-forward neural network (FFNN).

These cyclic connections are used to keep track of temporal relations or dependencies between the elements of a sequence. Hence, RNNs are suited for sequence prediction or related tasks.

In the picture below, you can observe an RNN on the left (that contains only one hidden unit) that is equivalent to the RNN on the right, which is its "unfolded" version. For example, we can observe that $$\bf h_1$$ (the hidden unit at time step $$t=1$$) receives both an input $$\bf x_1$$ and the value of the hidden unit at the previous time step, that is, $$\bf h_0$$.

The cyclic connections (or the weights of the cyclic edges), like the feed-forward connections, are learned using an optimisation algorithm (like gradient descent) often combined with back-propagation (which is used to compute the gradient of the loss function).

Convolutional neural networks (CNNs) are ANNs that perform one or more convolution (or cross-correlation) operations (often followed by a down-sampling operation).

The convolution is an operation that takes two functions, $$\bf f$$ and $$\bf h$$, as input and produces a third function, $$\bf g = f \circledast h$$, where the symbol $$\circledast$$ denotes the convolution operation. In the context of CNNs, the input function $$\bf f$$ can e.g. be an image (which can be thought of as a function from 2D coordinates to RGB or grayscale values). The other function $$\bf h$$ is called the "kernel" (or filter), which can be thought of as (small and square) matrix (which contains the output of the function $$\bf h$$). $$\bf f$$ can also be thought of as a (big) matrix (which contains, for each cell, e.g. its grayscale value).

In the context of CNNs, the convolution operation can be thought of as dot product between the kernel $$\bf h$$ (a matrix) and several parts of the input (a matrix).

In the picture below, we perform an element-wise multiplication between the kernel $$\bf h$$ and part of the input $$\bf h$$, then we sum the elements of the resulting matrix, and that is the value of the convolution operation for that specific part of the input.

To be more concrete, in the picture above, we are performing the following operation

\begin{align} \sum_{ij} \left( \begin{bmatrix} 1 & 0 & 0\\ 1 & 1 & 0\\ 1 & 1 & 1 \end{bmatrix} \otimes \begin{bmatrix} 1 & 0 & 1\\ 0 & 1 & 0\\ 1 & 0 & 1 \end{bmatrix} \right) = \sum_{ij} \begin{bmatrix} 1 & 0 & 0\\ 0 & 1 & 0\\ 1 & 0 & 1 \end{bmatrix} = 4 \end{align}

where $$\otimes$$ is the element-wise multiplication and the summation $$\sum_{ij}$$ is over all rows $$i$$ and columns $$j$$ (of the matrices).

To compute all elements of $$\bf g$$, we can think of the kernel $$\bf h$$ as being slided over the matrix $$\bf f$$.

In general, the kernel function $$\bf h$$ can be fixed. However, in the context of CNNs, the kernel $$\bf h$$ represents the learnable parameters of the CNN: in other words, during the training procedure (using e.g. gradient descent and back-propagation), this kernel $$\bf h$$ (which thus can be thought of as a matrix of weights) changes.

In the context of CNNs, there is often more than one kernel: in other words, it is often the case that a sequence of kernels $$\bf h_1, h_2, \dots, h_k$$ is applied to $$\bf f$$ to produce a sequence of convolutions $$\bf g_1, g_2, \dots, g_k$$. Each kernel $$\bf h_i$$ is used to "detect different features of the input", so these kernels are different from each other.

A down-sampling operation is an operation that reduces the input size while attempting to maintain as much information as possible. For example, if the input size is a $$2 \times 2$$ matrix $$\bf f = \begin{bmatrix} 1 & 2 \\ 3 & 0 \end{bmatrix}$$, a common down-sampling operation is called the max-pooling, which, in the case of $$\bf f$$, returns $$3$$ (the maximum element of $$\bf f$$).

CNNs are particularly suited to deal with high-dimensional inputs (e.g. images), because, compared to FFNNs, they use a smaller number of learnable parameters (which, in the context of CNNs, are the kernels). So, they are often used to e.g. classify images.

What is the fundamental difference between RNNs and CNNs? RNNs have recurrent connections while CNNs do not necessarily have them. The fundamental operation of a CNN is the convolution operation, which is not present in a standard RNN.

CNN vs RNN

• A CNN will learn to recognize patterns across space while RNN is useful for solving temporal data problems.
• CNNs have become the go-to method for solving any image data challenge while RNN is used for ideal for text and speech analysis.
• In a very general way, a CNN will learn to recognize components of an image (e.g., lines, curves, etc.) and then learn to combine these components to recognize larger structures (e.g., faces, objects, etc.) while an RNN will similarly learn to recognize patterns across time. So a RNN that is trained to convert speech to text should learn first the low level features like characters, then higher level features like phonemes and then word detection in audio clip.

CNN
A convolutional network (ConvNet) is made up of layers. In a convolutional network (ConvNet), there are basically three types of layers:

• Convolution layer
• Pooling layer
• Fully connected layer

Of these, the convolution layer applies convolution operation on the input 3D tensor. Different filters extract different kinds of features from an image. The below GIF illustrates this point really well:

Here the filter is the green 3x3 matrix while the image is the blue 7x7 matrix.

Many such layers passes through filters in CNN to give an output layer that can again be a NN Fully connected layer or a 3D tensor.

For example, in the above example, the input image passes through convolutional layer, then pooling layer, then convolutional layer, pooling layer, then the 3D tensor is flattened like a Neural Network 1D layer, then passed to a fully connected layer and finally a softmax layer. This makes a CNN.

RNN
Recurrent Neural Network(RNN) are a type of Neural Network where the output from previous step are fed as input to the current step.

Here, $$x_{t-1}$$, , $$x_{t}$$ and $$x_{t+1}$$ are the values of inputs data that occur at a specific time steps and are fed into the RNN that goes through the hidden layers namely $$h_{t-1}$$, , $$h_{t}$$ and $$h_{t+1}$$ which further produces output $$o_{t-1}$$, , $$o_{t}$$ and $$o_{t+1}$$ respectively.

On a basic level, an RNN is a neural network whose next state depends on its past state(s), while a CNN is a neural network that does dimensionality reduction (make large data smaller while preserving information) via convolution. See this for more info on convolutions

• This is misleading. CNNs are not strictly used for dimensionality reduction. Furthermore, it is the down-sampling operation that reduces the dimensions of the input (not necessarily the convolution). – nbro May 13 '19 at 21:58

In the case of applying both to natural language, CNN's are good at extracting local and position-invariant features but it does not capture long range semantic dependencies. It just consider local key-phrasses.

So when the result is determined by the entire sentence or a long-range semantic dependency CNN is not effective as shown in this paper where the authors compared both architechrures on NLP takss.

This can be extended for general case.