【Python · PyTorch】循環神經網絡 RNN(基礎應用)

【Python · PyTorch】循環神經網絡 RNN(簡單應用)

  • 1. 簡介
  • 2. 模擬客流預測(數據集轉化Tensor)
    • 3.1 數據集介紹
    • 3.2 訓練過程
  • 3. 模擬股票預測(DataLoader加載數據集)
    • 3.1 IBM 數據集
      • 3.1.2 數據集介紹
      • 3.1.3 訓練過程
        • ① 利用Matplotlib繪圖
        • ② 利用Seaborn繪圖
    • 3.2 Amazon 數據集
      • 3.2.2 數據集介紹
      • 3.2.3 訓練結果

1. 簡介

此前介紹了RNN及其變體LSTM、GRU的結構,本章節介紹相關神經網絡的代碼,并通過 模擬數據 展示簡單的應用場景。

RNN核心代碼:

nn.RNN(input_size, hidden_size, num_layers, nonlinearity, bias, batch_first, dropout, bidirectional)

參數介紹:

  • input_size:輸入層神經元數量,對應輸入特征的維度
  • hidden_size:隱藏層神經元數量
  • num_layers:RNN單元堆疊層數,默認1
  • bias:是否啟用偏置,默認True
  • batch_first:是否將batch放在第一位,默認False
    • True:input 為 (batch, seq_len, input_size)
    • False:input 為 (seq_len, batch, input)
  • dropout:丟棄率,值范圍為0~1
  • bidirectional:是否使用雙向RNN,默認False

調用時參數:

  • input:前側輸入
  • h[n-1]:前側傳遞狀態

調用時返回:

  • output:本層輸出

  • h[n]:本層向后側傳遞狀態

GRU與RNN參數相同,但LSTM有所不同:

  • input/output:本層輸入/輸出
  • (h[n-1], c[n-1])/(h[n-1], c[n-1]):傳遞狀態

輸入時 seq_len表示序列長度/傳遞長度:即 輸入向量維度為 (seq_len, batch, input)

  • 以自然語言訓練為例,假設句子長度為30個單詞,單詞為50維向量,一次訓練10個句子。
  • seq_len=30input_size=50batch_size=10,LSTM結構會根據傳入數據 向前傳遞30次 輸出最終結果。

2. 模擬客流預測(數據集轉化Tensor)

3.1 數據集介紹

模擬航班數據,經常用于學習機器學習算法。

航班客流數據集

3.2 訓練過程

① 導入三方庫

import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler

定義使用設備

device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using {device} device")

② 讀取數據集

從CSV/Excel文件中讀取數據

# 模擬航空數據
pf = pd.read_csv('./data/flight.csv')
month, passengers = pf['Month'], pf['Passengers']
scaler = MinMaxScaler(feature_range=(-1, 1))
passengers = scaler.fit_transform(passengers.values.reshape(-1,1))

定義數據集劃分方法

def split_data(passengers, looback):# 1. 分段passengers = np.array(passengers)segments = []# 創建所有可能的時間序列for index in range(len(passengers) - lookback):segments.append(passengers[index: index + lookback])segments = np.array(segments)# 2. 確定train和test的數量test_set_size = int(np.round(0.2 *  segments.shape[0]))train_set_size = segments.shape[0] - (test_set_size)# 3. 分割:訓練集和測試集:x和yx_train = segments[:train_set_size,:-1]y_train = segments[:train_set_size,-1]   # 序列最后一個是yx_test = segments[train_set_size:,:-1]y_test = segments[train_set_size:,-1]return x_train, y_train, x_test, y_test

讀取數據集

lookback = 20 # 設置序列長度
x_train, y_train, x_test, y_test = split_data(passengers, lookback)
print('x_train.shape = ',x_train.shape)
print('y_train.shape = ',y_train.shape)
print('x_test.shape = ',x_test.shape)
print('y_test.shape = ',y_test.shape)

數據轉化為Tensor

x_train = torch.from_numpy(x_train).type(torch.Tensor).to(device)
x_test = torch.from_numpy(x_test).type(torch.Tensor).to(device)
y_train = torch.from_numpy(y_train).type(torch.Tensor).to(device)
y_test = torch.from_numpy(y_test).type(torch.Tensor).to(device)

③ 創建神經網絡

定義 輸入 / 隱藏 / 輸出 維度

input_dim = 1
hidden_dim = 32
num_layers = 2
output_dim = 1

定義三種神經網絡

class RNN(nn.Module):def __init__(self, input_dim, hidden_dim, num_layers, output_dim):super(RNN, self).__init__()self.rnn = nn.RNN(input_size=input_dim, hidden_size=hidden_dim, num_layers=num_layers, batch_first=True)self.fc = nn.Linear(hidden_dim, output_dim)def forward(self, x):# output維度:[num_steps,  batch_size, hidden_size]# hn維度    :[num_layers, batch_size, hidden_size]output, hn = self.rnn(x)# num_steps和hidden_size保留,取最后一次batchoutput = output[:,-1,:]# 最后幾步送入全連接output = self.fc(output)return output
class LSTM(nn.Module):def __init__(self, input_dim, hidden_dim, num_layers, output_dim):super(LSTM, self).__init__()self.input_dim = input_dimself.hidden_dim = hidden_dimself.num_layers = num_layersself.output_dim = output_dimself.lstm = nn.LSTM(input_size=input_dim, hidden_size=hidden_dim, num_layers=num_layers, batch_first=True)self.fc = nn.Linear(hidden_dim, output_dim)def forward(self, x):# 初始化隱藏狀態和單元狀態h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).to(device)c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).to(device)output, (hn, cn) = self.lstm(x, (h0, c0))output = output[:,-1,:]output = self.fc(output)return output
class GRU(nn.Module):def __init__(self, input_dim, hidden_dim, num_layers, output_dim):super(GRU, self).__init__()self.input_dim = input_dimself.hidden_dim = hidden_dimself.num_layers = num_layersself.output_dim = output_dimself.gru = nn.GRU(input_size=input_dim, hidden_size=hidden_dim, num_layers=num_layers, batch_first=True)self.fc = nn.Linear(hidden_dim, output_dim)def forward(self, x):# 初始化隱藏狀態和單元狀態h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).to(device)c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).to(device)output, hn = self.gru(x, h0)output = output[:,-1,:]output = self.fc(output)return output

④ 訓練神經網絡

預先定義

# 隨機種子
torch.manual_seed(20)
# 創建神經網絡對象
rnn = RNN(input_dim, hidden_dim, num_layers, output_dim)
lstm = LSTM(input_dim, hidden_dim, num_layers, output_dim)
gru = GRU(input_dim, hidden_dim, num_layers, output_dim)# 確定神經網絡運行設備
rnn.to(device)
lstm.to(device)
gru.to(device)# 損失函數
rnn_loss_function = nn.MSELoss()
lstm_loss_function = nn.MSELoss()
gru_loss_function = nn.MSELoss()# 優化器
rnn_optimizer = torch.optim.Adam(rnn.parameters(), lr=0.001)
lstm_optimizer = torch.optim.Adam(lstm.parameters(), lr=0.001)
gru_optimizer = torch.optim.Adam(gru.parameters(), lr=0.001)# 訓練輪次
epochs = 200
# 訓練損失記錄
rnn_final_losses = []
lstm_final_losses = []
gru_final_losses = []

定義神經網絡訓練方法

