【问题标题】:Tensorflow elementwise matrix multiplication (for-loop) errorTensorflow逐元素矩阵乘法(for-loop)错误
【发布时间】:2016-10-07 07:05:50
【问题描述】:

我想用 Tensorflow 计算矩阵乘法。
但是它发生了错误,而 numpy 的使用不会出错。
(进程以退出代码 139 结束(被信号 11:SIGSEGV 中断))

带有 CPU / GPU 的 Tensorflow:发生错误:进程以退出代码 139 完成(被信号 11:SIGSEGV 中断) 使用 Numpy(没有 Tensorflow):效果很好

import tensorflow as tf
mport numpy as np

num_doc = 1000 #10000
num_topic = 10
num_user = 20 #8000
cost = 0

#### Tensorflow with CPU ####
with tf.device('/cpu:0'):
    a_dk = tf.Variable(tf.random_normal([num_doc,num_topic]))
    x_uk = tf.Variable(tf.random_normal([num_user,num_topic]))
    x_dk = tf.Variable(tf.random_normal([num_doc,num_topic]))
    for u in range(num_user):
        print u
        for d in range(num_doc):
            for k in range(num_topic):
                cost = cost + a_dk[d,k] * x_uk[u,k] * x_dk[d,k]

#### Tensorflow with GPU ####
a_dk = tf.Variable(tf.random_normal([num_doc,num_topic]))
x_uk = tf.Variable(tf.random_normal([num_user,num_topic]))
x_dk = tf.Variable(tf.random_normal([num_doc,num_topic]))
for u in range(num_user):
    print u
    for d in range(num_doc):
        for k in range(num_topic):
            cost = cost + a_dk[d,k] * x_uk[u,k] * x_dk[d,k]

#### Numpy ####
a_dk = np.random.randn(num_doc, num_topic)
x_uk = np.random.randn(num_user, num_topic)
x_dk = np.random.randn(num_doc, num_topic)
for u in range(num_user):
    print u
    for d in range(num_doc):
        for k in range(num_topic):
            cost = cost + a_dk[d,k] * x_uk[u,k] * x_dk[d,k]

我使用了 i7-4k / 16GB ram / GTX-1070(8GB)
我需要一个使用 Tensorflow 的 for-loop elementwise 矩阵乘法解决方案。
(其实我的问题比上面的代码更复杂,所以很难向量化)

提前谢谢你!!

【问题讨论】:

  • 请添加更多详细信息,您到底想要什么?为什么你把它标记为 c++,你想要 c++ 中基于 for 循环的解决方案吗?如果你在 python 中需要帮助,请将其标记为 python。

标签: python tensorflow


【解决方案1】:

您粘贴的内容有一些错误。您没有正确导入 numpy。

对于 tensorflow,您需要创建一个会话。

with tf.Session() as sess:
    ... cpu or gpu code here...
    print(sess.run([cost]))

对于 numpy 来说,它是内置的元素乘法

result = np.multiply(A,B)

就用那个。调用时也可以索引到A、B

result = np.multiply(A[i,:],B[:,i])

【讨论】:

  • 我想通过tensorflow而不是numpy来计算。
  • 如果您使用的是 0.11 及更高版本,它几乎与 numpy 完全相同。结果 = tf.matmul(A,B) 或结果 = tf.matmul(A[i,:],B[:,i])
  • 我使用 tensorflow 0.10。我想用 GPU 计算上面的方程 #### Tensorflow #### 而不仅仅是 tf.matmul(A,B) 或 tf.matmul(A[i,:],B[:,i])
  • 您应该能够矢量化您的方法,这就是我的建议。它会比 for 循环快得多。
  • tmp = tf.matmul(A[i,:,:],B[:,i,:]); res = tf.matmul(tmp[i,:,:],C[:,:,i]) 你应该能够概括它。
【解决方案2】:

该错误很可能是由于创建了一个巨大的 TensorFlow 图,每次最内层循环迭代至少有 10 个节点。使用 TensorFlow 的内置元素运算符可以更有效地计算此类运算,这些运算符支持 broadcasting 以避免需要物化整个矩阵。例如,您的程序可以使用两个(按元素)tf.multiply() 操作和一个 tf.reduce_sum()

a_dk = tf.Variable(tf.random_normal([num_doc,num_topic]))
x_uk = tf.Variable(tf.random_normal([num_user,num_topic]))
x_dk = tf.Variable(tf.random_normal([num_doc,num_topic]))

ax_dk = tf.multiply(a_dk, x_dk)

# Add extra dimensions to broadcast ax_dk along the user dimension, and
# x_uk along the docs dimension:
cost = tf.reduce_sum(tf.multiply(ax_dk[:, None, :], x_uk[None, :, :]))

【讨论】:

    猜你喜欢
    • 2016-06-04
    • 2022-11-28
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 2015-01-08
    • 2014-11-13
    • 2018-12-20
    相关资源
    最近更新 更多