티스토리 뷰

반응형

|텐서플로우 GPU 작동 확인 방법

 

GPU버전을 설치하고 나서도 GPU버전이 제대로 작동하는건지 궁금할 수가 있습니다.

 

아래 소스는 GPU로 구동하는 소스이며 만약 제대로 설치가 안됬다면 실행이 안될겁니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import sys
import numpy as np
import tensorflow as tf
from datetime import datetime
 
shape=(int(10000),int(10000))
 
with tf.device("/gpu:0"):
    random_matrix = tf.random_uniform(shape=shape, minval=0, maxval=1)
    dot_operation = tf.matmul(random_matrix, tf.transpose(random_matrix))
    sum_operation = tf.reduce_sum(dot_operation)
 
startTime = datetime.now()
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as session:
        result = session.run(sum_operation)
        print(result)
 
print("\n" * 2)
print("Time taken:", datetime.now() - startTime)
print("\n" * 2)
 
cs

 

위 소스를 실행하면

 

 

위와 같이 걸린 시간과 어디에서 연산이 되었는지 로그가 나오게 됩니다. 위처럼 GPU로 나오면서 시간이 1~3초가 나온다면 정상적으로 GPU버전으로 설치가 되어있는 것입니다.

(필자는 GTX1070 환경에서 설치되어있습니다.

 

|텐서플로우 CPU와 GPU 속도 비교

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import sys
import numpy as np
import tensorflow as tf
from datetime import datetime
 
shape=(int(10000),int(10000))
 
with tf.device("/cpu:0"):
    random_matrix = tf.random_uniform(shape=shape, minval=0, maxval=1)
    dot_operation = tf.matmul(random_matrix, tf.transpose(random_matrix))
    sum_operation = tf.reduce_sum(dot_operation)
 
startTime = datetime.now()
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as session:
        result = session.run(sum_operation)
        print(result)
 
print("\n" * 2)
print("Time taken:", datetime.now() - startTime)
print("\n" * 2)
 
cs

 

위 소스와 동일하지만 이 소스는 CPU로 진행되는 소스입니다.

 

필자의 컴퓨터로는 대략 13초 정도 걸립니다.

 

GPU : 1.8초, CPU : 13초

 

대략 7배의 속도차이가 납니다.

 

위 두가지 소스를 실행해서 어떤 차이가 있는지 확인하면 GPU버전이 제대로 설치됬는지 확인할 수 있을 것입니다. 

 


출처/참고

 

반응형
댓글