def train_rnn():rnn.train()for epoch in range(epochs):# 1. 正向傳播y_train_pred_rnn = rnn(x_train)# 2. 計算誤差rnn_loss = rnn_loss_function(y_train_pred_rnn, y_train)rnn_final_losses.append(rnn_loss.item())# 3. 反向傳播rnn_optimizer.zero_grad()rnn_loss.backward()# 4. 優化參數rnn_optimizer.step()if epoch % 10 == 0:print("RNN:: Epoch: {}, Loss: {} ".format(epoch, rnn_loss.data))return y_train_pred_rnn
def train_lstm():lstm.train()for epoch in range(epochs):# 1. 正向傳播y_train_pred_lstm = lstm(x_train)# 2. 計算誤差lstm_loss = lstm_loss_function(y_train_pred_lstm, y_train)lstm_final_losses.append(lstm_loss.item())# 3. 反向傳播lstm_optimizer.zero_grad()lstm_loss.backward()# 4. 優化參數lstm_optimizer.step()if epoch % 10 == 0:print("LSTM:: Epoch: {}, Loss: {} ".format(epoch, lstm_loss.data))return y_train_pred_lstm
def train_gru():gru.train()for epoch in range(epochs):# 1. 正向傳播y_train_pred_gru = gru(x_train)# 2. 計算誤差gru_loss = gru_loss_function(y_train_pred_gru, y_train)gru_final_losses.append(gru_loss.item())# 3. 反向傳播gru_optimizer.zero_grad()gru_loss.backward()# 4. 優化參數gru_optimizer.step()if epoch % 10 == 0:print("GRU:: Epoch: {}, Loss: {} ".format(epoch, gru_loss.data))return y_train_pred_gru

執行訓練方法

y_train_pred_rnn = train_rnn()
torch.save(rnn.state_dict(), "rnn_test.pth")
print("Saved PyTorch Model State to rnn_test.pth")y_train_pred_lstm = train_lstm()
torch.save(lstm.state_dict(), "lstm_test.pth")
print("Saved PyTorch Model State to lstm_test.pth")y_train_pred_gru = train_gru()
torch.save(gru.state_dict(), "gru_test.pth")
print("Saved PyTorch Model State to gru_test.pth")

訓練過程

繪制訓練結果(最后一次)

數據逆歸一化 并轉換為DataFrame

original = pd.DataFrame(scaler.inverse_transform(torch.Tensor.cpu(y_train).detach().numpy()))rnn_predict = pd.DataFrame(scaler.inverse_transform(torch.Tensor.cpu(y_train_pred_rnn).detach().numpy()))
lstm_predict = pd.DataFrame(scaler.inverse_transform(torch.Tensor.cpu(y_train_pred_lstm).detach().numpy()))
gru_predict = pd.DataFrame(scaler.inverse_transform(torch.Tensor.cpu(y_train_pred_gru).detach().numpy()))

執行繪制程序

import seaborn as sns
sns.set_style("darkgrid") fig = plt.figure(figsize=(16, 6))# 畫左邊的趨勢圖
plt.subplot(1, 2, 1)
ax = sns.lineplot(x = original.index, y = original[0], label="Data", color='blue')
ax = sns.lineplot(x = rnn_predict.index, y = rnn_predict[0], label="RNN Prediction", color='red')
ax = sns.lineplot(x = lstm_predict.index, y = lstm_predict[0], label="LSTM Prediction", color='darkred')
ax = sns.lineplot(x = gru_predict.index, y = gru_predict[0], label="GRU Prediction", color='black')ax.set_title('Passengers', size = 14, fontweight='bold')
ax.set_xlabel("Days", size = 14)
ax.set_ylabel("Members", size = 14)
ax.set_xticklabels('', size=10)# 畫右邊的Loss下降圖
plt.subplot(1, 2, 2)
ax = sns.lineplot(data=rnn_final_losses, label="RNN Loss", color='red')
ax = sns.lineplot(data=lstm_final_losses, label="LSTM Loss", color='darkblue')
ax = sns.lineplot(data=gru_final_losses, label="GRU Loss", color='black')
ax.set_xlabel("Epoch", size = 14)
ax.set_ylabel("Loss", size = 14)
ax.set_title("Training Loss", size = 14, fontweight='bold')
plt.show()

繪圖

⑤ 測試神經網絡

定義測試函數

# 測試 RNN
def test_rnn():rnn.eval()total = len(x_test)currect = 0with torch.no_grad():y_test_pred_rnn = rnn(x_test)return y_test_pred_rnn# 測試 LSTM
def test_lstm():lstm.eval()total = len(x_test)currect = 0with torch.no_grad():y_test_pred_lstm = lstm(x_test)return y_test_pred_lstm# 測試 RNN
def test_gru():gru.eval()total = len(x_test)currect = 0with torch.no_grad():y_test_pred_gru = gru(x_test)return y_test_pred_gru

