πŸŽ‰ 75% of content is free forever β€” Unlock Premium from $10/mo β†’
CW
Search courses…
πŸ’Ό Servicesℹ️ Aboutβœ‰οΈ ContactView Pricing Plansfrom $10

Applications in Deep Learning

OptimizationDL Applications🟒 Free Lesson

Advertisement

Applications in Deep Learning


Portfolio Optimization (Markowitz Mean-Variance)


Resource Allocation


Network Design


Scheduling Problems


Feature Selection


Neural Architecture Search (NAS)


Reinforcement Learning as Optimization


AutoML


Python Implementation

Portfolio Optimization

import numpy as np
from scipy.optimize import minimize

def markowitz_optimize(mu, Sigma, target_return):
    n = len(mu)
    
    def portfolio_variance(w):
        return w @ Sigma @ w
    
    constraints = [
        {'type': 'eq', 'fun': lambda w: w @ mu - target_return},
        {'type': 'eq', 'fun': lambda w: np.sum(w) - 1.0}
    ]
    bounds = [(0, 1) for _ in range(n)]
    
    result = minimize(portfolio_variance, x0=np.ones(n)/n,
                      method='SLSQP', bounds=bounds, constraints=constraints)
    return result.x

# Example
mu = np.array([0.12, 0.08, 0.15])
Sigma = np.array([[0.04, 0.009, 0.012],
                  [0.009, 0.0225, 0.008],
                  [0.012, 0.008, 0.0625]])

w = markowitz_optimize(mu, Sigma, target_return=0.11)
print(f"Weights: {w}")
print(f"Portfolio return: {w @ mu:.4f}")
print(f"Portfolio risk: {np.sqrt(w @ Sigma @ w):.4f}")

Feature Selection with Lasso Path

import numpy as np
from sklearn.linear_model import lasso_path

def feature_selection_lasso(X, y, n_alphas=50):
    """Compute the full Lasso regularization path."""
    alphas, coefs, _ = lasso_path(X, y, n_alphas=n_alphas)
    
    # Identify feature entry order
    selected_order = []
    for i, alpha in enumerate(alphas[::-1]):
        nonzero = np.where(np.abs(coefs[:, i]) > 1e-8)[0]
        for f in nonzero:
            if f not in selected_order:
                selected_order.append(f)
    
    return alphas, coefs, selected_order

# Example
np.random.seed(42)
n, p = 100, 20
X = np.random.randn(n, p)
true_support = [0, 3, 7, 12]
beta_true = np.zeros(p)
beta_true[true_support] = [2.0, -1.5, 3.0, -0.8]
y = X @ beta_true + 0.5 * np.random.randn(n)

alphas, coefs, order = feature_selection_lasso(X, y)
print(f"Feature entry order: {order}")
print(f"True support: {true_support}")

Neural Architecture Search (Simplified DARTS)

import torch
import torch.nn as nn
import torch.nn.functional as F

class MixedOperation(nn.Module):
    """DARTS-style mixed operation over candidate edges."""
    def __init__(self, C_in, C_out, ops):
        super().__init__()
        self.ops = nn.ModuleList(ops)
        self.arch_weights = nn.Parameter(torch.randn(len(ops)))
    
    def forward(self, x):
        weights = F.softmax(self.arch_weights, dim=0)
        return sum(w * op(x) for w, op in zip(weights, self.ops))

# Candidate operations
ops = [
    nn.Sequential(nn.Conv2d(16, 16, 3, padding=1), nn.BatchNorm2d(16), nn.ReLU()),
    nn.Sequential(nn.Conv2d(16, 16, 5, padding=2), nn.BatchNorm2d(16), nn.ReLU()),
    nn.MaxPool2d(3, stride=1, padding=1),
    nn.Identity(),
]

mixed = MixedOperation(16, 16, ops)
x = torch.randn(1, 16, 32, 32)
out = mixed(x)
print(f"Output shape: {out.shape}")

# After bilevel optimization, extract best architecture
best_idx = mixed.arch_weights.argmax().item()
print(f"Best operation index: {best_idx}")

PPO Policy Optimization

import torch
import torch.nn as nn

class ActorCritic(nn.Module):
    def __init__(self, state_dim, action_dim):
        super().__init__()
        self.shared = nn.Sequential(
            nn.Linear(state_dim, 64), nn.ReLU(),
            nn.Linear(64, 64), nn.ReLU()
        )
        self.policy = nn.Linear(64, action_dim)
        self.value = nn.Linear(64, 1)
    
    def forward(self, state):
        features = self.shared(state)
        return F.softmax(self.policy(features), dim=-1), self.value(features)

