Pytorch 使用LSTM stateful传递上下文b/w批;可能是上下文传递中出现了一些错误,没有得到好的结果?
我在把数据交给网络之前已经检查过了。数据是正确的 使用LSTM并传递上下文b/w批处理。每级精度在变化,但损失不会下降。卡了很长时间,不确定代码中是否有错误? 我有一个基于不平衡数据集的多类分类问题 数据集类型:CSV 数据集大小:20000 基于传感器的CSV数据 X=0.6986111111,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,1,0,0,0,0 Y=利弗豪斯酒店 每级精度: {'leaveHouse':0.34932855,'getDressed':1.0,'idle':0.8074534,'prepareBreakfast':0.8,'goToBed':0.35583413,'getDrink':0.0,'takeShower':1.0,'usecooter':0.0,'eatbreant':0.8857143} 培训: 网络Pytorch 使用LSTM stateful传递上下文b/w批;可能是上下文传递中出现了一些错误,没有得到好的结果?,pytorch,loss-function,imbalanced-data,lstm-stateful,Pytorch,Loss Function,Imbalanced Data,Lstm Stateful,我在把数据交给网络之前已经检查过了。数据是正确的 使用LSTM并传递上下文b/w批处理。每级精度在变化,但损失不会下降。卡了很长时间,不确定代码中是否有错误? 我有一个基于不平衡数据集的多类分类问题 数据集类型:CSV 数据集大小:20000 基于传感器的CSV数据 X=0.6986111111,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,1,0,0,0,0 Y=利弗豪斯酒店 每级精度: {'leaveHouse':0.34932855,'getDre
您可能会遇到的一个问题是CrossEntropyLoss将对数softmax操作与负对数似然损失相结合,但您正在模型中应用softmax。您应该将原始登录从最后一层传递到CrossEntropyLoss
另外,我不能说没有看到模型向前传递,但看起来你正在将维度1上的softmax应用到一个张量,我推断这个张量有形状批次大小,序列长度,输出尺寸,当您应该沿输出dim应用它时。您可能会遇到的一个问题是,CrossEntropyLoss将对数softmax操作与负对数似然损失相结合,但您正在模型中应用softmax。您应该将原始登录从最后一层传递到CrossEntropyLoss
另外,我不能说没有看到模型向前传递,但看起来你正在将维度1上的softmax应用到张量,我推断它具有形状批次大小、序列长度、输出尺寸,当你应该沿着输出尺寸应用它时。好的。交叉熵是隐式的,我不知道。如果我删除softmax,则应注意1点和2点,是吗?我在代码部分添加了forward方法。此外,上下文传递是否正确?我是LSTM的新手,谢谢!损失正在改变。我真的很感激你回答了这个问题。交叉熵是隐式的,我不知道。如果我删除softmax,则应注意1点和2点,是吗?我在代码部分添加了forward方法。此外,上下文传递是否正确?我是LSTM的新手,谢谢!损失正在改变。我真的很感激你回答了这个问题。
# Using loss weights, the inverse of class frequency
criterion = nn.CrossEntropyLoss(weight = class_weights)
hn, cn = model.init_hidden(batch_size)
for i, (input, label) in enumerate(trainLoader):
hn.detach_()
cn.detach_()
input = input.view(-1, seq_dim, input_dim)
if torch.cuda.is_available():
input = input.float().cuda()
label = label.cuda()
else:
input = input.float()
label = label
# Forward pass to get output/logits
output, (hn, cn) = model((input, (hn, cn)))
# Calculate Loss: softmax --> cross entropy loss
loss = criterion(output, label)#weig pram
running_loss += loss
loss.backward() # Backward pass
optimizer.step() # Now we can do an optimizer step
optimizer.zero_grad() # Reset gradients tensors
class LSTMModel(nn.Module):
def init_hidden(self, batch_size):
self.batch_size = batch_size
if torch.cuda.is_available():
hn = torch.zeros(self.layer_dim, self.batch_size, self.hidden_dim).cuda()
# Initialize cell state
cn = torch.zeros(self.layer_dim, self.batch_size, self.hidden_dim).cuda()
else:
hn = torch.zeros(self.layer_dim, self.batch_size, self.hidden_dim)
# Initialize cell state
cn = torch.zeros(self.layer_dim, self.batch_size, self.hidden_dim)
return hn, cn
def __init__(self, input_dim, hidden_dim, layer_dim, output_dim, seq_dim):
super(LSTMModel, self).__init__()
# Hidden dimensions
self.hidden_dim = hidden_dim
# Number of hidden layers
self.layer_dim = layer_dim
self.input_dim = input_dim
# Building your LSTM
# batch_first=True causes input/output tensors to be of shape
# (batch_dim, seq_dim, feature_dim)
self.lstm = nn.LSTM(self.input_dim, hidden_dim, layer_dim, batch_first=True)
# Readout layer
self.fc = nn.Linear(hidden_dim, output_dim)
self.relu = nn.ReLU()
self.softmax = nn.Softmax(dim=1)
self.seq_dim = seq_dim
def forward(self, inputs):
# Initialize hidden state with zeros
input, (hn, cn) = inputs
input = input.view(-1, self.seq_dim, self.input_dim)
# time steps
out, (hn, cn) = self.lstm(input, (hn, cn))
# Index hidden state of last time step
out = self.fc(out[:, -1, :])
out = self.softmax(out)
return out, (hn,cn)