Skip to content

Instantly share code, notes, and snippets.

View vgoklani's full-sized avatar

Vishal Goklani vgoklani

View GitHub Profile
@vgoklani
vgoklani / torch_ddp_verify.py
Created April 17, 2024 22:21 — forked from jxmorris12/torch_ddp_verify.py
verify parameter weights & gradients in pytorch
def verify_ddp_weights_equal(model: torch.nn.Module, atol: float = 1e-5) -> None:
if hasattr(model, "module"):
model = model.module
world_size = get_world_size()
for name, param in model.named_parameters():
gathered_param = gather(param).reshape((world_size, -1))
absolute_diffs = (gathered_param[None, 0, :] - gathered_param).abs()
rank_params_eq = (absolute_diffs < atol).all()
assert rank_params_eq, f"❌ param [{name}] not equal - got max_absolute_diff={absolute_diffs.max()}"
from typing import Optional
import torch
import torch.nn as nn
@torch.no_grad()
def measure_time_device(
model: nn.Module,
dtype: Optional[torch.dtype] = torch.float32,
num_repeats: Optional[int] = 100,
cimport cython
import numpy as np
cimport numpy as np
from sklearn.metrics import f1_score
@cython.boundscheck(False)
@cython.wraparound(False)
def f1_opt(np.ndarray[long, ndim=1] label, np.ndarray[double, ndim=1] preds):
@vgoklani
vgoklani / custom_layers.md
Created July 13, 2019 21:13
Blog - Custom layers in Keras

Building custom layers in Keras

About Keras

Keras is currently one of the most commonly used deep learning libraries today. And part of the reason why it's so popular is its API. Keras was built as a high-level API for other deep learning libraries ie Keras as such does not perform low-level tensor operations, instead provides an interface to its backend which are built for such operations. This allows Keras to abstract a lot of the underlying details and allows the programmer to concentrate on the architecture of the model. Currently Keras supports Tensorflow, Theano and CNTK as its backends.

Let's see what I mean. Tensorflow is one of the backends used by Keras. Here's the code for MNIST classification in TensorFlow and Keras. Both models are nearly identical and applies to the same problem. But if you compare the codes you g

@vgoklani
vgoklani / crypto_news.json
Created April 5, 2019 13:32 — forked from stungeye/crypto_news.json
News Site RSS Feeds
[
{
"url": "http://money.cnn.com",
"rss": "http://rss.cnn.com/rss/money_topstories.rss"
},
{
"url": "http://thehill.com",
"rss": "http://thehill.com/rss/syndicator/19110"
},
{
@vgoklani
vgoklani / stats.py
Created February 1, 2019 23:45 — forked from impshum/stats.py
Get mongodb stats using python with pymongo
from pymongo import MongoClient
try:
client = MongoClient('localhost')
db = client.searchfollow
except:
print("Could not connect to MongoDB")
call = db.command("dbstats")
@vgoklani
vgoklani / Pytorch Wavenet.ipynb
Created November 11, 2018 16:26 — forked from lirnli/Pytorch Wavenet.ipynb
Pytorch Wavenet
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@vgoklani
vgoklani / Pytorch RNN.ipynb
Created November 7, 2018 16:38 — forked from lirnli/Pytorch RNN.ipynb
Pytorch RNN examples
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@vgoklani
vgoklani / Simple Dilation Network.ipynb
Created November 7, 2018 16:00 — forked from lirnli/Simple Dilation Network.ipynb
Simple Dilation Network to generate sine waves
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@vgoklani
vgoklani / Zoom.ipynb
Created November 7, 2018 15:53 — forked from lirnli/Zoom.ipynb
Attention layer in neural networks
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.