Skip to content

Instantly share code, notes, and snippets.

@EthanRosenthal
Last active October 24, 2017 03:26
Show Gist options
  • Save EthanRosenthal/ae1a6da04639292db28f345bec023b8a to your computer and use it in GitHub Desktop.
Save EthanRosenthal/ae1a6da04639292db28f345bec023b8a to your computer and use it in GitHub Desktop.
Speed test for explicit logistic losses
from time import time
import pandas as pd
import torch
from torch import nn
from torch.nn import functional as F
from torch.autograd import Variable
def hand(pred, obs):
obs = (obs + 1) / 2
return F.binary_cross_entropy_with_logits(pred,
obs,
size_average=True)
def relu(pred, obs):
obs = F.relu(obs)
return F.binary_cross_entropy_with_logits(pred,
obs,
size_average=True)
def clamp(pred, obs):
obs = torch.clamp(obs, 0, 1)
return F.binary_cross_entropy_with_logits(pred,
obs,
size_average=True)
def setup():
pred = torch.rand(10000, 1)
obs = (torch.rand(10000, 1) > 0.5).float()
layer = nn.Linear(1, 1)
layer.weight.data.normal_(0, 1)
pred = layer(Variable(pred))
obs = Variable(obs)
return pred, obs
if __name__ == '__main__':
n_iters = 1000
times = []
for func, name in zip((hand, relu, clamp),
('hand', 'relu', 'clamp')):
func_times = 0
torch.manual_seed(0)
losses = 0
for _ in range(n_iters):
pred, obs = setup()
t0 = time()
loss = func(pred, obs)
loss.backward()
losses += loss.data[0]
t1 = time()
func_times += t1 - t0
times.append((name,
func_times / n_iters * 1000,
losses / n_iters))
df = pd.DataFrame(times, columns=['function', 'time (ms)', 'loss'])
df = df.sort_values('time (ms)', ascending=False)
print(df)
"""
function time (ms) loss
1 relu 1.802599 0.771223
2 clamp 1.799747 0.771223
0 hand 1.761629 0.764366
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment