【问题标题】:Confusion about TensorFlow shape rank关于 TensorFlow 形状等级的困惑
【发布时间】:2016-11-27 08:29:17
【问题描述】:

我知道已经有类似标题的问题,但在您重复报告之前,请允许我说,这些问题的所有答案都是非常临时的,不适用于我的问题。

我无法理解为什么我不能在 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))

我被难住了。有没有人有任何线索?提前致谢。 :)

【问题讨论】:

    标签: python python-3.x tensorflow


    【解决方案1】:

    我已经在几个地方修复了您的代码,现在它可以工作了(见下文)。通常,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))
    

    【讨论】:

    • 惊人的!感谢您对我的错误的迅速回应和彻底的解释。接受你的回答!
    猜你喜欢
    • 2022-06-15
    • 2021-05-26
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    • 2021-06-22
    • 2019-05-17
    • 2020-03-06
    • 2016-03-16
    相关资源
    最近更新 更多