tensorflow 动态数组 TensorArray
生活随笔
收集整理的這篇文章主要介紹了
tensorflow 动态数组 TensorArray
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
tensorflow 動態數組隨時可以讀取
import tensorflow as tf ta = tf.TensorArray(tf.float32, size=0, dynamic_size=True, clear_after_read=False) ta = ta.write(0, 10) ta = ta.write(1, 20) ta = ta.write(2, 30)print(ta.read(0))print(ta.read(1))print(ta.read(2))print(ta.stack()) tf.Tensor(10.0, shape=(), dtype=float32) tf.Tensor(20.0, shape=(), dtype=float32) tf.Tensor(30.0, shape=(), dtype=float32) tf.Tensor([10. 20. 30.], shape=(3,), dtype=float32) @tf.function def fibonacci(n):n=5ta = tf.TensorArray(tf.float32, size=0, dynamic_size=True)ta = ta.unstack([0., 1.])for i in range(2, n):ta = ta.write(i, ta.read(i - 1) + ta.read(i - 2))return ta.stack()fibonacci(7) <tf.Tensor: shape=(5,), dtype=float32, numpy=array([0., 1., 1., 2., 3.], dtype=float32)> v = tf.Variable(1) @tf.function def f(x):ta = tf.TensorArray(tf.int32, size=0, dynamic_size=True)for i in tf.range(x):v.assign_add(i)ta = ta.write(i, v)return ta.stack() f(5) <tf.Tensor: shape=(5,), dtype=int32, numpy=array([ 1, 2, 4, 7, 11], dtype=int32)>總結
以上是生活随笔為你收集整理的tensorflow 动态数组 TensorArray的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 社会阶层及由此引起的社会动力学现象
- 下一篇: yolov3 数据预处理