小编Gol*_*ame的帖子

“运行时错误:4 维权重 32 3 3 的预期 4 维输入,但得到大小为 [3, 224, 224] 的 3 维输入”?

我正在尝试使用预先训练的模型。这就是问题发生的地方

模型不是应该接收简单的彩色图像吗?为什么它需要 4 维输入?

RuntimeError                              Traceback (most recent call last)
<ipython-input-51-d7abe3ef1355> in <module>()
     33 
     34 # Forward pass the data through the model
---> 35 output = model(data)
     36 init_pred = output.max(1, keepdim=True)[1] # get the index of the max log-probability
     37 

5 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py in forward(self, input)
    336                             _pair(0), self.dilation, self.groups)
    337         return F.conv2d(input, self.weight, self.bias, self.stride,
--> 338                         self.padding, self.dilation, self.groups)
    339 
    340 

RuntimeError: Expected 4-dimensional input for 4-dimensional weight 32 3 3, but got 3-dimensional input of …
Run Code Online (Sandbox Code Playgroud)

machine-learning computer-vision conv-neural-network pytorch torchvision

29
推荐指数
2
解决办法
4万
查看次数

使用 mock.patch 给我 AttributeError("&lt;module 'package1''&gt; does not have the attribute 'myfunc'"?

我正在尝试使用mock.patch 来模拟几个函数。为了便于阅读,我已经缩减了示例。这是带有我的测试用例的 test.py:

from unittest import mock
import pytest
@mock.patch("package1.myfunc")
def test_myfunc(mymock):
    inst1 = MyClass()
    inst1.myfunc()
Run Code Online (Sandbox Code Playgroud)

这是 mycode.py 中的源代码

import package1
class MyClass:
    def__init__(self):
        pass
    def myfunc(self): #wrapper
        package1.myfunc()
    
Run Code Online (Sandbox Code Playgroud)

我这样做正确吗?为什么我会收到属性错误?我没有对“mymock”做任何其他事情的原因是因为我只是希望调用的函数不执行任何操作。我还需要为其添加返回值吗?

详细错误信息:

exc_info = (<class 'AttributeError'>, AttributeError("<module 'package1' from '/.../site-packages/package1/__init__.py'> does not have the attribute 'myfunc'"), <traceback object at 0x7f2b86392640>)
patching = <unittest.mock._patch object at 0x7f2b86a479d0>
    @wraps(func)
    def patched(*args, **keywargs):
        extra_args = []
        entered_patchers = []
   
        exc_info = tuple()
        try:
            for patching in patched.patchings:
>               arg = patching.__enter__()
/.../python3.7/lib/python3.7/unittest/mock.py:1247:
_ _ …
Run Code Online (Sandbox Code Playgroud)

python testing unit-testing pytest python-unittest

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

避免调用成员变量的构造函数

我有以下C++ - 类:

// Header-File
class A
{
    public:
    A();

    private:
    B m_B;
    C m_C;
};

// cpp-File
A::A()
: m_B(1)
{
    m_B.doSomething();
    m_B.doMore();
    m_C = C(m_B.getSomeValue());
}
Run Code Online (Sandbox Code Playgroud)

我现在想避免class A调用任何的构造函数C m_C.因为在最后一行A::A(),我总是要m_C自己初始化,因为我需要先做好准备m_B.我可以提供一个空的默认构造函数class B.但那不是主意.

我已经尝试添加m_C(NULL)到init-list中A::A().有时它有效,有时它说没有构造函数NULL作为参数.

那我怎么能没有m_C初始化?我知道,通过指针,m_C(NULL)-way可行.我不想动态分配它new.

任何想法都表示赞赏.

c++ initializer-list

11
推荐指数
2
解决办法
5944
查看次数

Anaconda 中“基础”(最佳实践)的目的是什么?

它说这是一个默认环境,但“尽管如此,您不想将程序放入您的基本环境中”

那么我到底应该用它做什么呢?我创建的其他环境是否继承自基础?

anaconda conda

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

Huggingface:如何找到模型的最大长度?

给定huggingface上的变压器模型,如何找到最大输入序列长度?

例如,这里我想截断到模型的 max_length:tokenizer(examples["text"], padding="max_length", truncation=True)How do I find the value of "max_length"?

我需要知道,因为我正在尝试解决此错误“要求填充到 max_length 但未提供最大长度,并且模型没有预定义的最大长度。默认为无填充。”

pytorch huggingface-transformers huggingface-tokenizers huggingface

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

为什么 unordered_map 由于“保留”而有足够的桶时会增加大小?

考虑这个代码。我为 unordered_map 保留 6 个点并插入 6 个元素。之后,有7个桶。为什么是这样?max_load_factor 为 1,并且有足够的存储桶用于我插入的元素数量。

#include <iostream>
#include <unordered_map>
using namespace std;

int main () {
  unordered_map<std::string,std::string> mymap = { 
            {"house","maison"},
            {"apple","pomme"},
            {"tree","arbre"},
            {"book","livre"},
            {"door","porte"},
            {"grapefruit","pamplemousse"}
  };

    unordered_map<std::string,std::string> mymap2; // THIS ONE!!!
    mymap2.reserve(6);
    for (auto i:mymap) {
        mymap2[i.first] = i.second;
    }


    std::cout << "max_load factor " << mymap2.max_load_factor() << " mymap has " << mymap2.bucket_count() << " buckets.\n";

      for (unsigned i=0; i<mymap2.bucket_count(); ++i) {
        cout << "bucket #" << i << " contains: ";
        for (auto …
Run Code Online (Sandbox Code Playgroud)

c++ unordered-map hashtable c++14

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

图像大小为 256x256(不是 299x299)输入 Inception v3 模型(PyTorch)并且有效?

我正在 Pytorch 上测试预训练的 inception v3 模型。我给它提供了 256x256 的图像大小,并将其调整为 299x299。在这两种情况下,图像都被正确分类。

有人能解释一下为什么 PyTorch 预训练模型可以接受不是 299x299 的图像吗?

python machine-learning computer-vision deep-learning pytorch

4
推荐指数
2
解决办法
4085
查看次数

在 O(1) 复杂度 C++ 中删除 vector&lt;int&gt; 的最后 n 个元素?

我想以常数时间复杂度 O(1) 删除整数向量的最后 n 个元素。我认为这应该是可行的,因为只需要对结束指针进行一些算术运算。然而,擦除、调整大小和删除都具有 O(n) 复杂度。有什么功能可以使用?

c++ time-complexity stdvector

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

为什么const char*foo ="Hello"; 编译但不是const int*foo = 5;?

如果我错了,请纠正我,但我已经假设了这行代码:

const char *foo = "Hello";
Run Code Online (Sandbox Code Playgroud)

意味着在堆栈的某处创建了一个充满"Hello"的字符数组,而foo是指向它的指针.在这种情况下,我不明白为什么这行代码给出了"从'int'到'const int*'的无效转换"

const int *foo = 5;
Run Code Online (Sandbox Code Playgroud)

它不会只是在堆栈的某处创建一个5的整数并且foo指向它吗?

c c++ arrays pointers

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

为什么 g++ 不能编译元组?

#include <iostream>
#include <tuple>
#include <string>
using namespace std;

int main(){
  tuple<string, string, string> x;
  x = make_tuple("hi", "a", "b");
  cout << get<0>(x) << endl << endl;

}
Run Code Online (Sandbox Code Playgroud)

我的程序遇到了困难,所以我写了一个更简单的程序,即使这样也行不通。我不明白为什么在多次查看文档后会出现问题。它在 XCode 上也能很好地编译,但由于某种原因在 g++ 上出现故障。

这是完整的错误消息:

test.cpp:6:3: 错误:使用未声明的标识符“元组”

元组 x;

^

test.cpp:6:9: 错误:意外的类型名称“字符串”:预期的表达式

元组 x;

    ^
Run Code Online (Sandbox Code Playgroud)

test.cpp:7:3: 错误:使用未声明的标识符“x”

x = make_tuple("hi", "a", "b");

^

test.cpp:7:7: 错误:使用未声明的标识符“make_tuple”

x = make_tuple("hi", "a", "b");

  ^
Run Code Online (Sandbox Code Playgroud)

test.cpp:8:11: 错误:无法解析对重载函数的引用;你的意思是打电话吗?cout << get<0>x << endl << endl;

我使用的命令是 g++ test.cpp

c++

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