tensorflow tf.tile()使用教程

tf.tile()用于将一个tensor进行复制,构建一个新的tensor。

如下为tf.title()源码里面的说明。该函数需要传入两个参数,

  • 一个是input,即需要重复使用的tensor;
  • 另一个是multiples,表示需要重复的次数,multiples每一维表示构建的新张量在每一维上重复的次数;
  • name为可选参数。
  r"""Constructs a tensor by tiling a given tensor.

  This operation creates a new tensor by replicating `input` `multiples` times.

  The output tensor's i'th dimension has `input.dims(i) * multiples[i]` elements,

  and the values of `input` are replicated `multiples[i]` times along the 'i'th

  dimension. For example, tiling `[a b c d]` by `[2]` produces

  `[a b c d a b c d]`.

  Args:
    input: A `Tensor`. 1-D or higher.
    multiples: A `Tensor`. Must be one of the following types: `int32`, `int64`.
      1-D. Length must be the same as the number of dimensions in `input`
    name: A name for the operation (optional).

  Returns:
    A `Tensor`. Has the same type as `input`.
  """
## -*- coding: 'utf-8' -*-

import tensorflow as tf

a = tf.constant([1,2,3])
b = tf.constant([[4,5,6],[7,8,9]])

c = tf.tile(input=a, multiples=[2])
d = tf.tile(input=[a], multiples=[2,3])

f = tf.tile(input=b, multiples=[2,3])

with tf.Session() as sess:
    print (sess.run(c),'\n')
    print (sess.run(d),'\n')
    
    print (sess.run(f),'\n')

输出:

[1 2 3 1 2 3]

[[1 2 3 1 2 3 1 2 3]
 [1 2 3 1 2 3 1 2 3]]

[[4 5 6 4 5 6 4 5 6]
 [7 8 9 7 8 9 7 8 9]
 [4 5 6 4 5 6 4 5 6]
 [7 8 9 7 8 9 7 8 9]]

需要注意的是:multiples里面元素的个数要和input的维数一致,否者会报错(ValueError: Shape must be rank 1 but is rank 2 for 'Tile_1' (op: 'Tile') with input shapes: [3], [2].),因为其表示的是input每一维重复的次数。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值