圆月山庄资源网 Design By www.vgjia.com

初学tensorflow,如果写的不对的,请更正,谢谢!

tf.reshape(tensor, shape, name=None)

函数的作用是将tensor变换为参数shape的形式。

其中shape为一个列表形式,特殊的一点是列表中可以存在-1。-1代表的含义是不用我们自己指定这一维的大小,函数会自动计算,但列表中只能存在一个-1。(当然如果存在多个-1,就是一个存在多解的方程了)

好了我想说的重点还有一个就是根据shape如何变换矩阵。其实简单的想就是,

reshape(t, shape) => reshape(t, [-1]) => reshape(t, shape)

首先将矩阵t变为一维矩阵,然后再对矩阵的形式更改就可以了。

官方的例子:

# 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

在举几个例子或许就清楚了,有一个数组z,它的shape属性是(4, 4)

z = np.array([[1, 2, 3, 4],
     [5, 6, 7, 8],
     [9, 10, 11, 12],
     [13, 14, 15, 16]])
z.shape
(4, 4)

z.reshape(-1)

z.reshape(-1)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])

z.reshape(-1, 1)
也就是说,先前我们不知道z的shape属性是多少,但是想让z变成只有一列,行数不知道多少,通过`z.reshape(-1,1)`,Numpy自动计算出有12行,新的数组shape属性为(16, 1),与原来的(4, 4)配套。

z.reshape(-1,1)
 array([[ 1],
    [ 2],
    [ 3],
    [ 4],
    [ 5],
    [ 6],
    [ 7],
    [ 8],
    [ 9],
    [10],
    [11],
    [12],
    [13],
    [14],
    [15],
    [16]])

z.reshape(-1, 2)

newshape等于-1,列数等于2,行数未知,reshape后的shape等于(8, 2)

 z.reshape(-1, 2)
 array([[ 1, 2],
    [ 3, 4],
    [ 5, 6],
    [ 7, 8],
    [ 9, 10],
    [11, 12],
    [13, 14],
    [15, 16]])
标签:
TensorFlow,reshape,tf.reshape

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。