अपना पहला training loop लिखना

PyTorch के साथ Deep Learning परिचय

Jasmin Ludolf

Senior Data Science Content Developer, DataCamp

एक neural network को train करना

  1. एक मॉडल बनाएँ
  2. एक loss function चुनें
  3. एक डेटासेट परिभाषित करें
  4. एक optimizer सेट करें
  5. training loop चलाएँ:
    • loss निकालें (forward pass)
    • gradients compute करें (backpropagation)
    • model parameters अपडेट करें
PyTorch के साथ Deep Learning परिचय

Data Science Salary डेटासेट से परिचय

 experience_level  employment_type  remote_ratio  company_size  salary_in_usd  
        0                0               0.5             1            0.036 
        1                0               1.0             2            0.133     
        2                0               0.0             1            0.234  
        1                0               1.0             0            0.076  
        2                0               1.0             1            0.170

$$

  • Features: categorical, target: salary (USD)
  • Final output: linear layer
  • Loss: regression-specific
PyTorch के साथ Deep Learning परिचय

Mean Squared Error (MSE) Loss

$$

  • MSE loss भविष्यवाणियों और ground truth के बीच squared difference का औसत है
def mean_squared_loss(prediction, target):
  return np.mean((prediction - target)**2)

$$

  • PyTorch में:
criterion = nn.MSELoss()
# Prediction and target are float tensors
loss = criterion(prediction, target)
PyTorch के साथ Deep Learning परिचय

training loop से पहले

# Create the dataset and the dataloader
dataset = TensorDataset(torch.tensor(features).float(),
                        torch.tensor(target).float())


dataloader = DataLoader(dataset, batch_size=4, shuffle=True)
# Create the model model = nn.Sequential(nn.Linear(4, 2), nn.Linear(2, 1))
# Create the loss and optimizer criterion = nn.MSELoss() optimizer = optim.SGD(model.parameters(), lr=0.001)
PyTorch के साथ Deep Learning परिचय

training loop

for epoch in range(num_epochs):

for data in dataloader:
# Set the gradients to zero optimizer.zero_grad()
# Get feature and target from the data loader feature, target = data
# Run a forward pass pred = model(feature) # Compute loss and gradients loss = criterion(pred, target) loss.backward()
# Update the parameters optimizer.step()
PyTorch के साथ Deep Learning परिचय

अभ्यास करते हैं!

PyTorch के साथ Deep Learning परिचय

Preparing Video For Download...