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.