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每一维重复的次数。