cnn+rnn+attention
来源:互联网 发布:网络支付有哪些 编辑:程序博客网 时间:2024/05/29 02:21
下面是单层rnn+attention的代码,若考虑多层rnn请参考博主的:tf.contrib.rnn.static_rnn与tf.nn.dynamic_rnn区别
def attention(inputs, attention_size, time_major=False): if isinstance(inputs, tuple): # In case of Bi-RNN, concatenate the forward and the backward RNN outputs. inputs = tf.concat(inputs, 2) if time_major: # (T,B,D) => (B,T,D) inputs = tf.transpose(inputs, [1, 0, 2]) inputs_shape = inputs.shape sequence_length = inputs_shape[1].value # the length of sequences processed in the antecedent RNN layer hidden_size = inputs_shape[2].value # hidden size of the RNN layer # Attention mechanism W_omega = tf.Variable(tf.random_normal([hidden_size, attention_size], stddev=0.1)) b_omega = tf.Variable(tf.random_normal([attention_size], stddev=0.1)) u_omega = tf.Variable(tf.random_normal([attention_size], stddev=0.1)) v = tf.tanh(tf.matmul(tf.reshape(inputs, [-1, hidden_size]), W_omega) + tf.reshape(b_omega, [1, -1])) vu = tf.matmul(v, tf.reshape(u_omega, [-1, 1])) exps = tf.reshape(tf.exp(vu), [-1, sequence_length]) alphas = exps / tf.reshape(tf.reduce_sum(exps, 1), [-1, 1]) # Output of Bi-RNN is reduced with attention vector output = tf.reduce_sum(inputs * tf.reshape(alphas, [-1, sequence_length, 1]), 1) return output# cnn的输出为B*4*8*128chunk_size = 128 chunk_n = 32 rnn_size = 256 attention_size = 50n_output_layer = MAX_CAPTCHA*CHAR_SET_LEN # 输出层 # 定义待训练的神经网络 def recurrent_neural_network(): data = crack_captcha_cnn() data = tf.reshape(data, [-1, chunk_n, chunk_size]) data = tf.transpose(data, [1,0,2]) data = tf.reshape(data, [-1, chunk_size]) data = tf.split(data,chunk_n) # 只用RNN #layer = {'w_':tf.Variable(tf.random_normal([rnn_size, n_output_layer])), 'b_':tf.Variable(tf.random_normal([n_output_layer]))} #lstm_cell = tf.contrib.rnn.BasicLSTMCell(rnn_size) #outputs, status = tf.contrib.rnn.static_rnn(lstm_cell, data, dtype=tf.float32) #ouput = tf.add(tf.matmul(outputs[-1], layer['w_']), layer['b_']) # RNN + Attention lstm_cell = tf.contrib.rnn.BasicLSTMCell(rnn_size) outputs, status = tf.contrib.rnn.static_rnn(lstm_cell, data, dtype=tf.float32) attention_output = attention(outputs, attention_size, True) # output drop = tf.nn.dropout(attention_output, keep_prob) # Fully connected layer W = tf.Variable(tf.truncated_normal([rnn_size, n_output_layer], stddev=0.1), name="W") b = tf.Variable(tf.constant(0., shape=[n_output_layer]), name="b") ouput = tf.nn.xw_plus_b(drop, W, b, name="scores") return ouput
阅读全文
0 0
- cnn+rnn+attention
- CNN+RNN
- 用深度学习(CNN RNN Attention)解决大规模文本分类问题
- 用深度学习(CNN RNN Attention)解决大规模文本分类问题
- 人工机器:NDC-谷歌机器翻译破世界纪录,仅用Attention模型,无需CNN和RNN
- 用深度学习(CNN RNN Attention)解决大规模文本分类问题
- 用深度学习(CNN RNN Attention)解决大规模文本分类问题
- 阿里AI工程师教你如何用CNN RNN Attention解决大规模文本分类问题
- 用深度学习(CNN RNN Attention)解决大规模文本分类问题
- 用深度学习(CNN RNN Attention)解决大规模文本分类问题
- 图解RNN、RNN变体、Seq2Seq、Attention机制
- rnn Attention网络的实现
- DNN、CNN、RNN简析
- CNN RNN 杂想
- CNN、RNN与LSTM
- 【笔记】CNN RNN要点
- CNN、RNN、DNN区别
- CNN和RNN区别
- day 19 Linux文件压缩与打包
- 俄罗斯方块
- Class类深入理解
- 这里有十个优质技术公众号,你都关注了吗?
- NLP入门+实战必读:一文教会你最常见的10种自然语言处理技术(附代码)
- cnn+rnn+attention
- 加速动态磁共振重建的低秩系数分解模型
- Boxes and Balls UVALive
- list
- opencv识别直线并显示文本
- 超励志!这位90后盲人程序员首创奥运历史
- 老玩家才懂的过往,Android系统消失不见的经典设计
- 在 Python 中实现单例模式
- 如何从一双腿判断男人行不行?