小编Ama*_*bek的帖子

nn.CrossEntropyLoss()的Pytorch输入

我试图在PyTorch中对简单的0,1标记的数据集执行Logistic回归。标准或损失定义为:criterion = nn.CrossEntropyLoss()。该模型是:model = LogisticRegression(1,2)

我有一个成对的数据点:dat = (-3.5, 0),第一个元素是数据点,第二个元素是相应的标签。
然后,将输入的第一个元素转换为张量:tensor_input = torch.Tensor([dat[0]])
然后我将该模型应用到tensor_input: outputs = model(tensor_input)
然后,将标签转换为张量:tensor_label = torch.Tensor([dat[1]])
现在,当我尝试执行此操作时,事情就中断了:loss = criterion(outputs, tensor_label)。它给出和错误:RuntimeError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

import torch
import torch.nn as nn

class LogisticRegression(nn.Module):
    def __init__(self, input_size, num_classes):
        super(LogisticRegression, self).__init__()
        self.linear = nn.Linear(input_size, num_classes) 

    def forward(self, x):
        out = self.linear(x)
        return out

model = LogisticRegression(1,2) …
Run Code Online (Sandbox Code Playgroud)

logistic-regression pytorch

6
推荐指数
1
解决办法
7795
查看次数

安装python模块IOError:[Errno 13]权限被拒绝:

我尝试通过键入终端使用pip安装python库openpyxl:

    pip install openpyxl
Run Code Online (Sandbox Code Playgroud)

但它没有正确执行.它引发了一个例外:

    Successfully built openpyxl jdcal et-xmlfile
    Installing collected packages: jdcal, et-xmlfile, openpyxl
    Exception:
    Traceback (most recent call last):
    File "/Library/Python/2.7/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
    File "/Library/Python/2.7/site-packages/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
    File "/Library/Python/2.7/site-packages/pip/req/req_set.py", line 784, in install
    **kwargs
    File "/Library/Python/2.7/site-packages/pip/req/req_install.py", line 851, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
    File "/Library/Python/2.7/site-packages/pip/req/req_install.py", line 1064, in move_wheel_files
    isolated=self.isolated,
    File "/Library/Python/2.7/site-packages/pip/wheel.py", line 345, in move_wheel_files
    clobber(source, lib_dir, True)
    File "/Library/Python/2.7/site-packages/pip/wheel.py", line 323, in clobber
    shutil.copyfile(srcfile, destfile)
    File …
Run Code Online (Sandbox Code Playgroud)

python pip permission-denied

1
推荐指数
1
解决办法
1万
查看次数