tf.reshape

xiaoxiao2021-02-28  48

# -*- 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]]])
转载请注明原文地址: https://www.6miu.com/read-2622581.html

最新回复(0)