250118 TIL #593 AI Tech #125 Tensorflow vs Keras vs PyTorch

김춘복·2025년 1월 17일
0

TIL : Today I Learned

목록 보기
595/604

Today I Learned

오늘은 이번 프로젝트 진행하는동안 머리아프게했던 텐서플로우를 비교해서 정리해봤다.


DL Frameworks

현재 가장 인기 있는 딥러닝 프레임워크는 PyTorch, TensorFlow, Keras가 있다.
PyTorch는 Facebook이 개발한 저수준 API로 연구자들 사이에서 선호되고,
TensorFlow는 Google이 개발한 프레임워크로 산업계에서 널리 사용된다.
Keras는 고수준 API로 사용하기 쉽고 간단한 문법을 제공한다.

API 레벨

  • Keras: 고수준 API로 TensorFlow, CNTK, Theano 위에서 동작 가능

  • TensorFlow: 고수준과 저수준 API 모두 제공

  • PyTorch: 저수준 API로 배열 표현식과 직접 작동

아키텍처 특징

  • Keras: 간단하고 읽기 쉬운 구조로 설계

  • TensorFlow: 복잡한 구조로 초보자가 사용하기 어려움

  • PyTorch: 복잡한 구조이지만 Python과 유사한 직관적인 코딩 방식
    Dynamic Graph(동적 그래프)를 사용해 실시간으로 데이터 바꿔가며 비교 가능

디버깅 기능

  • Keras: 단순한 네트워크 구조로 디버깅이 덜 필요

  • TensorFlow: 복잡한 디버깅 과정

  • PyTorch: 뛰어난 디버깅 기능과 직관적인 오류 추적

주 활용 분야

  • PyTorch: 유연한 구조와 직관적인 디버깅으로 연구자들이 선호하지만 최근들어 산업 전반적으로 텐서플로우 이상으로 사용이 대폭 증가중.

  • TensorFlow: 안정적인 서빙 시스템과 다양한 배포 옵션 제공해 대규모 프로덕션 환경에서 사용

  • Keras: 간단한 구문과 빠른 개발이 가능해 학습용으로 적합

MLP 구현 비교

  • PyTorch
import torch
import torch.nn as nn

class MLP(nn.Module):
    def __init__(self):
        super(MLP, self).__init__()
        self.fc1 = nn.Linear(784, 256)  # 입력층 -> 첫 번째 은닉층
        self.fc2 = nn.Linear(256, 128)  # 첫 번째 -> 두 번째 은닉층
        self.fc3 = nn.Linear(128, 10)   # 두 번째 은닉층 -> 출력층
        self.relu = nn.ReLU()
        self.dropout = nn.Dropout(0.5)
        
    def forward(self, x):
        x = x.view(-1, 784)  # 입력 데이터 평탄화
        x = self.relu(self.fc1(x))
        x = self.dropout(x)
        x = self.relu(self.fc2(x))
        x = self.dropout(x)
        x = self.fc3(x)
        return x[1][4]
  • tensorflow/keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout

model = Sequential([
    Dense(256, activation='relu', input_shape=(784,)),
    Dropout(0.5),
    Dense(128, activation='relu'),
    Dropout(0.5),
    Dense(10, activation='softmax')
])[2][12]
  • keras functional API
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model

inputs = Input(shape=(784,))
x = Dense(256, activation='relu')(inputs)
x = Dropout(0.5)(x)
x = Dense(128, activation='relu')(x)
x = Dropout(0.5)(x)
outputs = Dense(10, activation='softmax')(x)

model = Model(inputs=inputs, outputs=outputs)[9]
profile
Backend Dev / Data Engineer

0개의 댓글