Tensorflow elementwise matrix multiplication (for-loop) error
<p>I want to calculate matrix multiplication with Tensorflow. <br>
But it occured errors while numpy usage doesn't make errors. <br>
(Process finished with exit code 139 (interrupted by signal 11: SIGSEGV))</p>

<p><strong>Tensorflow with CPU / GPU : occured the error : Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
With Numpy (Without Tensorflow) : It occurs well</strong></p>

<pre><code>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]
</code></pre>

<p>I used i7-4k / 16GB ram / GTX-1070(8GB) <br>
<strong>And I need a for-loop elementwise matrix multiplication solution with Tensorflow.</strong> <br>
(Actually, My problem is more complex than above code. So it is hard to makes it vectorization) <br></p>

<p>Thank you in advance !!</p>

<p>You have a few errors in what you pasted.  You aren't importing numpy correctly.</p>

<p>for tensorflow you need to create a session. </p>

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

<p>for numpy it as built in element wise multiplication</p>

<pre><code>result = np.multiply(A,B)
</code></pre>

<p>Just use that.  You can also index into A, B when calling it</p>

<pre><code>result = np.multiply(A[i,:],B[:,i])
</code></pre>

python tensorflow