tensorflow cnn+rnn基本结构
#CNN
x = tf.placeholder(tf.float32,[None,input_node],name="x_input")
y_ = tf.placeholder(tf.float32,[None,output_node],name="y_output") #input-->layer1
w_1 = tf.Variable(tf.truncted_normal([input_node,L1_node],stdev=0.5))
b_1 = tf.Variable(tf.constant(0.1,shape=[L1_node]))
l_conv1 = tf.nn.relu(tf.matmul(x,w_1)+b_1,strides=[1,2,2,1])
l_pool1 = tf.nn.max_pool(l_conv1,strides=[1,2,2,1],ksize = [1,2,2,1],padding='SAME') #layer1-->layder2
w_2 = tf.Variable(tf.truncted_normal([L1_node,L2_node],stddev=0.5))
b_2 = tf.Variable(tf.constant(0.1,shape=[L2_node]))
l_conv2 = tf.nn.relu(tf.matmul(l_pool1,w_2)+b_2)
l_pool2 = tf.nn.max_pool(l_conv2,strides=[1,2,2,1],ksize = [1,2,2,1],padding='SAME') #layser2-->fc
w_3 = tf.Variable(tf.truncted_normal([L2_node,fc_node],stddev=0.5))
b_3 = tf.Variable(tf.constant(0.1,shape=[fc_node]))
l_3 = tf.reshape(l_pool2,[-1,])
fc_1 = tf.nn.relu(tf.matmul(l_3,w_3)+b_3) #fc-->dropout
drop = tf.nn.dropout(fc_1,keep_prob) #dropout-->softmax
w_4 = tf.Variable(tf.truncted_normal([fc_node,output_node],stddev=0.5))
b_4 = tf.Variable(tf.constant(0.1,shape=[output_node]))
y = tf.nn.softmax(tf.matmul(drop,w_4)+b_4) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=tf.argmax(y,1),labels=tf.argmax(y_,1))
cross_entropy_mean = tf.reduce_mean(cross_entropy)
loss = cross_entropy+reularation train_step = tf.train.GradientDescentOptimizer(leraning_rate).minimize(loss)#以何种方式何种学习率去优化何种目标 correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_predictiontf.float32)) with tf.session() as sess:
tf.global_variable_initializer().run() for i in range(max_steps):
sess.run(train_step,feed_dict={x:,y:}) if i%1000 == 0:
validate_accu = sess.run(accuracy,feed_dict={x:x_val,y:y_val}) test_accu = sess.run(accuracy,feed_dict = {x:x_test,y:y_dict}) #RNN
input_size = 28(28*28 image)
hidden_size = 256
layer_num = 2
class_num =10 x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,class_num])
keep_prob = tf.placeholder(tf.float32) x = tf.reshape(x,[-1,28,28]) #一层lstm
lstm_layer = tf.contrib.rnn.BasicLSTMCell(num_units=hidden_size,forget_bias=1.0,..) #添加dropout
lstm_layer = tf.contrib.rnn.DropoutWrrapper(lstm_layer,input_keep_prob =1.0,output_keep_prob=keep_prob) #堆叠多层
mlstm = tf.contrib.rnn.MultiRNNCell([lstm_layer]*layer_sum,...) init_state = mlstm.zero_state(batch_size,dtype=tf.float32) output = mlstm(x) #添加softmax层
w = tf.Variable(tf.truncted_normal([hidden_size,class_num],stddev=0.1),dtype=tf.float32)
b = tf.Variable(tf.constant(0.1,shape=[class_num]),dtype=tf.float32)
y_ = tf.nn.softmax(tf.matmul(output,w)+b) cross_entropy = tf.reduce_mean(-y*tf.log(y_))
train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy) correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
#tf.argmax(input, axis=None, name=None, dimension=None)此函数是对矩阵按行或列计算最大值,0:按列,此处按行
accuracy = tf.reduce_mean(tf.cast(correct_prediction,'float'))#tf.cast():数据格式转换,此处bool-->float with tf.Session as sess: sess.run(train_step,feed_dict={x:,y:,keep_prob:}) #train if i%1000 ==0:
train_accuracy = sess.run(accuracy,feed_dict={x:x_val,y:y_val,keep_prob:})
print(train_accuracy) #测试集
test_accuracy = sess.run(accuracy,feed_dict={x:x_test,y:y_test,keep_prob:})
print(test_accuracy)
tensorflow cnn+rnn基本结构的更多相关文章
- Android+TensorFlow+CNN+MNIST 手写数字识别实现
Android+TensorFlow+CNN+MNIST 手写数字识别实现 SkySeraph 2018 Email:skyseraph00#163.com 更多精彩请直接访问SkySeraph个人站 ...
- 用tensorflow搭建RNN(LSTM)进行MNIST 手写数字辨识
用tensorflow搭建RNN(LSTM)进行MNIST 手写数字辨识 循环神经网络RNN相比传统的神经网络在处理序列化数据时更有优势,因为RNN能够将加入上(下)文信息进行考虑.一个简单的RNN如 ...
- TensorFlow之RNN:堆叠RNN、LSTM、GRU及双向LSTM
RNN(Recurrent Neural Networks,循环神经网络)是一种具有短期记忆能力的神经网络模型,可以处理任意长度的序列,在自然语言处理中的应用非常广泛,比如机器翻译.文本生成.问答系统 ...
- 第二十二节,TensorFlow中RNN实现一些其它知识补充
一 初始化RNN 上一节中介绍了 通过cell类构建RNN的函数,其中有一个参数initial_state,即cell初始状态参数,TensorFlow中封装了对其初始化的方法. 1.初始化为0 对于 ...
- 用深度学习(CNN RNN Attention)解决大规模文本分类问题 - 综述和实践
https://zhuanlan.zhihu.com/p/25928551 近来在同时做一个应用深度学习解决淘宝商品的类目预测问题的项目,恰好硕士毕业时论文题目便是文本分类问题,趁此机会总结下文本分类 ...
- [转] 用深度学习(CNN RNN Attention)解决大规模文本分类问题 - 综述和实践
转自知乎上看到的一篇很棒的文章:用深度学习(CNN RNN Attention)解决大规模文本分类问题 - 综述和实践 近来在同时做一个应用深度学习解决淘宝商品的类目预测问题的项目,恰好硕士毕业时论文 ...
- 深度学习-CNN+RNN笔记
以下叙述只是简单的叙述,CNN+RNN(LSTM,GRU)的应用相关文章还很多,而且研究的方向不仅仅是下文提到的1. CNN 特征提取,用于RNN语句生成图片标注.2. RNN特征提取用于CNN内容分 ...
- 使用Keras搭建cnn+rnn, BRNN,DRNN等模型
Keras api 提前知道: BatchNormalization, 用来加快每次迭代中的训练速度 Normalize the activations of the previous layer a ...
- TensorFlow 实现 RNN 入门教程
转子:https://www.leiphone.com/news/201705/zW49Eo8YfYu9K03J.html 最近在看RNN模型,为简单起见,本篇就以简单的二进制序列作为训练数据,而不实 ...
随机推荐
- HITOJ 2739 The Chinese Postman Problem(欧拉回路+最小费用流)
The Chinese Postman Problem My Tags (Edit) Source : bin3 Time limit : 1 sec Memory limit : 6 ...
- ACM ICPC China final G Pandaria
目录 ACM ICPC China final G Pandaria ACM ICPC China final G Pandaria 题意:给一张\(n\)个点\(m\)条边的无向图,\(c[i]\) ...
- python带header
headers = { "Accept":"text/html,application/xhtml+xml,application/xml;", "A ...
- hadoop学习之HDFS
1.什么是大数据?什么是云计算?什么是hadoop? 大数据现在很火,到底什么是大数据,多大的数据才算大,一般而言对于TB级以上的数据我们成为大数据,对于这些数据它的价值在哪?大数据的价值就是我们大量 ...
- Topcoder SRM 604 div1题解
CTSC考完跑了过来日常TC--- Easy(250pts): 题目大意:有个机器人,一开始的位置在(0,0),第k个回合可以向四个方向移动3^k的距离(不能不动),问是否可以到达(x,y),数据满足 ...
- python通过SSH登陆linux并操作
使用python通过SSH登陆linux并操作 用的昨天刚接触到的库,在windows下通过paramiko来登录linux系统并执行了几个命令,基本算是初试成功,后面会接着学习的. 代码: > ...
- HDU 5245 Joyful(概率题求期望)
D - Joyful Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit S ...
- windows7下检测耳机麦克拔插(转)
原文转自 https://blog.csdn.net/rankun1/article/details/50972990 #include "stdafx.h" #define SA ...
- 自定义topo遇到的坑
错误:TypeError: __init__() got an unexpected keyword argument 'delay' 解决办法:在创建topo的地方加一个link=TCLink即可, ...
- Curl请求方法封装
/** * GET请求 * @param $url * @return bool|mixed */ function http_get($url) { $oCurl = curl_init (); i ...