執行測試程序

y_test_pred_rnn  = test_rnn()
y_test_pred_lstm = test_lstm()
y_test_pred_gru  = test_gru()

數據逆歸一化 并轉換為DataFrame

test_original = pd.DataFrame(scaler.inverse_transform(torch.Tensor.cpu(y_test).detach().numpy()))test_rnn_predict = pd.DataFrame(scaler.inverse_transform(torch.Tensor.cpu(y_test_pred_rnn).detach().numpy()))
test_lstm_predict = pd.DataFrame(scaler.inverse_transform(torch.Tensor.cpu(y_test_pred_lstm).detach().numpy()))
test_gru_predict = pd.DataFrame(scaler.inverse_transform(torch.Tensor.cpu(y_test_pred_gru).detach().numpy()))

執行繪制程序

import seaborn as sns
sns.set_style("darkgrid") ax = sns.lineplot(x = test_original.index, y = test_original[0], label="Data", color='blue')ax = sns.lineplot(x = test_rnn_predict.index, y = test_rnn_predict[0], label="RNN Prediction", color='red')
ax = sns.lineplot(x = test_lstm_predict.index, y = test_lstm_predict[0], label="LSTM Prediction", color='darkred')
ax = sns.lineplot(x = test_gru_predict.index, y = test_gru_predict[0], label="GRU Prediction", color='black')ax.set_title('Passengers', size = 14, fontweight='bold')
ax.set_xlabel("Days", size = 14)
ax.set_ylabel("Members", size = 14)
ax.set_xticklabels('', size=10)plt.show()

測試

3. 模擬股票預測(DataLoader加載數據集)

3.1 IBM 數據集

3.1.2 數據集介紹

IBM股價數據,經常用于學習機器學習算法。

IBM

3.1.3 訓練過程

① 導入三方庫

import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler

定義使用設備

device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using {device} device")

② 讀取數據集

PyTorch提供了一種標準的數據集讀取方法:

  • 自定義繼承自torch.utils.data.Dataset類的XXDataset類,并重寫__getitem__()__len__()方法
  • 利用torch.utils.data.DataLoader加載自定義DataSet實例中讀取的數據

例如,官方提供的CIFAR10數據集 就是 使用這種方法讀取:

import torchvision.datasets
from torch.utils.data import DataLoadertrain_data=torchvision.datasets.CIFAR10(root="datasets",train=False,transform=torchvision.transforms.ToTensor(),download=True)
train_loader=DataLoader(dataset=train_data,batch_size=4,shuffle=True)

DataLoader類初始化參數:

DataLoader

其中常用的參數包括:

  • dataset:Dataset類
  • batch_size:批量大小
  • shuffle:是否每輪打亂數據
  • num_workers:讀取數據時工作線程數(默認0:代表只使用主進程)
  • drop_last:是否丟棄所余非完整batch數據(數據長度不能整除batch_size,是否丟棄最后不完整的batch)
  • sampler:從數據集中抽取樣本的策略
  • batch_sampler:類似于sampler,但每次返回一個batch的索引。與batch_size、shuffle、sampler和drop_last互斥

DataLoader參數
DataLoader參數

部分參數簡介:

num_worker 工作方式

DataLoader一次創建num_worker數量個名為worker工作進程。并用batch_samplerbatch分配給worker,由workerbatch加載進RAM/內存DataLoader迭代時會從RAM/內存中檢索并獲取batch;若檢索失敗,則用num_workerworker繼續加載batchRAM/內存DataLoader再嘗試從中獲取batch

