Skip to content

Instantly share code, notes, and snippets.

View idleuncle's full-sized avatar

Jiangwen Su idleuncle

View GitHub Profile
def softmax(x):
e_x = np.exp(x - np.max(x, axis=1).reshape(-1, 1))
return e_x / e_x.sum(axis=1).reshape(-1, 1)
@idleuncle
idleuncle / keras_progbar.py
Created December 18, 2019 16:23
[Keras Progbar] #keras
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, time
import collections
import numpy as np
class Progbar(object):
"""Displays a progress bar.
@idleuncle
idleuncle / shuffle.py
Last active March 7, 2020 04:35
[Shuffle]
def shuffle(train_data: np.array, seed: int = 8864):
np.random.seed(seed)
n = train_data.shape[0]
indices = np.random.randint(0, n, n)
train_data = [train_data[i] for i in indices]
@idleuncle
idleuncle / cjk_beamer.tex
Created October 28, 2019 02:03
[Beamer] #latex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%  这是一份 beamer 文档. 本源文件仅供学习参考之用.                                         %%%
%%%  使用请注明出处. 本文作者拥有版权 (c)2017. 保留所有权利.                               %%%
%%%  若不能编译通过, 可能是您的 beamer 需要更新了.                                             %%%
%%%  更新的具体方法可参考: http://bbs.ctex.org/forums/index.php?showtopic=27695 %%%
%%%  温馨提示:文档基本对所都的宏包和命令都加了注释,一般情况下不要删除,会发生错误喔 %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[CJK,notheorems,compress,mathserif,table,11pt]{beamer} %更改全文字体大小,设置xxpt(如9pt,10pt,.....)%一定要定义documentclass[cjk]{beamer},别忘了“cjk”,否则编译不通过
%\useoutertheme[height=0.1\textwidth,width=0.15\textwidth,hideothersubsections]{sidebar}%加上此命令会出现上部和左侧边框
\usetheme{Madrid}%主题AnnArbor Antibes Bergen Berkeley Berlin Boadilla boxes CambridgeUS Copenhagen Darmstadt
@idleuncle
idleuncle / keras_swish.py
Last active October 27, 2019 14:12
[Activation Functions]
# https://github.com/ShahariarRabby/Mnist_cnn_Swish
from keras import backend as K
from keras.layers import Activationfrom
keras.utils.generic_utils import get_custom_objects
def swish(x):
return (K.sigmoid(x) * x)
get_custom_objects().update({'swish': swish})
#Now just add Swish as an activation
model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = ‘Same’,
@idleuncle
idleuncle / keras_rcnn.py
Created October 21, 2019 13:05
[Keras RCNN
# coding=utf-8
from keras import Input, Model
from keras import backend as K
from keras.layers import Embedding, Dense, SimpleRNN, Lambda, Concatenate, Conv1D, GlobalMaxPooling1D
class RCNN(object):
def __init__(self, maxlen, max_features, embedding_dims,
class_num=1,
@idleuncle
idleuncle / keras_rcnn_variant.py
Created October 21, 2019 13:03
[Keras RCNN Variant]
# coding=utf-8
from keras import Input, Model
from keras.layers import Embedding, Dense, Concatenate, Conv1D, Bidirectional, CuDNNLSTM, GlobalAveragePooling1D, GlobalMaxPooling1D
class RCNNVariant(object):
"""Variant of RCNN.
Base on structure of RCNN, we do some improvement:
1. Ignore the shift for left/right context.
@idleuncle
idleuncle / keras_fasttext
Created October 21, 2019 12:59
[Keras FastText]
# coding=utf-8
from keras import Input, Model
from keras.layers import Embedding, GlobalAveragePooling1D, Dense
class FastText(object):
def __init__(self, maxlen, max_features, embedding_dims,
class_num=1,
last_activation='sigmoid'):
@idleuncle
idleuncle / keras_han.py
Created October 21, 2019 12:57
[Keras HAN]
# coding=utf-8
from keras import Input, Model
from keras.layers import Embedding, Dense, Bidirectional, CuDNNLSTM, TimeDistributed
from attention import Attention
class HAN(object):
def __init__(self, maxlen_sentence, maxlen_word, max_features, embedding_dims,
@idleuncle
idleuncle / keras_bilstm_attention.py
Created October 21, 2019 12:54
[Keras BiLSTM Attention]
# coding=utf-8
from keras import Input, Model
from keras.layers import Embedding, Dense, Dropout, Bidirectional, CuDNNLSTM
from attention import Attention
class TextAttBiRNN(object):
def __init__(self, maxlen, max_features, embedding_dims,