Skip to content

Instantly share code, notes, and snippets.

View sharkdeng's full-sized avatar
💭
single

Shark Deng sharkdeng

💭
single
View GitHub Profile
<html>
<script type="text/javascript">
function d2xy(n, d) {
var rx, ry, s, t, x, y;
t = d;
x = y = 0;
for (s = 1; s < n; s *= 2) {
rx = 1 & (t / 2);
@CMCDragonkai
CMCDragonkai / get_abs_path_for_immediate_children.py
Last active October 7, 2019 11:03
Python: Rose Tree - every node has a name, and dictionary of children
def get_abs_paths_for_immediate_children (tree, input_paths_string):
input_paths = input_paths_string.strip().split(".")
tmp_tree = tree
for input_path in input_paths:
tmp_tree = tmp_tree.n(input_path)
output_paths = []
children = list(tmp_tree.children.keys())
@btfak
btfak / useHexo.md
Created May 26, 2016 09:41
How to use Hexo and deploy to GitHub Pages
@takluyver
takluyver / make_nb.py
Created July 21, 2016 13:09
Make a notebook from a script
"""Create a notebook containing code from a script.
Run as: python make_nb.py my_script.py
"""
import sys
import nbformat
from nbformat.v4 import new_notebook, new_code_cell
nb = new_notebook()
@longsangstan
longsangstan / react-text-file-reader.js
Created April 27, 2017 03:30
A React component to read text file.
import React from "react";
/*
Read a text file and out put the content.
Example Usage:
var myTxt = require("./myTxt.txt");
...
@thomwolf
thomwolf / gradient_accumulation.py
Last active January 16, 2024 02:38
PyTorch gradient accumulation training loop
model.zero_grad() # Reset gradients tensors
for i, (inputs, labels) in enumerate(training_set):
predictions = model(inputs) # Forward pass
loss = loss_function(predictions, labels) # Compute loss function
loss = loss / accumulation_steps # Normalize our loss (if averaged)
loss.backward() # Backward pass
if (i+1) % accumulation_steps == 0: # Wait for several backward steps
optimizer.step() # Now we can do an optimizer step
model.zero_grad() # Reset gradients tensors
if (i+1) % evaluation_steps == 0: # Evaluate the model when we...
@sharkdeng
sharkdeng / wodpress2txt.py
Last active October 2, 2020 08:22
turn wordpress xml to txt
## this snippet allows you to fastly convert post from wordpress xml format to txt
from xml.dom.minidom import parse, parseString
import os
dom = parse('jobyme88.xml') # parse an XML file by name
os.path.makedirs('blogs')
items = dom.getElementsByTagName('item')
@sharkdeng
sharkdeng / pdf2jpg.py
Created October 2, 2020 08:24
turn pdf pages to jpg (not png, since jpg is smaller than png, website friendly)
# extract pdf to jpg
from pdf2image import convert_from_path
from tqdm import tqdm
import os
import uuid
input_path = 'corridor.pdf'
uid = uuid.uuid4().hex[:4]
output_path = uid + '-' + input_path.split('.')[0]
@sharkdeng
sharkdeng / is_nan.py
Last active October 9, 2020 11:16
judge if the given value is nan
def is_nan(a):
import math
import numpy as np
if isinstance(a, float):
return math.isnan(a)
elif isinstance(a, np.float64):
return np.isnan(a)
else:
return False
@sharkdeng
sharkdeng / code.tex
Created October 12, 2020 19:13
latex code style
%Source: https://www.overleaf.com/learn/latex/code_listing%
\usepackage{listings}
\usepackage{xcolor}
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}