Why is TensorFlow matmul () so much slower than NumPy multiply ()?

In the python code below, why is the multiplication time through numpy much less than through tensorflow?

import tensorflow as tf
import numpy as np
import time
size=10000
x = tf.placeholder(tf.float32, shape=(size, size))
y = tf.matmul(x, x)

with tf.Session() as sess:
  rand_array = np.random.rand(size, size)

  start_time = time.time()
  np.multiply(rand_array,rand_array)
  print("--- %s seconds numpy multiply ---" % (time.time() - start_time))

  start_time = time.time()
  sess.run(y, feed_dict={x: rand_array})
  print("--- %s seconds tensorflow---" % (time.time() - start_time))

Output signal

--- 0.22089099884 seconds numpy multiply ---
--- 34.3198359013 seconds tensorflow---
+4
source share
1 answer

Well, referring to the documentation:

numpy.multiply (x1, x2 [, out]) = Multiple arguments elementwise.

and

tf.matmul (a, b, transpose_a = False, transpose_b = False, a_is_sparse = False, b_is_sparse = False, name = None)

Multiplies matrix a by matrix b, creating a * b.

The inputs should be two-dimensional matrices, with corresponding internal dimensions, possibly after transposition.

, : O (n ^ 2) O (n ^ 3). , 2 :

import tensorflow as tf
import numpy as np
import time
size=2000
x = tf.placeholder(tf.float32, shape=(size, size))
y = tf.matmul(x, x)
z = tf.matmul(y, x)

with tf.Session() as sess:
  rand_array = np.random.rand(size, size)

  start_time = time.time()
  for _ in xrange(10):
      np.dot(np.dot(rand_array,rand_array), rand_array)
  print("--- %s seconds numpy multiply ---" % (time.time() - start_time))

  start_time = time.time()
  for _ in xrange(10):
      sess.run(z, feed_dict={x: rand_array})
  print("--- %s seconds tensorflow---" % (time.time() - start_time))

:

--- 2.92911195755 seconds numpy multiply ---
--- 0.32932305336 seconds tensorflow---

GPU (gtx 1070).

+6

Source: https://habr.com/ru/post/1664309/


All Articles