tensorflow程序-最简单的CNN模型
来源:互联网 发布:php redis 订阅发布 编辑:程序博客网 时间:2024/06/16 06:45
#!/usr/bin/python#-*- coding=utf-8 -*-import tensorflow as tfimport numpy as npimport sys from tensorflow.examples.tutorials.mnist import input_data def weight_variable(shape): return tf.Variable( tf.truncated_normal(shape, stddev=0.1) ) def bias_variable(shape): return tf.Variable( tf.constant(0.1, shape=shape) ) def conv2d(x, W): return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')def max_pool_2x2(x): return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')# 随机取batch个训练样本def next_batch(train_data, train_target, batch_size): idx = [ i for i in range(0,len(train_target)) ] np.random.shuffle(idx); batch_data = []; batch_target = []; for i in range(0,batch_size): batch_data.append(train_data[idx[i]]); batch_target.append(train_target[idx[i]]) return batch_data, batch_target""" 初始化参数 """mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) #注意存放数据的路径train_data = mnist.train.images #55000的数据量train_target = mnist.train.labelstest_data = mnist.test.images #10000的数据量test_target = mnist.test.labelsx = tf.placeholder("float", shape=[None, 784]) #训练向量y = tf.placeholder("float", shape=[None, 10]) #真实结果keep_prob = tf.placeholder("float") # keep_probability 隐含层节点保持工作的概率epochs_num = 5000 #训练次数batch_size = 100 #分批次大小""" 创建CNN第一卷积层 """# 定义卷积核的大小5*5,传入一个图像,32个卷积核,所以传出32个图像。W_conv1 = weight_variable([5, 5, 1, 32]) b_conv1 = bias_variable([32]) # 定义bias的大小,为卷积核的个数x_image = tf.reshape(x, [-1, 28, 28, 1]) # 图片变成标准网络的输入参数h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # 使用relu激活函数h_pool1 = max_pool_2x2(h_conv1) # 输出[14,14,32,64]""" 创建CNN第二卷积层 """W_conv2 = weight_variable([5, 5, 32, 64]) b_conv2 = bias_variable([64]) h_conv2 = tf.nn.relu( conv2d(h_pool1, W_conv2) + b_conv2 ) h_pool2 = max_pool_2x2(h_conv2) # 输出[7,7,64,1024]""" 创建CNN第一全连接层 """ W_fc1 = weight_variable([7 * 7 * 64, 1024]) b_fc1 = bias_variable([1024]) h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) h_fc1 = tf.nn.relu( tf.matmul(h_pool2_flat, W_fc1) + b_fc1 ) h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) #某些隐含层节点的权重不工作""" 创建CNN第二全连接层 """ W_fc2 = weight_variable([1024, 10]) b_fc2 = bias_variable([10]) y_conv=tf.nn.softmax( tf.matmul(h_fc1_drop, W_fc2) + b_fc2 ) """ session """sess = tf.InteractiveSession() cross_entropy = -tf.reduce_sum(y*tf.log(y_conv)) train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) sess.run(tf.initialize_all_variables()) for i in range(epochs_num): batch_data, batch_target = next_batch(train_data,train_target,batch_size) if i%100 == 0: train_accuracy = accuracy.eval(feed_dict={ x:batch_data, y: batch_target, keep_prob: 1.0} ) print "step %d, training accuracy %.3f"%(i, train_accuracy) train_step.run(feed_dict={x: batch_data, y:batch_target, keep_prob: 0.5}) print "Training finished" print "test accuracy %.3f" % accuracy.eval(feed_dict={ x: test_data, y:test_target , keep_prob: 1.0})
0 0
- tensorflow程序-最简单的CNN模型
- tensorflow程序-最简单softmax模型
- Tensorflow CNN 的简单实现
- keras程序—最简单CNN实现
- 基于Tensorflow的CNN简单实现
- [TensorFlow实战] 简单CNN
- Tensorflow简单CNN实现
- TensorFlow学习笔记(1):最简单的入门程序
- TensorFlow MNIST CNN LeNet5模型
- Tensorflow 训练自己的cnn模型 行人识别
- 基于Tensorflow的英文评论二分类CNN模型
- Tensorflow实现cnn模型的训练与使用
- Tensorflow实现cnn模型的训练与使用
- tensorflow实现一个最基本的cnn分类mnist
- TensorFlow笔记之简单cnn
- 【Tensorflow】实现简单的卷积神经网络CNN实际代码
- TensorFlow-5实现简单的卷积神经网络CNN
- 基于tensorflow实现的简单卷积网络(CNN)
- Mybatis xml配置
- hdu 1000 A+B Problem
- 基于mini2440嵌入式linux上整合一套Domoticz智能家居系统(十)使用domoticz+mosquitto+Android客户端实现控制STM32板上的LED(一)
- CUDA-GPU programming introduction (2)
- SniperOJ pwn100-bof writeup
- tensorflow程序-最简单的CNN模型
- MySQL Cluster
- 网易复试(技术面)
- hdu 1001 Sum problem
- APP与服务端保持登录状态
- hdu 1002 A + B Problem II
- hdu 1005 Number Sequence
- Distributed System: Naming Service (命名服务)
- Hexo + Gitpage 搭建个人博客