[DL] Is CNN better than RNN for time series data?
본문 바로가기
AI/ML&DL

[DL] Is CNN better than RNN for time series data?

2022. 8. 23.

 1. Neural Network Architecture

- CNN (Convolutional Neural Networks) : (이미지와 같은) spatial data 기반 
- RNN (Recurrent Neural Networks) : (텍스트나 time series data와 같은) sequential data 기반 

 

  흔히 input data type이 이미지나 비디오면 Neural Network Architecture 중 CNN (Convolution Neural Networks), 텍스트와 같은 sequential data (순차 데이터)이면 RNN (Recurrent Neural Networks)이 적합하다고 한다. deep learning을 공부할 때도 이미지나 비디오 데이터이면 CNN이고, 텍스트, 또는 날씨 정보와 같은 time  series data (또는 time-stamped data;시계열 데이터)이면 RNN을 사용하는 것이 유용하다고 배우는데, 그 이유는 CNN는 각 pixel의 주변 정보를 처리하는 Convolution 연산을 사용하고, RNN은 time series data에서의 시간 정보를 기억할 수 있기 때문이다. 

그런데, 정말로 time series data에서 RNN이 CNN보다 유용할까? 


Q) What is time series data?

A) Time series data, also referred to as time-stamped data, is a sequence of data points indexed in time order. Time-stamped is data collected at different points in time. 
ex) Electrical activity in the brain, Rainfall measurements, Stock prices, Number of sunspots, Annual retail sales, Monthly subscribers, Heartbeats per minute

Reference : What is Time Series Data? | Definition, Examples, Types & Uses (influxdata.com)

 

2. CNN in time seires data

2-1. When to use CNN?

Conv1D : 시간 차원에서의 Convolution

 CNN에서 Convolution은 일반적으로 image 데이터와 같은 2D 데이터, 공간 데이터에 활용되고 있지만, 1D sequence 데이터에 대한 Convolution 연산(실제로는 Convolution 연산이 아닌 Correlation 연산을 수행하지만) 도 당연히 수행 가능하다. 그 때문에 텍스트나 time series data과 같은 data type에도 CNN을 사용할 수 있다. (관련 논문이나 연구를 살펴보면 성능이 입증되었다는 것을 알 수 있음 https://journalofbigdata.springeropen.com/articles/10.1186/s40537-022-00599-y)  spatial information을 추출하는 것이 아닌 1D Convolution을 통해 시간 순서에 따라 정보를 추출하는 것이다. 

 

Use CNN For :

  • Image data
  • Classification Prediction Problems
  • Regression Prediction Problems

Try CNN On :

  ( One-to-One )

  • Text data
  • Time series data
  • Sequence Input data

2-2. Why should we use CNN (not RNN) for time series data?

 1. CNN이 RNN보다 계산 비용이 저렴하다.

순서대로 Complexity per Layer / Sequential Operations / Length of Maximum Path

    CNN은 고정된 Input을 주입하고, 고정된 Output을 출력하는 반면, RNN은 임의의 Input과 Output을 다루므로 계산 시간이 더 오래 걸릴 수밖에 없다. 또한, CNN은 RNN보다 sequential operation (순차 계산)이 적어 계산 효율성이 더 높은 편이다. 같은 계산 비용으로 더 긴 history를 track할 수 있다.

 

 2. CNN은 기록이 완전하다는 가정 (매 data point마다 timestep이 존재) 이 필요하지 않다.

    RNN은 time series의 '모든' point는 '모든'  이전 timestep에 의존한다는 가정이 필요하다. 이러한 RNN과 달리, CNN은 time window 내에서 pattern을 학습하므로 해당 가정이 필요하지 않다. (time dependence가 RNN보다 떨어진다) 즉, 시간 정보가 누락된 데이터인 경우 CNN을 사용하는 것이 더 좋다. 예를 들어, 문장에서 단어를 추측하는 데에 있어 CNN은 문장의 전체 텍스트를 알 필요 없다는 것이다. 

 

 3. CNN은 앞을 예측할 수 있다.

    RNN은 예측해야하는 timestep 이전의 데이터만 학습할 수 있지만, CNN은 앞을 예측할 수 있다.

 

 4. RNN에 비해 CNN의 연구가 활발하게 수행되고 있다.

  (일반적으로 time series보다 이미지 분류/예측이 더 까다롭기 때문에 CNN 연구가 더 활발한 편이다.)

  time series data 예측, 예를 들어 주식 가격 예측 같은 경우 기술적인 문제보다는 우리가 알 수 없는 변수 / 데이터가 불충분 등의 문제로 예측이 어려운 것이다. 

 

2-3. When to use RNN?

Task 1 : Sentiment Analysis (감정 분석)  ex) 영화 리뷰가 긍정 / 부정인지 분류

Task 2 : Machine Translation (기계 번역) ex) 영어 → 한국어로 번역

1D Convolutions

  Task 1 (Sentiment Analysis)인 경우 CNN을 활용하는 것(1D Convolutions; Temporal Convolutions)이 좋지만, Taks 2 (Machine Translation)에서는 RNN을 사용하는 것이 옳다. CNN이 이미지에 주요한 feature를 잘 추출하는 것과 같이 텍스트에서도 유의미한 feature를 잘 추출할 수 있고, 단어 수준의 간단한 NLP Task인 경우 RNN보다 CNN의 성능이 좋기 때문이다. (한 단어에서의 의미를 더 잘 추출하기 때문이다.) 1D Convolution,  CNN은 convolution window 내에 local pattern을 식별할 수 있기 때문에 특정 지역에 pattern이 있는 새소리를 분류할 때도 CNN을 사용하는 것이 좋다. (CNN은 local pattern을 효율적으로 학습할 수 있기 때문이다.)

 

+) Character-level Convolutional Networks for Text Classifcation 

(https://proceedings.neurips.cc/paper/2015/file/250cf8b51c773f3f8dc8b4be867a9a02-Paper.pdf )

 

[자연어처리][paper review] Character-level Convolutional Networks for Text Classification (tistory.com)

 

[자연어처리][paper review] Character-level Convolutional Networks for Text Classification

CNN CNN은 컴퓨터 비전 분야를 위해 개발되었으며, 대중적으로 사용되고 있는 가장 보편화된 이미지 처리 알고리즘입니다. CNN은 가중치를 갖는 필터를 사용하기 때문에 개별 픽셀에 대한 가중치

supkoon.tistory.com

srviest/char-cnn-text-classification-pytorch: Character-level Convolutional Neural Networks for text classification in PyTorch (github.com)

 

GitHub - srviest/char-cnn-text-classification-pytorch: Character-level Convolutional Neural Networks for text classification in

Character-level Convolutional Neural Networks for text classification in PyTorch - GitHub - srviest/char-cnn-text-classification-pytorch: Character-level Convolutional Neural Networks for text clas...

github.com

  반면, RNN은 전체 sequence of words가 있을 때 유용하다. 단어들 간의 관계, 순서를 CNN보다 잘 기억하기 때문이다. Taks 2 (Machine Translation) Many-to-Many 유형으로, 임의의 Input / Output을 다루고, 많은 데이터가 필요하므로 RNN을 사용하는 것이 좋다. 이외에도 RNN은 ChatBot / NLP필기 인식 등의 분야에 유용하다.

 

Use RNN For :

  Sequence Prediction Problems

  ( One-to-Many / Many-to-One / Many-to-Many; Seq2Seq)

  • Text data
  • Speech data
  • Classification Prediction Problems
  • Regression Prediction Problems
  • Generative Models

3. 결론

 일반적으로 CNN의 용도는 이미지/비디오, RNN의 용도는 텍스트/time series data라고 보는 통념을 그대로 좇기보다는 항상 모델의 다양한 용도 (time series data에서 사용되는 CNN와 같은)를 생각하는 태도를 지녀야 할 것 같다. 자신이 수행해야 하는 Task와 Input data type에 제일 적합한 모델을 선택하기 위해서 말이다.

 

 

이번 블로그 글의 주제를 제시해주신 분께 고맙습니다.

Reference

https://machinelearningmastery.com/how-to-develop-convolutional-neural-network-models-for-time-series-forecasting/

 

How to Develop Convolutional Neural Network Models for Time Series Forecasting

Convolutional Neural Network models, or CNNs for short, can be applied to time series forecasting. There are many types of CNN models that can be used for each specific type of time series forecasting problem. In this tutorial, you will discover how to dev

machinelearningmastery.com

https://machinelearningmastery.com/when-to-use-mlp-cnn-and-rnn-neural-networks/

 

When to Use MLP, CNN, and RNN Neural Networks

What neural network is appropriate for your predictive modeling problem? It can be difficult for a beginner to the field of deep learning to know what type of network to use. There are so many types of networks to choose from and new methods being publishe

machinelearningmastery.com

https://www.projectpro.io/article/rnn-vs-cnn-the-difference/491

 

CNN vs RNN- Choose the Right Neural Network for Your Project

Confused making a decision on which neural network to choose for your next deep learning project. RNN vs CNN blog has answers to all your questions.

www.projectpro.io

https://www.techtarget.com/searchenterpriseai/feature/CNN-vs-RNN-How-they-differ-and-where-they-overlap

 

CNN vs. RNN: How are they different?

Convolutional neural networks and recurrent neural nets underlie many of the AI apps driving business value. Learn what they are, how they work, how they differ and where they overlap in this primer on ANNs, CNNs vs. RNNs.

www.techtarget.com

https://www.quora.com/What-is-the-reason-for-applying-convolutional-neural-nets-to-time-series-instead-using-recurrent-neural-nets

 

What is the reason for applying convolutional neural nets to time series instead using recurrent neural nets?

Answer (1 of 5): Here is one example in which a CNN will be a good choice. You have some timeseries and want to make a prediction at each datapoint. You have the entire timeseries when making inference. An RNN will only account for past datapoints, but a C

www.quora.com

https://www.macnica.co.jp/business/ai_iot/columns/135112/#:~:text=CNNs%20are%20computationally%20cheaper%20than,patterns%20within%20the%20time%20window.

 

Convolutional Neural Network (CNN) for Time Series Classification - スマートファクトリー - マクニカ

 

www.macnica.co.jp

댓글