torch.nn.RNN()相关的参数设置

郭庆汝 2024-08-21 15:01:02 阅读 96

torch.nn.RNN()相关的参数设置

写一个单层、单向的RNN模型训练实例:写一个单层、双向的RNN模型训练实例:

<code>class torch.nn.RNN(input_size, hidden_size, num_layers=1, nonlinearity='tanh', bias=True, batch_first=False, dropout=0, bidirectional=False, proj_size=0)code>

在这里插入图片描述

<code>import torch

import torch.nn as nn

# 定义 RNN 模型

class RNNModel(nn.Module):

def __init__(self, input_size, hidden_size, num_layers, num_classes):

super(RNNModel, self).__init__()

self.hidden_size = hidden_size

self.num_layers = num_layers

self.rnn = nn.RNN(input_size, hidden_size, num_layers, batch_first=True)

self.fc = nn.Linear(hidden_size, num_classes)

def forward(self, x):

# 初始化隐藏状态

h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(x.device)

# 前向传播

out, _ = self.rnn(x, h0)

# 取最后一个时间步的输出

out = self.fc(out[:, -1, :])

return out

# 超参数设置

input_size = 10 # 输入特征维度

hidden_size = 20 # 隐藏层神经元数量

num_layers = 2 # RNN 层数

num_classes = 5 # 输出类别数

sequence_length = 15 # 序列长度

batch_size = 3 # 批次大小

# 实例化模型

model = RNNModel(input_size, hidden_size, num_layers, num_classes)

# 打印模型结构

print(model)

# 创建输入数据 (batch_size, sequence_length, input_size)

inputs = torch.randn(batch_size, sequence_length, input_size)

# 前向传播

outputs = model(inputs)

# 打印输出

print(outputs)

假设你有一段语音信号,每段语音信号被切分为 15 个时间步,每个时间步包含 10 个特征(例如 MFCC 特征)。你希望使用一个两层的 RNN 模型来处理这些数据,并且最终的输出是 5 个类别中的一个。

input_size = 10 # 每个时间步的特征向量长度

sequence_length = 15 # 每个输入序列的时间步数量

batch_size = 3 # 每个训练批次中的样本数量

# 创建输入数据 (batch_size, sequence_length, input_size)

inputs = torch.randn(batch_size, sequence_length, input_size)

以上代码创建了一个形状为 (3, 15, 10) 的输入张量,表示 3 个样本,每个样本包含 15 个时间步,每个时间步的特征向量长度为 10。这就定义了 input_size 的具体含义,即每个时间步输入特征的维度。

在这里插入图片描述

写一个单层、单向的RNN模型训练实例:

<code>import torch

import torch.nn as nn

import numpy as np

# 1. 数据准备

# 假设我们的输入序列是一个简单的正弦波序列

time_steps = np.linspace(0, np.pi, 100)

data = np.sin(time_steps)

# 我们将使用前90个时间步作为训练数据,后10个时间步用于预测

train_data = data[:90]

test_data = data[90:]

# 转换为 PyTorch 张量

train_data = torch.FloatTensor(train_data).view(-1)

test_data = torch.FloatTensor(test_data).view(-1)

# 创建输入序列和目标序列

def create_inout_sequences(input_data, tw):

inout_seq = []

L = len(input_data)

for i in range(L - tw):

train_seq = input_data[i:i + tw]

train_label = input_data[i + tw:i + tw + 1]

inout_seq.append((train_seq, train_label))

return inout_seq

# 序列长度

seq_length = 10

train_inout_seq = create_inout_sequences(train_data, seq_length)

# 2. 定义 RNN 模型

class RNNModel(nn.Module):

def __init__(self, input_size, hidden_size, output_size):

super(RNNModel, self).__init__()

self.hidden_size = hidden_size

self.rnn = nn.RNN(input_size, hidden_size, batch_first=True)

self.fc = nn.Linear(hidden_size, output_size)

def forward(self, x, hidden):

out, hidden = self.rnn(x, hidden)

out = self.fc(out[:, -1, :])

return out, hidden

def init_hidden(self, batch_size):

return torch.zeros(1, batch_size, self.hidden_size)

# 超参数设置

input_size = 1

hidden_size = 50

output_size = 1

learning_rate = 0.01

# 实例化模型

model = RNNModel(input_size, hidden_size, output_size)

# 定义损失函数和优化器

criterion = nn.MSELoss()

optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

# 3. 训练模型

epochs = 200

for epoch in range(epochs):

for seq, labels in train_inout_seq:

optimizer.zero_grad()

seq = seq.view(1, -1, input_size)

labels = labels.view(1, -1, output_size)

hidden = model.init_hidden(1)

y_pred, hidden = model(seq, hidden)

loss = criterion(y_pred, labels)

loss.backward()

optimizer.step()

if epoch % 10 == 0:

print(f'Epoch { epoch + 1}/{ epochs}, Loss: { loss.item()}')

# 4. 预测

model.eval()

test_inputs = train_data[-seq_length:].tolist()

model.hidden = model.init_hidden(1)

for i in range(len(test_data)):

seq = torch.FloatTensor(test_inputs[-seq_length:])

seq = seq.view(1, -1, input_size)

with torch.no_grad():

model.hidden = model.init_hidden(1)

y_pred, model.hidden = model(seq, model.hidden)

test_inputs.append(y_pred.item())

# 将结果转换为 numpy 数组

predicted = np.array(test_inputs[seq_length:])

# 5. 绘图

import matplotlib.pyplot as plt

plt.figure(figsize=(10, 5))

plt.plot(time_steps, data, label='True Data')code>

plt.plot(time_steps[90:], predicted, label='Predicted Data', linestyle='--')code>

plt.legend()

plt.show()

写一个单层、双向的RNN模型训练实例:

import torch

import torch.nn as nn

import numpy as np

import matplotlib.pyplot as plt

# 1. 数据准备

# 假设我们的输入序列是一个简单的正弦波序列

time_steps = np.linspace(0, np.pi, 100)

data = np.sin(time_steps)

# 我们将使用前90个时间步作为训练数据,后10个时间步用于预测

train_data = data[:90]

test_data = data[90:]

# 转换为 PyTorch 张量

train_data = torch.FloatTensor(train_data).view(-1)

test_data = torch.FloatTensor(test_data).view(-1)

# 创建输入序列和目标序列

def create_inout_sequences(input_data, tw):

inout_seq = []

L = len(input_data)

for i in range(L-tw):

train_seq = input_data[i:i+tw]

train_label = input_data[i+tw:i+tw+1]

inout_seq.append((train_seq, train_label))

return inout_seq

# 序列长度

seq_length = 10

train_inout_seq = create_inout_sequences(train_data, seq_length)

# 2. 定义双向 RNN 模型

class BiRNNModel(nn.Module):

def __init__(self, input_size, hidden_size, output_size):

super(BiRNNModel, self).__init__()

self.hidden_size = hidden_size

self.rnn = nn.RNN(input_size, hidden_size, batch_first=True, bidirectional=True)

self.fc = nn.Linear(hidden_size * 2, output_size) # *2 because of bidirectional

def forward(self, x, hidden):

out, hidden = self.rnn(x, hidden)

out = self.fc(out[:, -1, :]) # We take the output from the last time step

return out, hidden

def init_hidden(self, batch_size):

# Because it's bidirectional, we need to initialize two hidden states

return torch.zeros(2, batch_size, self.hidden_size)

# 超参数设置

input_size = 1

hidden_size = 50

output_size = 1

learning_rate = 0.01

# 实例化模型

model = BiRNNModel(input_size, hidden_size, output_size)

# 定义损失函数和优化器

criterion = nn.MSELoss()

optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

# 3. 训练模型

epochs = 100

for epoch in range(epochs):

for seq, labels in train_inout_seq:

optimizer.zero_grad()

seq = seq.view(1, -1, input_size)

labels = labels.view(1, -1, output_size)

hidden = model.init_hidden(1)

y_pred, hidden = model(seq, hidden)

loss = criterion(y_pred, labels)

loss.backward()

optimizer.step()

if epoch % 10 == 0:

print(f'Epoch { epoch+1}/{ epochs}, Loss: { loss.item()}')

# 4. 预测

model.eval()

test_inputs = train_data[-seq_length:].tolist()

model.hidden = model.init_hidden(1)

for i in range(len(test_data)):

seq = torch.FloatTensor(test_inputs[-seq_length:])

seq = seq.view(1, -1, input_size)

with torch.no_grad():

model.hidden = model.init_hidden(1)

y_pred, model.hidden = model(seq, model.hidden)

test_inputs.append(y_pred.item())

# 将结果转换为 numpy 数组

predicted = np.array(test_inputs[seq_length:])

# 5. 绘图

plt.figure(figsize=(10, 5))

plt.plot(time_steps, data, label='True Data')code>

plt.plot(time_steps[90:], predicted, label='Predicted Data', linestyle='--')code>

plt.legend()

plt.show()

在这里插入图片描述



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。