tf.reshape()函数介绍和示例

tf.reshape()函数介绍和示例
tf.reshape(tensor, shape, name=None)

释义:将张量 tensor 的形状改为 shape

注意:shape 设置为 -1 的位置,表示不用设置这一维的大小,函数自动进行计算(只能存在一个 -1)

示例1

import tensorflow as tf

X = tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.float32, name=None)
Y = tf.reshape(X, (3, 2))        # (2, 3) --> (3, 2)

with tf.Session() as sess:
    print('原Tensor:\n', sess.run(X))
    print('='*30)
    
    print('shape(3, 2):\n', sess.run(Y))
原Tensor:
 [[1. 2. 3.]
 [4. 5. 6.]]
==============================
shape(3, 2):
 [[1. 2.]
 [3. 4.]
 [5. 6.]]

示例2

X = tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.float32, name=None)

Y1 = tf.reshape(X, (-1, 6))       # (2, 3) --> (1, 6)
Y2 = tf.reshape(X, (6, -1))       # (2, 3) --> (6, 1)

with tf.Session() as sess:
    print('原Tensor:\n', sess.run(X))
    print('='*30)
    
    print('shape(1, 6):\n', sess.run(Y1))
    print('='*30)
    
    print('shape(6, 1):\n', sess.run(Y2))
原Tensor:
 [[1. 2. 3.]
 [4. 5. 6.]]
==============================
shape(1, 6):
 [[1. 2. 3. 4. 5. 6.]]
==============================
shape(6, 1):
 [[1.]
 [2.]
 [3.]
 [4.]
 [5.]
 [6.]]
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值