Skip to content

Instantly share code, notes, and snippets.

@yukoba
yukoba / MD5Utils.java
Created November 4, 2012 12:12
MD5 Java
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Utils {
public static String convert(String s) {
try {
// Create MD5 Hash
MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(s.getBytes());
byte messageDigest[] = digest.digest();
@yukoba
yukoba / SVGinCanvas.html
Created November 5, 2012 13:10
SVG in Canvas
<!DOCTYPE html>
<html>
<body>
<p><canvas id="canvas" style="border:2px solid black;" width="200" height="200"></canvas>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var data = "<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200'>" +
"<foreignObject width='100%' height='100%'>" +
"<div xmlns='http://www.w3.org/1999/xhtml' style='font-size:40px'>" +
#include <queue>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void benchmark(int len)
{
priority_queue<int> q;
@yukoba
yukoba / Benchmark.groovy
Last active August 29, 2015 14:16
Boxing benchmark
import groovy.transform.CompileStatic
import org.junit.Test
@CompileStatic
class Benchmark {
@Test
void test1() {
int[] ary = new int[1024 * 1024]
for (int i = 0; i < ary.length; i++) {
ary[i] = i
@yukoba
yukoba / test_two_queues.py
Last active November 5, 2015 10:41
Facebookに書いた複数のキューから優先度の合計の最小を取ってくるやつ(この実装はpq1への動的追加で失敗します)
from heapq import heapify, heappop, heappush
from itertools import islice
from sys import maxsize
# サンプルデータ
pq0 = [(1, "a"), (2, "b"), (3, "f")]
pq1 = [(1, "c"), (2, "d"), (3, "g")]
heapify(pq0)
heapify(pq1)
@yukoba
yukoba / test_two_queues.py
Last active November 6, 2015 06:57
Facebookに書いた複数のキューから優先度の合計の最小を取ってくるやつ Ver.2
from bisect import bisect_left
from heapq import heapify, heappop, heappush
from itertools import islice
from sys import maxsize
# サンプルデータ
pq0 = [(1, "a"), (2, "b"), (3, "f")]
pq1 = [(1, "c"), (2, "d"), (3, "g")]
heapify(pq0)
heapify(pq1)
@yukoba
yukoba / autograd_benchmark.py
Created June 19, 2016 01:44
AutoGrad benchmark of matmul
import timeit
import autograd.numpy as np
from autograd import grad
def fn(a, b):
return np.sum(a @ b)
n = 1000
@yukoba
yukoba / theano_scan_steepest_descent_method.py
Last active September 9, 2016 04:39
Steepest descent method using theano.scan()
import theano
import theano.tensor as T
# Steepest descent method using theano.scan()
def fn(x, learning_rate):
y = x ** 2 - x
return x - learning_rate * T.grad(y, x)
init_x = T.dscalar()
@yukoba
yukoba / theano_scan_adagrad.py
Last active September 9, 2016 04:33
AdaGrad using theano.scan()
import theano
import theano.tensor as T
# AdaGrad using theano.scan()
def fn(x, r, learning_rate):
y = x ** 2 - x
g = T.grad(y, x)
r += g ** 2
return x - learning_rate / T.sqrt(r) * g, r
@yukoba
yukoba / theano_scan_adagrad_stochastic_gradient_descent.py
Last active September 10, 2016 06:12
AdaGrad + stochastic gradient descent using theano.scan()
import numpy as np
import theano
import theano.tensor as T
from theano.tensor.shared_randomstreams import RandomStreams
# AdaGrad + stochastic gradient descent using theano.scan()
train_x = np.random.rand(100)
train_y = train_x + np.random.rand(100) * 0.01