【问题标题】:How to insert certain values to a tensor in tensorflow?如何将某些值插入张量流中的张量?
【发布时间】:2022-01-24 03:42:51
【问题描述】:

我要转换

from = [
    [1],[2],[3],[4],
]

形状 (4, 1)

这个张量

to = [
    [1,1],[2,2],[3,3],[4,4],
]

形状 (4, 2)

这个张量。

【问题讨论】:

    标签: python tensorflow tensorflow2.0 tensor


    【解决方案1】:

    使用tf.tiletf.repeattf.concattf.stack

    import tensorflow as tf
    
    from_tensor = tf.constant([[1],[2],[3],[4]])
    to_tensor_with_tile = tf.tile(from_tensor, [1, 2])
    to_tensor_with_repeat = tf.repeat(from_tensor, repeats=2, axis=1)
    to_tensor_with_concat = tf.concat([from_tensor, from_tensor], axis=1)
    to_tensor_with_stack = tf.squeeze(tf.stack([from_tensor, from_tensor], axis=-1), axis=1)
    
    print(to_tensor_with_tile)
    print(to_tensor_with_repeat)
    print(to_tensor_with_concat)
    print(to_tensor_with_stack)
    
    tf.Tensor(
    [[1 1]
     [2 2]
     [3 3]
     [4 4]], shape=(4, 2), dtype=int32)
    tf.Tensor(
    [[1 1]
     [2 2]
     [3 3]
     [4 4]], shape=(4, 2), dtype=int32)
    tf.Tensor(
    [[1 1]
     [2 2]
     [3 3]
     [4 4]], shape=(4, 2), dtype=int32)
    tf.Tensor(
    [[1 1]
     [2 2]
     [3 3]
     [4 4]], shape=(4, 2), dtype=int32)
    

    【讨论】:

    • 这是一个天才的解决方案!谢谢
    猜你喜欢
    • 2019-11-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    相关资源
    最近更新 更多