sampler / batch_sampler 采樣方式

  • Random Sampler(隨機采樣)
    • 隨機從數據集中選擇樣本,可設置隨機數種子,保證采樣結果相同
  • Subset Random Sampler(子集隨機采樣)
    • 從數據集指定子集隨機采集樣本,可用于數據集劃分(訓練集、驗證集等)
  • Weighted Random Sampler(加權隨機采樣)
    • 根據指定的樣本權重隨機采樣,可用于處理類別不平衡問題
  • BatchSample(批采樣)
    • 將樣本索引分為多個batch,每個batch包含指定數量樣本索引

有時設置 num_worker 會不同步致使程序卡頓,這里博主將其設置為 num_worker=0 避免卡頓。


自定義股價Dataset類

# 定義StockDataset類  繼承Dataset類  重寫__getitem()__和__len__()方法
class StockDataset(torch.utils.data.Dataset):# 初始化函數 得到數據def __init__(self, data, seq_length):self.data = dataself.seq_length = seq_length# Index是根據 batch_size 劃分數據后得到的索引,最后將data和對應的labels一并返回def __getitem__(self, idx):x = self.data[idx:idx + self.seq_length]  # 輸入序列y = self.data[idx + self.seq_length]      # 輸出值return torch.tensor(x, dtype=torch.float32), torch.tensor(y, dtype=torch.float32)# 該函數返回數據長度大小,DataLoader會調用此方法def __len__(self):return len(self.data) - self.seq_length

利用DataLoader讀取Dataset數據

train_length = 2000   # 訓練長度
seq_length   = 20     # 序列長度
batch_size   = 32     # 批量大小# 利用DataLoader讀取Dataset數據
train_dataset = StockDataset(origin_data[:train_length], seq_length)
test_dataset = StockDataset(origin_data[train_length:], seq_length)
train_dataloader = torch.utils.data.DataLoader(train_dataset, batch_size = batch_size, shuffle=True, num_workers = 0)
test_dataloader  = torch.utils.data.DataLoader(test_dataset, batch_size = batch_size, shuffle=True, num_workers = 0)

③ 創建神經網絡

class RNN(nn.Module):def __init__(self, input_dim, hidden_dim, num_layers, output_dim):super(RNN, self).__init__()self.rnn = nn.RNN(input_size=input_dim, hidden_size=hidden_dim, num_layers=num_layers, batch_first=True)self.fc = nn.Linear(hidden_dim, output_dim)def forward(self, x):output, hn = self.rnn(x)output = output[:,-1,:]output = self.fc(output)return output

④ 訓練神經網絡

預先定義

# 隨機種子
torch.manual_seed(20)
# 創建神經網絡對象
rnn = RNN(input_dim, hidden_dim, num_layers, output_dim)
# 確定神經網絡運行設備
rnn.to(device)
# 損失函數
rnn_loss_function = nn.MSELoss()
# 優化器
rnn_optimizer = torch.optim.Adam(rnn.parameters(), lr=0.001)
# 訓練輪次
epochs = 50
# 訓練損失記錄
rnn_final_losses = []

定義訓練函數

def train_rnn():min_loss = 1.0for epoch in range(epochs):rnn.train()rnn_loss = 0.0for x_train, y_train in train_dataloader:x_train = x_train.to(device=device)y_train = y_train.to(device=device)# 1. 正向傳播y_train_pred_rnn = rnn(x_train)# 2. 計算誤差loss_func_result = rnn_loss_function(y_train_pred_rnn, y_train)rnn_loss += loss_func_result.item()# 3. 反向傳播rnn_optimizer.zero_grad()loss_func_result.backward()# 4. 優化參數rnn_optimizer.step()rnn_loss = rnn_loss / len(train_dataloader)rnn_final_losses.append(rnn_loss)# 對比if(rnn_loss < min_loss):min_loss = rnn_losstorch.save(rnn.state_dict(), "rnn_test.pth")print("Saved PyTorch Model State to rnn_test.pth")if epoch % 10 == 0:print("RNN:: Epoch: {}, Loss: {} ".format(epoch + 1, rnn_loss))

執行訓練函數

train_rnn()

訓練

⑤ 測試神經網絡

