小编Luc*_*uca的帖子

在bash中解析类似.csv的文件

我有一个格式如下的文件:

string1,string2,string3,...
...
Run Code Online (Sandbox Code Playgroud)

我必须分析第二列,计算每个字符串的出现次数,并生成一个格式如下的文件:

"number of occurrences of x",x
"number of occurrences of y",y        
...
Run Code Online (Sandbox Code Playgroud)

我设法编写以下脚本,工作正常:

#!/bin/bash

> output
regExp='^\s*([0-9]+) (.+)$'
while IFS= read -r line
do
    if [[ "$line" =~ $regExp ]]
    then
        printf "${BASH_REMATCH[1]},${BASH_REMATCH[2]}\n" >> output
    fi
done <<< "`gawk -F , '!/^$/ {print $2}' $1 | sort | uniq -c`"
Run Code Online (Sandbox Code Playgroud)

我的问题是:有一种更好,更简单的方法来完成这项工作吗?

特别是我不知道如何解决这个问题:

gawk -F , '!/^$/ {print $2}' miocsv.csv | sort | uniq -c | gawk '{print $1","$2}'
Run Code Online (Sandbox Code Playgroud)

问题是string2可以包含空格,如果是这样,gawk上的第二次调用将截断字符串.我不知道如何打印所有字段"从2到NF",保持分隔符,这可以连续发生几次.

非常感谢,再见

编辑:

如上所述,这里有一些示例数据:

(这是一项练习,对于创造性而言遗憾)

输入:

*,*,*
test,  test …
Run Code Online (Sandbox Code Playgroud)

regex csv bash awk gawk

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

如何使用就地操作破坏 PyTorch autograd

我试图更好地理解就地操作在 PyTorch autograd 中的作用。我的理解是,它们可能会导致问题,因为它们可能会覆盖后退步骤中所需的值。

我正在尝试构建一个示例,其中就地操作打破了自动微分,我的想法是在用于计算其他张量后覆盖反向传播期间所需的一些值。

我使用赋值作为就地操作(我尝试了+=相同的结果),我以这种方式仔细检查它是就地操作:

x = torch.arange(5, dtype=torch.float, requires_grad=True)
y = x
y[3] = -1
print(x)
Run Code Online (Sandbox Code Playgroud)

印刷:

tensor([ 0.,  1.,  2., -1.,  4.], grad_fn=<CopySlices>)
Run Code Online (Sandbox Code Playgroud)

这是我打破 autograd 的尝试:

  1. 没有就地操作:
tensor([ 0.,  1.,  2., -1.,  4.], grad_fn=<CopySlices>)
Run Code Online (Sandbox Code Playgroud)

这打印

tensor([0.0000, 0.2000, 0.4000, 0.6000, 0.8000])
Run Code Online (Sandbox Code Playgroud)
  1. 使用就地操作:
x = torch.arange(5, dtype=torch.float, requires_grad=True)
out1 = x ** 2
out2 = out1 / 10
# out1[3] += 100  
out2.sum().backward()
print(x.grad)
Run Code Online (Sandbox Code Playgroud)

这打印:

tensor([0.0000, 0.2000, 0.4000, 0.6000, 0.8000])
Run Code Online (Sandbox Code Playgroud)

我期待获得不同的毕业生。

  • 项目分配在做什么?我不明白grad_fn=<CopySlices>
  • 为什么它返回相同的毕业生?
  • 是否有破坏 autograd …

python pytorch autograd

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

为什么[^\d\w\s,]匹配"leonardo,davinci"?

我无法理解为什么正则表达式:

[^\d\s\w,]
Run Code Online (Sandbox Code Playgroud)

匹配字符串:

"leonardo,davinci"
Run Code Online (Sandbox Code Playgroud)

那是我的考验:

$ echo "leonardo,davinci" | egrep '[^\d\w\s,]'
leonardo,davinci
Run Code Online (Sandbox Code Playgroud)

虽然这按预期工作:

$ echo "leonardo,davinci" | egrep '[\S\W\D]'
$ 
Run Code Online (Sandbox Code Playgroud)

非常感谢

regex grep

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

带有GitHub markdown的表格中的图像

我在github上格式化README时遇到一些问题。

这是原始的自述文件:

| Italic             |  Block letters |
:-------------------------:|:-------------------------:
![](outputs/output_Biotouch/18-15_02-02-2018/Identification/ITALIC/ITALIC_movementPoints_cmc.png)  |  ![](outputs/output_Biotouch/18-15_02-02-2018/Identification/BLOCK_LETTERS/BLOCK_LETTERS_movementPoints_cmc.png)
![](outputs/output_Biotouch/18-15_02-02-2018/Verification/ITALIC/ITALIC_movementPoints_notbalanced_roc.png)  |  ![](outputs/output_Biotouch/18-15_02-02-2018/Verification/BLOCK_LETTERS/BLOCK_LETTERS_movementPoints_notbalanced_roc.png)
![](outputs/output_Biotouch/18-15_02-02-2018/Verification/ITALIC/ITALIC_movementPoints_notbalanced_frrVSfpr.png)  |  ![](outputs/output_Biotouch/18-15_02-02-2018/Verification/BLOCK_LETTERS/BLOCK_LETTERS_movementPoints_notbalanced_frrVSfpr.png)
Run Code Online (Sandbox Code Playgroud)

它只是一张具有对某些图像的相对引用的表。

参考的图像具有相同的尺寸

结果如下: 在此处输入图片说明

为什么中间行中包含的图像较小?

github github-flavored-markdown readme

4
推荐指数
2
解决办法
5385
查看次数

标签 统计

regex ×2

autograd ×1

awk ×1

bash ×1

csv ×1

gawk ×1

github ×1

github-flavored-markdown ×1

grep ×1

python ×1

pytorch ×1

readme ×1