小编ttb*_*ttb的帖子

我可以使用break来退出多个嵌套for循环吗?

是否可以使用该break函数退出几个嵌套for循环?如果是这样,你会怎么做呢?你还可以控制休息退出的循环次数吗?

c++ for-loop break nested-loops

282
推荐指数
12
解决办法
21万
查看次数

如何在PyTorch中做矩阵的乘积

在numpy中我可以做一个简单的矩阵乘法,如下所示:

a = numpy.arange(2*3).reshape(3,2)
b = numpy.arange(2).reshape(2,1)
print(a)
print(b)
print(a.dot(b))
Run Code Online (Sandbox Code Playgroud)

但是,当我使用PyTorch Tensors进行此操作时,这不起作用:

a = torch.Tensor([[1, 2, 3], [1, 2, 3]]).view(-1, 2)
b = torch.Tensor([[2, 1]]).view(2, -1)
print(a)
print(a.size())

print(b)
print(b.size())

print(torch.dot(a, b))
Run Code Online (Sandbox Code Playgroud)

此代码抛出以下错误:

RuntimeError:/Users/soumith/code/builder/wheel/pytorch-src/torch/lib/TH/generic/THTensorMath.c:503中的张量大小不一致

有关在PyTorch中如何进行矩阵乘法的任何想法?

python matrix pytorch

40
推荐指数
4
解决办法
6万
查看次数

使用 std::conditional 根据模板参数选择成员类型

这是我的程序,它尝试使用 std::conditional 根据整数模板参数的值设置成员变量的类型。

#include <stdio.h>
#include <type_traits>
using namespace std;

class cool{
public:
  cool(){ printf("Cool!\n");  }
};

class notcool{
public:
  notcool(){ printf("Not cool!\n");  }
};

template<int N>
class myclass{
public:
  typedef std::conditional<N==5,cool,notcool> thetype;
  thetype theinst;
};

int main(){
  printf("Testing\n");
  myclass<5> myc5;
  myclass<6> myc6;
  printf("Done testing\n");
  return 0; 
} 
Run Code Online (Sandbox Code Playgroud)

我希望我的程序给出以下输出:

测试

凉爽的!

不酷!

完成测试

相反,输出是

测试

完成测试

我的编译器是 GCC 4.9,我编译这个程序的方式是使用命令 g++ test -std=c++11 -o test

谁能告诉我为什么程序没有给出我期望的输出?

c++ templates type-traits c++11

3
推荐指数
2
解决办法
1580
查看次数

Tensorflow 数据加载问题

我正在使用一个使用 MNIST 数据集的示例程序。

它尝试使用以下行加载数据集:

dataset = tfds.load(name='mnist', split=split)
Run Code Online (Sandbox Code Playgroud)

但是,这会产生以下错误:

2020-07-30 12:08:17.926262: W tensorflow/core/platform/cloud/google_auth_provider.cc:184] All attempts to get a Google authentication bearer token failed, returning an empty token. Retrieving token from files failed with "Not found: Could not locate the credentials file.". Retrieving token from GCE failed with "Failed precondition: Error executing an HTTP request: libcurl code 6 meaning 'Couldn't resolve host name', error details: Couldn't resolve host 'metadata'".
Traceback (most recent call last):
File "/home/tflynn/pylocal/lib/python3.7/site- 
packages/tensorflow_datasets/core/utils/py_utils.py", line 399, in try_reraise …
Run Code Online (Sandbox Code Playgroud)

tensorflow tensorflow-datasets

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