将尺寸扩展到张量并分配值

问题描述 投票:1回答:1

我的张量形状是32,4喜欢

input_boxes =  [
                 [1,2,3,4],
                 [2,2,6,4],
                 [[1,5,3,4],[1,3,3,8]],#some row has two 
                 [1,2,3,4],#some has one row
                 [[1,2,3,4],[1,3,3,4]],
                 [1,7,3,4],
                  ......
                 [1,2,3,4]
               ]

我想像tf.expand_dims(input_boxes,0)那样在第一列扩展为32.5。然后将值分配给第一行的行号,例如

input_boxes =  [
                 [0,1,2,3,4],
                 [1,2,2,6,4],
                 [[2,1,5,3,4],[2,1,3,3,8]],#some row has two 
                 [3,1,2,3,4],#some has one row
                 [[4,1,2,3,4],[4,1,3,3,4]],
                 [5,1,7,3,4],
                  ......
                 [31,1,2,3,4]
               ]

我如何在Tensorflow中做什么?

tensorflow tensor
1个回答
0
投票

即使在注释部分(感谢jdehesa)中存在社区的利益],但仍在此处提及解决方案(答案部分)。

例如,我们有一个形状为(7,4)的张量,如下所示:

import tensorflow as tf

input_boxes =  tf.constant([[1,2,3,4],
                 [2,2,6,4],
                 [1,5,3,4],
                 [1,2,3,4],
                 [1,2,3,4],
                 [1,7,3,4],
                 [1,2,3,4]])

print(input_boxes)

expand(7,5)First Column的代码,其中First Columns的值分别为Row Number,显示如下:

input_boxes = tf.concat([tf.dtypes.cast(tf.expand_dims(tf.range(tf.shape(input_boxes)[0]), 1), input_boxes.dtype), input_boxes], axis=1)
print(input_boxes)

以上代码的输出如下所示:

<tf.Tensor: shape=(7, 5), dtype=int32, numpy=
array([[0, 1, 2, 3, 4],
       [1, 2, 2, 6, 4],
       [2, 1, 5, 3, 4],
       [3, 1, 2, 3, 4],
       [4, 1, 2, 3, 4],
       [5, 1, 7, 3, 4],
       [6, 1, 2, 3, 4]], dtype=int32)>

希望这会有所帮助。祝您学习愉快!

© www.soinside.com 2019 - 2024. All rights reserved.