JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Implementing the ReLU function in Python

Author:JIYIK Last Updated:2025/05/05 Views:

This tutorial will discuss the Relu function and how to implement it in Python.


ReLUfunction

ReluFunctions are the foundation of machine learning and are essential when using deep learning.

ReLUThe term is Rectified Linear Unitan acronym for and is used as an activation function in the case of artificial neural networks, which are the basis of deep learning. Python, as one of the programming languages ​​suitable for implementing machine learning and deep learning algorithms, has ReLUa scope for the use of function.

In simple mathematical terms, ReLUthe function can be defined as,

f(x) = max(0,x)

This function is linear in x and sets all negative values ​​to zero.


ReLUImplementing the function in Python

To implement the function in Python ReLU, we can define a new function and use the NumPy library.

The NumPy library makes it possible to work with matrices and arrays in Python, as they cannot be directly implemented in this programming language. maximum()The NumPy function from the NumPy library can be used in our newly created function definition to create a NumPy ReLUfunction.

The following code implements the function in Python ReLU.

import numpy as np


def relu1(a):
    return np.maximum(0, a)


print(relu1(-3))

The above code gives the following output:

0

In the code above, we deal with a single integer. However, ReLUthe functions we create can easily handle anything from single integers to NumPy arrays and similar objects.

When this function gets a number as input, the output will always be a number. The same rule is followed when we pass an array to this function.

The object type of the input is always the object type returned as output.

Interestingly, when working with arrays, we can use plotlythe library and even create graphs that describe ReLUwhat the functions do on arrays and similar objects.

To better explain ReLUhow the function works, we will now take an example of a simple array and complete the task at hand. In addition, we will also draw a graph and see ReLUthe actual action of the function on this array.

The following code uses the function on an array in Python ReLU.

import numpy as np
import plotly.express as px


def relu1(a):
    return np.maximum(0, a)


x1 = np.linspace(start=-5, stop=5, num=26)
print(x1)
x2 = relu1(x1)
print(x2)
px.line(x=x1, y=x2)

The above code gives the following output:

ReLU function output

Previous:Killing a Python process

Next: None

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Killing a Python process

Publish Date:2025/05/05 Views:152 Category:Python

When programming in Python, sometimes our program gets stuck in an infinite loop. In this case, we need to terminate the program manually. This article will discuss different ways to kill a Python process. Killing a Python process using a k

Get file extension in Python

Publish Date:2025/05/05 Views:63 Category:Python

This tutorial shows how to get the file extension from a file name in Python. os.path Extract extension from file using module in Python The Python module os.path pre-makes useful utility functions for manipulating operating system file pat

Read the first line of a file in Python

Publish Date:2025/05/05 Views:192 Category:Python

In Python, we have built-in functions to handle different file operations. A text file contains a sequence of strings where each line \n is terminated by a newline character. In this tutorial, we will learn how to read the first line of a t

Reading binary files in Python

Publish Date:2025/05/05 Views:119 Category:Python

The program or internal processor interprets the binary file. It contains bytes as content. When we read a binary file, an bytes object of type is returned. open() Read a binary file using the function in Python In Python, we have open() th

Writing bytes to a file in Python

Publish Date:2025/05/05 Views:193 Category:Python

In this tutorial, we will look at how to write bytes to a binary file in Python. Binary files contain strings of type bytes. When we read a binary file, an object of type bytes is returned. In Python, bytes are represented by hexadecimal nu

Python get file name without extension from path

Publish Date:2025/05/05 Views:81 Category:Python

This tutorial will demonstrate various ways to get the file name without extension from a file path in Python. Suppose our goal is to get the file name from a list of file paths that are in the form of strings, such as from the path Desktop

Calculate the time difference between two time strings in Python

Publish Date:2025/05/05 Views:182 Category:Python

Sometimes we have to deal with date and time related problems in programming. In Python, date and time are not data types themselves. Despite this, Python provides a large number of functions and libraries to help deal with such problems. O

Optional parameters in Python

Publish Date:2025/05/05 Views:151 Category:Python

In Python, there is something called default arguments. It is also called optional parameters or optional arguments in python. Parameters refer to the inputs of a function. Functions with multiple optional parameters in Python Whenever you

Writing floating point numbers to a file in Python

Publish Date:2025/05/05 Views:91 Category:Python

Python makes writing data to a file a seamless task. The data is written to the file in the form of strings. In this article, you will learn how to write float values ​​to a file in Python. Writing floating point numbers to a file in Py

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial