Tensorflow 将维度展开为张量并赋值

Tensorflow 将维度展开为张量并赋值,tensorflow,tensor,Tensorflow,Tensor,我的张量形状是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,

我的张量形状是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]
               ]
我喜欢在第一列扩展到32,5,比如tf.expand_dims(输入框,0)。 然后将值赋给第一列,行数如下

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?

在这里提及解决方案(答案部分),尽管它出现在评论部分(感谢
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)
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)>
代码到第一列处展开(7,5),其中第一列的值为相应的行号,如下所示:

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)
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)>
上述代码的输出如下所示:

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)
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)>


希望这有帮助。学习愉快

在这里提及解决方案(答案部分),即使它出现在评论部分(感谢
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)
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)>
代码到第一列处展开(7,5),其中第一列的值为相应的行号,如下所示:

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)
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)>
上述代码的输出如下所示:

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)
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)>


希望这有帮助。学习愉快

我不明白
输入框的形状
,如果是
(32,4)
,怎么会有一些行有两个“子行”?或者,
input\u box
只是一个Python列表?您如何在TensorFlow中表示这些数据?另外,
tf.expand\u dims
不会增加轴的大小,就像(我认为)您想要的那样,它只是添加了一个新的单体维度-因此,例如,如果
input\u box
具有shape
(32,4)
,tf.expand\u dims(input\u box,0)
将具有shape
(1,32,4)
。是的,我需要找出两行和一行。比如说,如果我想从(32,4)更改为(32,5),我如何更改和更新行号呢?这个简单的例子有一个形状为
(32,4)
的张量,只需执行
tf.concat([tf.dtypes.cast(tf.expand_-dims)(tf.range(tf.shape(input_-box)[0]),1),input_-box.dtype),input_-box],axis=1)
。你的效率非常高。如何将行索引分配给第一列?
tf.range(tf.shape(input_box)[0])
生成一个从0到31的向量,然后使用
expand_dims
将其转换为
(32,1)
矩阵,然后进行铸造以匹配
input_box
类型(例如,如果它不是整数),然后沿着水平轴
concat
。我不明白
输入框的形状,如果它是
(32,4)
,怎么会有一些带有两个“子行”的行呢?或者,
input\u box
只是一个Python列表?您如何在TensorFlow中表示这些数据?另外,
tf.expand\u dims
不会增加轴的大小,就像(我认为)您想要的那样,它只是添加了一个新的单体维度-因此,例如,如果
input\u box
具有shape
(32,4)
,tf.expand\u dims(input\u box,0)
将具有shape
(1,32,4)
。是的,我需要找出两行和一行。比如说,如果我想从(32,4)更改为(32,5),我如何更改和更新行号呢?这个简单的例子有一个形状为
(32,4)
的张量,只需执行
tf.concat([tf.dtypes.cast(tf.expand_-dims)(tf.range(tf.shape(input_-box)[0]),1),input_-box.dtype),input_-box],axis=1)
。你的效率非常高。如何将行索引分配给第一列?
tf.range(tf.shape(input_box)[0])
生成一个从0到31的向量,然后使用
expand_dims
将其转换为
(32,1)
矩阵,然后进行铸造以匹配
input_box
类型(例如,如果它不是整数),然后沿水平轴
concat