为什么我的GPU无法使用以下代码加速?

时间:2018-07-12 01:25:49

标签: python tensorflow gpu lstm

我刚刚检查我的计算机是否正在使用GPU运行它。 但是运行时间与我的CPU大致相同。

我正在使用Windows10,i7-7700,NV GTX1050,Python 3.6,cuda9.0。

是否有不支持GPU的代码?

或者我该如何解决?谢谢!

X = tf.placeholder(tf.float32,[None, n_steps, n_inputs])
y = tf.placeholder(tf.int32, [None])

lstm_cells = [tf.contrib.rnn.LSTMCell(num_units = n_neurons, use_peepholes=True) for layer in range(n_layers)] 
multi_cell = tf.contrib.rnn.MultiRNNCell(lstm_cells)
outputs, states = tf.nn.dynamic_rnn(multi_cell,X, dtype= tf.float32)
top_layer_h_state = states[-1][1]
logits = tf.layers.dense(top_layer_h_state, n_outputs, name="softmax")
xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=logits)
loss = tf.reduce_mean(xentropy, name="loss")
optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate)
training_op = optimizer.minimize(loss)
correct = tf.nn.in_top_k(logits, y, 1)
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
init = tf.global_variables_initializer()

saver = tf.train.Saver()


n_epochs = 2000



with tf.Session() as sess:
    init.run()
    for epoch in range(n_epochs):
        for iteration in range(n_examples):
            X_batch, y_batch = next_batch(iteration)
            sess.run(training_op, feed_dict={X: X_batch, y: y_batch})

        if epoch % 100 == 0 :
            X_test, y_test, batch_num = test_batch(n_examples)
            acc_test = accuracy.eval(feed_dict={X: X_test, y: y_test})
            print(epoch, "Test accuracy:", acc_test)

0 个答案:

没有答案