#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基本结构的更多相关文章

  1. Android+TensorFlow+CNN+MNIST 手写数字识别实现

    Android+TensorFlow+CNN+MNIST 手写数字识别实现 SkySeraph 2018 Email:skyseraph00#163.com 更多精彩请直接访问SkySeraph个人站 ...

  2. 用tensorflow搭建RNN(LSTM)进行MNIST 手写数字辨识

    用tensorflow搭建RNN(LSTM)进行MNIST 手写数字辨识 循环神经网络RNN相比传统的神经网络在处理序列化数据时更有优势,因为RNN能够将加入上(下)文信息进行考虑.一个简单的RNN如 ...

  3. TensorFlow之RNN:堆叠RNN、LSTM、GRU及双向LSTM

    RNN(Recurrent Neural Networks,循环神经网络)是一种具有短期记忆能力的神经网络模型,可以处理任意长度的序列,在自然语言处理中的应用非常广泛,比如机器翻译.文本生成.问答系统 ...

  4. 第二十二节,TensorFlow中RNN实现一些其它知识补充

    一 初始化RNN 上一节中介绍了 通过cell类构建RNN的函数,其中有一个参数initial_state,即cell初始状态参数,TensorFlow中封装了对其初始化的方法. 1.初始化为0 对于 ...

  5. 用深度学习(CNN RNN Attention)解决大规模文本分类问题 - 综述和实践

    https://zhuanlan.zhihu.com/p/25928551 近来在同时做一个应用深度学习解决淘宝商品的类目预测问题的项目,恰好硕士毕业时论文题目便是文本分类问题,趁此机会总结下文本分类 ...

  6. [转] 用深度学习(CNN RNN Attention)解决大规模文本分类问题 - 综述和实践

    转自知乎上看到的一篇很棒的文章:用深度学习(CNN RNN Attention)解决大规模文本分类问题 - 综述和实践 近来在同时做一个应用深度学习解决淘宝商品的类目预测问题的项目,恰好硕士毕业时论文 ...

  7. 深度学习-CNN+RNN笔记

    以下叙述只是简单的叙述,CNN+RNN(LSTM,GRU)的应用相关文章还很多,而且研究的方向不仅仅是下文提到的1. CNN 特征提取,用于RNN语句生成图片标注.2. RNN特征提取用于CNN内容分 ...

  8. 使用Keras搭建cnn+rnn, BRNN,DRNN等模型

    Keras api 提前知道: BatchNormalization, 用来加快每次迭代中的训练速度 Normalize the activations of the previous layer a ...

  9. TensorFlow 实现 RNN 入门教程

    转子:https://www.leiphone.com/news/201705/zW49Eo8YfYu9K03J.html 最近在看RNN模型,为简单起见,本篇就以简单的二进制序列作为训练数据,而不实 ...

随机推荐

  1. 《c程序设计语言》读书笔记-4.2-扩充atof函数

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...

  2. poj 2186 强连通入门题目

    每头牛的梦想就是成为牛群中最受欢迎的牛. 在一群N(1 <= N <= 10,000)母牛中, 你可以得到M(1 <= M <= 50,000)有序的形式对(A,B),告诉你母 ...

  3. Python之面向对象:闭包和装饰器

    一.闭包 1. 如果一个函数定义在另一个函数的作用域内,并且引用了外层函数的变量,则该函数称为闭包. def outter(): name='python' def inner(): print na ...

  4. 阿里云服务器ubuntu安装java运行环境

    服务器 阿里云服务器ubuntu安装java运行环境 转:http://www.codingyun.com/article/45.html 今天来给大家介绍一下在阿里云ubuntu服务器下安装java ...

  5. 狂K 线段树

    想写好树剖~~线段树very important HDU 1166 敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  6. VS2013 更改MFC标题栏图标和生成的执行文件图标

    创建一个新工程,可以什么都不加.打开.rc,  创建或打开Icon资源(以下都以Icon为例).     单击工程窗口的资源视图标签,选中资源ID为IDR_MAINFRAME图标资源,然后按Delet ...

  7. Charles安装

    Charles 是一个网络抓包工具,在做 APP 抓包的时候会用到,相比 Fiddler 来说,Charles 的功能更为强大,而且跨平台支持更好,所以在这里我们选用 Charles 来作为主要的移动 ...

  8. error while loading shared libraries:libmysqlclient.so.18 错误

    error while loading shared libraries:libmysqlclient.so.18错误 新手安装php的时候如果出现这种问题,解决办法很简单,就是查看你的mysql安装 ...

  9. iis日志详解

    c-客户端操作.cs-客户端到服务器的操作.sc-服务器到客户端的操作. s-sitename s-computername s-ip cs-method cs-uri-stem cs-uri-que ...

  10. Ribbon负载均衡(四)

    一.Ribbon定义 spring cloud Ribbon是基于Netflix Ribbon实现的一套客户端,负载均衡工具 简单的说,Ribbon是Netflix发布的开源项目,主要功能是提供客户端 ...