***Loan application evaluation**: Banks issue loans to customers based on different factors, including demographical information, credit history, and so on. Their main objective in this process is to minimize the number of customers who will default on loan payments (minimize the failure rate), thereby maximizing the returns obtained through the loans that have been issued.
***Detection of fraud**: Fraud detection is crucial for banks and financial providers, considering the advancements of technology that, in spite of making our lives easier, also leave us exposed to greater financial risks.
***Credit card customer selection**: To remain profitable in the long term, credit card providers need to find the right customers. For instance, approving a credit card to a customer with limited credit card needs (that is, a customer who will not use it) will result in low credit card revenues.
***Take a look at the data**: After reading the dataset using Pandas, print the head of the dataset. This helps to ensure that the correct dataset has been loaded. Additionally, it serves to provide evidence of transforming the dataset after preparation.
***Remove irrelevant features**: By performing analysis on each of the features, it is possible to determine that two of the features should be removed from the dataset as they are irrelevant to the purpose of the study:
***Check for missing values**: Next, it is time to check whether the dataset is missing any values, and, if so, calculate the percentage of how much they represent each feature, which can be done using the following lines of code:
***Check for outliers**: As we mentioned in *Chapter 2*, *Building Blocks of Neural Networks*, there are several ways to check for outliers. However, in this book, we will stick to the standard deviation methodology, where values that are three standard deviations away from the mean will be considered outliers. Using the following code, it is possible to identify outliers from each feature and calculate the proportion they represent against the entire set of values:
***Check for class imbalance**: Class imbalance occurs when the class labels in the target feature are not equally represented; for instance, a dataset containing 90% of customers who did not default on the next payment, against 10% of customers who did, is considered to be imbalanced.
***Hidden layer**: Hidden layers can vary in quantity. ANNs can have one hidden layer, more, or none. To choose the right number, it is important to consider the following:
@@ -378,7 +378,7 @@ class Classifier(torch.nn.Module):
使用前面解释的理论,我们将使用定制模块的语法定义模型的架构:
1.Open a Jupyter Notebook and import the required libraries:
1.打开Jupyter 笔记本并导入所需的库:
```py
import torch
...
...
@@ -386,7 +386,7 @@ class Classifier(torch.nn.Module):
import torch.nn.functional as F
```
2.Define the necessary variables for the input, hidden, and output dimensions. Set them to`10`,`5`, and`2`, respectively:
2.定义输入,隐藏和输出尺寸的必要变量。 将它们分别设置为`10`,`5`和`2`:
```py
D_i = 10
...
...
@@ -394,7 +394,7 @@ class Classifier(torch.nn.Module):
D_o = 2
```
3.Using PyTorch's custom modules, create a class called **Classifier** and define the model's architecture so that it has two linear layers—the first one followed by a **ReLU** activation function, and the second one by a **Softmax** activation function:
@@ -408,7 +408,7 @@ class Classifier(torch.nn.Module):
return o
```
4.Instantiate the class and feed it with the three variables we created in *Step 2*. Print the model:
4.实例化该类,并将我们在“步骤 2”中创建的三个变量输入该类。打印模型。
```py
model = Classifier(D_i, D_h, D_o)
...
...
@@ -462,7 +462,7 @@ batch_size = 100
2. 通过比较上一步的预测值和训练集的标签(真实情况)来计算损失。
3. 将梯度设置为零并针对当前步骤再次计算。
4. 网络的参数基于梯度进行更新。
5.The accuracy of the model over the training data is calculated as follows:
5.该模型对训练数据的准确性计算如下:
获取模型预测的指数,以便获得属于每个类标签的给定数据片段的概率。
...
...
@@ -474,7 +474,7 @@ batch_size = 100
1. 该模型对验证集中的数据执行预测。
2. 通过将先前的预测与验证集中的标签进行比较来计算损失函数。
3.The accuracy is calculated over the validation set. To calculate the accuracy of the model over the validation set, use the same set of steps that you did for the same calculation over the training data:
8.Train the network using the training set's data. Use the validation sets to measure performance. To do this, save the loss and the accuracy for both the training and validation sets in each epoch.
2.Using this measure, calculate the error rate of each of the sets by subtracting the performance metric that was previously calculated from 1\. Take, for instance, the following equation:
2.使用此度量,通过减去以前从 1 中计算出的性能指标来计算每个集合的错误率。以下面的等式为例:
![Figure 3.9: The equation to calculate the error rate of the model over the training set ](img/B15778_03_09.jpg)
...
...
@@ -603,7 +603,7 @@ batch_size = 100
3. 从训练集误差`A)`中减去贝叶斯误差。 保存差异,将用于进一步分析。
4. 从验证集错误`B)`中减去训练集错误,并保存差值。
5.Take the differences calculated in Steps 3 and 4 and use the following set of rules:
5.取步骤3和步骤4中计算出的差值,并使用以下一组规则。
如果在“步骤 3”中计算出的差大于在“步骤 4”中计算出的差,则该模型不适合,也称为遭受高偏差。
...
...
@@ -617,11 +617,11 @@ batch_size = 100
让我们解释一下如何处理这些问题:
***High bias**: An underfitted model, or a model suffering from high bias, is a model that is not capable of understanding the training data, and, hence, it is not able to uncover patterns and generalize with other sets of data. This means that the model does not perform well over any set of data.
***High variance**: An overfitted model, or a model suffering from high variance, is a model that is having trouble generalizing the training data; it is learning the details of the training data too well (this means that through the training process, the model learned the information from the training set too well, which means that it is unable to generalize to other sets of data), including its outliers. This means that the model is performing too well over the training data, but poorly over other sets of data.
3.Considering that the model is suffering from a high bias, the focus should be on increasing the number of epochs or increasing the size of the network by adding additional layers or units to each layer. The aim should be to approximate the accuracy over the testing set to 80%.
1.Originally, a checkpoint of a model will only include the model's parameters. However, when loading the model, this is not the only information that's required. Depending on the arguments that your classifier takes in (that is, the class containing the network architecture), it may be necessary to save further information, such as the number of **input** units. Considering this, the first step is to define the information to be saved:
***Hypertext Transfer Protocol** (**HTTP**): This is the primary means of transferring data on the web. It works using **methods**, which help to determine the way in which data is transferred. The two most commonly used methods are explained as follows:
4.Define the route where the API will be accessible, as well as the method(s) that can be used for sending information to the API in order to perform an action. This syntax is called a decorator and should be located immediately before a function:
5.Define a function that performs the desired action. In this case, the function will take the information that was sent to the API and feed it to the previously loaded model to perform a prediction. Once the prediction has been obtained, the function should return a response, which will be displayed as the result of the API request:
使用 Flask,我们将创建一个 Web API,该 Web API 会在调用时接收一些数据,并返回一段显示在浏览器中的文本。 请按照以下步骤完成此练习:
1.In a Jupyter Notebook, import the required libraries:
1.在Jupyter 笔记本中,导入所需的库。
```py
import flask
from flask import request
```
2.Initialize the Flask app:
2.初始化Flask应用。
```py
app = flask.Flask(__name__)
app.config["DEBUG"] = True
```
3.Define the route of your API so that it's **/<name>**. Set the method to **GET**. Next, define a function that takes in an argument (**name**) and returns a string that contains an **h1** tag with the word **HELLO**, followed by the argument received by the function: