这个函数就是将输入的张量,按第一维度切分成多个小的张量。代码如下:
import tensorflow as tf
b = tf.random.normal([3, 1, 2, 1])
bb = tf.data.Dataset.from_tensor_slices(b)
print(bb)
print('-----------------------')
for i in bb:
print(i)
输出的结果如下:
<TensorSliceDataset shapes: (1, 2, 1), types: tf.float32>
-----------------------
tf.Tensor(
[[[-0.53834546]
[ 0.6661047 ]]], shape=(1, 2, 1), dtype=float32)
tf.Tensor(
[[[-0.44865417]
[ 0.5713346 ]]], shape=(1, 2, 1), dtype=float32)
tf.Tensor(
[[[-1.2513908 ]
[-0.60661495]]], shape=(1, 2, 1), dtype=float32)
Process finished with exit code 0
本文介绍使用TensorFlow将张量按第一维度切分成多个小张量的方法。通过实例演示了如何利用tf.data.Dataset.from_tensor_slices函数实现张量的切分,并展示了具体的代码和输出结果。
3135

被折叠的 条评论
为什么被折叠?



