#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. 【Luogu】P2465山贼集团(树形状压DP)

    题目链接 写了个70分暴力还挂了,第一遍提交只拿了十分……海星 首先建虚拟节点多叉树转成二叉,然后子集枚举DP 设g[x][i]是以x为根的子树内山贼集合i,x啥都不选也没贡献的时候的最大价值 f[x ...

  2. [bzoj3813] 奇数国 [线段树+欧拉函数]

    题面 传送门 思路 这题目是真的难读......阅读理解题啊...... 但是理解了以后就发现,题目等价于: 给你一个区间,支持单点修改,以及查询一段区间的乘积的欧拉函数值,这个答案对19961993 ...

  3. 自动设置 rem es模块写法

    export default function () { let html = document.documentElement; function onWindowResize() { if (ht ...

  4. Tomcat给我的java.lang.OutOfMemoryError: PermGen

    今天,Tomcat给了我这么一个异常:java.lang.OutOfMemoryError: PermGen space.自己是第一次遇到,抱着好奇的心情google了一下,居然是个很常见的异常!故记 ...

  5. wordpress对使用的国外主题进行本地汉化

    wordpress有非常多优秀与专业的主题,当然大多数是非中文的 这些主题本身总会有些无法通过wordpress admin后台来配置的在页面上的英文输出 此时你可以去对应的代码去改掉那些输出,不过这 ...

  6. 栅栏涂漆(color)

    栅栏涂漆测评 题目描述 zed 最近总是受到 Farmer 的困扰,因此他在自家的门前插了一排栅栏以防农气的入侵.栅栏由 N 个竖条栅栏横向组成,每个竖条栅栏宽度为 1.过了一段时间,zed 觉得栅栏 ...

  7. authencated认证登录

    #coding:utf-8 import tornado.httpserver import tornado.ioloop import tornado.options import tornado. ...

  8. 使用clamav查杀病毒

    cd ~ wget http://www.zlib.net/fossils/zlib-1.2.8.tar.gz .tar.gz cd zlib- ./configure --prefix=/usr/l ...

  9. APscheduler总结

    APscheduler使用总结 APscheduler是执行定时任务的python库,其作用可以代替Linux系统下的crontab,github上有该库的例子. APsheduler基本使用 该模块 ...

  10. 【linux高级程序设计】(第十五章)UDP网络编程应用 4

    socket信号驱动 为了使一个套接字能够使用信号驱动I/O,至少需要以下3步操作. 1.安装SIGIO信号 2.套接字的拥有者设定为当前进程.因为SIGIO信号只会送到socket拥有者进程. 通过 ...