DNN深度神经网络 的原理 以及 使用
2017-06-13 20:09
441 查看
DNN 深度神经网络,就是 把原有的多层神经网络 扩展到深度学习里面,加上了BP 反馈,是的整理上 loss 收敛 直至不变,同时也有dropout 前面 有很多这个词 出现,dropout 是指 随机用一定概率 把一些 节点失效,进行参与训练 放置数据整理上陷入overfitting 局部最优解。DNN,就是去掉C 之后 使用全连接层+dropout下降+relu激活 一层一层的WX+B的 网络模式。
import input_data mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
import tensorflow as tf # Parameters learning_rate = 0.001 training_iters = 100000 batch_size = 128 display_step = 10 # Network Parameters n_input = 784 # MNIST data input (img shape: 28*28) n_classes = 10 # MNIST total classes (0-9 digits) dropout = 0.75 # Dropout, probability to keep units x = tf.placeholder(tf.float32, [None, n_input]) y = tf.placeholder(tf.float32, [None, n_classes]) keep_prob = tf.placeholder(tf.float32)
def conv2d(img, w, b):return tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(img, w, strides=[1, 1, 1, 1], padding='SAME'),b))def max_pool(img, k):return tf.nn.max_pool(img, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME')def conv_net(_X, _weights, _biases, _dropout):# Reshape input picture_X = tf.reshape(_X, shape=[-1, 28, 28, 1])# Convolution Layerconv1 = conv2d(_X, _weights['wc1'], _biases['bc1'])# Max Pooling (down-sampling)conv1 = max_pool(conv1, k=2)# Apply Dropoutconv1 = tf.nn.dropout(conv1, _dropout)# Convolution Layerconv2 = conv2d(conv1, _weights['wc2'], _biases['bc2'])# Max Pooling (down-sampling)conv2 = max_pool(conv2, k=2)# Apply Dropoutconv2 = tf.nn.dropout(conv2, _dropout)# Fully connected layerdense1 = tf.reshape(conv2, [-1, _weights['wd1'].get_shape().as_list()[0]]) # Reshape conv2 output to fit dense layer inputdense1 = tf.nn.relu(tf.add(tf.matmul(dense1, _weights['wd1']), _biases['bd1'])) # Relu activationdense1 = tf.nn.dropout(dense1, _dropout) # Apply Dropout# Output, class predictionout = tf.add(tf.matmul(dense1, _weights['out']), _biases['out'])return outweights = {'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])), # 5x5 conv, 1 input, 32 outputs'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])), # 5x5 conv, 32 inputs, 64 outputs'wd1': tf.Variable(tf.random_normal([7*7*64, 1024])), # fully connected, 7*7*64 inputs, 1024 outputs'out': tf.Variable(tf.random_normal([1024, n_classes])) # 1024 inputs, 10 outputs (class prediction)}biases = {'bc1': tf.Variable(tf.random_normal([32])),'bc2': tf.Variable(tf.random_normal([64])),'bd1': tf.Variable(tf.random_normal([1024])),'out': tf.Variable(tf.random_normal([n_classes]))}# Construct modelpred = conv_net(x, weights, biases, keep_prob)cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))init = tf.initialize_all_variables()with tf.Session() as sess:sess.run(init)step = 1while step * batch_size < training_iters:batch_xs, batch_ys = mnist.train.next_batch(batch_size)sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys, keep_prob: dropout})if step % display_step == 0:acc = sess.run(accuracy, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.})loss = sess.run(cost, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.})print "Iter " + str(step*batch_size) + ", Minibatch Loss= " + "{:.6f}".format(loss) + ", Training Accuracy= " + "{:.5f}".format(acc)step += 1print "Optimization Finished!"print "Testing Accuracy:", sess.run(accuracy, feed_dict={x: mnist.test.images[:256], y: mnist.test.labels[:256], keep_prob: 1.})
相关文章推荐
- python keras (一个超好用的神经网络框架)的使用以及实例
- 深度学习之四:使用Theano编写神经网络
- 【神经网络与深度学习】leveldb的实现原理
- C++从零实现深度神经网络之五——模型的保存和加载以及画出实时输出曲线
- python keras (一个超好用的神经网络框架)的使用以及实例
- 深度学习算法原理——神经网络的基本原理
- 【神经网络与深度学习】Caffe使用step by step:使用自己数据对已经训练好的模型进行finetuning
- Retrofit网络框架的使用以及原理解析
- 【神经网络与深度学习】【Qt开发】【VS开发】从caffe-windows-visual studio2013到Qt5.7使用caffemodel进行分类的移植过程<二>
- 深度学习原理——从神经网络开始
- 深度神经网络结构以及Pre-Training的理解
- 物理学家揭示深度学习原理:神经网络与宇宙本质惊人关联
- 从图像到知识:深度神经网络实现图像理解的原理解析
- 【神经网络与深度学习】GLog使用笔记
- 深度神经网络结构以及Pre-Training的理解
- 深度学习之(DNN)深度神经网络
- [action] deep learning 深度学习 tensorflow 实战(2) 实现简单神经网络以及随机梯度下降算法S.G.D
- 深度学习算法实践7---前向神经网络算法原理
- 学习笔记:Deep Learning(二)深度神经网络以及正则化
- 【神经网络与深度学习】【C/C++】使用blas做矩阵乘法