Skip to content

Instantly share code, notes, and snippets.

@artoby
artoby / get_hash.py
Last active July 25, 2024 09:30
Hashing of an arbitrary Python object so that it can be used for cache identification or other hash-related use cases. Produces either bytes or a string
from typing import Any, Optional
import hashlib
import base64
import json
def get_hash_bytes(value: Any, hash_bytes=10, salt=None) -> Optional[bytes]:
"""
param hash_bytes: how many bytes to hake for the trimmed hash, 10 bytes of data -> 16 symbols string
"""
@artoby
artoby / auto-stop-instance-gpu.sh
Last active July 15, 2020 21:29
Stop Cloud instance in case there's no GPU activity
#!/bin/bash
# This script is used to stop Cloud instance in case there's no GPU activity for certain period of time
# Feel free to contribute https://gist.github.com/artoby/468a7ccbbbc35f6cc904ed668a189a45
idle_seconds_to_shutdown=3600
threshold=0
check_period=0.1
wait_before_shutdown=10
last_activity=$(date +%s)
@artoby
artoby / symlog_bins.py
Last active May 30, 2022 05:08
Symlog bins - splits a data range into log-like bins but with 0 and negative values taken into account
import numpy as np
def symlog_bins(arr, n_bins, zero_eps=0.1, padding=0):
"""
Splits a data range into log-like bins but with 0 and negative values taken into account.
Can be used together with matplotlib 'symlog' axis sacale (i.e. ax.set_xscale('symlog'))
Feel free to contribute: https://gist.github.com/artoby/0bcf790cfebed5805fbbb6a9853fe5d5
"""
a = min(arr) / (1 + padding)
b = max(arr) * (1 + padding)
@artoby
artoby / bert_pytorch_to_tensorflow.py
Last active July 12, 2020 11:10
Converts a PyTorch transformers BertForSequenceClassification model to TensorFlow
"""
This code is based on implementation from transformers library https://github.com/huggingface/transformers/blob/5daca95dddf940139d749b1ca42c59ebc5191979/src/transformers/convert_bert_pytorch_checkpoint_to_original_tf.py
But this particular code snippet implements conversion of BertForSequenceClassification model which isn't supported by transformers library as of now.
A sample tf_bert_config_file json file can be found in this gist https://gist.github.com/artoby/b13d2b9d2d6d7f21e195bdb8542709c6
"""
import os
import tensorflow as tf
from transformers import (