I am implementing an operation in keras, such that it can work on both theano and tensorflow backend. Suppose the input of the operation is:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]], dtype=int64)
then its output should be:
array([[ 0, 1, 2, 3, 4, 5],
[ 3, 4, 5, 0, 1, 2],
[ 6, 7, 8, 9, 10, 11],
[ 9, 10, 11, 6, 7, 8]], dtype=int64)
My codes are as follows:
from keras import backend as K
def pairreshape(x,target_dim,input_shape):
x1, x2 = x[0::2,], x[1::2,]
x1_concate = K.concatenate((x1,x2), axis=target_dim)
x2_concate = K.concatenate((x2,x1), axis=target_dim)
if K.image_dim_ordering() == 'th':
import theano.tensor as T
x_new = T.repeat(x,2,axis=target_dim)
x_new = T.set_subtensor(x_new[0::2], x1_concate)
x_new = T.set_subtensor(x_new[1::2], x2_concate)
elif K.image_dim_ordering() == 'tf':
import tensorflow as tf
repeats = [1] * len(input_shape)
repeats[target_dim] = 2
x_new = tf.tile(x, repeats)
x_new[0::2] = x1_concate #TypeError: 'Tensor' object does not support item assignment
x_new[1::2] = x2_concate #TypeError: 'Tensor' object does not support item assignment
I have successfully implemented it by theano, but I can not figure out how to assign a tensor by tensorflow. The last two lines of tensor assignment in tensorflow will report error. Is there a T.set_subtensor equivalence in tensorflow? or can you please recommend a better implementation of the operation? Thanks.
TensorFlow tensors are read-only. In order to modify things you need to use variables and .assign
(= can not be overriden in Python)
tensor = tf.Variable(tf.ones((3,3)))
sess.run(tf.initialize_all_variables())
sess.run(tensor[1:, 1:].assign(2*tensor[1:,1:]))
print(tensor.eval())
Output
[[ 1. 1. 1.]
[ 1. 2. 2.]
[ 1. 2. 2.]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With