我有一个向量v1,和一个v2相同大小的布尔向量.我想从v1所有值中删除,以便并行元素v2是false:
vector<int> v3; // assume v1 is vector<int>
for (size_t i=0; i<v1.size(); i++)
if (v2[i])
v3.push_back(v1[i]);
v1=v3;
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法呢?
这个问题不同于“如何通过 Conda 安装 Python OpenCV?” 因为这个问题是在 5 年前提出的,当时所有软件包都有不同的版本。我尝试了该问题的所有答案,但都没有奏效。详情见问题正文。
现在如何在 2019 年 7 月使用 conda 安装 opencv?在新安装的 anaconda 上,我做了conda update conda(成功)然后尝试了以下操作:
(base) C:\Users\mlearning>python
Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
>>> import cv
Traceback (most recent call last):
File "<stdin>", line …Run Code Online (Sandbox Code Playgroud) 这是Jupyter的摘录:
在[1]:
import torch, numpy as np, datetime
cuda = torch.device('cuda')
Run Code Online (Sandbox Code Playgroud)
在[2]:
ac = torch.randn(10000, 10000).to(cuda)
bc = torch.randn(10000, 10000).to(cuda)
%time cc = torch.matmul(ac, bc)
print(cc[0, 0], torch.sum(ac[0, :] * bc[:, 0]))
Run Code Online (Sandbox Code Playgroud)
挂墙时间:349毫秒
张量(17.0374,device ='cuda:0')张量(17.0376,device ='cuda:0')
时间很短,但仍然很合理(1e12乘法的时间为0.35秒)
但是,如果我们重复同样的话:
ac = torch.randn(10000, 10000).to(cuda)
bc = torch.randn(10000, 10000).to(cuda)
%time cc = torch.matmul(ac, bc)
print(cc[0, 0], torch.sum(ac[0, :] * bc[:, 0]))
Run Code Online (Sandbox Code Playgroud)
壁挂时间:999 µs
张量(-78.7172,device ='cuda:0')张量(-78.7173,device ='cuda:0')
1e12乘法1ms?!
为什么时间从349ms变为1ms?
信息:
numpy.rint 和 numpy.round/numpy.around 有什么区别?它们似乎都执行相同的功能:
>>> a
array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2. ])
>>> np.round(a)
array([-2., -2., -0., 0., 2., 2., 2.])
>>> np.rint(a)
array([-2., -2., -0., 0., 2., 2., 2.])
Run Code Online (Sandbox Code Playgroud) 我应该写:
template<class T> class Foo {
typename const T* x;
};
Run Code Online (Sandbox Code Playgroud)
或者:
template<class T> class Foo {
const typename T* x;
};
Run Code Online (Sandbox Code Playgroud) 如果我使用相同的 unique_ptr 两次调用 std::move 会发生什么?
unique_ptr<int> foo(new int(5));
unique_ptr<int> bar = move(foo);
unique_ptr<int> baz = move(foo);
Run Code Online (Sandbox Code Playgroud)
PS:不,std::unique_ptr 的用法不是同一个问题。这两个问题都是关于unique_ptr的,这是他们之间唯一的共同点。
在 Python 中:
>>> import numpy as np
>>> x0=np.random.rand(3000,3000)
>>> t=time.perf_counter(); y0=np.matmul(x0,x0); time.perf_counter()-t
0.8358144999947399
>>> import torch
>>> x=torch.rand(3000,3000)
>>> t=time.perf_counter(); y0=np.matmul(x,x); time.perf_counter()-t
0.4304323000833392
Run Code Online (Sandbox Code Playgroud)
在 R 中:
> a=matrix(runif(9000000), 3000, 3000)
> a1=a%*%a
> system.time({a1=a%*%a})
user system elapsed
16.53 0.04 16.57
Run Code Online (Sandbox Code Playgroud)
为什么 numpy 的差异是 20 倍,而 Torch 的差异是 40 倍?
目前我写:
warn = getOption("warn")
options(warn=-1)
foo()
options(warn=warn)
Run Code Online (Sandbox Code Playgroud)
有更好的解决方案吗?
[~] python3
Python 3.6.8 (default, Jan 14 2019, 11:02:34)
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np, cv2
>>> a=np.arange(10,250,10).reshape((6,4))
>>> a
array([[ 10, 20, 30, 40],
[ 50, 60, 70, 80],
[ 90, 100, 110, 120],
[130, 140, 150, 160],
[170, 180, 190, 200],
[210, 220, 230, 240]])
>>> cv2.resize(a,(3,2))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
cv2.error: OpenCV(4.1.0) /io/opencv/modules/imgproc/src/resize.cpp:3596: …Run Code Online (Sandbox Code Playgroud) 有没有相关的问题
vector< vector<int> > v(m, vector<int>(n));
Run Code Online (Sandbox Code Playgroud)
写得更安全:
vector< vector<int> > v(m);
for (size_t i=0; i<m; i++)
v[i].resize(n);
Run Code Online (Sandbox Code Playgroud)