我想将多个空格合并到单个空格中(空格也可以是制表符)并删除尾随/前导空格.
例如...
string <- "Hi buddy what's up Bro"
Run Code Online (Sandbox Code Playgroud)
至
"Hi buddy what's up bro"
Run Code Online (Sandbox Code Playgroud)
我检查了Regex给出的解决方案,用一个空格替换多个空格.请注意,不要将\ t或\n作为玩具字符串中的精确空间并将其作为模式输入gsub.我希望在R.
请注意,我无法在玩具串中放置多个空格.谢谢
我已经为crf损失计算编写了一个自定义的 keras 损失。
def myLoss(self,y_true, y_pred):
"""
Args:
y_true: a tensor of shape batch_size X num_labels
y_pred: a tensor of shape batch_size X seq_length X num_labels
"""
with tf.init_scope():
self.seqlen = tf.constant(self.batch_size, shape=(self.seq_length,))
log_likelihood, transtion = tfa.text.crf.crf_log_likelihood(y_pred,y_true,self.seqlen )# logits, labels, seq_length
loss = tf.reduce_sum(-log_likelihood)
return loss
Run Code Online (Sandbox Code Playgroud)
但是上面的代码引发了以下问题:
ValueError: Tensor-typed variable initializers must either be wrapped in an init_scope or callable (e.g., `tf.Variable(lambda : tf.truncated_normal([10, 40]))`) when building functions. Please file a feature request if this restriction inconveniences you. …Run Code Online (Sandbox Code Playgroud) 有人可以告诉我为什么以下不起作用?(我的意思是没有输出)
if(0.0001<0.001<0.01)
cout<<"hi\n"<<endl;
output: (blank)
Run Code Online (Sandbox Code Playgroud)
虽然以下工作:
if(0.0001<0.001 && 0.001<0.01)
cout<<"hi\n"<<endl;
output:hi
Run Code Online (Sandbox Code Playgroud) 我不明白为什么+和-操作在犰狳稀疏矩阵上不起作用,而*和/却正常工作。(根据文档,+并且-应该也可以链接)。
#include <iostream>\n#include <stdlib.h>\n#include <math.h>\n#include<armadillo> \n\nusing namespace std;\nusing namespace arma;\n\nint main(int argc, char** argv) {\n sp_mat A(5,6);\n A(0,0) = 1;\n A(1,0) = 2;\n cout << 2 + A << endl;\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n请参阅下面的错误。
\n\nIn file included from /usr/include/c++/4.8/bits/stl_algobase.h:67:0,\n from /usr/include/c++/4.8/bits/char_traits.h:39,\n from /usr/include/c++/4.8/ios:40,\n from /usr/include/c++/4.8/ostream:38,\n from /usr/include/c++/4.8/iostream:39,\n from demo.cpp:1:\n /usr/include/c++/4.8/bits/stl_iterator.h:327:5: note: template<class _Iterator> typename std::reverse_iterator<_Iterator>::difference_type std::operator-(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)\n operator-(const reverse_iterator<_Iterator>& __x,\n ^\n/usr/include/c++/4.8/bits/stl_iterator.h:327:5: note: …Run Code Online (Sandbox Code Playgroud)