Skip to content

Instantly share code, notes, and snippets.

@saitejamalyala
saitejamalyala / app.py
Created April 11, 2023 10:20 — forked from init27/app.py
ArXiv Chat: Chat with the latest Arxiv papers
# Credit 🙏: I just used the example from langchain docs and it works quite well: https://python.langchain.com/en/latest/use_cases/question_answering.html
# Note 2: The Arxiv -> PDF logic is a bit messy, I'm sure it can be done better
# Note 3: Please install the following:
# To run:
# Save this in a `app.py`
# pip install arxiv PyPDF2 langchain chromadb
# The chat feature was shipped in H2O nightly this week, we will need to install from nightly link:
@saitejamalyala
saitejamalyala / sparsematrix.py
Last active October 27, 2021 03:26
Sparse Matrix class implementation without using numpy or scipy. sparse matrix represented using dictionary, tuple of matrix indices as keys and matrix elements themselves as values.
import math
from typing import Dict, Tuple, Optional, List
class SparseMatrix:
"""Sparse Matrix class
Args:
data: A dictionary of (i, j) -> value
"""
def __init__(self, data: Dict[Tuple[int, int], float] = None):
@saitejamalyala
saitejamalyala / CustomLoss_with_outside_param.py
Created August 11, 2021 09:40
This gist provides an example of how to use another parmeter/information to calculate loss in keras model while using model.fit
from tensorflow.keras import backend as K
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras import models
import tensorflow as tf
from tensorflow.keras.regularizers import l1,l1_l2,l2
from tensorflow.python.keras.regularizers import L1
from rosbag2numpy.config import params
def euclidean_distance_loss(y_true, y_pred):
#! /usr/bin/env python
# Copyright (c) 2019 Uber Technologies, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
@saitejamalyala
saitejamalyala / CustomLayer_serializable.py
Created June 28, 2021 17:43
You can provide manually the mapping custom_objects in the load_model method as mentioned in the answer https://stackoverflow.com/a/62326857/8056572 but it can be tedious when you have a lot of custom layers (or any custom callables defined. e.g. metrics, losses, optimizers, ...). Tensorflow provides a utils function to do it automatically: tf.k…
import tensorflow as tf
@tf.keras.utils.register_keras_serializable()
class CustomLayer(tf.keras.layers.Layer):
def __init__(self, k, **kwargs):
self.k = k
super(CustomLayer, self).__init__(**kwargs)
def get_config(self):
@saitejamalyala
saitejamalyala / CustomWandbCallback.py
Last active August 6, 2021 00:01
Custom wandb callback for Keras model training (.fit) to predict on sample data and save plot at the beginning of each epoch,.
import wandb
from wandb.keras import WandbCallback
import tensorflow as tf
class cd_wandb_custom(WandbCallback):
def __init__(
self,
# newly added
ds_test,
np_test_dataset:Dict[str,Union[ndarray,List]],
@saitejamalyala
saitejamalyala / CustomMaskLayer.py
Last active August 5, 2021 23:58
Custom keras layer to mask input tensor
from tensorflow.keras import layers
from tensorflow.keras import models
import tensorflow as tf
class CustomMaskLayer(layers.Layer):
"""Layer that masks tensor at specific locations as mentioned in binary tensor
Args:
layers (layers.Layer): keras.layers baseclass
"""
@saitejamalyala
saitejamalyala / CustomLayer.py
Last active June 27, 2021 12:49
Keras custom layer to multiply input by a scalar
import tensorflow as tf
class CustomLayer(tf.keras.layers.Layer):
def __init__(self, k, name=None, **kwargs):
super(CustomLayer, self).__init__(name=name)
self.k = k
super(CustomLayer, self).__init__(**kwargs)
def get_config(self):
config = super(CustomLayer, self).get_config()