Python course with AI – Lesson 4: Recurrent Neural Network (RNN)

This article was originally published at:

Intro to how to use Recurrent Neural Network

In today's episode of the Python course with AI, we will use the Recurrent Neural Network, we will learn how to make predictions with AI, and we will predict the prices.

Last episodes you can find here:

Lesson 3 - How to create and train AI:

Python course with building a fintech investment AI – Lesson 3: How to build and train AI

Lesson 2 - How to find financial data and how to use Pandas:

Python course with building a fintech investment AI – Lesson 2: Pandas and getting financial data

Lesson 1 - Prepare the AI project:

Python course with building a fintech investment AI – Lesson 1: Start the project

In the fourth lesson of the AI course, we will use the AI that we created in the previous episode.

We will predict some first stock prices and will compare with the real ones, to have an idea if our AI is smart.

Let's start!

If you prefer video, here is the youtube version:

Prepare data for prediction

As the first step, we should create a function that will prepare data for the prediction.

Create a function named "preparePredictData" in the file, below function "trainModel".

Next, pass "stockPricesLastMonth", and "stockPricesTest" as params.

def preparePredictData(stockPricesLastMonth, stockPricesTest):

Take all data

As the second step of preparing data, we should concatenate all the data that we have into the one data frame.

We can use the pandas "concat" method for that.

Next, we need to create the variable named "predictInputs".

To this variable, we should assign all the items from the position that we will get by calculation, up to the end of the data frame.

The calculation is the length of the total data, minus length of the lastMonth data, minus 120(6 months).

dataset_total = pd.concat((stockPricesTest['Open'], stockPricesLastMonth['Open']), axis = 0)
predictInputs = dataset_total[len(dataset_total) - len(stockPricesLastMonth) - 120:].values

Reshape the data

In the third step of the data preparation, we should reshape our data by using "reshape" method.

That will give us the formatted list of the "predictInputs", and our data will be ready to be scaled later.

predictInputs = predictInputs.reshape(-1,1)

Scale data to fit this one from training and prepare testPredicts

The fourth step should be to take 20 items from the "predictInputs", and append them into the array named "testPredicts".

predictInputs = scaler.transform(predictInputs)
testPredicts = []

for i in range(120, 140):
    testPredicts.append(predictInputs[i-120:i, 0])

Make 3d array and reshape

As the last step of the data preparation, we need to do 3d array from those items and reshape them.

Next, we need to return "testPredicts" variable.

testPredicts = np.array(testPredicts)
testPredicts = np.reshape(testPredicts, (testPredicts.shape[0], testPredicts.shape[1], 1))

return testPredicts

Make prediction

Super, we are done with data preparation now!

We can create the logic that will predict the prices for us and use the Recurrent Neural Network.

After the prediction, we cannot forget to reinverse the data to the values that will be human-readable.

def getPrediction(dataToPredict):
  nextPrices = model.predict(dataToPredict)
  nextPrices = scaler.inverse_transform(nextPrices)
  return nextPrices

Create a graph to compare with real prices

If we have created the logic that can predict the prices, we would like to know if the AI is close to reality.

To do that, we predict the prices, for the last month, and we will compare them with the real ones.

We need to create a matplotlib graph.

Let's start by creating a function named "createGraph".

def createGraph(nextPrices, stockPricesLastMonth):

Separate real prices

Next, we need to separate the "Open" prices from all of the last month's data.

The "Open" column is the second one in the table.

So we should use "iloc" method, and take all the values from that column.

Let's take a look at the example below.

real = stockPricesLastMonth.iloc[:, 1:2].values

Create a graph

In this step, we can create a matplotlib graph.

We will use green color for the real prices, and orange color for the predicted ones.

plt.plot(real, color = 'green', label = 'Real prices')
plt.plot(nextPrices, color = 'orange', label = 'Prices from ai')


As we did in the previous lesson, we need to pack all of the logic into one function.

We will use the function called "predict" and put all of the functions that we built today.

