这是我在numpy中的代码示例
import numpy as np
big = np.zeros((7,7))
upper_left = np.ones((4,4))
upper_right = np.ones((4,3))*2
botton_left= np.ones((3,4))*3
botton_right= np.ones((3,3))*4
big[0::2,0::2]=upper_left
big[0::2,1::2]=upper_right
big[1::2,0::2]=botton_left
big[1::2,1::2]=botton_right
如果所有大/ upper_left / upper_right / botton_left / botton_right都是张量流中的张量。 我该如何在Tensorflow中执行此运算符? 谢谢您的帮助!
答案 0 :(得分:0)
尝试一下:
import tensorflow as tf
big = tf.zeros((7,7),tf.float32)
upper_left = tf.ones((4,4),tf.float32)
upper_right = tf.ones((4,3),tf.float32)*2
botton_left = tf.ones((3,4),tf.float32)*3
botton_right = tf.ones((3,3),tf.float32)*4
big = tf.Variable(big)
one = big[0::2,0::2].assign(upper_left)
two = big[0::2,1::2].assign(upper_right)
three = big[1::2,0::2].assign(botton_left)
with tf.control_dependencies([one,two,three]):
big = big[1::2, 1::2].assign(botton_right)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(big))
#print
[[1. 2. 1. 2. 1. 2. 1.]
[3. 4. 3. 4. 3. 4. 3.]
[1. 2. 1. 2. 1. 2. 1.]
[3. 4. 3. 4. 3. 4. 3.]
[1. 2. 1. 2. 1. 2. 1.]
[3. 4. 3. 4. 3. 4. 3.]
[1. 2. 1. 2. 1. 2. 1.]]