Skip to content

Instantly share code, notes, and snippets.

@Anuj040
Anuj040 / seesaw.py
Last active February 15, 2022 05:50
Tensorflow implementation of seesaw loss. Can be used for classification tasks with long tailed distribution.
"""Custom losses"""
import tensorflow as tf
# pylint: disable = attribute-defined-outside-init, no-name-in-module, unexpected-keyword-arg
# pylint: disable = no-value-for-parameter
from tensorflow.python.ops import state_ops as tf_state_ops
class SeeSawWeightCalculator(tf.keras.layers.Layer):
@Anuj040
Anuj040 / time2vec.py
Last active February 21, 2022 09:44
keras implementation of time2vec encoding
"""module for implementing time to vector encoding"""
import tensorflow as tf
import tensorflow.keras.layers as KL
class Time2Vec(KL.Layer):
"""time2vector encoding layer"""
def __init__(self, kernel: int = 64, activation: str = "sin") -> None:
import os
from typing import Tuple
import tensorflow as tf
import tensorflow.keras.backend as K
import tensorflow.keras.layers as KL
import tensorflow.keras.models as KM
import tensorflow_datasets as tfds
from tensorflow.python.keras.callbacks import ModelCheckpoint
@Anuj040
Anuj040 / naive_importance_sampling.py
Last active June 16, 2021 03:32
keras implementation for selective training on most lossy samples
"""Module implementing custom model.fit for model training"""
import tensorflow as tf
import tensorflow.keras.models as KM
import tensorflow_probability as tfp
class Trainer(KM.Model): # pylint: disable=too-many-ancestors
"""Custom function for model training using model.fit()
With functionality for selective back propagation for accelerated training.
@Anuj040
Anuj040 / cbam.py
Last active May 8, 2021 05:16
Convolutional Block Attention Module
import tensorflow as tf
import tensorflow.keras.layers as KL
import tensorflow.keras.models as KM
def channel_attention(features: int, reduction: int = 16, name: str = "") -> KM.Model:
"""channel attention model
Args:
features (int): number of features for incoming tensor
reduction (int, optional): Reduction ratio for the MLP to squeeze information across channels. Defaults to 16.