-
Notifications
You must be signed in to change notification settings - Fork 0
Barebones training pipeline #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cl676767
wants to merge
5
commits into
main
Choose a base branch
from
barebones-training-pipeline
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
366b73a
training pipeline
cl676767 33af425
finished the barebones training pipeline
cl676767 13d81f8
Updated based on feedback
cl676767 e86debe
created test in different file, but smth wrong /w importing pipeline
cl676767 6b77d40
idk
cl676767 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import torch | ||
import torch.nn as nn | ||
import torch.optim as optim | ||
from torch.utils.data import DataLoader, random_split | ||
|
||
class TrainingPipeline (): | ||
def __init__(self,model,data,batch_size=32,lr =0.01,epochs=100, optimizer = optim.Adam, criterion = nn.BCELoss, device = None, train_percent = 0.8): | ||
''' | ||
Initialize the Training Pipeline | ||
Args: | ||
:param model: PASS IN MODEL CLASS. | ||
:param data: pass into data in tensor form. | ||
:param batch_size: batch size for training. | ||
:param lr: learning rate. | ||
:param epochs: Number of training epochs. | ||
:param device: The device model and data will be passed into(ie cpu or cuda). | ||
:param optimizer: PASS IN THE OPTIMIZER CLASS. | ||
:param criterion: PASS IN LOSS FUNCTION CLASS. | ||
:param train_percent: used to split into training and validation | ||
''' | ||
self.device = device or torch.device("cuda" if torch.cuda.is_available() else "cpu") | ||
self.model = model.to(self.device) | ||
self.batch_size = batch_size | ||
self.criterion = criterion() | ||
self.optimizer = optimizer(self.model.parameters(),lr = lr) | ||
self.epochs = epochs | ||
|
||
#train test split(default 8:2) | ||
train_size = int(train_percent* len(data)) | ||
val_size = len(data) - train_size | ||
train_data , val_data = random_split(data,[train_size,val_size]) | ||
|
||
self.train_loader = DataLoader(train_data,batch_size = batch_size,shuffle = True) | ||
self.val_loader = DataLoader(val_data, batch_size = batch_size, shuffle = False) | ||
|
||
|
||
|
||
def train_one_epoch(self): | ||
''' | ||
Runs one training loop through the model using training data | ||
Returns: | ||
float: Average loss for the batch | ||
''' | ||
self.model.train() | ||
total_loss = 0 | ||
#loops through each batch | ||
for xb,yb in self.train_loader: | ||
xb , yb = xb.to(self.device) , yb.to(self.device) | ||
preds = self.model(xb) | ||
loss = self.criterion(preds,yb) | ||
total_loss += loss.item() | ||
|
||
self.optimizer.zero_grad() | ||
loss.backward() | ||
self.optimizer.step() | ||
|
||
return total_loss / len(self.train_loader) #returns average loss for the batch | ||
|
||
|
||
def validate(self): | ||
''' | ||
Evaluates the current model using testing data | ||
Returns: | ||
float: average loss for the batch | ||
''' | ||
self.model.eval() | ||
total_loss = 0 | ||
with torch.inference_mode(): | ||
#loops through each batch | ||
for xb,yb in self.val_loader: | ||
xb , yb = xb.to(self.device) , yb.to(self.device) | ||
preds = self.model(xb) | ||
loss = self.criterion(preds,yb) | ||
total_loss += loss.item() | ||
|
||
|
||
return total_loss / len(self.val_loader) #returns average loss for the batch | ||
|
||
def training(self): | ||
''' | ||
Loop where all the training actually happens, prints out training and validation loss | ||
''' | ||
for epoch in range(self.epochs): | ||
train_loss = self.train_one_epoch() | ||
val_loss = self.validate() | ||
if(epoch%5 == 0): | ||
print(f"Train loss: {train_loss} Validation loss:{val_loss}") | ||
|
||
def save(self,path): | ||
''' | ||
Saves the model to the given path | ||
Args: | ||
:param path: path where model is saved | ||
|
||
''' | ||
torch.save(self.model.state_dict(),path) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from src.humanoid_vision.training_pipeline import TrainingPipeline | ||
#from training_pipeline import TrainingPipeline | ||
import torch | ||
import torch.nn as nn | ||
from torch.utils.data import TensorDataset | ||
|
||
testModel = nn.Sequential( | ||
nn.Linear(3,1), | ||
nn.Sigmoid() | ||
) | ||
|
||
X = torch.randn(100,3) | ||
print(X) | ||
y = torch.randint(0, 2, (100, 1)).float() | ||
|
||
testData = TensorDataset(X,y) | ||
|
||
pipeline = TrainingPipeline(model = testModel,batch_size = 10,data = testData, device = "cpu") | ||
pipeline.training() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you write a docstring at the start with the explanation on how to use the training pipeline?