def predict():
  stockPricesLastMonth, stockPricesTest = splitData(prices)
  dataToPredict = preparePredictData(stockPricesLastMonth, stockPricesTest)
  predicted = getPrediction(dataToPredict)
  createGraph(predicted, stockPricesLastMonth)

Start prediction

Finally, we can run the prediction!

I'm super excited we came into this moment, and finally, we will take a look at how smart our AI is.

Let's call the function named "predict".




Now, your project can not only get data, train AI but also predict the prices and show the graphs.

It's very important because you can compare prices with the real ones before you predict the future.

That will give you a chance to tune up the AI to get the best results.

Code repository for the Python course Lesson 4 is here:

Stay with us for the next episodes, where we will build the next awesome and interesting things together.

And don't forget to share the info about the AI course with your friends!

Thanks for reading,

Radek from Duomly

#python #machine-learning #data-science #web-development #sql #database

What is GEEK

Buddha Community

Python course with AI – Lesson 4: Recurrent Neural Network (RNN)
Ray  Patel

Ray Patel


top 30 Python Tips and Tricks for Beginners

Welcome to my Blog , In this article, you are going to learn the top 10 python tips and tricks.

1) swap two numbers.

2) Reversing a string in Python.

3) Create a single string from all the elements in list.

4) Chaining Of Comparison Operators.

5) Print The File Path Of Imported Modules.

6) Return Multiple Values From Functions.

7) Find The Most Frequent Value In A List.

8) Check The Memory Usage Of An Object.

#python #python hacks tricks #python learning tips #python programming tricks #python tips #python tips and tricks #python tips and tricks advanced #python tips and tricks for beginners #python tips tricks and techniques #python tutorial #tips and tricks in python #tips to learn python #top 30 python tips and tricks for beginners

Ray  Patel

Ray Patel


Lambda, Map, Filter functions in python

Welcome to my Blog, In this article, we will learn python lambda function, Map function, and filter function.

Lambda function in python: Lambda is a one line anonymous function and lambda takes any number of arguments but can only have one expression and python lambda syntax is

Syntax: x = lambda arguments : expression

Now i will show you some python lambda function examples:

#python #anonymous function python #filter function in python #lambda #lambda python 3 #map python #python filter #python filter lambda #python lambda #python lambda examples #python map

Marlon  Boyle

Marlon Boyle


Recurrent Neural Networks for Multilabel Text Classification Tasks

The purpose of this project is to build and evaluate Recurrent Neural Networks(RNNs) for sentence-level classification tasks. I evaluate three architectures: a two-layer Long Short-Term Memory Network(LSTM), a two-layer Bidirectional Long Short-Term Memory Network(BiLSTM), and a two-layer BiLSTM with a word-level attention layer. Although they do learn useful vector representation, BiLSTM with attention mechanism focuses on necessary tokens when learning text representation. To that end, I’m using the 2019 Google Jigsaw published dataset on Kaggle labeled “Jigsaw Unintended Bias in Toxicity Classification.” The dataset includes 1,804,874 user comments, with the toxicity level being between 0 and 1. The final models can be used for filtering online posts and comments, social media policing, and user education.


Recurrent Neural Networks Overview

RNNs are neural networks used for problems that require sequential data processing. For instance:

  • In a sentiment analysis task, a text’s sentiment can be inferred from a sequence of words or characters.
  • In a stock prediction task, current stock prices can be inferred from a sequence of past stock prices.

At each time step of the input sequence, RNNs compute the output yt and an internal state update ht using the input xt and the previous hidden-state ht-1. They then pass information about the current time step of the network to the next. The hidden-state ht summarizes the task-relevant aspect of the past sequence of the input up to t, allowing for information to persist over time.

Image for post

Recurrent Neural Network

Image for post

Recurrent Neural Network

During training, RNNs re-use the same weight matrices at each time step. Parameter sharing enables the network to generalize to different sequence lengths. The total loss is a sum of all losses at each time step, the gradients with respect to the weights are the sum of the gradients at each time step, and the parameters are updated to minimize the loss function.

