【问题标题】:tensorflow: convert a list of tensor to ragged tensor with a fixed dim in a certain axistensorflow:将张量列表转换为在某个轴上具有固定暗度的参差不齐的张量
【发布时间】:2022-01-01 00:49:24
【问题描述】:

假设我有三个形状为(1,3) 的numpy 数组,并将它们堆叠成形状为(2,3)(1,3) 的组。然后我将它们与tf.ragged.stack 堆叠起来,得到一个参差不齐的张量:

x1 = np.asarray([1,0,0])
x2 = np.asarray([0,1,0])
x3 = np.asarray([0,0,1])

group_a = np.stack([x1,x2])
group_b = np.stack([x3])


ac = tf.ragged.stack([group_a,group_b], axis=0)

我希望它的形状是(2, None, 3),但实际上它是(2, None, None)。如何获得所需的形状?我正在使用张量流 2.5.2

【问题讨论】:

    标签: python tensorflow tensor


    【解决方案1】:

    发生这种情况是因为tf.ragged.stack 正在创建一个等于 2 的 ragged_rank。请查看docs 了解更多信息。您可以像这样显式定义如何划分参差不齐的张量:

    import tensorflow as tf
    import numpy as np
    
    x1 = np.asarray([1,0,0])
    x2 = np.asarray([0,1,0])
    x3 = np.asarray([0,0,1])
    
    ac = tf.RaggedTensor.from_row_splits(
        values=[x1, x2, x3],
        row_splits=[0, 2, 3])
    
    print(ac.shape)
    print(ac)
    
    (2, None, 3)
    <tf.RaggedTensor [[[1, 0, 0], [0, 1, 0]], [[0, 0, 1]]]>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多