This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#生成文本 | |
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
###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] |