PyTorch와 TensorFlow모델의 모델 파라미터의 수를 계산하는 방법
이 글은 TensorFlow와 PyTorch 딥 러닝 모델의 파라미터의 수를 계산하는 방법에 관한 튜토리얼을 예제와 함께 제공합니다.
Created on March 23|Last edited on May 30
Comment
우리는 바로 액세스가 가능한 대규모 모델의 시대에 살고 있습니다. 누구나 사전에 훈련된 Deberta v3 모델과 이를 임의의 데이터 집합에서 미세 조정하여 Kaggle Kernel 을 만들 수 있습니다. 많은 사람들이 깨닫지 못하는 것은 100GB 이상의 훈련 데이터에서 사전에 훈련된 75-100 M 파라미터 모델을 사용하고 있다는 것입니다.
물론, 과도한 파라미터화(over-parameterization)가 더 나은 결과로 이어질 수도 있지만, 저장 공간이 늘어나고 그 결과 추론 시간이 오래 걸리게 됩니다. 따라서 모델이 보유한 파라미터의 수를 기록할 수 있습니다.
상대적으로 동일하게 수행하면서도 10배 또는 20배 더 적은 파라미터를 보유한 모델이 있다면 흥미롭지 않겠습니까? 성과 그래프 대 모델 파라미터 또는 단순히 벤치마킹의 경우, 이는 필수적으로 알아야 할 사실입니다.
목차
DeepMind's Flamingo: Visual & Language Communication Combined
DeepMind recently released a combined visual and language model (a VLM) called Flamingo, capable of a variety of tasks taking text and image input simultaneously.
Meta AI Releases OPT-175B, Set Of Free-To-Use Pretrained Language Models
Meta AI announced a blog post today that they have released a new set of language models under the name "Open Pretrained Transformer". These models aim to replicate GPT-3 while being freely available for local use and training.
코드
PyTorch
PyTorch는 (적어도 현재는) 모델 파라미터의 수를 세는 유틸리티 기능이 없지만, 모델 파라미터를 얻는 데 사용할 수 있는 모델 클래스의 속성이 있습니다.
다음 스니펫을 사용하여 모든 모델 파라미터를 받아보세요
total_params = sum(param.numel() for param in model.parameters())
이 스니펫에 대해 빠르게 살펴보겠습니다:
- sum(...): 모든 파라미터의 그룹을 더합니다(모듈에는 레이어와 같은 하위모듈이 포함되어 있을 수도 있습니다)
참고: 이 스니펫은 모듈에서 모든 파라미터를 리턴합니다. 학습 가능과 학습 불가능 둘 다 가능합니다. 학습 가능 파라미터만을 원한다면 다음 스니펫을 사용하십시오.
💡
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
Tensor의 .requires_grad 추가 속성을 사용하여 학습 가능 파라미터인지를 판단합니다. 만약 tensor에 requires_grad가 참으로 설정되어 있으면, autograd 엔진이 이 tensor를 수정할 수 있습니다. 즉, 그것은 '학습가능'한 상태입니다.
Tensorflow
Tensorflow는 keras 유틸(keras.utils.layer_utils)에서 사용 가능한 count_params라고 불리는 파라미터의 수를 계산하는 유틸리티 기능을 제공합니다.
다음 스니펫을 사용하여 Tensorflow 모델의 모든 학습 가능 파라미터와 학습 불가능 파라미터를 셀 수 있습니다.
from keras.utils.layer_utils import count_paramsmodel = ...trainable_params = sum(count_params(layer) for layer in model.trainable_weights)non_trainable_params = sum(count_params(layer) for layer in model.non_trainable_weights)
이제 이 정보로 무엇을 할 수 있을지 궁금하신가요? Weights & Biases의 도움으로, wandb.config 파라미터로 또는 요약으로 W&B에 파라미터의 수를 기록한 다음 나중에 검토와 비교를 수행할 수 있습니다.
wandb.config.update({"Model Parameters": trainable_model_params})###################### または #####################wandb.run.summary["Model Parameters"] = trainable_model_params
요약
이 글에서 TensorFlow와 PyTorch 모델의 파라미터의 수를 계산하는 방법을 보았습니다. W&B의 전체 기능을 보려면, 이 5분 분량의 짧은 가이드 영상을 보세요. 수학과 '처음부터 시작하는' 코드 구현을 다루는 리포트를 더 많이 보고 싶다면, 아래 의견란이나 포럼 ✨에 남겨주세요!
추천 자료
Setting Up TensorFlow And PyTorch Using GPU On Docker
A short tutorial on setting up TensorFlow and PyTorch deep learning models on GPUs using Docker.
How to Compare Keras Optimizers in Tensorflow for Deep Learning
A short tutorial outlining how to compare Keras optimizers for your deep learning pipelines in Tensorflow, with a Colab to help you follow along.
Preventing The CUDA Out Of Memory Error In PyTorch
A short tutorial on how you can avoid the "RuntimeError: CUDA out of memory" error while using the PyTorch framework.
How to Initialize Weights in PyTorch
A short tutorial on how you can initialize weights in PyTorch with code and interactive visualizations.
Recurrent Neural Network Regularization With Keras
A short tutorial teaching how you can use regularization methods for Recurrent Neural Networks (RNNs) in Keras, with a Colab to help you follow along.
Tutorial: Regression and Classification on XGBoost
A short tutorial on how you can use XGBoost with code and interactive visualizations.
Add a comment
Iterate on AI agents and models faster. Try Weights & Biases today.