Pytorch vs Tensorflow (<2.0)

반디·2023년 9월 14일
0

PyTorch

목록 보기
3/3

Pytorch

Meta AI에서 만든 딥러닝 프레임 워크이다. 언어는 Python을 사용하지만, Pytorch는 Torch라는 컴퓨팅 프레임 워크를 기반으로 하고 있고, Torch는 C와 CUDA 기반이다.

  • CUDA: NVIDIA가 개발한 병렬 컴퓨팅 플랫폼

Tensorflow

Google Brain에서 만든 딥러닝 프레임워크로, pytorch에 비해서 시각화와 모델 서빙에 좀 더 유리한 측면이 있어서 산업계에서는 tensorflow를 더 많이 사용했었다고 한다.

Pytorch vs Tensorflow

pytorch와 tensorflow의 가장 큰 차이점은 동적 그래프를 이용하느냐, 정적 그래프를 이용하느냐였는데, Tensorflow 2.0는 Pytorch처럼 동적 그래프 방식으로 바뀌었다.

PytorchTensorflow (<2.0)
PythonPython, Javascript, C++, Java, (Go, Swift)
Define by RunDefine and Run
동적 그래프정적 그래프
디버깅이 비교적 쉬움디버깅이 비교적 어려움
  • 코드로 살펴본다면?
    - tensorflow (<2.0)
# Importing tensorflow version 1
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

# Initializing placeholder variables of
# the graph
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)

# Defining the operation
c = tf.multiply(a, b)

# Instantiating a tensorflow session
with tf.Session() as sess:

	# Computing the output of the graph by giving
	# respective input values
	out = sess.run(, feed_dict={a: [15.0], b: [20.0]})[0][0]

	# Computing the output gradient of the output with
	# respect to the input 'a'
	derivative_out_a = sess.run(tf.gradients(c, a), feed_dict={
								a: [15.0], b: [20.0]})[0][0]

	# Computing the output gradient of the output with
	# respect to the input 'b'
	derivative_out_b = sess.run(tf.gradients(c, b), feed_dict={
								a: [15.0], b: [20.0]})[0][0]

위와 같이 with tf.Session()이 있고, 이하의 작업이 한꺼번에 실행되다보니, 일부를 실행해보거나 디버깅하는 작업이 어려웠다.

반면, pytorch는

# Importing torch
import torch

# Initializing input tensors
a = torch.tensor(15.0, requires_grad=True)
b = torch.tensor(20.0, requires_grad=True)

# Computing the output
c = a * b

# Computing the gradients
c.backward()

# Collecting the output gradient of the
# output with respect to the input 'a'
derivative_out_a = a.grad

# Collecting the output gradient of the
# output with respect to the input 'b'
derivative_out_b = b.grad

코드의 일부를 실행해보며 결과를 확인할 수 있는 구조로 되어있다.

  • 어떤 라이브러리를 더 많이 쓸까?

    papers with code의 트렌드를 보면 pytorch가 압도적인 것을 확인할 수 있다.

참고문헌

profile
꾸준히!

0개의 댓글