search
HomeBackend DevelopmentPython TutorialNeural network examples in Python

Python has always been widely used and loved for its simple, flexible syntax and powerful ecosystem and libraries, including in fields such as scientific computing and machine learning. Neural networks play a vital role in the field of machine learning and can be used in many fields such as computer vision, natural language processing, and recommendation systems. This article will introduce neural networks in Python and give some examples.

What is a neural network

Neural network is a deep learning model that has the characteristics of simulating the nervous system of animals. A neural network is composed of multiple neurons. Each neuron is equivalent to a function. Its input is the output from other neurons, which is processed by an activation function to generate an output. Neural networks utilize the backpropagation algorithm to continuously adjust weights and biases, allowing the model to better fit the data and make predictions or classifications.

TensorFlow

TensorFlow is a popular deep learning framework launched by Google for building neural networks and other machine learning algorithms. Originally developed for internal Google researchers, TensorFlow quickly became one of the most popular deep learning frameworks after being open sourced.

In TensorFlow, we can use the following steps to create a neural network:

  1. Prepare the data set: The data set must be divided into two parts: a training set and a test set. The training set is used to train the model, while the test set is used to test the accuracy of the model.
  2. Create a neural network: You can use Python to write and implement a neural network. Neural network models can be built using the TensorFlow API.
  3. Train the model: Train the neural network model to predict the output of new data. This can be done by using the stochastic gradient descent algorithm and performing backpropagation, continuously updating the weights and biases of the model.
  4. Test the model: Test the model using the test set to determine its accuracy. Different metrics can be used to evaluate the performance of the model, such as precision, recall, and F1 score.

Now, we will introduce two examples of neural networks implemented using TensorFlow.

Neural Network Example 1: Handwritten Digit Recognition

Handwritten digit recognition is an important issue in the field of computer vision, and neural networks have achieved good results on this issue. In TensorFlow, you can train a neural network using the MNIST dataset, which contains 60,000 28x28 grayscale images and corresponding labels.

First, we need to install the TensorFlow and NumPy libraries. The following is a complete code for handwritten digit recognition:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

#创建模型
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)

#定义损失函数和优化器
y_actual = tf.placeholder(tf.float32, [None, 10])
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_actual, logits=y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(loss)

#初始化变量
init = tf.global_variables_initializer()

#训练模型
sess = tf.Session()
sess.run(init)
for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_actual: batch_ys})

#评估模型
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_actual,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_actual: mnist.test.labels}))

In this example, we first prepared the dataset MNIST, and then created a simple neural network model containing 784 inputs and 10 outputs. Next, we defined the loss function and optimizer and fed the training data into the model for training. Finally, we performed test evaluation on the test data and obtained an accuracy of 92.3%.

Neural Network Example 2: Spam Filter

Almost everyone uses the mail system now, but everyone faces the spam problem. A spam filter is a program that checks whether an email is spam. Let’s see how to use neural networks to build a spam filter.

First, we need to prepare a spam data set, including emails that have been marked as spam and non-spam. Please note that when building a spam filter, there will be two categories of messages: non-spam and spam.

The following is the complete code of the spam filter:

import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

#读取数据集
data = pd.read_csv('spam.csv')
data = data.drop(['Unnamed: 2', 'Unnamed: 3', 'Unnamed: 4'], axis=1)

#转换标签
data['v1'] = data['v1'].map({'ham': 0, 'spam': 1})

#划分数据集
X_train, X_test, y_train, y_test = train_test_split(data['v2'], data['v1'], test_size=0.33, random_state=42)

#创建神经网络模型
max_words = 1000
tokenize = tf.keras.preprocessing.text.Tokenizer(num_words=max_words, char_level=False)
tokenize.fit_on_texts(X_train)