def ppo_loss(old_log_probs, new_log_probs, advantages, eps=0.2):
    ratio = torch.exp(new_log_probs - old_log_probs)
    clipped = torch.clamp(ratio, 1 - eps, 1 + eps)
    return -torch.min(ratio * advantages, clipped * advantages).mean()

# Example usage
state_dim, action_dim = 4, 2
model = ActorCritic(state_dim, action_dim)
state = torch.randn(1, state_dim)
probs, value = model(state)
print(f"Action probs: {probs}, Value: {value.item():.4f}")

Common Mistakes

MistakeWhy It FailsFix
Using SGD without momentum on deep networksConverges slowly, gets stuck in saddle pointsAdd momentum () or use Adam
Setting learning rate too high in AdamLoss diverges, gradients explodeStart with , use LR finder
Forgetting to zero gradientsGradients accumulate across batches, wrong updatesCall optimizer.zero_grad() before each backward pass
Not scaling features for LassoFeatures with larger scale dominate the penaltyStandardize features to zero mean, unit variance
Using penalty instead of weight decay in AdamL2 and weight decay are NOT equivalent in AdamUse AdamW for proper weight decay
Ignoring covariance in portfolio optimizationUnderestimates risk, over-concentrates positionsAlways use full covariance matrix
Stopping NAS too earlyFinal architecture is suboptimal, no convergenceUse early stopping with patience, not fixed epochs
Not clipping gradients in RNNsExploding gradients cause NaN loss and divergenceAlways clip gradients for recurrent architectures
Using discrete search for NAS without warm-startProhibitively expensive (years of GPU time)Use DARTS or Bayesian optimization with warm-starting
Over-regularizing with high in LassoModel becomes too sparse, high biasCross-validate using the one-standard-error rule

Interview Questions

  1. Why is the Lasso able to perform feature selection while Ridge regression is not? The Lasso's penalty creates a diamond-shaped constraint region with corners on the axes. The solution (intersection of the loss ellipse with the constraint) often lands exactly on a corner, setting some coefficients to zero. Ridge's ball has no corners, so coefficients shrink but never reach exactly zero.

  2. Explain the efficient frontier in portfolio theory. What assumption breaks it? The efficient frontier is the set of portfolios achieving minimum variance for each target return. It breaks when returns are not normally distributed (fat tails, skewness) or when the covariance matrix is estimated with error (estimation error amplifies with the number of assets).

  3. Why is PPO preferred over vanilla policy gradient in practice? PPO constrains policy updates via clipping, preventing destructive large steps that can collapse performance. It uses off-policy data (collected by the old policy) efficiently, reduces variance with advantage estimation, and is more stable and sample-efficient than REINFORCE.

  4. When does DARTS fail? What is the "performance collapse" problem? DARTS tends to select skip connections (identity operations) disproportionately, especially in deeper cells, because skip connections have lower training loss early in search. This leads to degenerate architectures. Solutions: enforce a minimum number of non-skip operations, use early stopping before convergence.

  5. How would you scale AutoML to millions of hyperparameter configurations? Use Bayesian optimization (Gaussian processes or Tree-structured Parzen Estimators) with warm-starting from prior runs. Meta-learning from similar datasets reduces the search space. For extreme scale, use population-based training (PBT) which evolves a population of models in parallel.

  6. What is the difference between the 0-1 knapsack and the fractional knapsack? The 0-1 knapsack (items cannot be divided) is NP-hard, solved by dynamic programming in . The fractional knapsack (items can be divided) is solvable greedily in by taking the highest value-to-weight ratio items first.

  7. Why does gradient clipping use the norm rather than clipping each gradient component independently? Clipping by norm preserves the direction of the gradient (the relative magnitudes of components are maintained). Component-wise clipping distorts the gradient direction, which can harm convergence. Norm clipping rescales: .

  8. Explain the irrepresentability condition for Lasso feature selection. It requires that the correlation between relevant and irrelevant features is bounded: . If violated, the Lasso includes irrelevant features (false positives) because they are correlated with relevant features. Group Lasso or adaptive Lasso can partially address this.


Practice Problems


Quick Reference

TopicKey Formula / ConceptWhen to Use
Portfolio Optimization s.t. Asset allocation, risk management
Resource Allocation s.t. Knapsack, scheduling, budgeting
Network Design s.t. flow constraintsTelecommunications, logistics
SchedulingMinimize makespan with precedence constraintsManufacturing, cloud computing
Feature Selection (Lasso)High-dimensional regression, interpretability
NAS (DARTS)Bilevel optimization over architecture and weights Automated model design
Policy GradientRL policy optimization
PPOClipped surrogate: Stable RL training
AutoMLHyperparameter optimization + pipeline searchEnd-to-end ML automation

Cross-References


Key Takeaways

Need Expert Mathematics Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement