present_state = states[:, :1].contiguous()中present_state的形状
时间: 2024-04-04 16:31:59 浏览: 49
假设`states`是一个形状为`(b, s, c, h, w)`的5维张量,其中`b`表示batch size,`s`表示时间步,`c`表示通道数,`h`和`w`表示空间维度。那么,`present_state`的形状是`(b, 1, c, h, w)`。
具体来说,`present_state`是由`states`沿时间步的维度取第一个时间步的数据得到的,因此其时间步的维度为1,而其他维度与`states`的相同。这里使用`contiguous()`函数是为了保证`present_state`在内存中是连续存储的,以便后续的计算。
相关问题
if self.n_future > 0: present_state = states[:, :1].contiguous() if self.cfg.PROBABILISTIC.ENABLED: # Do probabilistic computation sample, output_distribution = self.distribution_forward( present_state, future_distribution_inputs, noise ) output = {**output, **output_distribution} # Prepare future prediction input b, _, _, h, w = present_state.shape hidden_state = present_state[:, 0] if self.cfg.PROBABILISTIC.ENABLED: future_prediction_input = sample.expand(-1, self.n_future, -1, -1, -1) else: future_prediction_input = hidden_state.new_zeros(b, self.n_future, self.latent_dim, h, w) # Recursively predict future states future_states = self.future_prediction(future_prediction_input, hidden_state) # Concatenate present state future_states = torch.cat([present_state, future_states], dim=1)这段代码得到什么?
这段代码的作用是进行未来状态的预测,输出一个字典 `output`,其中包含模型的预测结果。具体来说,这段代码做了以下几件事情:
1. 从输入的 `states` 中取出当前时刻的状态 `present_state`,即第一个时间步的状态。
2. 如果在模型的配置中启用了概率计算(`self.cfg.PROBABILISTIC.ENABLED` 为 True),则进行概率计算,并得到预测结果的采样值 `sample` 和输出分布 `output_distribution`。
3. 根据当前状态,准备好未来预测的输入数据 `future_prediction_input`,其形状为 `(batch_size, n_future, latent_dim, height, width)`,其中 `batch_size` 为批大小,`n_future` 为未来状态的时间步数,`latent_dim` 为隐藏状态的维度,`height` 和 `width` 分别为输入数据的高度和宽度。
4. 使用 `future_prediction` 函数递归地进行未来状态预测,其中 `future_prediction_input` 为输入数据,`hidden_state` 为隐藏状态,输出为 `future_states`,其形状为 `(batch_size, n_future, latent_dim, height, width)`。
5. 将当前状态 `present_state` 和预测的未来状态 `future_states` 进行拼接,得到完整的预测结果 `future_states`,其形状为 `(batch_size, n_future+1, latent_dim, height, width)`。
6. 将预测结果 `future_states` 加入到输出字典 `output` 中,返回该字典。
这段代码中加一个test loss功能 class LSTM(nn.Module): def __init__(self, input_size, hidden_size, num_layers, output_size, batch_size, device): super().__init__() self.device = device self.input_size = input_size self.hidden_size = hidden_size self.num_layers = num_layers self.output_size = output_size self.num_directions = 1 # 单向LSTM self.batch_size = batch_size self.lstm = nn.LSTM(self.input_size, self.hidden_size, self.num_layers, batch_first=True) self.linear = nn.Linear(65536, self.output_size) def forward(self, input_seq): h_0 = torch.randn(self.num_directions * self.num_layers, self.batch_size, self.hidden_size).to(self.device) c_0 = torch.randn(self.num_directions * self.num_layers, self.batch_size, self.hidden_size).to(self.device) output, _ = self.lstm(input_seq, (h_0, c_0)) pred = self.linear(output.contiguous().view(self.batch_size, -1)) return pred if __name__ == '__main__': # 加载已保存的模型参数 saved_model_path = '/content/drive/MyDrive/危急值/model/dangerous.pth' device = 'cuda:0' lstm_model = LSTM(input_size=1, hidden_size=64, num_layers=1, output_size=3, batch_size=256, device='cuda:0').to(device) state_dict = torch.load(saved_model_path) lstm_model.load_state_dict(state_dict) dataset = ECGDataset(X_train_df.to_numpy()) dataloader = DataLoader(dataset, batch_size=256, shuffle=True, num_workers=0, drop_last=True) loss_fn = nn.CrossEntropyLoss() optimizer = optim.SGD(lstm_model.parameters(), lr=1e-4) for epoch in range(200000): print(f'epoch:{epoch}') lstm_model.train() epoch_bar = tqdm(dataloader) for x, y in epoch_bar: optimizer.zero_grad() x_out = lstm_model(x.to(device).type(torch.cuda.FloatTensor)) loss = loss_fn(x_out, y.long().to(device)) loss.backward() epoch_bar.set_description(f'loss:{loss.item():.4f}') optimizer.step() if epoch % 100 == 0 or epoch == epoch - 1: torch.save(lstm_model.state_dict(), "/content/drive/MyDrive/危急值/model/dangerous.pth") print("权重成功保存一次")
以下是加入 Test Loss 功能后的代码:
```python
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
class LSTM(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, output_size, batch_size, device):
super().__init__()
self.device = device
self.input_size = input_size
self.hidden_size = hidden_size
self.num_layers = num_layers
self.output_size = output_size
self.num_directions = 1 # 单向LSTM
self.batch_size = batch_size
self.lstm = nn.LSTM(self.input_size, self.hidden_size, self.num_layers, batch_first=True)
self.linear = nn.Linear(65536, self.output_size)
def forward(self, input_seq):
h_0 = torch.randn(self.num_directions * self.num_layers, self.batch_size, self.hidden_size).to(self.device)
c_0 = torch.randn(self.num_directions * self.num_layers, self.batch_size, self.hidden_size).to(self.device)
output, _ = self.lstm(input_seq, (h_0, c_0))
pred = self.linear(output.contiguous().view(self.batch_size, -1))
return pred
if __name__ == '__main__':
# 加载已保存的模型参数
saved_model_path = '/content/drive/MyDrive/危急值/model/dangerous.pth'
device = 'cuda:0'
lstm_model = LSTM(input_size=1, hidden_size=64, num_layers=1, output_size=3, batch_size=256, device=device).to(device)
state_dict = torch.load(saved_model_path)
lstm_model.load_state_dict(state_dict)
# 加载测试数据集
test_dataset = ECGDataset(X_test_df.to_numpy())
test_loader = DataLoader(test_dataset, batch_size=256, shuffle=False, num_workers=0, drop_last=False)
# 定义损失函数和优化器
loss_fn = nn.CrossEntropyLoss()
optimizer = optim.SGD(lstm_model.parameters(), lr=1e-4)
# 开始训练
for epoch in range(200000):
print(f'epoch:{epoch}')
lstm_model.train()
epoch_bar = tqdm(dataloader)
for x, y in epoch_bar:
optimizer.zero_grad()
x_out = lstm_model(x.to(device).type(torch.cuda.FloatTensor))
loss = loss_fn(x_out, y.long().to(device))
loss.backward()
epoch_bar.set_description(f'loss:{loss.item():.4f}')
optimizer.step()
# 计算测试集上的平均损失
test_loss = 0.0
lstm_model.eval()
with torch.no_grad():
for x, y in test_loader:
x_out = lstm_model(x.to(device).type(torch.cuda.FloatTensor))
loss = loss_fn(x_out, y.long().to(device))
test_loss += loss.item() * x.size(0)
test_loss /= len(test_dataset)
print(f'Test Loss: {test_loss:.4f}')
if epoch % 100 == 0 or epoch == epoch - 1:
torch.save(lstm_model.state_dict(), "/content/drive/MyDrive/危急值/model/dangerous.pth")
print("权重成功保存一次")
```
在这个代码中,我们首先加载了测试数据集 `test_dataset` 和测试数据加载器 `test_loader`。在每个 epoch 完成后,我们计算测试集上的平均损失值。我们使用 `lstm_model.eval()` 将模型设为评估模式,并利用 `torch.no_grad()` 避免计算梯度,以加速计算。最后,我们输出测试集上的平均损失值。
阅读全文