Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何有效地跟踪张量流张量的历史?_Python 3.x_Tensorflow_Tensorflow2.0 - Fatal编程技术网

Python 3.x 如何有效地跟踪张量流张量的历史?

Python 3.x 如何有效地跟踪张量流张量的历史?,python-3.x,tensorflow,tensorflow2.0,Python 3.x,Tensorflow,Tensorflow2.0,这种使用张量数组的模式是跟踪张量历史的有效方法吗?内部环路中的所有内容是否都在GPU上完成,而不向CPU传输任何内容?我如何验证这一点 import tensorflow as tf with tf.device('/device:GPU:0'): @tf.function def f(x, y): return y, x + y x_array = tf.TensorArray(tf.float32, 0, dynamic_size=True,

这种使用
张量数组的模式是跟踪张量历史的有效方法吗?内部环路中的所有内容是否都在GPU上完成,而不向CPU传输任何内容?我如何验证这一点

import tensorflow as tf


with tf.device('/device:GPU:0'):
    @tf.function
    def f(x, y):
        return y, x + y

    x_array = tf.TensorArray(tf.float32, 0, dynamic_size=True,
                             clear_after_read=False)
    y_array = tf.TensorArray(tf.float32, 0, dynamic_size=True,
                             clear_after_read=False)

    x = tf.Variable([1.0])
    y = tf.Variable([1.0])
    x_array.write(0, x)
    y_array.write(0, y)

    for i in tf.range(10):
        x = x_array.read(i)
        y = y_array.read(i)
        new_x, new_y = f(x, y)
        x_array.write(i + 1, new_x)
        y_array.write(i + 1, new_y)

    print(x_array.stack())
    print(y_array.stack())
这就是我实际上想要做的,但它甚至没有运行:

import tensorflow as tf


with tf.device('/device:GPU:0'):
    @tf.function
    def f(x, y):
        return y, x + y

    @tf.function
    def g(n):
        for i in tf.range(n):
            x = x_array.read(i)
            y = y_array.read(i)
            new_x, new_y = f(x, y)
            x_array.write(i + 1, new_x)
            y_array.write(i + 1, new_y)

    x_array = tf.TensorArray(tf.float32, 0, dynamic_size=True,
                             clear_after_read=False)
    y_array = tf.TensorArray(tf.float32, 0, dynamic_size=True,
                             clear_after_read=False)

    x = tf.Variable([1.0])
    y = tf.Variable([1.0])
    x_array.write(0, x)
    y_array.write(0, y)

    g(tf.constant(10))

    print(x_array.stack())
    print(y_array.stack())

下面是修复第二个代码段的方法:

import tensorflow as tf

with tf.device('/device:GPU:0'):
    @tf.function
    def f(x, y):
        return y, x + y

    @tf.function
    def g(x, y, n):
        x_array = tf.TensorArray(tf.float32, n + 1, dynamic_size=False,
                                 clear_after_read=True)
        y_array = tf.TensorArray(tf.float32, n + 1, dynamic_size=False,
                                 clear_after_read=True)
        x_array = x_array.write(0, x)
        y_array = y_array.write(0, y)
        for i in range(n):
            x, y = f(x, y)
            x_array = x_array.write(i + 1, x)
            y_array = y_array.write(i + 1, y)
        return x_array.stack(), y_array.stack()


    x = tf.Variable([1.0])
    y = tf.Variable([1.0])

    x_hist, y_hist = g(x, y, tf.constant(10))

    print(x_hist)
    # tf.Tensor(
    # [[ 1.]
    #  [ 1.]
    #  [ 2.]
    #  [ 3.]
    #  [ 5.]
    #  [ 8.]
    #  [13.]
    #  [21.]
    #  [34.]
    #  [55.]
    #  [89.]], shape=(11, 1), dtype=float32)
    print(y_hist)
    # tf.Tensor(
    # [[  1.]
    #  [  2.]
    #  [  3.]
    #  [  5.]
    #  [  8.]
    #  [ 13.]
    #  [ 21.]
    #  [ 34.]
    #  [ 55.]
    #  [ 89.]
    #  [144.]], shape=(11, 1), dtype=float32)
有一些问题。
tf.function
应将其输入作为参数,而不是全局范围。您可以在函数中创建张量数组,并且可以将它们设置为固定大小,并在读取后清除,因为在读取后不会使用它们。但是,将每个
write
操作的结果指定给数组变量是很重要的,因为这将使其成为“写入后的张量数组”。您不需要在循环中使用
tf.range
。只需编写每次迭代的结果,而不是从数组中再次读取,就可以更有效地填充“history”数组


关于GPU,由于所有内容都在
tf.device
上下文中,因此将在GPU上分配并运行所有内容,如果某些内容无法在GPU上运行,则会出现错误,因此只要所有内容都运行,就应该可以了。

谢谢!但是现在你传入了
x
y
,我很担心,因为我实际上有,比如说,一百个这样的变量和张量数组。我希望能把它们放进字典或物品里。惯用的TensorFlow方法是什么?我会把它们放在keras层吗?@Neig我不知道你的实际用例,所以我不能确定,但我认为有数百个独立变量并不常见,而是有数百个(或更多)值的变量。但无论如何,我想你可以把所有这些都作为一个属性放在一个对象中。但是,我不确定
tf.function
是否支持迭代张量集合,因为我尝试使用它时会出错……感谢您的关注。我遇到了同样的问题,但更多的调查表明,我可能应该使用
AbstractRNNCell
来实现一个对象,该对象的状态随着每次迭代而变化@我想我现在更明白了,谢谢你的反馈。