卷积神经网络的训练和测试(针对电脑内存比较小的,运行速度比较慢的)
来源:互联网 发布:windows退出安全模式 编辑:程序博客网 时间:2024/06/11 03:43
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import numpy as np
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
sess = tf.InteractiveSession()
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
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')
def my_conv(input_image, out_dim, name,channel):
with tf.variable_scope(name):
w_conv1 = weight_variable([5, 5, channel, out_dim])
b_conv1 = bias_variable([out_dim])
h_conv1 = tf.nn.relu(conv2d(input_image, w_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
return h_pool1
def my_fc_layer(input_image, out_dim, name):
with tf.variable_scope(name):
w_fc1 = weight_variable([7*7* 64, out_dim])
b_fc1 = bias_variable([out_dim])
h_pool2_flat = tf.reshape(input_image, [-1,7 *7 * 64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, w_fc1) + b_fc1)
return h_fc1
def build_net(input_data, keep_prob):
conv1 = my_conv(input_image=input_data, out_dim=32, name='conv_layer1',channel=1)
conv2 = my_conv(input_image=conv1, out_dim=64, name='conv_layer2',channel=32)
fc1 = my_fc_layer(input_image=conv2, out_dim=1024, name='fc_layer1')
h_fc1_drop = tf.nn.dropout(fc1, keep_prob)
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)
return y_conv
x = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])
x_image = tf.reshape(x, [-1, 28, 28, 1])
sum=tf.Variable(0.0,name="sum")
temp=tf.Variable(0.0,name="temp")
keep_prob = tf.placeholder(tf.float32)
y_conv=build_net(input_data=x_image,keep_prob=keep_prob)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1]))
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, tf.float32))
tf.global_variables_initializer().run() # 启动Session
for i in range(500):
batch = mnist.train.next_batch(50)
if i % 100 == 0:
train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
print("step %d, training accuracy %g" % (i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
#下面是训练时发现电脑内存较小,只能把训练集拆分成多步完成
for i in range(200):
testSet = mnist.test.next_batch(50)
temp=accuracy.eval(feed_dict={ x: testSet[0], y_: testSet[1], keep_prob: 1.0})
#print("test accuracy %g"%accuracy.eval(feed_dict={ x: testSet[0], y_: testSet[1], keep_prob: 1.0}))
print("test accuracy %g"%temp)
sum= tf.add(sum , temp)
s=sess.run(sum)
print(s/200)
# -*- coding: utf-8 -*-
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import numpy as np
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
sess = tf.InteractiveSession()
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
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')
def my_conv(input_image, out_dim, name,channel):
with tf.variable_scope(name):
w_conv1 = weight_variable([5, 5, channel, out_dim])
b_conv1 = bias_variable([out_dim])
h_conv1 = tf.nn.relu(conv2d(input_image, w_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
return h_pool1
def my_fc_layer(input_image, out_dim, name):
with tf.variable_scope(name):
w_fc1 = weight_variable([7*7* 64, out_dim])
b_fc1 = bias_variable([out_dim])
h_pool2_flat = tf.reshape(input_image, [-1,7 *7 * 64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, w_fc1) + b_fc1)
return h_fc1
def build_net(input_data, keep_prob):
conv1 = my_conv(input_image=input_data, out_dim=32, name='conv_layer1',channel=1)
conv2 = my_conv(input_image=conv1, out_dim=64, name='conv_layer2',channel=32)
fc1 = my_fc_layer(input_image=conv2, out_dim=1024, name='fc_layer1')
h_fc1_drop = tf.nn.dropout(fc1, keep_prob)
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)
return y_conv
x = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])
x_image = tf.reshape(x, [-1, 28, 28, 1])
sum=tf.Variable(0.0,name="sum")
temp=tf.Variable(0.0,name="temp")
keep_prob = tf.placeholder(tf.float32)
y_conv=build_net(input_data=x_image,keep_prob=keep_prob)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1]))
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, tf.float32))
tf.global_variables_initializer().run() # 启动Session
for i in range(500):
batch = mnist.train.next_batch(50)
if i % 100 == 0:
train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
print("step %d, training accuracy %g" % (i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
#下面是训练时发现电脑内存较小,只能把训练集拆分成多步完成
for i in range(200):
testSet = mnist.test.next_batch(50)
temp=accuracy.eval(feed_dict={ x: testSet[0], y_: testSet[1], keep_prob: 1.0})
#print("test accuracy %g"%accuracy.eval(feed_dict={ x: testSet[0], y_: testSet[1], keep_prob: 1.0}))
print("test accuracy %g"%temp)
sum= tf.add(sum , temp)
s=sess.run(sum)
print(s/200)
阅读全文
0 0
- 卷积神经网络的训练和测试(针对电脑内存比较小的,运行速度比较慢的)
- 卷积神经网络的训练
- 卷积神经网络的训练方法
- 感觉CakePHP的速度比较慢.
- 3用于MNIST的卷积神经网络-3.3简单卷积神经网络的训练和评估会话
- 用卷积神经网络检测脸部关键点的教程(二)卷积神经网络训练和数据扩充
- 卷积神经网络的小知识
- 卷积神经网络的卷积层和池化层
- 比较Java和C++的运行速度
- 卷积神经网络CNN解析-针对斯坦福的深度学习教程
- .Net 程序第一次运行比较慢的原因
- 卷积神经网络的卷积操作
- 4用于cifar10的卷积神经网络-4.6设计模型训练和评估的会话流程
- 神经网络训练样本和测试样本的区别
- 神经网络训练样本和测试样本的区别
- 卷积神经网络(CNN)的训练及代码实现
- caffe 进行卷积神经网络训练的命令参数解析
- 卷积神经网络(二):卷积神经网络CNN的BP算法
- 线性链条件随机场简介
- 创建虚拟机快照
- C语言使用popen获得FTP命令的输出信息
- ORACLE中通过SQL语句(alter table)来增加、删除、修改字段
- opencv 常用使用方法
- 卷积神经网络的训练和测试(针对电脑内存比较小的,运行速度比较慢的)
- 图像的卷积运算
- 快速定位Android闪退问题的方法
- 公路修建 P1265
- 《Flowable基础二 Flowable是什么》
- Kernel数据包处理基础
- 创建共享内存分以下几个步骤
- 学习方法推荐——番茄工作法
- Android事件机制之ViewPager的分发与拦截