#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. JS设置、获取DOM自定义属性

    jQuery方式 // 获取 $('#test').attr('mydata'); // 设置 $('#test').attr('mydata','data-content'); // 移除 $('# ...

  2. BZOJ3236 [Ahoi2013]作业 【莫队 + 树状数组】

    题目链接 BZOJ3236 题解 没想到这题真的是如此暴力 #include<algorithm> #include<iostream> #include<cstring ...

  3. [解决方案]IIS7.5 报错:无法启动计算机“."上的服务W3SVC

    报错场景: 在云服务器上,正常使用着,突然今天一打开网站就都用不了了,上去服务器一看,IIS中站点被停止了,我还怀疑是回收的问题,结果一直启动无果,我打算重启来解决这个问题,重启后发现所有站点都变成停 ...

  4. hdu 1847 Good Luck in CET-4 Everybody! SG函数SG引理

    大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧张得连短学期的ACM都没工夫练习了,反正我知道的Kiki和Cici都是如此.当然,作为在考场浸润了十几载的当代大学生,Kiki和Cici更懂得考 ...

  5. COM RTS/CTS, DTR/DSR

    COM: 串行通讯端口cluster communication port它是串行接口,现在的PC 机一般有两个串行口COM 1 和COM 2 .串行口不同于并行口之处在于它的数据和控制信息是一位接一 ...

  6. Build RPM package from source code

    # yum install rpm-build# wget http://rsync.samba.org/ftp/rsync/rsync-3.0.9.tar.gz# vim rsync.specNam ...

  7. float存储

    浮点型变量在计算机内存中占用4字节(Byte),即32-bit.遵循IEEE-754格式标准. 一个浮点数由2部分组成:底数m 和 指数e.                          ±man ...

  8. linq使用 count与sum等

    using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; us ...

  9. (转)python爬虫----(scrapy框架提高(1),自定义Request爬取)

    摘要 之前一直使用默认的parse入口,以及SgmlLinkExtractor自动抓取url.但是一般使用的时候都是需要自己写具体的url抓取函数的. python 爬虫 scrapy scrapy提 ...

  10. 软中断网卡处理&Linux高性能外部设备处理机制&SMP

    转载:http://blog.csdn.net/freas_1990/article/details/9238183 看了一些linux网卡驱动的处理技术,对有些概念还是无法理解,突然搜到这篇文章,挺 ...