x_train = tokenize.texts_to_matrix(X_train)
x_test = tokenize.texts_to_matrix(X_test)

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(512, input_shape=(max_words,), activation='relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(256, activation='sigmoid'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

#训练模型
model.fit(x_train, y_train,
          batch_size=32,
          epochs=5,
          validation_data=(x_test, y_test))

#评估模型
y_predict = model.predict(x_test)
print("Accuracy:", accuracy_score(y_test, y_predict.round()))

In this example, we use sklearn's train_test_split() method to divide the data set, and then use the Keras library Text preprocessing tools convert datasets into matrices (one-hot encoding). Next, we use Sequential to declare the neuron and set its parameters. Finally, we used the trained model to predict the test data and evaluated it to obtain an accuracy of 98.02%.

Conclusion

Neural networks in Python are a powerful technique that can be used in a variety of applications such as image recognition, spam filters, etc. Using TensorFlow, we can easily create, train, and test neural network models and obtain satisfactory results. As people's demand for machine learning grows, neural network technology will become a more important tool and be more widely used in future application scenarios.

The above is the detailed content of Neural network examples in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you append elements to a Python array?How do you append elements to a Python array?Apr 30, 2025 am 12:19 AM

InPython,youappendelementstoalistusingtheappend()method.1)Useappend()forsingleelements:my_list.append(4).2)Useextend()or =formultipleelements:my_list.extend(another_list)ormy_list =[4,5,6].3)Useinsert()forspecificpositions:my_list.insert(1,5).Beaware

How do you debug shebang-related issues?How do you debug shebang-related issues?Apr 30, 2025 am 12:17 AM

The methods to debug the shebang problem include: 1. Check the shebang line to make sure it is the first line of the script and there are no prefixed spaces; 2. Verify whether the interpreter path is correct; 3. Call the interpreter directly to run the script to isolate the shebang problem; 4. Use strace or trusts to track the system calls; 5. Check the impact of environment variables on shebang.

How do you remove elements from a Python array?How do you remove elements from a Python array?Apr 30, 2025 am 12:16 AM

Pythonlistscanbemanipulatedusingseveralmethodstoremoveelements:1)Theremove()methodremovesthefirstoccurrenceofaspecifiedvalue.2)Thepop()methodremovesandreturnsanelementatagivenindex.3)Thedelstatementcanremoveanitemorslicebyindex.4)Listcomprehensionscr

What data types can be stored in a Python list?What data types can be stored in a Python list?Apr 30, 2025 am 12:07 AM

Pythonlistscanstoreanydatatype,includingintegers,strings,floats,booleans,otherlists,anddictionaries.Thisversatilityallowsformixed-typelists,whichcanbemanagedeffectivelyusingtypechecks,typehints,andspecializedlibrarieslikenumpyforperformance.Documenti

What are some common operations that can be performed on Python lists?What are some common operations that can be performed on Python lists?Apr 30, 2025 am 12:01 AM

Pythonlistssupportnumerousoperations:1)Addingelementswithappend(),extend(),andinsert().2)Removingitemsusingremove(),pop(),andclear().3)Accessingandmodifyingwithindexingandslicing.4)Searchingandsortingwithindex(),sort(),andreverse().5)Advancedoperatio

How do you create multi-dimensional arrays using NumPy?How do you create multi-dimensional arrays using NumPy?Apr 29, 2025 am 12:27 AM

Create multi-dimensional arrays with NumPy can be achieved through the following steps: 1) Use the numpy.array() function to create an array, such as np.array([[1,2,3],[4,5,6]]) to create a 2D array; 2) Use np.zeros(), np.ones(), np.random.random() and other functions to create an array filled with specific values; 3) Understand the shape and size properties of the array to ensure that the length of the sub-array is consistent and avoid errors; 4) Use the np.reshape() function to change the shape of the array; 5) Pay attention to memory usage to ensure that the code is clear and efficient.

Explain the concept of 'broadcasting' in NumPy arrays.Explain the concept of 'broadcasting' in NumPy arrays.Apr 29, 2025 am 12:23 AM

BroadcastinginNumPyisamethodtoperformoperationsonarraysofdifferentshapesbyautomaticallyaligningthem.Itsimplifiescode,enhancesreadability,andboostsperformance.Here'showitworks:1)Smallerarraysarepaddedwithonestomatchdimensions.2)Compatibledimensionsare

Explain how to choose between lists, array.array, and NumPy arrays for data storage.Explain how to choose between lists, array.array, and NumPy arrays for data storage.Apr 29, 2025 am 12:20 AM

ForPythondatastorage,chooselistsforflexibilitywithmixeddatatypes,array.arrayformemory-efficienthomogeneousnumericaldata,andNumPyarraysforadvancednumericalcomputing.Listsareversatilebutlessefficientforlargenumericaldatasets;array.arrayoffersamiddlegro

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.