umz21/wyk/12_RNN.ipynb

255 lines
34 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Uczenie maszynowe UMZ 2019/2020\n",
"### 2 czerwca 2020\n",
"# 12. Rekurencyjne sieci neuronowe"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## RNN _Recurrent Neural Network_\n",
"\n",
"## LSTM _Long Short Term Memory_"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"image/jpeg": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAUDBAcJCAgJCAgKCAgGBwgIBwcJCQYICAgICAgHCAkI\nCQgIChANCAkQCggJDiEODhERHx8TCAsiJCISJBwSExIBBQUFCAcIDwkJDx4NDw0SEhISEhUeEhIS\nEhISEhISEhISEhUVEhISEhISEhISEhISFRUSEhUSEhUSEhISEhUSEv/AABEIAWgB4AMBIgACEQED\nEQH/xAAcAAEAAgMBAQEAAAAAAAAAAAAABQYCBAcDAQj/xABKEAACAgEDAgIHBQUFBgQEBwABAgAD\nBAUREhMhBhQWIjFRU5LTBxUjQZEyYZXU1SQzQlW1VHF1gZShQ0RidDRSsfAIJWRlcpOl/8QAGgEB\nAAMBAQEAAAAAAAAAAAAAAAECAwQFBv/EADQRAAIAAggEBgEFAQADAQAAAAABAhEDBBIhMVFhkRVB\nUvATFHGBocEiMrHR4fFCcqLSBf/aAAwDAQACEQMRAD8A/GUREAREQBERAEREAREQBERAEREAREQB\nERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQ\nBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREAREQBERAEREARE\nQBERAEREAREQBERAEREAREQBERAEREAREQBERAPu0bS1+guX8Sn5rvpx6C5fxKfmu+nO3h1Y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/9FxINo2cCVOJkhluWgqcfI5C91DJQRw3FpUghPaQQ\ndp8TSM1ioXEyGNjWqgFGQS7Ub9ZVAT1mTY7gezY77TQ4j3D9I4j3D9JEn3/omsu9jc+7Mrjz8tdw\n6HmefRv4+X328xy47d
"text/html": [
"\n",
" <iframe\n",
" width=\"800\"\n",
" height=\"600\"\n",
" src=\"https://www.youtube.com/embed/WCUNPb-5EYI\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" ></iframe>\n",
" "
],
"text/plain": [
"<IPython.lib.display.YouTubeVideo at 0x7f9868e6cf50>"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import IPython\n",
"IPython.display.YouTubeVideo('WCUNPb-5EYI', width=800, height=600)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Rekurencyjna sieć neuronowa schemat\n",
"\n",
"<img style=\"margin: auto\" width=\"20%\" src=\"http://colah.github.io/posts/2015-08-Understanding-LSTMs/img/RNN-rolled.png\"/>"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Rekurencyjna sieć neuronowa schemat\n",
"\n",
"<img style=\"margin: auto\" width=\"80%\" src=\"http://colah.github.io/posts/2015-08-Understanding-LSTMs/img/RNN-unrolled.png\"/>"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Zależności długodystansowe (_long-distance dependencies_) w sieciach rekurencyjnych\n",
"\n",
"<img style=\"margin: auto\" width=\"60%\" src=\"http://colah.github.io/posts/2015-08-Understanding-LSTMs/img/RNN-longtermdependencies.png\"/>"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### RNN typy sekwencji\n",
"\n",
"<img style=\"margin: auto\" width=\"80%\" src=\"http://karpathy.github.io/assets/rnn/diags.jpeg\"/>"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Prosta sieć RNN schemat\n",
"\n",
"<img src=\"rnn.png\" style=\"margin: auto;\" width=\"100%\" />"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### LSTM schemat\n",
"\n",
"<img src=\"lstm.jpg\" style=\"margin: auto;\" width=\"80%\" />\n",
"\n",
"* Rekurencyjne sieci neuronowe znajduja zastosowanie w przetwarzaniu sekwencji, np. szeregów czasowych i tekstów.\n",
"* LSTM są rozwinięciem RNN, umożliwiają „zapamiętywanie” i „zapominanie”."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Co potrafią generować rekurencyjne sieci neuronowe?\n",
"\n",
"http://karpathy.github.io/2015/05/21/rnn-effectiveness/"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Generowanie tekstu za pomocą LSTM przykład\n",
"\n",
"https://github.com/keras-team/keras/blob/master/examples/lstm_text_generation.py"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Przewidywanie ciągów czasowych za pomocą LSTM przykład\n",
"\n",
"https://machinelearningmastery.com/time-series-forecasting-long-short-term-memory-network-python/"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## GRU _Gated Recurrent Unit_\n",
"\n",
"* Rodzaj rekurencyjnej sieci neuronowej wprwadzony w 2014 roku\n",
"* Ma prostszą budowę niż LSTM (2 bramki zamiast 3).\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### GRU schemat\n",
"\n",
"<img src=\"gru.png\" style=\"margin: auto;\" width=\"50%\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### GRU vs LSTM\n",
"* LSTM 3 bramki: wejścia (_input_), wyjścia (_output_) i zapomnienia (_forget_); GRU 2 bramki: resetu (_reset_) i aktualizacji (_update_). Bramka resetu pełni podwójną funkcję: zastępuje bramki wyjścia i zapomnienia.\n",
"* GRU i LSTM mają podobną skuteczność, ale GRU dzięki prostszej budowie bywa bardziej wydajna.\n",
"* LSTM sprawdza się lepiej w przetwarzaniu tekstu, ponieważ lepiej zapamiętuje zależności długosystansowe."
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
},
"livereveal": {
"start_slideshow_at": "selected",
"theme": "amu"
}
},
"nbformat": 4,
"nbformat_minor": 4
}