Skip to content

Instantly share code, notes, and snippets.

View speedcell4's full-sized avatar

Yiran Wang speedcell4

  • NICT
  • Nara, Japan
  • 08:13 (UTC +09:00)
  • X @nlp_yiran
View GitHub Profile
@speedcell4
speedcell4 / iris_classifier.py
Last active December 7, 2016 05:15
Tensorflow practice
import numpy as np
import tensorflow as tf
from numpy.random import RandomState
from sklearn.model_selection import StratifiedShuffleSplit
from tensorflow.contrib.layers import fully_connected
tf.logging.set_verbosity(tf.logging.INFO)
random_state = RandomState()
@speedcell4
speedcell4 / distributed.py
Created December 7, 2016 05:25
tensorflow distributed framework
import tensorflow as tf
tf.app.flags.DEFINE_string('ps_hosts', '', 'Comma-separated list of hostname:port pairs')
tf.app.flags.DEFINE_string('worker_hosts', '', 'Comma-separated list of hostname:port pairs')
tf.app.flags.DEFINE_string('job_name', '', 'One of ps or worker')
tf.app.flags.DEFINE_integer('task_index', 0, 'Index of task within the job')
tf.app.flags.DEFINE_string('logdir', 'logdir', 'Directory to log')
FLAGS = tf.app.flags.FLAGS
@speedcell4
speedcell4 / common_citings.py
Created March 2, 2017 12:18
find out the common citings of given papers
from functools import reduce
from typing import List
from typing import Set
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import Chrome
URLs = [
r'https://scholar.google.com/scholar?cites=17690265189765140803&as_sdt=2005&sciodt=0,5&hl=ja',
r'https://scholar.google.com/scholar?cites=14180674818440394404&as_sdt=2005&sciodt=0,5&hl=ja',
@speedcell4
speedcell4 / A.cpp
Last active April 30, 2017 13:54
Google Code Jam 2017 - Round 1C
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <numeric>
#include <deque>
#include <map>
#include <queue>
import chainer.functions as F
import chainer.links as L
import numpy as np
from chainer import Chain
from chainer import cuda, training, Variable
from chainer import datasets, iterators, optimizers, initializers
from chainer.training import extensions
from sklearn.datasets import load_iris
@speedcell4
speedcell4 / dynet-helper.cc
Created July 21, 2017 04:51
DyNet helpers
#ifndef DYNET_HELPER_H
#define DYNET_HELPER_H
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include "dynet/dynet.h"
#include "dynet/expr.h"
@speedcell4
speedcell4 / masked.py
Last active August 8, 2017 01:35
masked_softmax and masked_softmax_cross_entropy
import chainer.functions as F
from chainer import Variable, cuda
def masked_softmax(x, mask, axis=1):
xp = cuda.get_array_module(x, mask)
with cuda.get_device_from_array(x, mask):
exp_x = F.exp(x)
zeros = xp.zeros_like(x.data)
sum_exp_x = F.sum(F.where(mask, exp_x, zeros), axis=axis, keepdims=True)
@speedcell4
speedcell4 / multi-task-classifier.py
Created August 11, 2017 05:09
multi-task classifier of chainer
from chainer import Chain, report
class MultiTaskClassifier(Chain):
def __init__(self, task1, task2, loss1, loss2, acc1, acc2, lambda_=1.0):
"""
:param task1: `Chain` of task 1
:param task2: `Chain` of task 2
:param loss1: loss function of task1
:param loss2: loss function of task2
import tensorflow as tf
class Summary(object):
def __init__(self, logdir: str):
self._placeholders = {}
self._observations = {}
self._session = tf.Session()
self._writer = tf.summary.FileWriter(logdir=logdir)
@speedcell4
speedcell4 / binary_sort.cpp
Last active November 8, 2017 07:01
return the index of value
#include <iostream>
using namespace std;
int binary_search(int *a, int value, int left, int right) {
if (left >= right) return -1;
else {
int mid = (left + right) / 2;
if (a[mid] == value) return mid;
else if (a[mid] > value) return binary_search(a, value, left, mid);