Skip to content

Instantly share code, notes, and snippets.

@yasufumy
yasufumy / data.py
Last active August 22, 2019 09:06
import os
import torch
import lineflow as lf
class Dictionary(object):
def __init__(self):
self.word2idx = {}
self.idx2word = []
def add_word(self, word):
this is the first line .
this is the second line .
this is the third line .
from collections import deque
INF = float('inf')
def bfs(graph, s):
d = {v: INF for v in graph}
d[s] = 0
pi = {s: None}
queue = deque([s])
while queue:
INF = float('inf')
def bellman_ford(graph, weights, s):
d = {v: INF for v in graph}
d[s] = 0
pi = {s: None}
edges = [(u, v) for u, vs in graph.items() for v in vs]
for _ in range(len(graph) - 1):
for (u, v) in edges:
d_temp = d[u] + weights[u, v]
INF = float('inf')
def dag_shortest_path(graph, weights, torder, s):
d = {v: INF for v in graph}
d[s] = 0
pi = {s: None}
for u in torder:
for v in graph[u]:
d_temp = d[u] + weights[u, v]
if d_temp < d[v]:
import heapq
INF = float('inf')
def dijkstra(graph, weights, s):
d = {v: INF for v in graph}
d[s] = 0
pi = {s: None}
queue = []
heapq.heappush(queue, (0, s))
$latex = 'platex';
$bibtex = 'pbibtex';
$dvipdf = 'dvipdfmx %O -o %D %S';
$makeindex = 'mendex %O -o %D %S';
$pdf_mode = 3;
$ENV{TZ} = 'Asia/Tokyo';
$ENV{OPENTYPEFONTS} = '/usr/share/fonts//:';
$ENV{TTFONTS} = '/usr/share/fonts//:';
ls -t * | xargs -I{} file {} | grep "PNG image data" | cut -d ":" -f 1 | parallel -j 8 convert "{}" -colorspace sRGB -type truecolor "target/{.}.jpg"
@yasufumy
yasufumy / normalization.py
Created April 27, 2017 12:34
the forward calculation of batch normalization and layer normalization
import numpy as np
def batch_normalization(x, gamma, beta, eps=2e-5):
mean = x.mean(axis=0)
var = x.var(axis=0)
x_hat = (x - mean) / np.sqrt(var + eps)
return gamma * x_hat + beta
def layer_normalization(x, gamma, beta, eps=2e-5):
mean = x.mean(axis=1)[:, np.newaxis]
def replace_chars(text):
chars = ",.?!:;"
for c in chars:
if c in text:
text = text.replace(c, '')
return text