# 使用訓練好的模型進行預測
rnn.load_state_dict(torch.load("rnn_test.pth"))
rnn.eval()
with torch.no_grad():# 準備所有輸入序列X_train = torch.stack([x for x, y in train_dataset])train_predictions = rnn(X_train.to(device)).squeeze().cpu().detach().numpy()with torch.no_grad():# 準備所有輸入序列X_test = torch.stack([x for x, y in test_dataset])test_predictions = rnn(X_test.to(device)).squeeze().cpu().detach().numpy()# 將預測結果逆歸一化
origin_data = scaler.inverse_transform(origin_data.reshape(-1, 1))
train_predictions = scaler.inverse_transform(train_predictions.reshape(-1, 1))
test_predictions = scaler.inverse_transform(test_predictions.reshape(-1, 1))
① 利用Matplotlib繪圖

執行繪圖程序

# 繪制結果
plt.figure(figsize=(12, 6))
plt.plot(origin_data, label='Original Data')
plt.plot(range(seq_length,seq_length+len(train_predictions)),train_predictions, label='RNN Train Predictions', linestyle='--')
plt.plot(range(seq_length+train_length,len(test_predictions)+seq_length+train_length), test_predictions, label='RNN Test Predictions', linestyle='--')plt.legend()
plt.title("Training Set Predictions")
plt.xlabel("Time Step")
plt.ylabel("Value")
plt.show()

繪圖Matplotlib

② 利用Seaborn繪圖

將數據轉換為DataFrame

original = pd.DataFrame(origin_data)
df_train_predictions = pd.DataFrame(train_predictions)
df_test_predictions = pd.DataFrame(test_predictions)

執行繪圖程序

import seaborn as sns
sns.set_style("darkgrid") fig = plt.figure(figsize=(16, 6))
fig.subplots_adjust(hspace=0.2, wspace=0.2)# 畫左邊的趨勢圖
plt.subplot(1, 2, 1)
ax = sns.lineplot(x = original.index, y = original[0], label="Original Data", color='blue')
ax = sns.lineplot(x = df_train_predictions.index + seq_length, y = df_train_predictions[0], label="RNN Train Prediction", color='red')
ax = sns.lineplot(x = df_test_predictions.index + seq_length + train_length, y = df_test_predictions[0], label="RNN Test Prediction", color='darkred')ax.set_title('Stock', size = 14, fontweight='bold')
ax.set_xlabel("Days", size = 14)
ax.set_ylabel("Value", size = 14)
ax.set_xticklabels('', size=10)# 畫右邊的Loss下降圖
plt.subplot(1, 2, 2)
ax = sns.lineplot(data=rnn_final_losses, label="RNN Loss", color='red')
ax = sns.lineplot(data=lstm_final_losses, label="LSTM Loss", color='darkblue')
ax = sns.lineplot(data=gru_final_losses, label="GRU Loss", color='black')
ax.set_xlabel("Epoch", size = 14)
ax.set_ylabel("Loss", size = 14)
ax.set_title("Training Loss", size = 14, fontweight='bold')
plt.show()

Seaborn繪圖

3.2 Amazon 數據集

3.2.2 數據集介紹

Amazon股價數據,經常用于學習機器學習算法。

Amazon

3.2.3 訓練結果

代碼與IBM數據集類似,這里直接展示運行結果。


訓練過程

訓練

Matplotlib繪圖

繪圖Matplotlib

Seaborn繪圖

繪圖Seaborn

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/72348.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/72348.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/72348.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【JSON2WEB】15 銀河麒麟操作系統下部署JSON2WEB

【JSON2WEB】系列目錄 【JSON2WEB】01 WEB管理信息系統架構設計 【JSON2WEB】02 JSON2WEB初步UI設計 【JSON2WEB】03 go的模板包html/template的使用 【JSON2WEB】04 amis低代碼前端框架介紹 【JSON2WEB】05 前端開發三件套 HTML CSS JavaScript 速成 【JSON2WEB】06 JSO…

地基簡識Spring MVC 組件

