2016-11-27 88 views
0

我知道已經有類似標題的問題,但是在您將此作爲重複報告之前,請允許我說所有對這些問題的答案都是非常特殊的,並且不適用於我的問題。關於TensorFlow形狀排名的困惑

我很難理解爲什麼我不能在TensorFlow中使用兩個張量的矩陣乘法(以及技術上的矩陣向量乘法)。我有形狀(1000,1000)的張量v和形狀(1000)的另一個張量h_previous。我正在做大量的矩陣乘法,其中兩個的張量在程序中完全一樣,但這只是拋出了一個神祕的錯誤。這裏是圖的關鍵部分:

# Variables 
# Encoder input 
X = tf.placeholder(tf.float32, shape=[k, None]) 
we = tf.Variable(tf.truncated_normal([500, k], -0.1, 0.1)) 
# Encoder update gate 
wz = tf.Variable(tf.truncated_normal([1000, 500], -0.1, 0.1)) 
uz = tf.Variable(tf.truncated_normal([1000, 1000], -0.1, 0.1)) 
# Encoder reset gate 
wr = tf.Variable(tf.truncated_normal([1000, 500], -0.1, 0.1)) 
ur = tf.Variable(tf.truncated_normal([1000, 1000], -0.1, 0.1)) 
# Encoder h~ [find name] 
w = tf.Variable(tf.truncated_normal([1000, 500], -0.1, 0.1)) 
u = tf.Variable(tf.truncated_normal([1000, 1000], -0.1, 0.1)) 
# Encoder representation weight 
v = tf.Variable(tf.truncated_normal([1000, 1000], -0.1, 0.1)) 

# Encoder 
h_previous = tf.zeros([1000]) 
for t in range(N): 
    # Current vector and its embedding 
    xt = tf.reshape(tf.slice(X, [t, 0], [1, k]), [k]) 
    e = tf.matmul(we, xt) 
    # Reset calculation 
    r = tf.sigmoid(tf.matmul(wr, e) + tf.matmul(ur, h_previous)) 
    # Update calculation 
    z = tf.sigmoid(tf.matmul(wz, e) + tf.matmul(uz, h_previous)) 
    # Hidden-tilde calculation 
    h_tilde = tf.tanh(tf.matmul(w, e) + tf.matmul(u, r * h_previous)) 
    # Hidden calculation 
    one = tf.ones([1000]) 
    h = z * h_previous + (one - z) * h_tilde 
    h_previous = h 
c = tf.tanh(tf.matmul(v, h_previous)) 

我很難過。有沒有人有任何線索?提前致謝。 :)

回答

2

我已經修復了你的代碼在幾個地方,現在它的工作(見下文)。通常,tf.matmul的輸入應該是兩個二維矩陣(請參閱文檔here),而您通過了二維矩陣(大小爲1000x1000)和一維矩陣(大小爲1000)。如果將第二個矩陣重塑爲1000x1或1x1000,則matmul將起作用。

k = 77 
N = 17 
# Variables 
# Encoder input 
X = tf.placeholder(tf.float32, shape=[k, None]) 
we = tf.Variable(tf.truncated_normal([500, k], -0.1, 0.1)) 
# Encoder update gate 
wz = tf.Variable(tf.truncated_normal([1000, 500], -0.1, 0.1)) 
uz = tf.Variable(tf.truncated_normal([1000, 1000], -0.1, 0.1)) 
# Encoder reset gate 
wr = tf.Variable(tf.truncated_normal([1000, 500], -0.1, 0.1)) 
ur = tf.Variable(tf.truncated_normal([1000, 1000], -0.1, 0.1)) 
# Encoder h~ [find name] 
w = tf.Variable(tf.truncated_normal([1000, 500], -0.1, 0.1)) 
u = tf.Variable(tf.truncated_normal([1000, 1000], -0.1, 0.1)) 
# Encoder representation weight 
v = tf.Variable(tf.truncated_normal([1000, 1000], -0.1, 0.1)) 

# Encoder 
h_previous = tf.zeros([1000, 1]) 
for t in range(N): 
    # Current vector and its embedding 
    xt = tf.reshape(tf.slice(X, [t, 0], [1, k]), [k, 1]) 
    e = tf.matmul(we, xt) 
    # Reset calculation 
    r = tf.sigmoid(tf.matmul(wr, e) + tf.matmul(ur, h_previous)) 
    # Update calculation 
    z = tf.sigmoid(tf.matmul(wz, e) + tf.matmul(uz, h_previous)) 
    # Hidden-tilde calculation 
    h_tilde = tf.tanh(tf.matmul(w, e) + tf.matmul(u, r * h_previous)) 
    # Hidden calculation 
    one = tf.ones([1000]) 
    h = z * h_previous + (one - z) * h_tilde 
    h_previous = h 
c = tf.tanh(tf.matmul(v, h_previous)) 
+0

現象!感謝您迅速回復並徹底解釋我的錯誤。接受你的答案! –