小编cha*_*esh的帖子

将多个空格合并到单个空间; 删除尾随/前导空格

我想将多个空格合并到单个空格中(空格也可以是制表符)并删除尾随/前导空格.

例如...

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.

请注意,我无法在玩具串中放置多个空格.谢谢

r pattern-matching

56
推荐指数
5
解决办法
4万
查看次数

张量类型的变量初始值设定项必须包装在 init_scope 或可调用中

我已经为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)

keras tensorflow

12
推荐指数
1
解决办法
3933
查看次数

在比较三个浮点数时双重不等式

有人可以告诉我为什么以下不起作用?(我的意思是没有输出)

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)

c++ floating-point

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

加法和减法不适用于犰狳稀疏矩阵

我不明白为什么+-操作在犰狳稀疏矩阵上不起作用,而*/却正常工作。(根据文档,+并且-应该也可以链接)。

\n\n
#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}\n
Run Code Online (Sandbox Code Playgroud)\n\n

请参阅下面的错误。

\n\n
In 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)

c++ armadillo

0
推荐指数
1
解决办法
1653
查看次数