tensorflow-gpu比cpu慢

时间:2018-06-28 07:31:07

标签: tensorflow

OS Platform and Distribution : windows 10

TensorFlow installed : anaconda install

TensorFlow version : tensorflow-gpu 1.8.0

Python version : 3.6

CUDA/cuDNN version : CUDA 9.0/cuDNN7.0.5

GPU model, memory and cpu : GTX1070/24GB/i7-7700HQ

import sys
import tensorflow as tf
import numpy as np
import os
from tqdm import tqdm

tf.set_random_seed(999) # for weight and bias

xy = np.genfromtxt('training_data.csv', delimiter=',', dtype=np.float32) 

test = np.genfromtxt('test_data.csv', delimiter=',', dtype=np.float32)

x_data = xy[:, 0:-1]
y_data = xy[:, [-1]]

test_x_data = test[:,0:-1]
test_y_data = test[:,[-1]]

X = tf.placeholder(tf.float32, shape=[None, 9])
Y = tf.placeholder(tf.float32, shape=[None, 1])

W = tf.Variable(tf.random_normal([9, 1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')

hypothesis = tf.matmul(X, W) + b
hypothesis_hist = tf.summary.histogram("hypothesis", hypothesis)

cost = tf.reduce_mean(tf.square(hypothesis - Y))
cost_summ = tf.summary.scalar("cost", cost)

optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5)
train = optimizer.minimize(cost)
merged_summary = tf.summary.merge_all()

with tf.device("/gpu:0"):
    with tf.Session() as sess:

        writer = tf.summary.FileWriter("./logs")
        writer.add_graph(sess.graph)

        sess.run(tf.global_variables_initializer())

        for step in tqdm(range(500001)):
            summary, _ = sess.run([merged_summary, train], feed_dict={X: x_data, Y: y_data})
            writer.add_summary(summary, global_step=step)

        tests = sess.run(hypothesis,feed_dict={X:test_x_data})

        errors = abs(tests- test_y_data) / tests * 100.0

print("max error(%) : ",np.max(errors))

print("min error(%) : ",np.min(errors))

print("avg error(%) : ",np.average(errors))

Here's my source.

当我用gpu尝试时,我得到900〜1100它/秒 但是当我用cpu尝试时,我得到2200〜2500 it / s

tensorflow-gpu在mnist和gpu测试中运行良好。 (结果:cpu:11s,gpu:1s)

我的问题是,为什么使用gpu时性能会慢得多。 我进行了很多搜索,但是找不到正确的答案。

我所得到的只是不使用feed_dect或重新安装tensorflow-gpu,瓶颈等...

使用gpu的线性回归是否还有其他问题?

感谢您阅读

0 个答案:

没有答案