Skip to content

Instantly share code, notes, and snippets.

View ShigekiKarita's full-sized avatar
🌴
I may be slow to respond.

Shigeki Karita ShigekiKarita

🌴
I may be slow to respond.
View GitHub Profile
@ShigekiKarita
ShigekiKarita / test_lm.py
Last active March 7, 2018 12:49
test_lm.py
import chainer
import torch
import lm_train
import lm_train_th
def transfer_lstm(ch_lstm, th_lstm):
th_lstm.weight_ih.data = torch.from_numpy(ch_lstm.upward.W.data)
th_lstm.bias_ih.data = torch.from_numpy(ch_lstm.upward.b.data)
th_lstm.weight_hh.data = torch.from_numpy(ch_lstm.lateral.W.data)
import numpy
import torch
import torch.nn.functional as F
from torch.autograd import Variable
tiny = float(numpy.finfo(numpy.float32).tiny)
def safe_cumprod(x, dim):
return x.clamp(tiny, 1.0).log().cumsum(dim=dim).exp();
@import url(http://fonts.googleapis.com/css?family=Droid+Sans|Droid+Sans+Mono|Droid+Serif);
@media all
{
html {
margin: 0;
font: .9em/1.6em "Droid Serif", Cambria, Georgia, "DejaVu Serif", serif;
background-attachment: fixed;
background-position: right bottom;
background-repeat: no-repeat;
#include <iostream>
#include <thrust/device_vector.h>
#include <cooperative_groups.h>
namespace cg = cooperative_groups;
// using DataType = float;
// using DataType4 = float4;
using DataType = int;
using DataType4 = int4;
(setq org-latex-classes '(("ltjsarticle"
"\\documentclass{ltjsarticle}
\\usepackage{url}
\\usepackage{amsmath}
\\usepackage{newtxtext,newtxmath}
\\usepackage{graphicx}
\\usepackage{luatexja}
\\usepackage{hyperref}
[NO-DEFAULT-PACKAGES]
[PACKAGES]
%matplotlib notebook
from matplotlib import pyplot
import torch
import math
import numpy
from torch.nn import Parameter, Module
def dirichlet_log_pdf(pi, alpha):
numel = torch.lgamma(alpha.sum(0)) + torch.sum(torch.log(pi) * (alpha - 1.0))
@ShigekiKarita
ShigekiKarita / bf.cpp
Created December 10, 2017 18:49
Brainfuck interpreter in C++11
#include <algorithm>
#include <functional>
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <vector>
// byte array
using Memory = std::vector<unsigned char>;
using Pointer = Memory::iterator;
(setq org-latex-classes '(("ltjsarticle"
"\\documentclass{ltjsarticle}
\\usepackage{url}
\\usepackage{amsmath}
\\usepackage{newtxtext,newtxmath}
\\usepackage{graphicx}
\\usepackage{luatexja}
\\usepackage{hyperref}
[NO-DEFAULT-PACKAGES]
[PACKAGES]
/++
This is a submodule of numir to provide formatting for ndslices.
Note: This relies on the formatting functionality from Phobos, the D standard
library. As of this writing, parts of it rely on the Garbage Collector and
potentially other non-Better C functionality in D.
+/
module numir.format;
import mir.ndslice : SliceKind, Slice;
import std.traits : isSomeChar, isSomeString;
@ShigekiKarita
ShigekiKarita / dijkstra.d
Created September 25, 2017 10:06
dlang snippet for graph algorithm
/* library code */
/++
Referrence:
- https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#Using_a_priority_queue
- http://shifth.hatenablog.com/entry/2015/05/31/104829
+/
auto dijkstra(alias distfun, E, V)(E[V[2]] tree, V start) {
import std.container : Array, PriorityQueue = BinaryHeap;