利⽤BiLSTM⽹络实现⽂本分类
这⾥数据的Preprocess过程不贴代码了,训练过程(train)也不贴了,只是记录⼀下BiLSTM⽹络实现代码:
这⾥隐层数为2,词向量为100维。
import tensorflow as tf
ib import rnn
class Model(object):
def __init__(lf, num_layers, q_length, embedding_size, vocab_size, rnn_size, label_size): # 输⼊数据以及数据标签
lf.input_x = tf.placeholder(tf.int32, [None, q_length], name="input_x1")
lf.input_y = tf.placeholder(tf.float32, [None, label_size], name="input_y")
今生注定我爱你
lf.dropout_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob")
lf.l2_loss = tf.constant(0.0)
with tf.name_scope('embeddingLayer'):
# w : 词表(embedding 向量),后⾯⽤来训练.
w = tf.Variable(tf.random_uniform([vocab_size, embedding_size], -1.0, 1.0), name='W')
embedded = bedding_lookup(w, lf.input_x)
# 根据第⼆维展开,维度从0开始
# 删除所有⼤⼩为1的维度,删除[1]为要删除维度的参数
inputs = tf.split(embedded, q_length, 1)商务英语考试
inputs = [tf.squeeze(input_, [1]) for input_ in inputs]
with tf.name_scope("fw"):诊断证明书模板
stacked_rnn_fw = []
for _ in range(num_layers):
fw_cell = _cell.BasicLSTMCell(rnn_size, forget_bias=1.0, state_is_tuple=True)
stacked_rnn_fw.append(fw_cell)
花瓣怎么画lstm_fw_cell_m = _cell.MultiRNNCell(cells=stacked_rnn_fw, state_is_tuple=True) with tf.name_scope("bw"):
stacked_rnn_bw = []
for _ in range(num_layers):
bw_cell = _cell.BasicLSTMCell(rnn_size, forget_bias=1.0, state_is_tuple=True)
stacked_rnn_bw.append(bw_cell)
lstm_bw_cell_m = _cell.MultiRNNCell(cells=stacked_rnn_bw, state_is_tuple=True) with tf.name_scope("output"):
outputs, _, _ = rnn.static_bidirectional_rnn(lstm_fw_cell_m, lstm_bw_cell_m, inputs, dtype=tf.float32)
with tf.name_scope("result"):
w = tf.Variable(tf.random_uniform([2 * rnn_size, label_size], -1.0, 1.0), name='W')
b = tf.get_variable('b', [label_size])
lf.output = tf.nn.xw_plus_b(outputs[-1], w, b)
lf.w_plus_b(outputs1, w, b)
lf.softmax(lf.output, dim1)
with tf.name_scope("loss")
我最向往的地方
lf.softmax_cross_entropy_with_logits(logits lf.logits, labels lf.input_y) lf.duce_mean(lf.loss)
with tf.name_scope("accuracy")
lf.accuracys tf.equal(tf.argmax(lf.logits, axis1), tf.argmax(lf.input_y, axis1), name"equal")
lf.duce_mean(tf.cast(lf.accuracys, "float"), name"accuracy")
# with tf.name_scope("ans"):
# lf.ans = tf.s_like(lf.distance), lf.distance, name="temp_sim")
# print(lf.ans)
# blstm = Model(num_layers=3,育儿问答>症状性高血糖
# q_length=30,
# embedding_size=399,
# vocab_size=120,
什么地打开
# rnn_size=20,
# label_size=6)