Image for post

forward pass: compute the loss function

Image for post

Image for post

loss function

Image for post

Backward Pass: compute the gradients

Image for post

gradient equation

Although RNNs learn contextual representations of sequential data, they suffer from the exploding and vanishing gradient phenomena in long sequences. These problems occur due to the multiplicative gradient that can exponentially increase or decrease through time. RNNs commonly use three activation functions: RELU, Tanh, and Sigmoid. Because the gradient calculation also involves the gradient with respect to the non-linear activations, architectures that use a RELU activation can suffer from the exploding gradient problem. Architectures that use Tanh/Sigmoid can suffer from the vanishing gradient problem. Gradient clipping — limiting the gradient within a specific range — can be used to remedy the exploding gradient. However, for the vanishing gradient problem, a more complex recurrent unit with gates such as Gated Recurrent Unit (GRU) or Long Short-Term Memory (LSTM) can be used.

#ai #recurrent-neural-network #attention-network #machine-learning #neural-network

A Comparative Analysis of Recurrent Neural Networks

Recurrent neural networks, also known as RNNs, are a class of neural networks that allow previous outputs to be used as inputs while having hidden states. RNN models are mostly used in the fields of natural language processing and speech recognition.

The vanishing and exploding gradient phenomena are often encountered in the context of RNNs. The reason why they happen is that it is difficult to capture long term dependencies because of multiplicative gradient that can be exponentially decreasing/increasing with respect to the number of layers.

Gated Recurrent Unit (GRU) and Long Short-Term Memory units (LSTM) deal with the vanishing gradient problem encountered by traditional RNNs, with LSTM being a generalization of GRU.

1D Convolution_ layer_ creates a convolution kernel that is convolved with the layer input over a single spatial (or temporal) dimension to produce a tensor of outputs. It is very effective for deriving features from a fixed-length segment of the overall dataset. A 1D CNN works well for natural language processing (NLP).

DATASET: IMDb Movie Review

TensorFlow Datasets is a collection of datasets ready to use, with TensorFlow or other Python ML frameworks, such as Jax. All datasets are exposed as [](, enabling easy-to-use and high-performance input pipelines.


This is a dataset for binary sentiment classification containing substantially more data than previous benchmark datasets. It provides a set of 25,000 highly polar movie reviews for training, and 25,000 for testing.

Import Libraries

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

Load the Dataset

import tensorflow as tf
import tensorflow_datasets

imdb, info=tensorflow_datasets.load("imdb_reviews", with_info=True, as_supervised=True)

Image for post


Image for post

Training and Testing Data

train_data, test_data=imdb['train'], imdb['test']

for s,l in train_data:
for s,l in test_data:

Tokenization and Padding

from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
tokenizer= Tokenizer(num_words=vocab_size, oov_token=oov_tok)
padded=pad_sequences(sequences, maxlen=max_length, truncating=trunc_type)
testing_padded=pad_sequences(testing_sequences, maxlen=max_length)
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Embedding

Multi-layer Bidirectional LSTM

#imdb #convolutional-network #long-short-term-memory #recurrent-neural-network #gated-recurrent-unit #neural networks

Ananya Gupta


Python Roadmap To Become a Python Developer

In a universe of Java, Python is rapidly advancing into the graphs. Python engineers are highly sought after, and not all that high in gracefully. This implies they likewise get paid a ton. In case you’re understanding this, it presumably implies you’ve chosen or are choosing to begin a profession in Python and need to turn into a Python Developer. How to get an occupation as a Python Developer? This article will attempt to answer every one of your inquiries about the a Python Developer.

Before we figure out how to turn into a python designer, how about we investigate why you ought to learn Python.

Why Learn Python?

To just say, Python is at #3 in the TIOBE Index (for the fame of programming dialects) for 2019. Furthermore, in the rundown of top programming dialects for 2019 by the IEEE Spectrum, it is at #1. Python is incredible both for tenderfoots to programming and for experienced coders. In any case, what makes it far better is that Python is being favored for areas like Data Science, Machine Learning, and Artificial Intelligence. It has incredible logical capacities and different libraries designed for these areas, it is an exceptionally amazing universally useful language to learn Python? Presently, how about to Jumpstart Your Career With Python how to do it to find a new line of work as a Python Developer.

**How to Become a Python Developer? **

The following is the Python designer guide –

Choose to learn Python; choose why you need to do it.

Follow online instructional exercises. DataFlair has this extensive rundown of Python instructional exercises where you’ll discover everything at one spot. Continue rehearsing as you learn.

Peruse some great books on Python and join up with an online Python course. DataFlair just propelled its Certified Python Course to assist you with learning Python. Here, you’ll comprehend the ideas, unravel practicals and tasks, and even work on some energizing ventures.

**Continue perusing the official Python documentation for various builds. **

Bring a look into the source code of your preferred Python bundle to know how it functions.

Step by step instructions to turn into a python designer

Follow these means to turn into the following Python Developer –

1.What to Learn in Python?

Get familiar with the fundamentals. Find out about its history, language structure, establishment, and some fundamental builds like articulations, factors, and administrators.

Get some answers concerning the utilization of Python. Likewise, comprehend the contrasts between Python 2 and Python 3.

Find out about fundamental information structures like records, sets, and word references.

Comprehend significant ideas like dynamics and circles.

Figure out how to make a virtual domain.

Proceed onward to capacities and recursion.

Begin with object-arranged ideas like classes and strategies, and legacy and over-burdening.

Get some answers concerning modules and bundles, and get some involvement in like manner modules like os, namedtuple, and schedule.

Learn document taking care of. Find out about more intricate themes like generators and decorators, and shallow and profound duplicating.

Likewise, figure out how to produce and utilize arbitrary numbers and customary articulations.

Find out about more mind-boggling subjects like systems administration, XML preparation, and multiprocessing.

Figure out how to fabricate GUIs with Python.

Get some answers concerning special cases and how to deal with them.

Figure out how to utilize SciPy, NumPy, and Pandas.

Figure out how to investigate, unit-test, log, serialize, and get to the database.

**2.Things to ace Python **

**Systems **

You should now figure out how to chip away at a system. Python has some extremely incredible structures like Django, Flask, and CherryPy. You can start with Django, which is an extremely ground-breaking structure following the DRY (Don’t Repeat Yourself) guideline. It makes work simpler for you and deals with inconsequential things.

ORM Libraries

ORM represents Object Relational Mapping. This is an approach to question and control information from a database utilizing an item situated worldview. You can figure out how to utilize ORM libraries like SQLAlchemy and Django ORM. This is simpler and quicker than composing SQL.

**Front-End Technologies **

Innovations like HTML5, CSS3, and JavaScript/jQuery are not necessity to be a Python engineer. In any case, in the event that you can, attempt to increase a fundamental comprehension of these, and they will let you see how things work and what is conceivable. As a Python engineer, you may need to work with the front-end group.

**Adaptation Control **

Changes to code on numerous occasions by various individuals can eventually break it. You ought to learn GitHub and its straightforward terms like push, pull, fork, and submit on the off chance that you need to execute rendition control (you should).

3.Fabricate Projects in Python

You presently have enough aptitudes; assembling some close to home python ventures will give you certainty. You can likewise fabricate something to attempt to take care of a real issue you face. When you feel sufficiently sure, you would then be able to work for famous open-source ventures like Django.

**Who is a Good Python Developer? **

To be a decent Python designer, you will require something other than specialized information and the accompanying abilities which you get through online python certification training class:

**A critical thinking attitude **

Solid relational abilities You’ll have to impart venture necessities and highlights to your group. This will likewise assist you with composing better documentation.

Excitement to learn new instruments and libraries

Information on how things work inside

Strong Specialized Aptitudes

Since you comprehend what to realize and how to do it, prepare and take on this Python course to transform you.

#python online training #python online course #python course #python training #python certification course