tensorflow56 《TensorFlow技术解析与实战》06 神经网络的发展及其Tensorflow实现
来源:互联网 发布:传奇手游刷元宝软件 编辑:程序博客网 时间:2024/05/18 10:53
# 《TensorFlow技术解析与实战》06 神经网络的发展及其TensorFlow实现# win10 Tensorflow1.2.0-RC0 python3.5.3# CUDA v8.0 cudnn-8.0-windows10-x64-v5.1# filename:nntf06.01.py mnist的AlexNet实现# 参考:# https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/convolutional_network.py# https://github.com/tensorflow/models/blob/master/tutorials/image/alexnet/alexnet_benchmark.pyimport tensorflow as tf# 输入数据from tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("/tmp/data/", one_hot=True)# 定义网络的超参数learing_rate = 0.001training_iters = 200000batch_size = 128displayer_step = 10# 定义网络的参数n_input = 784 # 输入维度(img shape: 28x28)n_classes = 10 # 标记维度(0-9 digits)dropout = 0.75 # Dropout概率,输出的可能性# 输入占位符x = tf.placeholder(tf.float32, [None, n_input])y = tf.placeholder(tf.float32, [None, n_classes])keep_prob = tf.placeholder(tf.float32) # dropout# 定义卷积操作def conv2d(name, x, W, b, strides=1): x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME') x = tf.nn.bias_add(x, b) return tf.nn.relu(x, name=name) # 使用relu激活函数# 定义池化操作def maxpool2d(name, x, k=2): return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME', name=name)# 规范化操作def norm(name, l_input, lsize=4): return tf.nn.lrn(l_input, lsize, bias=1.0, alpha=0.001/9.0, beta=0.75, name=name)# 定义所有网络参数weights = { 'wc1': tf.Variable(tf.random_normal([11, 11, 1, 96])), 'wc2': tf.Variable(tf.random_normal([5, 5, 96, 256])), 'wc3': tf.Variable(tf.random_normal([3, 3, 256, 384])), 'wc4': tf.Variable(tf.random_normal([3, 3, 384, 384])), 'wc5': tf.Variable(tf.random_normal([3, 3, 384, 256])), 'wd1': tf.Variable(tf.random_normal([4*4*256, 4096])), 'wd2': tf.Variable(tf.random_normal([4096, 4096])), 'out': tf.Variable(tf.random_normal([4096, 10]))}biases = { 'bc1': tf.Variable(tf.random_normal([96])), 'bc2': tf.Variable(tf.random_normal([256])), 'bc3': tf.Variable(tf.random_normal([384])), 'bc4': tf.Variable(tf.random_normal([384])), 'bc5': tf.Variable(tf.random_normal([256])), 'bd1': tf.Variable(tf.random_normal([4096])), 'bd2': tf.Variable(tf.random_normal([4096])), 'out': tf.Variable(tf.random_normal([n_classes]))}# 定义网络def alex_net(x, weights, biases, dropout): x = tf.reshape(x, shape=[-1, 28, 28, 1]) conv1 = conv2d('conv1', x, weights['wc1'], biases['bc1']) pool1 = maxpool2d('pool1', conv1, k=2) norm1 = norm('norm1', pool1, lsize=4) conv2 = conv2d('conv2', conv1, weights['wc2'], biases['bc2']) pool2 = maxpool2d('pool2', conv2, k = 2) norm2 = norm('norm2', pool2, lsize=4) conv3 = conv2d('conv3', norm2, weights['wc3'], biases['bc3']) pool3 = maxpool2d('pool3', conv3, k = 2) norm3 = norm('norm3', pool3, lsize=4) conv4 = conv2d('conv4', norm3, weights['wc4'], biases['bc4']) conv5 = conv2d('conv5', norm3, weights['wc5'], biases['bc5']) pool5 = maxpool2d('pool5', conv5, k = 2) norm5 = norm('norm5', pool5, lsize=4) fc1 = tf.reshape(norm5, [-1, weights['wd1'].get_shape().as_list()[0]]) fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1']) fc1 = tf.nn.relu(fc1) fc1 = tf.nn.dropout(fc1, dropout) fc2 = tf.reshape(fc1, [-1, weights['wd2'].get_shape().as_list()[0]]) fc2 = tf.add(tf.matmul(fc2, weights['wd2']), biases['bd2']) fc2 = tf.nn.relu(fc2) fc2 = tf.nn.dropout(fc2, dropout) out = tf.add(tf.matmul(fc2, weights['out']), biases['out']) return out#构建预测模型predict_model = alex_net(x, weights, biases, keep_prob)# 定义损失函数和优化器cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=predict_model))optimizer = tf.train.AdamOptimizer(learning_rate=learing_rate).minimize(cost)# 评估函数correct_pred = tf.equal(tf.argmax(predict_model, 1), tf.argmax(y, 1))accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))# 训练模型和评估模型# 初始化变量init = tf.global_variables_initializer()with tf.Session() as sess: sess.run(init) step = 1 # 开始训练 while step*batch_size < training_iters: batch_x, batch_y = mnist.train.next_batch(batch_size) sess.run(optimizer, feed_dict={x: batch_x, y: batch_y, keep_prob: dropout}) if step % displayer_step == 0: # 计算损失值和准确度,输出 loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x, y: batch_y, keep_prob: 1.}) print("Iter " + str(step*batch_size) + ", Minibatch Loss= " + "{:.6f}".format(loss) + ", Training Accuracy= " + "{:.5f}".format(acc)) step += 1 print("Optimizer Finished!") # 计算测试集的准确度 print("Testing Accuracy: ", sess.run(accuracy, feed_dict={x: mnist.test.images[:256], y: mnist.test.labels[:256], keep_prob: 1.}))'''Iter 1280, Minibatch Loss= 460011.468750, Training Accuracy= 0.36719Iter 2560, Minibatch Loss= 303076.562500, Training Accuracy= 0.62500...Iter 198400, Minibatch Loss= 4899.899414, Training Accuracy= 0.97656Iter 199680, Minibatch Loss= 447.203613, Training Accuracy= 0.99219Optimizer Finished!Testing Accuracy: 0.992188'''
阅读全文
0 0
- tensorflow56 《TensorFlow技术解析与实战》06 神经网络的发展及其Tensorflow实现
- TensorFlow技术解析与实战 6 神经网络的发展及其 TensorFlow 实现
- TensorFlow技术解析与实战 7 TensorFlow 的高级框架
- tensorflow55 《TensorFlow技术解析与实战》16 TensorFlow Debugger
- TensorFlow技术解析与实战 8 第一个tensorflow程序
- TensorFlow 技术解析与实战 笔记 01
- TensorFlow技术解析与实战 4 基础知识
- BP神经网络及其Tensorflow实现
- tensorflow57 《TensorFlow技术解析与实战》07 Tensorflow的高级框架-tflearn
- tensorflow58 《TensorFlow技术解析与实战》07 Tensorflow的高级框架-keras
- TensorFlow实战4:实现简单的多层神经网络案例
- TensorFlow技术解析与实战 10 人脸识别
- TensorFlow技术解析与实战 11 自然语言处理
- TensorFlow技术解析与实战 13 生成式对抗网络
- tensorflow32《TensorFlow实战》笔记-05 TensorFlow实现卷积神经网络 code
- TensorFlow技术解析与实战 12 图像与语音的结合
- TensorFlow实现进阶的神经网络
- tensorflow54 《TensorFlow技术解析与实战》15 TensorFlow线性代数编译框架XLA
- MD5Utils
- OkHttp3源码分析[任务队列]
- java 设置window系统时间和设置Linux系统时间
- JGibbLDA使用记录
- Linux自学过程(1)
- tensorflow56 《TensorFlow技术解析与实战》06 神经网络的发展及其Tensorflow实现
- Linker Tools Error LNK1168
- maven的pom文件说明
- 【spfa 判断负权回路】POJ
- atom快捷键
- Javascript计算器
- String.valueOf引发的错误
- HTTP Method
- LeetCode@Stack_20_Valid_Parentheses