Mnist数据集用神经网络处理-tensorflow实现

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets('./mnist/', one_hot=True)

INPUT_SIZE = 28
HIDDEN1_SIZE = 256
HIDDEN2_SIZE = 256
LR = 0.002
N_CLASSES = 10
TRAINING_EPOCH = 25
BATCH_SIZE = 50

X = tf.placeholder(tf.float32, [None, INPUT_SIZE * INPUT_SIZE])
Y = tf.placeholder(tf.float32, [None, N_CLASSES])

W1 = tf.Variable(tf.random_normal([INPUT_SIZE * INPUT_SIZE, HIDDEN1_SIZE]))
b1 = tf.Variable(tf.random_normal([HIDDEN1_SIZE]))
L1=tf.nn.sigmoid(tf.matmul(X,W1) + b1)

W2 = tf.Variable(tf.random_normal([HIDDEN1_SIZE, HIDDEN2_SIZE]))
b2 = tf.Variable(tf.random_normal([HIDDEN2_SIZE]))
L2=tf.nn.sigmoid(tf.matmul(L1,W2) + b2)

W3 = tf.Variable(tf.random_normal([HIDDEN1_SIZE, HIDDEN2_SIZE]))
b3 = tf.Variable(tf.random_normal([HIDDEN2_SIZE]))
L3=tf.nn.sigmoid(tf.matmul(L2,W3) + b3)

W = tf.Variable(tf.random_normal([HIDDEN2_SIZE, N_CLASSES]))
b = tf.Variable(tf.random_normal([N_CLASSES]))
hypothesis = tf.nn.sigmoid(tf.matmul(L3,W) + b)
#损失函数
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = hypothesis,labels = Y))
#ada梯度下降
optimizer=tf.train.AdamOptimizer(learning_rate=LR).minimize(cost)

sess = tf.Session()
sess.run(tf.global_variables_initializer())#全部初始化

print('Learning stared. It takes sometime.')
for epoch in range(TRAINING_EPOCH):#25次
    avg_cost = 0
    total_batch = int(mnist.train.num_examples / BATCH_SIZE)
    for i in range(total_batch):
        batch_xs, batch_ys = mnist.train.next_batch(BATCH_SIZE)
        c, _, = sess.run([cost, optimizer], feed_dict= {X: batch_xs, Y: batch_ys})
        avg_cost += c / total_batch
    print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost))
print('Learning Finished!')

correct_prediction = tf.equal(tf.argmax(hypothesis, 1), tf.argmax(Y, 1))#查看预测和正确相等的
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))#bool型转为float
print('Accuracy:', sess.run(accuracy, feed_dict={X: mnist.test.images, Y: mnist.test.labels}))#显示准确率

在这里插入图片描述

展开阅读全文

Windows版YOLOv4目标检测实战:训练自己的数据集

04-26
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值