我的平台是Mac和C++ 11(或更高版本).我是一名C++初学者,正在处理一个处理中文和英文的个人项目.UTF-8是此项目的首选编码.
我在Stack Overflow上阅读了一些帖子,其中许多人建议std::string在处理UTF-8时使用,并避免wchar_t因为char8_tUTF-8现在没有.
然而,他们没有谈论如何正确地与像函数处理str[i],std::string::size(),std::string::find_first_of()或者std::regex因为这些功能通常面临UTF-8时,返回意外的结果.
我应该继续std::string或切换到std::wstring?如果我应该std::string坚持下去,那么处理上述问题的最佳做法是什么?
我正在尝试使用tensorflow实现跳过思维模型,并在此处放置当前版本.

目前我使用我的机器的一个GPU(总共2个GPU)和GPU信息
2017-09-06 11:29:32.657299: I tensorflow/core/common_runtime/gpu/gpu_device.cc:940] Found device 0 with properties:
name: GeForce GTX 1080 Ti
major: 6 minor: 1 memoryClockRate (GHz) 1.683
pciBusID 0000:02:00.0
Total memory: 10.91GiB
Free memory: 10.75GiB
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试向模型提供数据时,我得到了OOM.我尝试调试如下:
我运行后立即使用以下代码段 sess.run(tf.global_variables_initializer())
logger.info('Total: {} params'.format(
np.sum([
np.prod(v.get_shape().as_list())
for v in tf.trainable_variables()
])))
Run Code Online (Sandbox Code Playgroud)
得到了2017-09-06 11:29:51,333 INFO main main.py:127 - Total: 62968629 params,大概是关于240Mb如果全部使用tf.float32.输出tf.global_variables是
[<tf.Variable 'embedding/embedding_matrix:0' shape=(155229, 200) dtype=float32_ref>,
<tf.Variable 'encoder/rnn/gru_cell/gates/kernel:0' shape=(400, 400) dtype=float32_ref>,
<tf.Variable 'encoder/rnn/gru_cell/gates/bias:0' shape=(400,) dtype=float32_ref>,
<tf.Variable 'encoder/rnn/gru_cell/candidate/kernel:0' shape=(400, …Run Code Online (Sandbox Code Playgroud) #include <fstream>
#include <iostream>
#include <map>
int main(int argc, char** argv) {
try {
std::map<std::string, int> m{{"a", 1}, {"b", 2}};
std::cout << m.at("c") << std::endl;
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在C++中,当检索映射的不存在的键时,异常看起来像map::at: key not found.没有提供关于密钥的信息.
此外,如果正在访问不存在的文件,则会出现std::ios_base::failure类似的异常消息ios_base::clear: unspecified iostream_category error.未提供导致异常的文件名.因此,如果项目中有许多map.at()或ifstream is用途,可能需要花费很长时间才能找出异常的来源.
与此相反,Python可能会告诉你KeyError: 'c'或FileNotFoundError: [Errno 2] No such file or directory: 'foo'.
这只是一个C++约定吗?谢谢.
这个版本的PyTorch似乎PackedSequence为递归神经网络提供了可变长度的输入.但是,我发现正确使用它有点困难.
使用pad_packed_sequence以恢复其通过喂食RNN层的输出pack_padded_sequence,我们得到了一个T x B x N张量outputs,其中T为最大的时间步长,B是批量大小和N是隐藏的尺寸.我发现对于批处理中的短序列,后续输出将全为零.
这是我的问题.
outputs[-1]将给出错误的结果,因为该张量包含许多短序列的零.需要按序列长度构建索引以获取所有序列的单个最后输出.有更简单的方法吗?N x O并将批量输出重新整形T x B x O为TB x O并使用真实目标TB(通常是语言模型中的整数)计算交叉熵损失.在这种情况下,批量输出中的这些零值是否重要?我在emacs中使用js2-mode中的swank-js.我可以用它来完成的node.js内置的或第三方API,如fs.readFile,fs.writeFile,express.use,async.forEach等在JS2模式?如果是的话,最好的设置是什么?
任何帮助表示赞赏:)
我想写一个简单的字符串split函数.
该函数应该采用一个std::basic_string和一个分隔符(可能是一个CharT或std::basic_string),并将结果放入一个ContainerT.
我的第一次尝试是
template <typename StringT, typename DelimiterT, typename ContainerT>
void split(
const StringT &str, const DelimiterT &delimiters, ContainerT &conts) {
conts.clear();
std::size_t start = 0, end;
std::size_t len = delimiters.size();
while ((end = str.find(delimiters, start)) != StringT::npos) {
if (end - start) {
conts.emplace_back(str, start, end - start);
}
start = end + len;
}
if (start != StringT::npos && start < str.size()) {
conts.emplace_back(str, start, str.size() - start); …Run Code Online (Sandbox Code Playgroud) 下面的代码工作正常
#include <iostream>
#include <random>
int main() {
std::default_random_engine generator;
std::normal_distribution<double> distribution(5.0, 2.0);
std::vector<double> v(100);
std::generate(v.begin(), v.end(), [&] () { return distribution(generator); });
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,将lambda的捕获列表更改为[=]or [&, distribution]或or [&, generator]会导致
rand.cpp:9:59: error: no matching function for call to object of type 'const std::normal_distribution<double>'
error: assigning to 'double' from incompatible type 'void'
Run Code Online (Sandbox Code Playgroud)
是否存在无法通过Lambda中的值捕获的某些对象?
我看到了一些关于tensorflow和的基准pytorch.Tensorflow可能更快,但似乎不是更快,甚至有时更慢.
是否有关于静态图和动态图专门测试的基准测试,证明静态图比动态图快得多?
这里给出的例子encoder分别使用了两个优化器decoder。为什么?什么时候这样做?