Tensorflow无法在GPU上运行整数矩阵乘法

时间:2016-08-29 14:17:09

标签: tensorflow

看看这个例子,我尝试使用我的GPU多个两个tf.int32矩阵。

import tensorflow as tf
matrix1 = tf.constant([[3,3]])
matrix2 = tf.constant([[2],[2]])

with tf.device("/gpu:0"):
    product = tf.matmul(matrix1,matrix2)

with tf.Session() as sess:
    result = sess.run(product)
    print(result)

类似于https://www.tensorflow.org/versions/r0.10/get_started/basic_usage.html

上的示例

我得到了输出:

...
I tensorflow/core/common_runtime/gpu/gpu_init.cc:102] Found device 0 with properties: 
name: GeForce GTX 1080
major: 6 minor: 1 memoryClockRate (GHz) 1.7715
pciBusID 0000:03:00.0
Total memory: 7.92GiB
Free memory: 213.62MiB
I tensorflow/core/common_runtime/gpu/gpu_init.cc:126] DMA: 0 
I tensorflow/core/common_runtime/gpu/gpu_init.cc:136] 0:   Y 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:839] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:03:00.0)
E tensorflow/core/client/tensor_c_api.cc:485] Cannot assign a device to node 'MatMul': Could not satisfy explicit device specification '/device:GPU:0' because no supported kernel for GPU devices is available.
     [[Node: MatMul = MatMul[T=DT_INT32, transpose_a=false, transpose_b=false, _device="/device:GPU:0"](Const, Const_1)]]

为什么我不能在GPU上执行矩阵乘法?我可以使用allow_soft_placement = True解决这个问题,但我想在GPU上执行此操作..

1 个答案:

答案 0 :(得分:2)

TensorFlow中的GPU目前尚未实现整数乘法,您的矩阵matrix1matrix2的类型为tf.int32。 (事实证明它很容易实现,但由于this answer中讨论的各种原因,TensorFlow并未在GPU设备上包含tf.int32的操作注册。)

假设您实际上对乘以(大得多)浮点矩阵感兴趣,可以将程序更改为:

import tensorflow as tf
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.], [2.]])

with tf.device("/gpu:0"):
    product = tf.matmul(matrix1,matrix2)

with tf.Session() as sess:
    result = sess.run(product)
    print(result)

...并且乘法将在您的GPU上执行。