使用复发性神经网络进行二进制值预测

发布于 2025-01-21 21:36:27 字数 2530 浏览 0 评论 0原文

编辑:所述的问题已经解决,您将首先找到解决方案,最初的问题如下所示!

解决方案:

.unsqueeze(0)应用于我的输入,解决了尺寸的问题。

第二个问题:

RuntimeError: expected scalar type Double but found Float

这是由我的培训集的错误数据型引起的。网络想要x_train as dtype = type = Torch.float32y y ,但是AS dtype = type = torch.long。但是,我不知道这样的解释,也许有人可以作为评论回答。

另外,由于我的数据具有35个功能,因此模型的input_size将其更改为35。输出已固定在2上,因为我确实想要一个预测1 for Good和0 for Bad。

model = RNN(35, hidden_size, num_layers, 2).to(device)

问题:

我试图根据每个样本35个功能的训练集预测每个样本的10个不同的二进制值。我的方法是使用具有2层的RNN,但无法正确实施。当我尝试使用64号批次训练模型时,回报简单是

RuntimeError: input.size(-1) must be equal to input_size. Expected 64, got 35

如果我确实将批次大小设置为35以查看下一步,我会收到

RuntimeError: expected scalar type Double but found Float

为什么该模型将功能使用作为输入尺寸,以及在哪里可以双重问题来自其他论坛或帖子中没有找到类似的符号。

预先感谢您的帮助。

RNN:

class RNN(nn.Module):
    def __init__(self, input_size, hidden_size ,num_layers, output_size):
        super(RNN, self).__init__()
        self.num_layers = num_layers
        self.hidden_size = hidden_size
        self.rnn = nn.RNN(input_size, hidden_size, num_layers, nonlinearity='relu',batch_first=True)

        self.fc = nn.Linear(hidden_size, output_size)
        self.softmax = nn.Softmax(dim=1)
        

    def forward(self, x):
        
        # self.hidden_size).to(device)
        h = torch.zeros(self.num_layers, x.size(0), self.hidden_size)

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

        out = out.contiguous().view(-1, self.hidden_size)

        out = self.fc(out)

        out = self.softmax(out)

        return out

来实现模型

model = RNN(batch_size, hidden_size, num_layers, output_size).to(device)

通过:使用训练循环

for epoch in range(num_epochs):
    for index, (inputs, labels) in enumerate(dataloader):  
        # forward, backward, update
        print(f'inputs {inputs.shape}')
        

        # Forward pass
        outputs = model(inputs.unsqueeze(1))
        loss = criterion(outputs, labels)
        
        # Backward and optimize
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        
        print (f'Epoch [{epoch+1}/{num_epochs}], Step [{index+1}/{n_total_steps}], Loss: {loss.item():.4f}')

:使用启动循环以添加输入维度,因为该模型需要3个维度,但是数据集只有2个维度

EDIT: The problems stated have been solved, you'll first find the solution, the initial question is stated below!

SOLUTION:

Applying the .unsqueeze(0) to my inputs solved the problem with the dimensions.

Second Problem:

RuntimeError: expected scalar type Double but found Float

This was caused by a wrong datatype of my training set. The networks wanted the X_train as dtype=torch.float32, the y however as dtype=torch.long. I do not know an explanation for this however, maybe someone could answer this as a comment.

Also, the input_size for the model was changed to 35, as my data has 35 features. The output was fixed to 2, since I do want a prediction 1 for good and 0 for bad.

model = RNN(35, hidden_size, num_layers, 2).to(device)

PROBLEM:

I'm trying to predict 10 different binary values for every sample based on a training set with 35 features per sample. My approach was using an RNN with 2 layers, but fail to implement it properly. As I try to train the model with batches of size 64, the return simply is

RuntimeError: input.size(-1) must be equal to input_size. Expected 64, got 35

If I do set the batch size to 35 to see what's next, I receive the

RuntimeError: expected scalar type Double but found Float

Why does the model use the features as input size and where could the double problem come from, as I did not find similar notations in other forums or posts.

Thank you in advance for your help.

The RNN:

class RNN(nn.Module):
    def __init__(self, input_size, hidden_size ,num_layers, output_size):
        super(RNN, self).__init__()
        self.num_layers = num_layers
        self.hidden_size = hidden_size
        self.rnn = nn.RNN(input_size, hidden_size, num_layers, nonlinearity='relu',batch_first=True)

        self.fc = nn.Linear(hidden_size, output_size)
        self.softmax = nn.Softmax(dim=1)
        

    def forward(self, x):
        
        # self.hidden_size).to(device)
        h = torch.zeros(self.num_layers, x.size(0), self.hidden_size)

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

        out = out.contiguous().view(-1, self.hidden_size)

        out = self.fc(out)

        out = self.softmax(out)

        return out

Implementing the model by:

model = RNN(batch_size, hidden_size, num_layers, output_size).to(device)

and using the training loop:

for epoch in range(num_epochs):
    for index, (inputs, labels) in enumerate(dataloader):  
        # forward, backward, update
        print(f'inputs {inputs.shape}')
        

        # Forward pass
        outputs = model(inputs.unsqueeze(1))
        loss = criterion(outputs, labels)
        
        # Backward and optimize
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        
        print (f'Epoch [{epoch+1}/{num_epochs}], Step [{index+1}/{n_total_steps}], Loss: {loss.item():.4f}')

The unsqueeze is used in order to add a dimension of the input, as the model requires 3 dimensions, but only 2 were given by the dataset

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽蝶幻影 2025-01-28 21:36:27

网络定义应独立于批处理大小。输入_size是功能数量。

The Network definition should be independent of the batch size. The input_size is the number of features.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文