tf.reshape

# -*- coding: utf-8 -*-
"""
Created on Thu Apr 26 09:19:06 2018

@author: Loulch C.C
"""
import tensorflow as tf
"""
reshape(tensor, shape, name=None)

    Given `tensor`, this operation returns a tensor that has the same values
    as `tensor` with shape `shape`.
    
    If one component of `shape` is the special value -1, the size of that dimension
    is computed so that the total size remains constant.  In particular, a `shape`
    of `[-1]` flattens into 1-D.  At most one component of `shape` can be -1.
    
    If `shape` is 1-D or higher, then the operation returns a tensor with shape
    `shape` filled with the values of `tensor`. In this case, the number of elements
    implied by `shape` must be the same as the number of elements in `tensor`.
Args:
      tensor: A `Tensor`.
      shape: A `Tensor`. Must be one of the following types: `int32`, `int64`.
        Defines the shape of the output tensor.
      name: A name for the operation (optional).
    
    Returns:
      A `Tensor`. Has the same type as `tensor`.
"""
t = [1, 2, 3, 4, 5, 6, 7, 8, 9]
a=tf.reshape(t, [3, 3]) 
sess= tf.Session()
sess.run(a)
#输出:array([[1, 2, 3],
#       [4, 5, 6],
#       [7, 8, 9]])
t=[[[1, 1], [2, 2]],[[3, 3], [4, 4]]]
a=tf.reshape(t, [2, 4])
sess.run(a)
#输出:array([[1, 1, 2, 2],
#       [3, 3, 4, 4]])
t=[[[1, 1, 1],[2, 2, 2]],[[3, 3, 3],[4, 4, 4]],[[5, 5, 5],[6, 6, 6]]]
a=tf.reshape(t, [-1])# -1 can also be used to infer the shape
sess.run(a)
#输出:array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6])
a=tf.reshape(t, [2, -1])# -1 is inferred to be 9
sess.run(a)
#输出:array([[1, 1, 1, 2, 2, 2, 3, 3, 3],
#       [4, 4, 4, 5, 5, 5, 6, 6, 6]])
a=tf.reshape(t, [-1, 9])# -1 is inferred to be 2
sess.run(a)
#输出:array([[1, 1, 1, 2, 2, 2, 3, 3, 3],
#       [4, 4, 4, 5, 5, 5, 6, 6, 6]])
a=tf.reshape(t, [ 2, -1, 3])# -1 is inferred to be 3
sess.run(a)
#输出:array([[[1, 1, 1],
#        [2, 2, 2],
#        [3, 3, 3]],

#       [[4, 4, 4],
#        [5, 5, 5],
#        [6, 6, 6]]])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值