小编cya*_*ang的帖子

什么是沙漏导入,为什么要在代码库中避免它们?

我在 Python 代码库中看到了一些删除“沙漏导入”的提交。我以前从未见过这个术语,我无法通过 Python 文档或网络搜索找到任何关于它的信息。

什么是沙漏进口,何时使用或不使用它们?我最好的猜测是删除它们会使子模块更容易找到,但还有其他原因吗?

从链接提交之一中删除沙漏导入的示例更改:

diff --git a/tensorflow/contrib/slim/python/slim/nets/vgg.py b/tensorflow/contrib/slim/python/slim/nets/vgg.py
index 3c29767f2..d4eb43cbb 100644
--- a/tensorflow/contrib/slim/python/slim/nets/vgg.py
+++ b/tensorflow/contrib/slim/python/slim/nets/vgg.py
@@ -37,13 +37,20 @@ Usage:
 @@vgg_16
 @@vgg_19
 """
+
 from __future__ import absolute_import
 from __future__ import division
 from __future__ import print_function

-import tensorflow as tf
-
-slim = tf.contrib.slim
+from tensorflow.contrib import layers
+from tensorflow.contrib.framework.python.ops import arg_scope
+from tensorflow.contrib.layers.python.layers import layers as layers_lib
+from tensorflow.contrib.layers.python.layers import regularizers
+from tensorflow.contrib.layers.python.layers import utils
+from tensorflow.python.ops import array_ops …
Run Code Online (Sandbox Code Playgroud)

python coding-style

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

`clang -ansi`扩展

我最近遇到了一个问题,下面的玩具示例使用以下方式编译clang -ansi:

int main(void)
{
    for (int i = 0; 0; );
    return i;
}
Run Code Online (Sandbox Code Playgroud)

但是gcc -ansi给出了以下错误:

a.c: In function ‘main’:
a.c:3:5: error: ‘for’ loop initial declarations are only allowed in C99 mode
a.c:3:5: note: use option -std=c99 or -std=gnu99 to compile your code
Run Code Online (Sandbox Code Playgroud)

编译clang -ansi -pedantic显示正在使用C99扩展名.

a.c:3:10: warning: variable declaration in for loop is a C99-specific feature [-pedantic,-Wc99-extensions]
    for (int i = 0; 0; );
         ^
1 warning generated.
Run Code Online (Sandbox Code Playgroud)

clang允许哪些其他扩展-ansi选项?我怎样才能禁用它们?

c gcc clang c89

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

训练数据中不存在新的因子水平

当尝试使用输出randomForest来分类新数据(甚至原始训练数据)时,我收到以下错误:

> res.rf5 <- predict(model.rf5, train.rf5)
Error in predict.randomForest(model.rf5, train.rf5) :
  New factor levels not present in the training data
Run Code Online (Sandbox Code Playgroud)

这个错误是什么意思?为什么即使我尝试预测用于训练的相同数据时也会发生此错误?

下面是一个可用于重现错误的小例子.

train.rf5 <- structure(
  list(A = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 3L),
                     .Label = c("(-0.1,19.9]", "(19.9,40]", "(80.1,100]"),
                     class = c("ordered", "factor")),
       B = structure(c(3L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 4L),
                     .Label = c("1", "2", "4", "5"),
                     class = c("ordered", "factor")),
       C = structure(c(1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, …
Run Code Online (Sandbox Code Playgroud)

r random-forest

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

调用Enumerable#reduce时我到底在做什么?

为什么我不能在Enumerable#reduce(sym)没有括号的情况下拨打电话,如下所示?

>> [1, 2, 3].reduce :+
?>
Run Code Online (Sandbox Code Playgroud)

使用括号时会产生以下结果:

>> [1, 2, 3].reduce(:+)
=> 6
Run Code Online (Sandbox Code Playgroud)

我不小心打电话了Enumerable#reduce {| memo, obj | block }吗?

此外,为什么会发生这种情况?

>> [1, 2, 3].reduce &:+
?> ^C
>> [1, 2, 3].reduce(&:+)
=> 6
Run Code Online (Sandbox Code Playgroud)

非常感谢!

ruby whitespace coding-style irb

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

在红宝石中使用`除非'的'next`时执行顺序混淆

next语句用于跳过循环的一部分并继续循环的下一次迭代.它可以forwhile语句结合使用.

我已经看到人们使用next,如果在评估某些条件后存在复杂的代码,即

next if @state!=:some_state
# some long complicated code
Run Code Online (Sandbox Code Playgroud)

现在,这里我已经与发挥next我的IRB如下:

n = 1
loop do
  n = n + 1
  next unless n == 10
  print "Good"
  break
end
# Good=> nil
Run Code Online (Sandbox Code Playgroud)

以上是理解的.很清楚.

n = 1
#=> 1
loop do
  print "#{n}"
  n = n + 1
  next puts "hi" unless n == 5
  p "good"
  break
end
#1hi
#2hi
#3hi
#4"good"
#=> nil
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,无法了解线路puts "hi"unless …

ruby

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

标签 统计

coding-style ×2

ruby ×2

c ×1

c89 ×1

clang ×1

gcc ×1

irb ×1

python ×1

r ×1

random-forest ×1

whitespace ×1