概述
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials import input_data
mnist = input_data.read_data_sets('data/', one_hot= True)
#定义神经网络各层的神经元数量
hidden1 = 256
hidden2 = 128
num_input = 784
num_output = 10
#设置输出和输入
x = tf.placeholder('float' , [None,num_input])
y = tf.placeholder('float', [None, num_output])
#初始化权重
std = 0.1
w = {
'w1': tf.Variable(tf.random.normal([num_input, hidden1],stddev=std)),
'w2': tf.Variable(tf.random.normal([hidden1, hidden2],stddev=std)),
'w3': tf.Variable(tf.random.normal([hidden2, num_output],stddev=std))
}
b = {
'b1': tf.Variable(tf.random.normal([hidden1],stddev=std)),
'b2': tf.Variable(tf.random.normal([hidden2],stddev=std)),
'b3': tf.Variable(tf.random.normal([num_output],stddev=std))
}
#定义网络
def Lnet(X, weight, biases):
layer1 = tf.nn.sigmoid(tf.add(tf.matmul(X,weight['w1']),biases['b1']))
layer2 = tf.nn.sigmoid(tf.add(tf.matmul(layer1, weight['w2']), biases['b2']))
out1 = tf.matmul(layer2, weight['w3'])+biases['b3']
return out1
#定义反向误差传播
pre = Lnet(x, w, b)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels= y, logits=pre))
optimizer = tf.train.GradientDescentOptimizer(0.001).minimize(loss)
correct = tf.equal(tf.argmax(pre, 1), tf.argmax(y, 1))
accr = tf.reduce_mean(tf.cast(correct,'float'))
init = tf.global_variables_initializer()
batch_size =100
with tf.Session() as sess:
sess.run(init)
for epoch in range(20):
avg_cost = 0
num_batch = int(mnist.train.num_examples/batch_size)
for i in range(num_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
sess.run(optimizer, feed_dict={x:batch_xs, y:batch_ys})
avg_cost += sess.run(loss, feed_dict={x:batch_xs, y:batch_ys})
avg_cost = avg_cost/num_batch
if (epoch+1) %5 ==0:
feeds_train = {x:batch_xs, y:batch_ys}
feeds_test = {x:mnist.test.images, y: mnist.test.labels}
train_acc = sess.run(accr, feed_dict=feeds_train)
test_acc = sess.run(accr, feed_dict=feeds_test )
print('epoch %03d/%03d loss:%.9f train_acc:%.3f, test_acc:%.3f'
%(epoch, 100,avg_cost,train_acc,test_acc))
最后
以上就是紧张热狗为你收集整理的tensorflow学习(四)设置两层神经网络对mnist数据进行十分类的全部内容,希望文章能够帮你解决tensorflow学习(四)设置两层神经网络对mnist数据进行十分类所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复