当前位置: 代码迷 >> 综合 >> Tensorflow 中的tf.reshape()理解和操作
  详细解决方案

Tensorflow 中的tf.reshape()理解和操作

热度:91   发布时间:2023-11-10 17:10:36.0

Tensorflow 中的tf.reshape()

前沿:
        最近学习Tensorflow, 可以说是感触颇深,其中tf.reshape()可以说是处理数据格式的好方法,值得学习一番。
以下就是官方的例子:
import tensorflow as tf

# tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]
# tensor 't' has shape [9]
reshape(t, [3, 3]) ==> [[1, 2, 3],[4, 5, 6],[7, 8, 9]]# tensor 't' is [[[1, 1], [2, 2]],
#                [[3, 3], [4, 4]]]
# tensor 't' has shape [2, 2, 2]
reshape(t, [2, 4]) ==> [[1, 1, 2, 2],[3, 3, 4, 4]]# tensor 't' is [[[1, 1, 1], # [2, 2, 2]],
#                [[3, 3, 3], # [4, 4, 4]],
#                [[5, 5, 5], # [6, 6, 6]]]
# tensor 't' has shape [3, 2, 3]
# pass '[-1]' to flatten 't'
reshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]# -1 can also be used to infer the shape# -1 is inferred to be 9:
reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],[4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 2:
reshape(t, [-1, 9]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],[4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 3:
reshape(t, [ 2, -1, 3]) ==> [[[1, 1, 1],[2, 2, 2],[3, 3, 3]],[[4, 4, 4],[5, 5, 5],[6, 6, 6]]]# tensor 't' is [7]
# shape `[]` reshapes to a scalar
reshape(t, []) ==> 7

话不多说,大家直接看,比较容易理解,依着葫芦画瓢,祝大家学习愉快!

 

  相关解决方案