Deep Learning Combat 41 - Prediction and Application of Diabetes Data Based on LSTM-GRU Model

Hello everyone, I am Weixue AI. Today I will introduce you to deep learning practice 41-Prediction and application of diabetes data based on LSTM-GRU model. This article will introduce a diabetes prediction model based on LSTM-GRU, including The principle of the model, the sample of Chinese diabetes csv data, the implementation of model training and prediction using the PyTorch framework, and the whole process of printing the accuracy rate and loss value during the training process.

Article directory:

  1. introduce
  2. The principle of LSTM and GRU
  3. data sample
  4. data loading
  5. model building
  6. Model Training and Prediction
  7. in conclusion

1 Introduction

As the number of diabetic patients rises, the prediction and control of diabetes becomes more and more important. In recent years, deep learning methods have made remarkable progress in the analysis of time series data, among which models based on long short-term memory network (LSTM) and gated recurrent unit (GRU) perform well in many applications. This article will introduce a diabetes prediction model based on LSTM-GRU. By analyzing the Chinese diabetes csv data, the PyTorch framework is used to train and predict the model.

2. The principle of LSTM and GRU

2.1 LSTM

A Long Short Term Memory Network (LSTM) is a special type of Recurrent Neural Network (RNN) capable of learning dependencies in long sequences of data. LSTM solves the problem of gradient disappearance and gradient explosion when traditional RNN processes long sequences by introducing a gating mechanism.

LSTM contains three gates: input gate, forget gate and output gate. The input gate determines whether the input information is added to the memory cell, the forget gate determines whether the past information in the memory cell is retained, and the output gate determines the final output value.

2.2 GRU

Gated Recurrent Unit (GRU) is a variant of LSTM that combines the forget and input gates of LSTM into a single update gate, while merging memory cells and hidden states. In this way, GRU can maintain good performance while reducing the number of parameters.

The benefits of combining LSTM and GRU models

Combining LSTM (Long Short-Term Memory Network) with GRU (Gated Recurrent Unit) model can bring the following benefits:

1. Better model flexibility: LSTM and GRU are two common recurrent neural network architectures that have different advantages when modeling. LSTM controls the flow and storage of information through input, forget, and output gates, and is suitable for sequence modeling tasks that require long-term memory. The GRU achieves information update and selective memory by updating the gate and resetting the gate, which is suitable for tasks that are more sensitive to recent inputs. Combining the two can make the model more flexible and able to capture both long-term dependencies and short-term changes.

2. Faster training speed: Since the GRU model has fewer parameters than the LSTM model, in some cases, the combination model of LSTM and GRU can achieve relatively fast training speed. This is very beneficial for large-scale datasets or environments with limited computing resources.

3. Better generalization ability: By combining LSTM and GRU models, the advantages of the two models can be fully utilized as much as possible, and can be adjusted and tuned according to specific tasks. This improves the generalization ability of the model, making it perform better in different sequence modeling tasks.

4. Stronger representation ability: Both LSTM and GRU models have good representation ability, and by combining them together, the representation ability of the model can be further enhanced. This is very important for complex sequence data modeling tasks, which can improve the model's ability to abstract and understand the input sequence.
insert image description here

3. Data sample

Here are some sample Chinese diabetes csv data:

年龄,性别,身高,体重,收缩压,舒张压,空腹血糖,糖尿病
45,男,175,83,130,85,5.6,否
58,女,162,62,140,90,7.2,是
32,男,180,90,120,80,5.2,否
67,女,155,72,150,95,8.1,是
51,男,168,75,135,88,6.5,是
39,女,170,63,122,78,4.9,否
62,男,176,85,148,92,6.0,是
56,女,160,70,142,93,7.9,是
29,男,182,92,116,78,4.6,否
58,女,168,68,130,85,6.2,是
43,男,177,80,124,80,5.4,否
63,女,157,74,148,91,7.6,是
35,男,181,87,118,76,6.2,否
53,女,165,67,137,88,6.8,是
46,男,173,78,132,86,6.0,否
61,女,163,71,145,92,8.3,是
28,男,179,88,114,74,4.4,否
50,女,166,65,134,87,6.6,是
38,男,174,82,126,81,5.7,否
59,女,161,69,144,94,7.8,是
31,男,178,84,120,77,4.9,否
55,女,159,73,139,90,7.4,是
40,男,175,81,130,84,5.9,否
64,女,156,76,147,94,8.0,是

4. Data loading

First, we need to load the data and preprocess it. Here, we use the pandas library to process csv data:

import pandas as pd

# 读取csv数据
data = pd.read_csv("diabetes.csv")

# 将性别和糖尿病标签转为数值
data["性别"] = data["性别"].map({
    
    "男": 0, "女": 1})
data["糖尿病"] = data["糖尿病"].map({
    
    "否": 0, "是": 1})

# 数据归一化处理
for column in ["年龄", "身高", "体重", "收缩压", "舒张压", "空腹血糖"]:
    data[column] = (data[column] - data[column].min()) / (data[column].max() - data[column].min())

# 划分训练集和测试集
from sklearn.model_selection import train_test_split

X = data.drop(columns=["性别","糖尿病"])
y = data["糖尿病"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 转换为PyTorch张量
import torch
from torch.utils.data import TensorDataset, DataLoader

train_data = TensorDataset(torch.tensor(X_train.values, dtype=torch.float), torch.tensor(y_train.values, dtype=torch.float))
test_data = TensorDataset(torch.tensor(X_test.values, dtype=torch.float), torch.tensor(y_test.values, dtype=torch.float))

train_loader = DataLoader(train_data, batch_size=2, shuffle=True)
test_loader = DataLoader(test_data, batch_size=2, shuffle=True)

5. Model Construction

Next, we build a prediction model based on LSTM-GRU:

import torch.nn as nn

class DiabetesPredictor(nn.Module):
    def __init__(self, input_size, hidden_size, num_layers, output_size):
        super(DiabetesPredictor, self).__init__()
        self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
        self.gru = nn.GRU(hidden_size, hidden_size, num_layers, batch_first=True)
        self.fc = nn.Linear(hidden_size, output_size)

    def forward(self, x):
        # LSTM层
        out, _ = self.lstm(x)
        # GRU层
        out, _ = self.gru(out)
        # 全连接层
        out = self.fc(out[:, -1, :])
        return out

input_size = X_train.shape[1]
hidden_size = 64
num_layers = 2
output_size = 1

model = DiabetesPredictor(input_size, hidden_size, num_layers, output_size)

6. Model Training and Prediction

Now we can use the PyTorch framework to train and predict the model:

import torch.optim as optim

# 设置损失函数和优化器
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# 训练模型
num_epochs = 50

for epoch in range(num_epochs):
    model.train()
    running_loss = 0
    correct = 0
    total = 0

    for inputs, labels in train_loader:
        inputs = inputs.unsqueeze(1)
        labels = labels.view(-1, 1)

        optimizer.zero_grad()

        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        running_loss += loss.item()
        preds = torch.round(torch.sigmoid(outputs))
        total += labels.size(0)
        correct += (preds == labels).sum().item()

    # 计算训练集准确率和损失值
    train_acc = correct / total
    train_loss = running_loss / len(train_loader)

    print(f"Epoch [{epoch + 1}/{num_epochs}] - Loss: {train_loss:.4f}, Accuracy: {train_acc:.4f}")

# 测试模型
model.eval()
correct = 0
total = 0

with torch.no_grad():
    for inputs, labels in test_loader:
        inputs = inputs.unsqueeze(1)
        labels = labels.view(-1, 1)

        outputs = model(inputs)
        preds = torch.round(torch.sigmoid(outputs))
        total += labels.size(0)
        correct += (preds == labels).sum().item()

    # 计算测试集准确率
    test_acc = correct / total
    print(f"Test Accuracy: {test_acc:.4f}")

operation result:

Epoch [36/50] - Loss: 0.0031, Accuracy: 1.0000
Epoch [37/50] - Loss: 0.0030, Accuracy: 1.0000
Epoch [38/50] - Loss: 0.0030, Accuracy: 1.0000
Epoch [39/50] - Loss: 0.0025, Accuracy: 1.0000
Epoch [40/50] - Loss: 0.0025, Accuracy: 1.0000
Epoch [41/50] - Loss: 0.0022, Accuracy: 1.0000
Epoch [42/50] - Loss: 0.0022, Accuracy: 1.0000
Epoch [43/50] - Loss: 0.0019, Accuracy: 1.0000
Epoch [44/50] - Loss: 0.0018, Accuracy: 1.0000
Epoch [45/50] - Loss: 0.0018, Accuracy: 1.0000
Epoch [46/50] - Loss: 0.0016, Accuracy: 1.0000
Epoch [47/50] - Loss: 0.0016, Accuracy: 1.0000
Epoch [48/50] - Loss: 0.0015, Accuracy: 1.0000
Epoch [49/50] - Loss: 0.0014, Accuracy: 1.0000
Epoch [50/50] - Loss: 0.0014, Accuracy: 1.0000
Test Accuracy: 1.0000

7. Conclusion

This article introduces an LSTM-GRU based diabetes prediction model and how to train and predict it using the PyTorch framework. Through the analysis of the Chinese diabetes csv data, we can find that the model has a good performance in predicting diabetes.

Guess you like

Origin blog.csdn.net/weixin_42878111/article/details/131540984