Spring MVC 是一個基于 MVC 設計模式的框架&#xff0c;其核心組件協同工作以處理 HTTP 請求并生成響應。以下是各組件的詳細說明及其協作流程&#xff1a; 一、?核心組件 ?DispatcherServlet&#xff08;前端控制器&#xff09;? ?作用&#xff1a;接收所有請求并協調其他…

Spring Boot(七):Swagger 接口文檔

1. Swagger 簡介 1.1 Swagger 是什么&#xff1f; Swagger 是一款 RESTful 風格的接口文檔在線自動生成 功能測試功能軟件。Swagger 是一個規范和完整的框架&#xff0c;用于生成、描述、調用和可視化 RESTful 風格的 Web 服務。目標是使客戶端和文件系統作為服務器以同樣的…

cursor 彈出在簽出前,請清理倉庫工作樹 窗口

問題出現的背景&#xff1a;是因為我有兩臺電腦開發&#xff0c;提交后&#xff0c;另一個電腦的代碼是舊的&#xff0c;這個時候我想拉取最新的代碼&#xff0c;就會出現如下彈窗&#xff0c;因為這個代碼暫存區有記錄或者工作區有代碼的修改&#xff0c;所以有沖突&#xff0…

Cocos Creator3.8.6拖拽物體的幾種方式

文章目錄 前言一、第一種通過UILocation二、第二種通過UIDelta實現總結 前言 在游戲開發中&#xff0c;拖拽物體是一個非常常見的交互功能&#xff0c;無論是用于UI元素的拖動&#xff0c;還是場景中物體的移動&#xff0c;拖拽操作都能極大地提升用戶體驗。Cocos Creator 3.8…

在 Mac mini M2 上本地部署 DeepSeek-R1:14B:使用 Ollama 和 Chatbox 的完整指南

隨著人工智能技術的飛速發展&#xff0c;本地部署大型語言模型&#xff08;LLM&#xff09;已成為許多技術愛好者的熱門選擇。本地部署不僅能夠保護隱私&#xff0c;還能提供更靈活的使用體驗。本文將詳細介紹如何在 Mac mini M2&#xff08;24GB 內存&#xff09;上部署 DeepS…

《UE5_C++多人TPS完整教程》學習筆記33 ——《P34 關卡與大廳之間的過渡(Transition Level And Lobby)》

本文為B站系列教學視頻 《UE5_C多人TPS完整教程》 —— 《P34 關卡與大廳之間的過渡&#xff08;Transition Level And Lobby&#xff09;》 的學習筆記&#xff0c;該系列教學視頻為計算機工程師、程序員、游戲開發者、作家&#xff08;Engineer, Programmer, Game Developer,…

Nginx 配置與常用命令速查手冊

Nginx 配置文件結構 Linux 中 Nginx 的配置文件&#xff1a; 通常位于 /etc/nginx/nginx.conf 或 /usr/local/nginx/conf/nginx.conf。 Nginx 采用模塊化設計&#xff0c;主要分為以下部分&#xff1a; 1. 全局塊 配置與服務器整體相關的參數&#xff0c;如工作進程數、日…

Vscode 便用快捷鍵設置教程

文章目錄 簡介&#xff1a;1. go to define (跳轉到函數定義的位置)2. go to declaration (跳轉到函數聲明的位置)3. move line &#xff08;上下移動本行代碼&#xff09;3.1上下復制本行代碼 4. 前進和后退&#xff08;就是前進到光標上一次停留的位置&#xff0c;和后退到那…

Vim 常用快捷鍵大全:跳轉、編輯、查找替換全解析

摘要&#xff1a; Vim 是一款非常強大的文本編輯器&#xff0c;許多程序員和系統管理員都離不開它。 本文詳細介紹了 Vim 編輯器中的常用快捷鍵和命令&#xff0c;從基本模式、光標移動、編輯操作到查找替換&#xff0c;再到文件保存等常用操作&#xff0c;幫助你快速上手并提…

【實戰篇】【深度解析DeepSeek:從機器學習到深度學習的全場景落地指南】

