Skip to content

Instantly share code, notes, and snippets.

@Lanme
Lanme / rnn.py
Created November 7, 2018 06:22
basic_knowledge
#生成文本
class RNNNumpy:
def __init__(self, word_dim=128, hidden_dim=100):
self.word_dim = word_dim
self.hidden_dim = hidden_dim
self.U = np.random.uniform(-np.sqrt(1./word_dim), np.sqrt(1./word_dim), (hidden_dim, word_dim))
self.V = np.random.uniform(-np.sqrt(1./hidden_dim), np.sqrt(1./hidden_dim), (word_dim, hidden_dim))
self.W = np.random.uniform(-np.sqrt(1./hidden_dim), np.sqrt(1./hidden_dim), (hidden_dim, hidden_dim))
@Lanme
Lanme / attn.py
Last active October 26, 2018 07:08
quick_code_to_keras
#attn和capsule来自 https://github.com/plantsgo
from keras import backend as K
from keras.layers import Layer
from keras import initializers, regularizers, constraints
def dot_product(x, kernel):
"""
Wrapper for dot product operation, in order to be compatible with both
Theano and Tensorflow
Args:
@Lanme
Lanme / keras_bilstm.py
Last active November 6, 2018 10:13
some_simple_algorithm
#forked from https://www.kaggle.com/CVxTz/keras-bidirectional-lstm-baseline-lb-0-069
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load in
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory
@Lanme
Lanme / attn.py
Last active November 1, 2018 06:46
quick_code_to_tensorflow
###tf.contrib.seq2seq.AttentionWrapper封装好了attn层
def task_specific_attention(self, inputs, output_size,
initializer=layers.xavier_initializer(),
activation_fn=tf.tanh, scope=None):
"""
multiply为加性模型
Performs task-specific attention reduction, using learned
attention context vector (constant within task of interest).
Args:
inputs: Tensor of shape [batch_size, units, input_size]