- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
考虑以下函数
def foo(x):
with tf.GradientTape() as tape:
tape.watch(x)
y = x**2 + x + 4
return tape.gradient(y, x)
如果函数被称为 foo(tf.constant(3.14))
,则调用 tape.watch(x)
是必要的,但不是直接传入一个变量,比如foo(tf.Variable(3.14))
。
现在我的问题是,即使在直接传入 tf.Variable
的情况下,对 tape.watch(x)
的调用是否安全?还是会因为变量已经被自动监视然后再次手动监视而发生一些奇怪的事情?编写这样可以同时接受 tf.Tensor
和 tf.Variable
的通用函数的正确方法是什么?
最佳答案
应该是安全的。一方面,tf.GradientTape.watch
的文档说:
Ensures that
tensor
is being traced by this tape.
“确保”似乎暗示它将确保它被跟踪以防万一。事实上,文档没有给出任何迹象表明在同一个对象上使用它两次应该是一个问题(尽管如果他们明确说明也不会造成伤害)。
但无论如何,我们都可以深入源码进行检查。最后,在变量上调用 watch
(如果它不是变量但路径略有不同,答案最终相同)归结为 WatchVariable
C++ 中 GradientTape
类的方法:
void WatchVariable(PyObject* v) {
tensorflow::Safe_PyObjectPtr handle(PyObject_GetAttrString(v, "handle"));
if (handle == nullptr) {
return;
}
tensorflow::int64 id = FastTensorId(handle.get());
if (!PyErr_Occurred()) {
this->Watch(id);
}
tensorflow::mutex_lock l(watched_variables_mu_);
auto insert_result = watched_variables_.emplace(id, v);
if (insert_result.second) {
// Only increment the reference count if we aren't already watching this
// variable.
Py_INCREF(v);
}
}
该方法的后半部分显示被监视的变量被添加到 watched_variables_
,这是一个 std::set
,因此再次添加一些东西不会有任何作用。这实际上是稍后检查以确保 Python 引用计数是正确的。上半场基本叫Watch
:
template <typename Gradient, typename BackwardFunction, typename TapeTensor>
void GradientTape<Gradient, BackwardFunction, TapeTensor>::Watch(
int64 tensor_id) {
tensor_tape_.emplace(tensor_id, -1);
}
tensor_tape_
是一个 map (特别是 tensorflow::gtl:FlatMap
,与标准 C++ map 几乎相同),因此如果 tensor_id
已经存在,这将无效.
因此,即使没有明确说明,一切都表明它应该没有问题。
关于python - 当 `tape.watch(x)` 在 TensorFlow 中已经是 `x` 时调用 `tf.Variable` 是否可以?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54479770/
在 Tensorflow(从 v1.2.1 开始)中,似乎有(至少)两个并行 API 来构建计算图。 tf.nn 中有函数,如 conv2d、avg_pool、relu、dropout,tf.laye
我正在处理眼睛轨迹数据和卷积神经网络。我被要求使用 tf.reduce_max(lastconv, axis=2)代替 MaxPooling 层和 tf.reduce_sum(lastconv,axi
TensorFlow 提供了 3 种不同的数据存储格式 tf.train.Feature .它们是: tf.train.BytesList tf.train.FloatList tf.train.In
我正在尝试为上下文强盗问题 (https://medium.com/emergent-future/simple-reinforcement-learning-with-tensorflow-part
我在使用 Tensorflow 时遇到问题: 以下代码为卷积 block 生成正确的图: def conv_layer(self, inputs, filter_size = 3, num_filte
我正在将我的训练循环迁移到 Tensorflow 2.0 API .在急切执行模式下,tf.GradientTape替换 tf.gradients .问题是,它们是否具有相同的功能?具体来说: 在函数
tensorflow 中 tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)) 的目的是什么? 更多上下文:
我一直在努力学习 TensorFlow,我注意到不同的函数用于相同的目标。例如,为了平方变量,我看到了 tf.square()、tf.math.square() 和 tf.keras.backend.
我正在尝试使用自动编码器开发图像着色器。有 13000 张训练图像。如果我使用 tf.data,每个 epoch 大约需要 45 分钟,如果我使用 tf.utils.keras.Sequence 大约
我尝试按照 tensorflow 教程实现 MNIST CNN 神经网络,并找到这些实现 softmax 交叉熵的方法给出了不同的结果: (1) 不好的结果 softmax = tf.nn.softm
其实,我正在coursera上做deeplearning.ai的作业“Art Generation with Neural Style Transfer”。在函数 compute_layer_styl
训练神经网络学习“异或” 我正在尝试使用“批量归一化”,我创建了一个批量归一化层函数“batch_norm1”。 import tensorflow as tf import nump
我正在尝试协调来自 TF“图形和 session ”指南以及 TF“Keras”指南和 TF Estimators 指南的信息。现在在前者中它说 tf.Session 使计算图能够访问物理硬件以执行图
我正在关注此处的多层感知器示例:https://github.com/aymericdamien/TensorFlow-Examples我对函数 tf.nn.softmax_cross_entropy
回到 TensorFlow = 2.0 中消失了。因此,像这样的解决方案...... with tf.variable_scope("foo"): with tf.variable_scope
我按照官方网站中的步骤安装了tensorflow。但是,在该网站中,作为安装的最后一步,他们给出了一行代码来“验证安装”。但他们没有告诉这段代码会给出什么输出。 该行是: python -c "imp
代码: x = tf.constant([1.,2.,3.], shape = (3,2,4)) y = tf.constant([1.,2.,3.], shape = (3,21,4)) tf.ma
我正在尝试从 Github 训练一个 3D 分割网络.我的模型是用 Keras (Python) 实现的,这是一个典型的 U-Net 模型。模型,总结如下, Model: "functional_3"
我正在使用 TensorFlow 2。我正在尝试优化一个函数,该函数使用经过训练的 tensorflow 模型(毒药)的损失。 @tf.function def totalloss(x): x
试图了解 keras 优化器中的 SGD 优化代码 (source code)。在 get_updates 模块中,我们有: # momentum shapes = [K.int_shape(p) f
我是一名优秀的程序员,十分优秀!