资源简介
切片循环神经网络(Sliced recurrent neural networks,SRNN),在不改变循环单元的情况下,比RNN结构快135倍。
代码片段和文件信息
‘‘‘
Author: Zeping Yu
Sliced Recurrent Neural Network (SRNN).
SRNN is able to get much faster speed than standard RNN by slicing the sequences into many subsequences.
This work is accepted by COLING 2018.
The code is written in keras using tensorflow backend. We implement the SRNN(82) here and Yelp 2013 dataset is used.
If you have any question please contact me at zepingyu@foxmail.com.
‘‘‘
import pandas as pd
import numpy as np
from keras.utils.np_utils import to_categorical
from keras.preprocessing.text import Tokenizer text_to_word_sequence
from keras.preprocessing.sequence import pad_sequences
from keras.models import Model
from keras.layers import Input embedding GRU TimeDistributed Dense
#load data
df = pd.read_csv(“yelp_2013.csv“)
#df = df.sample(5000)
Y = df.stars.values-1
Y = to_categorical(Ynum_classes=5)
X = df.text.values
#set hyper parameters
MAX_NUM_WORDS = 30000
embedDING_DIM = 200
VALIDATION_SPLIT = 0.1
TEST_SPLIT=0.1
NUM_FILTERS = 50
MAX_LEN = 512
Batch_size = 100
EPOCHS = 10
#shuffle the data
indices = np.arange(X.shape[0])
np.random.seed(2018)
np.random.shuffle(indices)
X=X[indices]
Y=Y[indices]
#training set validation set and testing set
nb_validation_samples_val = int((VALIDATION_SPLIT + TEST_SPLIT) * X.shape[0])
nb_validation_samples_test = int(TEST_SPLIT * X.shape[0])
x_train = X[:-nb_validation_samples_val]
y_train = Y[:-nb_validation_samples_val]
x_val = X[-nb_validation_samples_val:-nb_validation_samples_test]
y_val = Y[-nb_validation_samples_val:-nb_validation_samples_test]
x_test = X[-nb_validation_samples_test:]
y_test = Y[-nb_validation_samples_test:]
#use tokenizer to build vocab
tokenizer1 = Tokenizer(num_words=MAX_NUM_WORDS)
tokenizer1.fit_on_texts(df.text)
vocab = tokenizer1.word_index
x_train_word_ids = tokenizer1.texts_to_sequences(x_train)
x_test_word_ids = tokenizer1.texts_to_sequences(x_test)
x_val_word_ids = tokenizer1.texts_to_sequences(x_val)
#pad sequences into the same length
x_train_padded_seqs = pad_sequences(x_train_word_ids maxlen=MAX_LEN)
x_test_padded_seqs = pad_sequences(x_test_word_ids maxlen=MAX_LEN)
x_val_padded_seqs = pad_sequences(x_val_word_ids maxlen=MAX_LEN)
#slice sequences into many subsequences
x_test_padded_seqs_split=[]
for i in range(x_test_padded_seqs.shape[0]):
split1=np.split(x_test_padded_seqs[i]8)
a=[]
for j in range(8):
s=np.split(split1[j]8)
a.append(s)
x_test_padded_seqs_split.append(a)
x_val_padded_seqs_split=[]
for i in range(x_val_padded_seqs.shape[0]):
split1=np.split(x_val_padded_seqs[i]8)
a=[]
for j in range(8):
s=np.split(split1[j]8)
a.append(s)
x_val_padded_seqs_split.append(a)
x_train_padded_seqs_split=[]
for i in range(x_train_padded_seqs.shape[0]):
split1=np.split(x_train_padded_seqs[i]8)
a=[]
for j in range(8):
s=np.split(split1[j]8)
相关资源
- vae,autoencoderpython实现
- ArcGIS Python常用脚本.docx
- Python找不到cl.exe等
- 自动扫雷系统+Python
- 基于标签的用户协同算法python
- 12306抢票Python代码,内含视频教程
- 个人博客网站源码python3.6+django2.0+my
- python网盘.txt
- Python Flask开发自己敲的试验楼小Demo
- python内置K-means聚类算法对鸢尾花数据
- KCFpython算法
- 指定步数节点内容的PROCAST仿真结果导
- python自然语言处理中文停用词
- 最好中国大学近几年排名及python爬虫
- Tensorflow-BiLSTM分类
- 感知机算法Python实现
- python 实现将TXT文件内容逐行存到EXC
- python 打开并计算两幅dicom图像感兴趣
- python 决策树代码
- 银行ATM系统(Python实现)
- pygame实现的贪吃蛇游戏RetroSnaker.py
- Python文件
- QT文件转换成Python的自动化工具*.ui转
- fcntl模块 win
- python爬虫爬取企业详细信息
- Kruskal算法python实现
- 蚁群算法的python代码
- 最小二乘法python代码,不用库函数
- sm3 python encode
- openopc for python 3.x
评论
共有 条评论