{"id":36587,"date":"2024-11-01T09:49:47","date_gmt":"2024-11-01T09:49:47","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36587"},"modified":"2024-11-01T11:52:36","modified_gmt":"2024-11-01T11:52:36","slug":"deep-learning-pytorch-course-recurrent-neural-networks","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36587\/","title":{"rendered":"Deep Learning PyTorch Course, Recurrent Neural Networks"},"content":{"rendered":"<p><body><\/p>\n<h2>1. Introduction<\/h2>\n<p>\n        Deep learning is a branch of artificial intelligence that uses artificial neural networks to learn patterns from data and make predictions. In this lecture, we will take a closer look at the concept of Recurrent Neural Networks (RNNs) and how to implement RNN models using PyTorch.\n    <\/p>\n<h2>2. What is a Recurrent Neural Network?<\/h2>\n<p>\n        Recurrent Neural Networks (RNNs) are a type of neural network designed to process sequence data. While typical artificial neural networks have a fixed input size and process data at once, RNNs maintain an internal state that remembers past information and affects the current output. This is particularly useful in fields like Natural Language Processing (NLP).\n    <\/p>\n<h3>2.1 Structure of RNN<\/h3>\n<p>\n        The basic structure of an RNN is as follows. At each time step, the input \\( x_t \\) is processed along with the previous hidden state \\( h_{t-1} \\) to generate a new hidden state \\( h_t \\). This can be expressed with the following formula:\n    <\/p>\n<pre>\n    h_t = f(W_h * h_{t-1} + W_x * x_t)\n    <\/pre>\n<p>\n        Here, \\( f \\) is the activation function, \\( W_h \\) is the weight of the hidden state, and \\( W_x \\) is the weight of the input.\n    <\/p>\n<h3>2.2 Advantages and Disadvantages of RNN<\/h3>\n<p>\n        RNNs are strong at processing sequence data, but they exhibit challenges in learning from long sequences due to issues like vanishing gradients or exploding gradients. To overcome these problems, improved architectures like Long Short-Term Memory (LSTM) or Gated Recurrent Unit (GRU) are used.\n    <\/p>\n<h2>3. Implementing RNN Using PyTorch<\/h2>\n<p>\n        Now, let&#8217;s implement a basic RNN model using PyTorch. In this example, we will tackle a simple natural language processing problem, which is predicting the next word for each word in a sentence.\n    <\/p>\n<h3>3.1 Preparing the Data<\/h3>\n<p>\n        First, we will import the necessary libraries and prepare the data. For this example, we will use simple sentences.\n    <\/p>\n<pre>\n    import torch\n    import torch.nn as nn\n    import numpy as np\n    from sklearn.preprocessing import OneHotEncoder\n\n    # Data preparation\n    sentences = ['I ate rice', 'I like apples', 'I code']\n    words = set(' '.join(sentences).split())\n    word_to_index = {word: i for i, word in enumerate(words)}\n    index_to_word = {i: word for i, word in enumerate(words)}\n    <\/pre>\n<p>\n        The code above extracts words from the sentences and assigns an index to each word. Now, let\u2019s move forward to convert the words into one-hot encoding.\n    <\/p>\n<pre>\n    # One-hot encoding\n    ohe = OneHotEncoder(sparse=False)\n    X = []\n    y = []\n\n    for sentence in sentences:\n        words = sentence.split()\n        for i in range(len(words) - 1):\n            X.append(word_to_index[words[i]])\n            y.append(word_to_index[words[i + 1]])\n\n    X = np.array(X).reshape(-1, 1)\n    y = np.array(y).reshape(-1, 1)\n\n    X_onehot = ohe.fit_transform(X)\n    y_onehot = ohe.fit_transform(y)\n    <\/pre>\n<h3>3.2 Building the RNN Model<\/h3>\n<p>\n        Now let&#8217;s build the RNN model. In PyTorch, RNN can be implemented using the <code>nn.RNN<\/code> class.\n    <\/p>\n<pre>\n    class RNNModel(nn.Module):\n        def __init__(self, input_size, hidden_size, output_size):\n            super(RNNModel, self).__init__()\n            self.hidden_size = hidden_size\n            self.rnn = nn.RNN(input_size, hidden_size, batch_first=True)\n            self.fc = nn.Linear(hidden_size, output_size)\n\n        def forward(self, x):\n            h0 = torch.zeros(1, x.size(0), self.hidden_size)\n            out, _ = self.rnn(x, h0)\n            out = self.fc(out[:, -1, :])\n            return out\n    <\/pre>\n<h3>3.3 Training the Model<\/h3>\n<p>\n        After creating the model, we will set up the loss function and optimization method, and proceed with the training.\n    <\/p>\n<pre>\n    input_size = len(words)\n    hidden_size = 5\n    output_size = len(words)\n\n    model = RNNModel(input_size, hidden_size, output_size)\n    criterion = nn.CrossEntropyLoss()\n    optimizer = torch.optim.Adam(model.parameters(), lr=0.01)\n\n    num_epochs = 1000\n    for epoch in range(num_epochs):\n        model.train()\n        optimizer.zero_grad()\n\n        X_tensor = torch.Tensor(X_onehot).view(-1, 1, input_size)\n        y_tensor = torch.Tensor(y).long().view(-1)\n\n        outputs = model(X_tensor)\n        loss = criterion(outputs, y_tensor)\n        loss.backward()\n        optimizer.step()\n\n        if (epoch + 1) % 100 == 0:\n            print(f'Epoch [{epoch + 1}\/{num_epochs}], Loss: {loss.item():.4f}')\n    <\/pre>\n<h3>3.4 Evaluating the Model<\/h3>\n<p>\n        After the training is complete, we will evaluate the model. The following explains the process of predicting the next word for a new input.\n    <\/p>\n<pre>\n    def predict_next_word(model, current_word):\n        model.eval()\n        with torch.no_grad():\n            input_index = word_to_index[current_word]\n            input_onehot = ohe.transform([[input_index]])\n            input_tensor = torch.Tensor(input_onehot).view(-1, 1, input_size)\n            output = model(input_tensor)\n            next_word_index = torch.argmax(output).item()\n            return index_to_word[next_word_index]\n\n    # Prediction\n    next_word = predict_next_word(model, 'I')\n    print(f\"Next word prediction: {next_word}\")\n    <\/pre>\n<h2>4. Conclusion<\/h2>\n<p>\n        In this lecture, we explored the concept of Recurrent Neural Networks (RNNs) and how to implement a basic RNN model using PyTorch. RNNs are powerful tools for processing sequence data, but variations like LSTM or GRU may be required for long sequences.\n    <\/p>\n<h3>4.1 Future Directions for RNN<\/h3>\n<p>\n        RNNs are just the basic form, and recently, more advanced models like Transformer have gained attention in the field of natural language processing. To further advance to strong models, an understanding of various deep learning techniques and architectures is necessary.\n    <\/p>\n<h3>4.2 Additional Learning Resources<\/h3>\n<p>\n        If you want a deeper understanding of recurrent neural networks, the following resources are recommended:\n    <\/p>\n<ul>\n<li>Deep Learning Book: &#8220;Deep Learning&#8221; by Ian Goodfellow, Yoshua Bengio, and Aaron Courville<\/li>\n<li>PyTorch Official Documentation<\/li>\n<li>Deep Learning courses on Coursera<\/li>\n<\/ul>\n<h2>5. References<\/h2>\n<ul>\n<li>Goodfellow, I., Bengio, Y., &amp; Courville, A. (2016). Deep Learning. MIT Press.<\/li>\n<li>Pereyra, G., et al. (2017). Dealing with the curse of dimensionality in RNNs.<\/li>\n<li>Sepp Hochreiter, J\u00fcrgen Schmidhuber, (1997). Long Short-Term Memory.<\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Introduction Deep learning is a branch of artificial intelligence that uses artificial neural networks to learn patterns from data and make predictions. In this lecture, we will take a closer look at the concept of Recurrent Neural Networks (RNNs) and how to implement RNN models using PyTorch. 2. What is a Recurrent Neural Network? &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36587\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Deep Learning PyTorch Course, Recurrent Neural Networks&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[149],"tags":[],"class_list":["post-36587","post","type-post","status-publish","format-standard","hentry","category-pytorch-study"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Deep Learning PyTorch Course, Recurrent Neural Networks - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/36587\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Deep Learning PyTorch Course, Recurrent Neural Networks - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"1. Introduction Deep learning is a branch of artificial intelligence that uses artificial neural networks to learn patterns from data and make predictions. In this lecture, we will take a closer look at the concept of Recurrent Neural Networks (RNNs) and how to implement RNN models using PyTorch. 2. What is a Recurrent Neural Network? &hellip; \ub354 \ubcf4\uae30 &quot;Deep Learning PyTorch Course, Recurrent Neural Networks&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36587\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:49:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:52:36+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/36587\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36587\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Deep Learning PyTorch Course, Recurrent Neural Networks\",\"datePublished\":\"2024-11-01T09:49:47+00:00\",\"dateModified\":\"2024-11-01T11:52:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36587\/\"},\"wordCount\":547,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"PyTorch Study\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36587\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36587\/\",\"name\":\"Deep Learning PyTorch Course, Recurrent Neural Networks - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:49:47+00:00\",\"dateModified\":\"2024-11-01T11:52:36+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36587\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36587\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36587\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Deep Learning PyTorch Course, Recurrent Neural Networks\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Deep Learning PyTorch Course, Recurrent Neural Networks - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/36587\/","og_locale":"ko_KR","og_type":"article","og_title":"Deep Learning PyTorch Course, Recurrent Neural Networks - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"1. Introduction Deep learning is a branch of artificial intelligence that uses artificial neural networks to learn patterns from data and make predictions. In this lecture, we will take a closer look at the concept of Recurrent Neural Networks (RNNs) and how to implement RNN models using PyTorch. 2. What is a Recurrent Neural Network? &hellip; \ub354 \ubcf4\uae30 \"Deep Learning PyTorch Course, Recurrent Neural Networks\"","og_url":"https:\/\/atmokpo.com\/w\/36587\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:49:47+00:00","article_modified_time":"2024-11-01T11:52:36+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/36587\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36587\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Deep Learning PyTorch Course, Recurrent Neural Networks","datePublished":"2024-11-01T09:49:47+00:00","dateModified":"2024-11-01T11:52:36+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36587\/"},"wordCount":547,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["PyTorch Study"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36587\/","url":"https:\/\/atmokpo.com\/w\/36587\/","name":"Deep Learning PyTorch Course, Recurrent Neural Networks - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:49:47+00:00","dateModified":"2024-11-01T11:52:36+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36587\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36587\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36587\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Deep Learning PyTorch Course, Recurrent Neural Networks"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=36587"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36587\/revisions"}],"predecessor-version":[{"id":36588,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36587\/revisions\/36588"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}