Skip to content

Instantly share code, notes, and snippets.

View applenob's full-sized avatar
🏋️‍♂️
Focusing

Javen_陈俊文 applenob

🏋️‍♂️
Focusing
View GitHub Profile
@applenob
applenob / masked_softmax_tf.py
Last active April 24, 2018 23:49
Tensorflow Masked Softmax with mask after the exp operation.
import tensorflow as tf
def masked_softmax(logits, mask, axis):
"""softmax with mask after the exp operation"""
e_logits = tf.exp(logits)
masked_e = tf.multiply(e_logits, mask)
sum_masked_e = tf.reduce_sum(masked_e, axis, keep_dims=True)
# be careful when the sum is 0
ones = tf.ones_like(sum_masked_e)
sum_masked_e_safe = tf.where(tf.equal(sum_masked_e, 0), ones, sum_masked_e)
@applenob
applenob / top_queue.py
Created June 13, 2018 08:48
Keep top n number in a list with ascend order.
class TopQueue:
"""keep top n num in a list with ascend order."""
def __init__(self, n):
self.n = n
self.queue = []
def add(self, new_val):
bigger_index = -1
for i, one in enumerate(self.queue):
if one > new_val:
@applenob
applenob / keras_f1_metric.py
Last active November 9, 2018 03:15
F1 metric for keras.
"""Reference: https://stackoverflow.com/questions/43547402/how-to-calculate-f1-macro-in-keras"""
from keras import backend as K
def f1(y_true, y_pred):
def recall(y_true, y_pred):
"""Recall metric.
Only computes a batch-wise average of recall.
Computes the recall, a metric for multi-label classification of
@applenob
applenob / setup-zeromq.sh
Created December 18, 2018 09:23 — forked from katopz/setup-zeromq.sh
Setup zeromq in Ubuntu 16.04
#!/usr/bin/bash
# Download zeromq
# Ref http://zeromq.org/intro:get-the-software
wget https://github.com/zeromq/libzmq/releases/download/v4.2.2/zeromq-4.2.2.tar.gz
# Unpack tarball package
tar xvzf zeromq-4.2.2.tar.gz
# Install dependency
@applenob
applenob / tf_serving.sh
Created January 7, 2019 02:33 — forked from jarutis/tf_serving.sh
Install Tensorflow Serving on Centos 7 (CPU)
sudo su
# Java
yum -y install java-1.8.0-openjdk-devel
# Build Esentials (minimal)
yum -y install gcc gcc-c++ kernel-devel make automake autoconf swig git unzip libtool binutils
# Extra Packages for Enterprise Linux (EPEL) (for pip, zeromq3)
yum -y install epel-release
@applenob
applenob / freeze_ckpt_to_pb.py
Created January 7, 2019 08:00
freeze tensorflow checkpoint file to pb format.
"""
freeze tensorflow checkpoint file to pb format.
"""
import argparse
import os
import tensorflow as tf
import logging
from tensorflow.python.framework import graph_util
@applenob
applenob / freeze_model_to_pb.py
Last active January 7, 2019 08:25
Save a tensorflow model to a pb file.
# coding=utf-8
"""Save a tensorflow model to a pb file."""
# Build the model, then train it or load weights from somewhere else.
# ...
graph = tf.get_default_graph()
input_graph_def = graph.as_graph_def()
@applenob
applenob / load_from_pb.py
Last active April 26, 2023 18:37
Load tensorflow model from frozen pb file.
# coding=utf-8
import tensorflow as tf
def get_session():
"""load a new session"""
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
return tf.Session(config=config)
@applenob
applenob / simple_english_tokenizer.py
Created February 22, 2019 06:41
A simple english tokenizer.
# Refer to https://www.kaggle.com/jhoward/nb-svm-strong-linear-baseline
import re, string
re_tok = re.compile(f'([{string.punctuation}“”¨«»®´·º½¾¿¡§£₤‘’])')
def tokenize(s):
return re_tok.sub(r' \1 ', s).split()
@applenob
applenob / beamsearch.py
Created April 7, 2019 09:08 — forked from udibr/beamsearch.py
beam search for Keras RNN
# variation to https://github.com/ryankiros/skip-thoughts/blob/master/decoding/search.py
def keras_rnn_predict(samples, empty=empty, rnn_model=model, maxlen=maxlen):
"""for every sample, calculate probability for every possible label
you need to supply your RNN model and maxlen - the length of sequences it can handle
"""
data = sequence.pad_sequences(samples, maxlen=maxlen, value=empty)
return rnn_model.predict(data, verbose=0)
def beamsearch(predict=keras_rnn_predict,