我是从官方教程中学习 tensorflow2.0 的。我可以从下面的代码中理解结果。
def square_if_positive(x):
return [i ** 2 if i > 0 else i for i in x]
square_if_positive(range(-5, 5))
# result
[-5, -4, -3, -2, -1, 0, 1, 4, 9, 16]
Run Code Online (Sandbox Code Playgroud)
但是如果我用张量而不是 python 代码改变输入,就像这样
def square_if_positive(x):
return [i ** 2 if i > 0 else i for i in x]
square_if_positive(tf.range(-5, 5))
Run Code Online (Sandbox Code Playgroud)
我得到以下错误!!
OperatorNotAllowedInGraphError Traceback (most recent call last)
<ipython-input-39-6c17f29a3443> in <module>
2 def square_if_positive(x):
3 return [i**2 if i > 0 else i for i …Run Code Online (Sandbox Code Playgroud) 我正在阅读"了解JVM高级功能和最佳实践",其中包含一个代码段,用于解释java中的先发制人规则.我不明白.代码如下:
private int value = 0;
//executed by Thread A
public void setValue(int value){
this.value = value;
}
//executed by Thread B
public void getValue(){
return value;
}
Run Code Online (Sandbox Code Playgroud)
假设A线程B在代码中的线程之前启动.我可以理解,我们不知道getValue()线程B 返回的结果,因为它不是线程安全的.但是,这本书说,如果添加的同步关键字的功能setValue()和getValue(),那么就没有存在线程安全问题和方法getValue()将返回正确的值.这本书解释说,因为synchronized符合先前发生的规则.所以我在下面的代码中有两个问题.
public class VolatileDemo3 {
private volatile int value = 0;
public static void main(String[] args) {
VolatileDemo3 v = new VolatileDemo3();
Thread A = new Thread(v.new Test1());// Thread A
Thread B = new Thread(v.new Test2());//Thread B …Run Code Online (Sandbox Code Playgroud)