Skip to content

Instantly share code, notes, and snippets.

@panchishin
panchishin / VirtualResultSet.java
Created October 31, 2017 18:42
A wrapper for Java result set. The wrapper fetches all data and stores it but provides the same interface as a result set without having to keep the DB session open
package amp.util.db;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
@panchishin
panchishin / montecarlo.py
Last active March 13, 2018 17:33
example monte carlo solver for elemental chemistry
import numpy as np
import random
earth = np.array([40.,0.,0.,0.])
air = np.array([0.,40.,0.,0.])
fire = np.array([0.,0.,40.,0.])
water = np.array([0.,0.,0.,40.])
mud = ( earth*3 + water ) / 4
muck = ( earth + water*3 ) / 4
@panchishin
panchishin / share_data.py
Created April 20, 2018 21:05
Bare bones example of sharing a dictionary
"""
copied from https://docs.python.org/2.7/library/multiprocessing.html#sharing-state-between-processes
"""
from multiprocessing import Process, Manager
def f(d):
d[1] = '1'
d[0.25] = None
import numpy as np
from scipy.spatial.distance import cosine
def constant(x):
return x
# We compute the gramian matrix like so
def gram_matrix(x1,x2) :
assert( x1.shape == x2.shape )
return np.matmul(np.transpose(x1),x2) / ( x1.shape[0] * x1.shape[1] )
@panchishin
panchishin / aircraft.html
Created May 2, 2019 20:07
Aircraft dynamics calculator
<html>
<head><title>Basic Flight Calculator</title>
<body onload="javascript:calculateTopSpeed()">
<h1>FAA Flight Calculator</h1>
<p>Adapted from U.S. Department of Transportation Advisory Circular (AC No. 103-7)<br>
Adapted from Canadian C.A.R.S. #549 Subsection B</p>
<style>
a {
text-decoration: none;
font-weight: bold;
@panchishin
panchishin / Learn a line .py
Last active December 31, 2019 00:13
An example of machine learning from https://youtu.be/wUPFOFbCeFI
from random import random
from math import pow
ys_and_xs = [ (1, 3.8), (2, 1.9), (3, 2.9),
(4, 4.5), (5, 6.4), (6, 3.5),
(7, 6.7), (8, 6.2), (9, 8.0) ]
def grade(a, b):
error = 0
@panchishin
panchishin / Learn a line .js
Last active December 31, 2019 00:13
An example of machine learning from https://youtu.be/wUPFOFbCeFI
let ys_and_xs = [ [1, 3.8], [2, 1.9], [3, 2.9],
[4, 4.5], [5, 6.4], [6, 3.5],
[7, 6.7], [8, 6.2], [9, 8.0] ];
function grade(a, b) {
let error = 0;
@panchishin
panchishin / top_k_loss.py
Last active November 26, 2020 05:49
top k loss
def top_k_loss(k=25):
@tf.function
def loss(y_true, y_pred):
y_error_of_true = tf.keras.losses.categorical_crossentropy(y_true=y_true,y_pred=y_pred)
topk, indexs = tf.math.top_k( y_error_of_true, k=tf.minimum(k, y_true.shape[0]) )
return topk
return loss
@panchishin
panchishin / random_categorical_layer.py
Created November 23, 2020 05:23
Random Categorical Selection with Gradient capability
class RandomCategorical(L.Layer):
def __init__(self, num_classes=10, factor=0.999):
super(RandomCategorical, self).__init__()
self.num_classes = num_classes
self.factor = factor
@tf.function
def call(self, input):
sample = tf.random.categorical(logits=input, num_samples=1)
hot = tf.reshape( tf.one_hot( sample, depth=self.num_classes ), shape=[-1,self.num_classes] )
@panchishin
panchishin / dense_resnet.py
Created November 23, 2020 05:33
Dense ResNet Like Layer
class DenseResNet(tf.keras.layers.Layer):
def __init__(self, units=500, activation="tanh", kernel_initializer="glorot_uniform", layers=2):
super(DenseResNet, self).__init__()
self.layers = [tf.keras.layers.Dense(units, activation=activation, kernel_initializer=kernel_initializer) for _ in range(layers)]
def call(self, inputs):
result = inputs
for layer in self.layers:
result = layer(result)