一、機器學習模型:DeepSeek的降維打擊 1.1 監督學習與無監督學習的"左右互搏" 監督學習就像學霸刷題——給標注數據(參考答案)訓練模型。DeepSeek在信貸風控場景中,用邏輯回歸模型分析百萬級用戶數據,通過特征工程挖掘出"凌晨3點頻繁申請貸款"這類魔…

Vue核心知識:Vue動態權限到按鈕完整方案

為了進一步實現上面提到的動態路由功能&#xff0c;并且加入對每個路由的權限控制&#xff08;即增、刪、改、查按鈕的權限控制&#xff09;&#xff0c;我們需要對數據庫、后端接口、前端的設計做一些改進和擴展。下面我將詳細描述如何在現有方案的基礎上加入對路由的增、刪、…

swift 開發效率提升工具

安裝github copliot for xcode github/CopilotForXcode brew install --cask github-copilot-for-xcode安裝swiftformat for xcode brew install swiftformatXcode Swift File代碼格式化-SwiftFormat

Trae智能協作AI編程工具IDE:如何在MacBook Pro下載、安裝和配置使用Trae?

Trae智能協作AI編程工具IDE&#xff1a;如何在MacBook Pro下載、安裝和配置使用Trae&#xff1f; 一、為什么選擇Trae智能協作IDE&#xff1f; 在AI編程新時代&#xff0c;Trae通過以下突破性功能重新定義開發體驗&#xff1a; 雙向智能增強&#xff1a;AI不僅提供代碼補全&a…

【推薦項目】023-游泳俱樂部管理系統

023 游泳俱樂部管理系統 游泳俱樂部管理系統概述 前端技術框架&#xff1a; 我們優雅地采用了Vue.js作為游泳俱樂部管理系統的前端基礎框架。Vue.js以其輕盈、高效和易于上手的特點&#xff0c;為我們的用戶界面帶來了極致的流暢性和響應速度。通過Vue.js&#xff0c;我們為…

C語言:51單片機 基礎知識

一、單片機概述 單片機的組成及其特點 單片機是指在一塊芯片上集成了CPU、ROM、RAM、定時器/計數器和多種I/O接口電路等&#xff0c;具有一定規模的微型計算機。 特點&#xff1a; 1、單片機的存儲器以ROM、RAM嚴格分工。 2、采用面向控制的指令系統。 3、單片機的I/O口引腳通…

【計算機網絡入門】初學計算機網絡(八)

目錄 1. S-W協議的信道利用率 2. GBN、SR協議的信道利用率 3.術語補充 3.1 滑動窗口協議 3.2 ARQ協議、連續ARQ協議 4. 信道劃分介質訪問控制 4.1 時分復用&#xff08;TDM&#xff09; 4.2 統計時分復用&#xff08;STDM&#xff09; 4.3 頻分復用&#xff08;FDM&a…

HarmonyOS學習第7天: 文本組件點亮界面的文字魔法棒

一、引言 在 HarmonyOS 那豐富多彩的系統界面中&#xff0c;從簡潔直觀的應用圖標&#xff0c;到交互流暢的操作菜單&#xff0c;再到生動形象的圖文展示&#xff0c;每一處細節都經過精心雕琢&#xff0c;為用戶帶來了獨特而美妙的視覺與交互體驗。而在這琳瑯滿目的界面元素中…

從零開始:H20服務器上DeepSeek R1 671B大模型部署與壓力測試全攻略

前言 最近&#xff0c;我有幸在工作中接觸到了DeepSeek R1 671B模型&#xff0c;這是目前中文開源領域參數量最大的高質量模型之一。DeepSeek團隊在2024年推出的這款模型&#xff0c;以其驚人的6710億參數量和出色的推理性能&#xff0c;引起了業界廣泛關注。 作為一名AI基礎…

Unity中動態切換光照貼圖LightProbe的方法

關鍵代碼&#xff1a;LightmapSettings.lightmaps lightmapDatas; LightmapData中操作三張圖&#xff1a;lightmapColor,lightmapDir,以及一張ShadowMap 這里只操作前兩張&#xff1a; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI;public cl…