2017-06-15 81 views
0

我有一個矩陣M:張量流體張量操作 - 用尺寸對象平鋪不起作用?

[[101, 51, 12], 
[101, 19, 18]] 

長話短說,我需要創建tensorflow語法如下:

[[0, 0, 101], 
[0, 1, 51], 
[0, 2, 12], 
[1, 0, 101], 
[1, 1, 19], 
[1, 2, 18]] 

我的方法是創建3個單獨的載體和加入到其中來產生以上:

v1 = [0, 0, 0, 1, 1, 1] 

v2 = [0, 1, 2, 0, 1, 2] 

and 

M_flat = [101, 51, 12, 101, 19, 18] 

我可以用得到M_flat = [101, 51, 12, 101, 19, 18]tf.reshape(M, shape=(-1,))

我不知道M的提前尺寸,所以我用下面的語法來獲得它:

dim0 = M.get_shape()[0] 
dim1 = M.get_shape()[1] 

從這個我可以通過

tf.range(dim1) 

獲得[0, 1, 2]要獲得[0, 1, 2, 0, 1, 2]我試着平鋪:

tf.tile(tf.range(dim1), [dim0]) 

但是,這不起作用,因爲dim0是尺寸對象,並且tile想要一個整數,所以我得到的錯誤Expected binary or unicode string, got Dimension(100)

另外,我不知道如果tile可以用來生產這種[0, 0, 0, 1, 1, 1](也許有些重塑,調換,和瓷磚雖然可以,再假設這些操作可以關閉的dim0工作和dim1而不是顯式數字)

有沒有更好的方法來做我想做的事情?

+0

請你發表您的代碼複製此錯誤?人們只需要做一些改變就可以使其發揮作用。 – hars

回答

1

根據該錯誤,您可以通過類似M.get_shape[0].value的方式訪問Dimension對象的value

關於[0, 0, 0, 1, 1, 1]的問題,您可以非常類似於您已經完成的與tf.tile(僅在另一個軸上平鋪)完成的操作。

你可以得到你的結果如下(寫得非常快,但應該做的工作):

M = tf.convert_to_tensor([[101, 51, 12], [101, 19, 18]]) 
dim0 = M.get_shape()[0].value 
dim1 = M.get_shape()[1].value 
r0 = tf.range(dim0) 
r0x = tf.expand_dims(r0,1) 
A = tf.reshape(tf.tile(r0x, [1, dim1]), [-1, 1]) # A = [[0],[0],[0],[1],[1],[1]] 
r1 = tf.range(dim1) 
r1x = tf.expand_dims(r1,1) 
B = tf.reshape(tf.tile(r1x, [dim0, 1]), [-1, 1]) # B = [[0],[1],[2],[0],[1],[2]] 
M = tf.reshape(M, [-1, 1]) 
res = tf.concat((A, B, M), axis=1)