标签: eager-execution

当 `x` 已经是 TensorFlow 中的 `tf.Variable` 时,可以调用 `tape.watch(x)` 吗?

考虑以下函数

def foo(x):
  with tf.GradientTape() as tape:
    tape.watch(x)

    y = x**2 + x + 4

  return tape.gradient(y, x)
Run Code Online (Sandbox Code Playgroud)

tape.watch(x)如果函数被称为 as foo(tf.constant(3.14)),则调用是必要的,但当它直接传入变量时则不需要,例如foo(tf.Variable(3.14))

现在我的问题是,tape.watch(x)即使tf.Variable在直接传入的情况下也调用安全吗?还是会因为变量已经被自动监视然后再次手动监视而发生一些奇怪的事情?编写这样可以同时接受tf.Tensor和的通用函数的正确方法是什么tf.Variable

python autodiff tensorflow eager-execution

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

在open cv2 python中使用Tensor flow 2.0对象的方法是什么,为什么这么迂回?

我使用张量流 api (2.0) 加载图像,如下所示:

def load(image_file):
  image = tf.io.read_file(image_file)
  image = tf.image.decode_jpeg(image)
Run Code Online (Sandbox Code Playgroud)

现在我有了这个对象,我想显示这个图像,我可以简单地使用 matplotlib.pyplot,这很有效。

plt.figure()
plt.imshow(re/255.0)
plt.show()
Run Code Online (Sandbox Code Playgroud)

然而,从一开始就用 OpenCV2 尝试这个是有问题的,大多数示例来自 1.0,基于 .eval() 会话的 numpy 转换建议。一种方法是首先将张量流对象转换为 numpy,这是 API 文档中执行此操作的函数:

TensorFlow
API r2.0
TensorFlow Core 2.0a
Python
tf.make_ndarray
Create a numpy ndarray from a tensor.
Run Code Online (Sandbox Code Playgroud)

我不明白为什么这不起作用,我得到了许多错误,而我只想做一些简单的事情,然后使用一些开放的 cv2 函数,如重新映射、调整大小等:

文件“C:\Python\Python37\lib\site-packages\tensorflow\python\eager\def_function.py”,第 426 行,调用中 self._initialize(args, kwds, add_initializers_to=initializer_map) 文件“C:\Python\Python37\lib\site-packages\tensorflow\python\eager\def_function.py”,第 370 行,在 _initialize *args, **kwds 中) ) 文件“C:\Python\Python37\lib\site-packages\tensorflow\python\eager\function.py”,第 1313 行,在 _get_concrete_function_internal_garbage_collected graph_function, _, _ = self._maybe_define_function(args, kwargs) 文件“C: \Python\Python37\lib\site-packages\tensorflow\python\eager\function.py”,第 1580 行,在 _maybe_define_function graph_function = self._create_graph_function(args, kwargs) …

python training-data python-3.x eager-execution tensorflow2.0

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

在自定义层中禁用了 Tensorflow 2 急切执行

我在 ubuntu 18.04 盒子中使用通过 pip 安装的 TF2

$ pip freeze | grep "tensorflow"
tensorflow==2.0.0
tensorflow-estimator==2.0.1
Run Code Online (Sandbox Code Playgroud)

我正在玩一个自定义层。

import tensorflow as tf
from tensorflow.keras.preprocessing import sequence
from tensorflow.keras.layers import Input, Concatenate, Dense, Bidirectional, LSTM, Embedding
from tensorflow.keras.models import Model
from tensorflow.keras.datasets import imdb

class Attention(tf.keras.layers.Layer):

    def __init__(self, units):
        super(Attention, self).__init__()
        self.W1 = Dense(units)
        self.W2 = Dense(units)
        self.V = Dense(1)

    def call(self, features, hidden):
        hidden_with_time_axis = tf.expand_dims(hidden, 1)
        score = tf.nn.tanh(self.W1(features) + self.W2(hidden_with_time_axis))
        attention_weights = tf.nn.softmax(self.V(score), axis=1)
        context_vector = attention_weights * features
        context_vector …
Run Code Online (Sandbox Code Playgroud)

python tensorflow eager-execution tensorflow2.0

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

什么是关于 tf.function 的跟踪

