【发布时间】:2017-01-06 23:31:50
【问题描述】:
我正在 keras 中实现一个操作,以便它可以在 theano 和 tensorflow 后端上运行。假设操作的输入是:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]], dtype=int64)
那么它的输出应该是:
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)
我的代码如下:
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
我已经通过theano成功实现了它,但是我不知道如何通过tensorflow分配一个张量。 tensorflow中最后两行张量赋值会报错。张量流中是否存在 T.set_subtensor 等价性?或者你能推荐一个更好的操作实施吗?谢谢。
【问题讨论】:
标签: tensorflow theano keras