在C++中,假设我有一些x类型T,可以是整数或浮点类型.我想找到数量最多y类型的T为其y < x持有.需要模板化解决方案以使用整数和浮点数透明地工作.您可以忽略边缘情况,其中x已经是可以在a中表示的最小数字T.
可能的使用案例:这个问题被标记为过于本地化,因此我想提供一个我认为更通用的用例.请注意,我不是OP的原作者.
考虑这个结构:
struct lower_bound {
lower_bound(double value, bool open) : value(open? value+0.1 : value) {}
double value;
bool operator()(double x) { return x >= value; }
};
Run Code Online (Sandbox Code Playgroud)
此类模拟下限,可以打开或关闭.当然,在真实(双关语)生活中,我们不能这样做.流动是不可能的(或至少相当棘手)来计算S是所有实数.

但是,当S是一组浮点数时,这是一个非常有效的原则,因为我们基本上处理的是一个可数集; 然后没有开放或封闭的界限.也就是说,> =可以按照类似于lower_bound类的方式定义.
为了简化代码,我使用+0.1来模拟一个开放的下界.当然,0.1是原始值,因为可能存在值z,使得值<z <=值+ 0.1或值+ 0.1 = =浮点表示中的值.因此@ brett-hale答案非常有用:)
您可以考虑另一个更简单的解决方案:
struct lower_bound {
lower_bound(double value, bool open) : open(open), value(value) {}
bool open;
double value;
bool operator()(double x) { …Run Code Online (Sandbox Code Playgroud) 我实现了一个双向迭代器,但它不是对数据结构进行操作,而是返回一个数学系列,可以在两个方向上迭代计算.事实上,我正在迭代整数,使用++和 - 在int上.这意味着数据不存储在不同的结构中,因此当迭代器超出范围时,值也是如此.
尽管如此,我希望下一个代码(最小失败示例)示例能够正常工作,因为迭代器始终保持在范围内.但它不起作用:(
#include <iostream>
#include <iterator>
#include <vector>
class my_iterator : public std::iterator<std::bidirectional_iterator_tag, int> {
int d_val = 12;
public:
my_iterator operator--(int) { std::cout << "decrement--\n"; return my_iterator(); }
my_iterator &operator--() { std::cout << "--decrement\n"; return *this; }
my_iterator operator++(int) { std::cout << "increment++\n"; return my_iterator(); }
my_iterator &operator++() { std::cout << "++increment\n"; return *this; }
int &operator*() { std::cout << "*dereference\n"; return d_val; }
bool operator==(my_iterator const &o) { return false; }
bool operator!=(my_iterator const &o) { …Run Code Online (Sandbox Code Playgroud) 我注意到在theano中,当一个人创建一个基于1D numpy数组的共享变量时,这将成为一个向量,但不是一行:
import theano.tensor as T
import theano, numpy
shared_vector = theano.shared(numpy.zeros((10,)))
print(shared_vector.type)
# TensorType(float64, vector)
print(shared_vector.broadcastable)
# (False,)
Run Code Online (Sandbox Code Playgroud)
对于1xN矩阵也是如此,它变成矩阵而不是行:
shared_vector = theano.shared(numpy.zeros((1,10,)))
print(shared_vector.type)
# TensorType(float64, matrix)
print(shared_vector.broadcastable)
# (False, False)
Run Code Online (Sandbox Code Playgroud)
当我想将M×N矩阵添加到1 XN行向量时,这很麻烦,因为共享向量在第一维中不可广播.首先,这不起作用:
row = T.row('row')
mat=T.matrix('matrix')
f=theano.function(
[],
mat + row,
givens={
mat: numpy.zeros((20,10), dtype=numpy.float32),
row: numpy.zeros((10,), dtype=numpy.float32)
},
on_unused_input='ignore'
)
Run Code Online (Sandbox Code Playgroud)
有错误:
TypeError: Cannot convert Type TensorType(float32, vector) (of Variable <TensorType(float32, vector)>) into Type TensorType(float32, row). You can try to manually convert <TensorType(float32, vector)> into a TensorType(float32, row).
Run Code Online (Sandbox Code Playgroud)
好的,很清楚,我们不能将向量分配给行.不幸的是,这也不是很好: …
考虑这个包含两个相似函数的文件:
#include <iostream>
int main()
{
std::cout << "main\n";
}
int notmain()
{
std::cout << "notmain\n";
}
Run Code Online (Sandbox Code Playgroud)
我把它编译成一个共享库:
g++ -shared -Wl,-soname,code -o code.so -fPIC code.cpp
Run Code Online (Sandbox Code Playgroud)
我想从 python 中调用这些,因为main这很好用:
g++ -shared -Wl,-soname,code -o code.so -fPIC code.cpp
Run Code Online (Sandbox Code Playgroud)
哪个打印main。但是,notmain不起作用:
import ctypes
libc = ctypes.cdll.LoadLibrary("code.so")
libc.main()
Run Code Online (Sandbox Code Playgroud)
输出:
import ctypes
libc = ctypes.cdll.LoadLibrary("code.so")
libc.notmain()
Run Code Online (Sandbox Code Playgroud)
我假设 main 以不同于notmain因为main是 c++ 规范中的特殊情况的方式“导出”到外部世界(wrt code.so)。我怎样才能notmain以同样的方式“导出” ?或者:我该如何修复异常?
编辑正如我添加estern "C"到的@abdallahesam 所建议的那样notmain,这并没有改变(或解决)问题:
<ipython-input-63-d6bcf8b748de> in <module>()
----> …Run Code Online (Sandbox Code Playgroud) (编辑 wrt @ quirk的回答)
我在线阅读一些tensorflow代码并看到了这些陈述:
threshold = tf.select(input > RLSA_THRESHOLD, positive, negative)
Run Code Online (Sandbox Code Playgroud)
来源:https://github.com/Raverss/tensorflow-RLSA-NMS/blob/master/source.py#L31
positive是一个张量只是1's,negative同样大小的0's和输入是一些相同大小的热图(/张量)(所有类型tf.float32).
代码片段似乎合理地提前让我假设tf.cast(input > RLSA_THRESHOLD, tf.float32)如果没有特定的tf.select(...)表达原因,作者会使用它.特别是因为这会消除了对变量的需求positive和negative,并会节省内存,因为它们只是昂贵的冗余存储的方式0和1.
上述tf.select(...)表达式是否相当于tf.cast(input > RLSA_THRESHOLD, tf.float32)?如果没有,为什么不呢?
注意:我通常使用Keras,如果我在这里触摸一些非常微不足道的事情,我很抱歉.
获取“无效请求 [0].createImage:禁止访问提供的图像。” 将图像从谷歌驱动器添加到谷歌幻灯片 下面是我尝试过的代码片段,尽管通过高级共享选项公开图像。服务帐户对我要添加图像的幻灯片具有编辑权限。
import section: add here the required dependencies
# python build-in modules
import json
import time
import io
# external modules
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient.discovery import build
from apiclient.http import MediaFileUpload
# user modules
from via_types import BasePlugin
class GoogleSlidesPlugin(BasePlugin):
"""
this plugin allows to communicate with Google API and Google Spreadsheets
by using the already created credentials
"""
def __init__(self):
BasePlugin.__init__(self)
self.Scopes = ['https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/drive.file', 'https://www.googleapis.com/auth/drive.readonly','https://www.googleapis.com/auth/presentations','https://www.googleapis.com/auth/presentations.readonly']
self.Creds = ServiceAccountCredentials.from_json_keyfile_name(
r'Libraries/credentials/lucky-dahlia-268410-c4c0c57c1908.json', self.Scopes)
self.service = build('slides', 'v1', …Run Code Online (Sandbox Code Playgroud) 我注意到std :: for_each要求它的迭代器满足InputIterator的要求,而后者又需要Iterator然后复制{Contructable,Assignable}.
这不是唯一的事情,std :: for_each实际上使用了复制构造函数(cc)(就我的配置而言,不是赋值).也就是说,从迭代器中删除cc将导致:
error: use of deleted function ‘some_iterator::some_iterator(const some_iterator&)’
Run Code Online (Sandbox Code Playgroud)
为什么std :: for_each需要cc?我发现这特别不方便,因为我创建了一个迭代器,它递归地遍历文件夹中的文件,跟踪队列中的文件和文件夹.这意味着迭代器有一个队列数据成员,如果使用cc,也必须复制它:这是不必要的低效率.
奇怪的是,在这个简单的例子中没有调用cc:
#include <iostream>
#include <iterator>
#include <algorithm>
class infinite_5_iterator
:
public std::iterator<std::input_iterator_tag, int>
{
public:
infinite_5_iterator() = default;
infinite_5_iterator(infinite_5_iterator const &) {std::cout << "copy constr "; }
infinite_5_iterator &operator=(infinite_5_iterator const &) = delete;
int operator*() { return 5; }
infinite_5_iterator &operator++() { return *this; }
bool operator==(infinite_5_iterator const &) const { return false; }
bool operator!=(infinite_5_iterator const &) const { return true; }
}; …Run Code Online (Sandbox Code Playgroud) 我创建了一个简单的不可变双向迭代器:
#include <iostream>
#include <memory>
#include <iterator>
#include <vector>
#include <algorithm>
class my_iterator : public std::iterator<std::bidirectional_iterator_tag, int
//, std::ptrdiff_t, int*, int
> {
int d_val;
public:
my_iterator() : d_val(0) {}
my_iterator(int val) : d_val(val) {}
my_iterator operator--(int) { d_val--; return my_iterator(d_val + 1); }
my_iterator &operator--() { d_val--; return *this; }
my_iterator operator++(int) { d_val++; return my_iterator(d_val - 1); }
my_iterator &operator++() { d_val++; return *this; }
int operator*() const { return d_val; }
bool operator==(my_iterator const &o) …Run Code Online (Sandbox Code Playgroud) 我在标准C++和CUDA中创建了一些代码,用于在1300x1300灰度图像和15x15内核上进行2D聚合.两个版本:
中央处理器:
#include <iostream>
#include <exception>
#define N 1300
#define K 15
#define K2 ((K - 1) / 2)
template<int mx, int my>
inline int index(int x, int y)
{
return x*my + y;
}
int main() {
double *image = new double[N * N];
double *kernel = new double[K * K];
double *result = new double[N * N];
for (int x=0; x<N; ++x)
for (int y=0; y<N; ++y)
{
double r = 0;
for(int i=0; i<K; ++i)
for(int j=0; …Run Code Online (Sandbox Code Playgroud) 我想知道为什么+没有为发电机实现该运算符。这里建议使用串联生成器和项 itertools.chain的解决方案,但是我认为该+语法与串联列表一样可读。
gen1 = (x for x in [1,2,3])
gen2 = (x for x in [4,5,6])
# Works:
from itertools import chain
print(' '.join(map(str, chain(gen1, gen2))))
# TypeError: unsupported operand type(s) for +: 'generator' and 'generator'
print(' '.join(map(str, gen1 + gen2)))
Run Code Online (Sandbox Code Playgroud)
是否有(哲学上的)为什么+发电机不可用的原因?我认为这会使代码更具可读性interator.chain(...)。有什么模棱两可的gen1+gen2吗?
c++ ×5
python ×5
c++11 ×3
stl ×3
c++14 ×2
convolution ×1
ctypes ×1
cuda ×1
generator ×1
iterator ×1
math ×1
nvidia ×1
python-3.4 ×1
python-3.x ×1
templates ×1
tensorflow ×1
theano ×1