在 TensorFlow 的指南中经常提到“跟踪”这个词,例如使用 tf.function 实现更好的性能

  1. 究竟什么是“跟踪”,它是指作为第一次调用 tf.function 的结果(随后取决于参数)生成图形吗?
  2. 当只有一部分计算被注释时会发生什么 @tf.function,它会混合急切执行和图执行吗?

python tensorflow eager-execution tensorflow2.0

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

在 Rust 的 `Option`/`Result` 的 `map_or` 和 `map_or_else` 方法中,什么是惰性/急切求值?

我知道 Rust 迭代器(适配器)是“懒惰的”,因为在我们访问它们之前它们实际上不会执行任何操作。对我来说,迭代器就像某种用来窥视星星的望远镜,迭代器适配器就像添加更多过滤器,使望远镜更“用起来有趣”,例如红色玻璃,一切都会是红色的。当然,除非我们真的拿起望远镜并透过它看,否则我们既看不到原始的远景,也看不到全红色的远景。

操场

fn main(){
    let distant_things = &[4, 2, 0, 6, 9];
    let telescope = distant_things.iter();
    let telescope_with_red_lens = distant_things.iter().map(|i| {
        print!("Here's red version of ");
        i
    });
    for i in telescope {
        println!("{i}");
    }
    for red in telescope_with_red_lens {
        println!("{red}");
    }
}
Run Code Online (Sandbox Code Playgroud)

但我无法理解rust 文档中的这条语句,它指出传递给的参数Result::map_or是急切求值的,而传递给的参数Result::map_or_else是懒惰求值的;类似适用于Option::map_orOption::map_or_else。对我来说,如果惰性/急切评估存在差异,那么在下面的代码中,(stdout) 输出应该显示一些主要的时间差异,而不是全部接近 1 秒,特别是我希望map_or报告接近0我调用时的时间experiment_laziness(Ok(()));。我想,由于Result::map_or对变体的急切评估,实际上在线程休眠 1 秒并加入之前调用了Ok闭包,因此from被映射到一个闭包,该闭包在之前接近 1 秒的某个时间进行评估,导致经过的时间接近于零。但输出都接近 1 秒。Ok(_) …

lazy-evaluation rust eager-execution

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

tf.GradientTape() 位置对模型训练时间的影响

我试图更新每个时期的权重,但我正在批量处理数据。问题是,为了规范损失,我需要在训练循环之外录制 TensorFlow 变量(要跟踪和规范化)。但是当我这样做时,训练时间是巨大的。

我认为,它将所有批次的变量累积到图中并在最后计算梯度。

我已经开始跟踪 for 循环外和 for 循环内的变量,后者比第一次要快。我很困惑为什么会发生这种情况,因为无论我做什么,我的模型的可训练变量和损失都保持不变。

# Very Slow

loss_value = 0
batches = 0

with tf.GradientTape() as tape:
    for inputs, min_seq in zip(dataset, minutes_sequence):
        temp_loss_value = my_loss_function(inputs, min_seq)
        batches +=1
        loss_value = loss_value + temp_loss_value

# The following line takes huge time.
grads = tape.gradient(loss_value, model.trainable_variables)

Run Code Online (Sandbox Code Playgroud)
# Very Fast

loss_value = 0
batches = 0

for inputs, min_seq in zip(dataset, minutes_sequence):
    with tf.GradientTape() as tape:
        temp_loss_value = my_loss_function(inputs, min_seq)
        batches +=1
        loss_value = loss_value + temp_loss_value …
Run Code Online (Sandbox Code Playgroud)

python gradient training-data tensorflow eager-execution

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

无法启用 Tensorflows Eager 执行

我有一个安装了 Tensorflow 2.0.0-beta1 的 conda 环境。但是,每当我导入 tensorflow 并尝试启用急切执行时,我都会收到错误消息:

AttributeError: module 'tensorflow' has no attribute 'enable_eager_execution'
Run Code Online (Sandbox Code Playgroud)

我为此运行的唯一代码是:

import tensorflow as tf
print(tf.__version__)
tf.enable_eager_execution()
Run Code Online (Sandbox Code Playgroud)

这是 tensorflow 2.0 beta 模块的错误还是我的安装问题?

keras tensorflow eager-execution

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