简体   繁体   中英

How to form multiple layers tensor in Tensorflow

在张量流中,我有一个形状为 [2,3,3,1] 的张量,现在我想将张量复制到多层形状为 [2,3,3,3] 的张量,我该怎么做?

You can achieve this with tf.tile or tf.concat :

t = tf.random_uniform([2, 3, 3, 1], 0, 1)
s1 = tf.tile(t, [1, 1, 1, 3])
s2 = tf.concat([t]*3, axis=-1)

with tf.Session() as sess:
    tnp, s1np, s2np = sess.run([t, s1, s2])
    print(tnp.shape)
    print(s1np.shape)
    print(s2np.shape)

which prints

(2, 3, 3, 1)
(2, 3, 3, 3)
(2, 3, 3, 3)

To illustrate what happens, may be it's easier to look at a 2d example:

import tensorflow as tf

t = tf.random_uniform([2, 1], 0, 1)
s1 = tf.tile(t, [1, 3])
s2 = tf.concat([t]*3, axis=-1)

with tf.Session() as sess:
    tnp, s1np, s2np = sess.run([t, s1, s2])
    print(tnp)
    print(s1np)
    print(s2np)

which prints

[[0.52104855]
 [0.95304275]]
[[0.52104855 0.52104855 0.52104855]
 [0.95304275 0.95304275 0.95304275]]
[[0.52104855 0.52104855 0.52104855]
 [0.95304275 0.95304275 0.95304275]]

This is another solution if you want values for each duplicate generated by the same initializer (but not necessary the exact same values).

import tensorflow as tf
tf.reset_default_graph()
init_shape = [2, 3, 3, 1]
n_times = 2
var1 = tf.Variable(tf.random_normal(shape=init_shape))
vars_ = [tf.contrib.copy_graph.copy_variable_to_graph(var1, var1.graph)
         for _ in range(n_times)] + [var1]

result = tf.reshape(tf.concat(vars_, axis=3),
                    shape=init_shape[:-1] + [len(vars_)])
print(result.get_shape().as_list())
# [2, 3, 3, 3]

Copy variables using tf.contrib.copy_graph and then concatenate them as in previous answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM