Tensorflow实现简单的全连接神经网络

#coding : utf8

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data #读数据集

'''
1.数据
读取数据,划分训练集测试集labels,features
'''
data = input_data.read_data_sets('./MNIST_data', one_hot= True) #读取数据
train_x, train_y = data.train.images, data.train.labels
test_x, test_y = data.test.images, data.test.labels

'''
2.参数
本文使用两层隐藏层,各层neuron个数分别为:
input_layer: 28*28 (features of train_x or test_x)
hidden_layer1: 256
hidden_layer2: 128
output_layer: 10
'''
n_input = 28 * 28
n_hidden_layer_1 = 256
n_hidden_layer_2 = 128
n_output_layer = 10

x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_output_layer])

w = {
    'w1': tf.Variable(tf.random_normal([n_input, n_hidden_layer_1]), dtype= tf.float32),
    'w2': tf.Variable(tf.random_normal([n_hidden_layer_1, n_hidden_layer_2]),dtype= tf.float32),
    'w_out': tf.Variable(tf.random_normal([n_hidden_layer_2, n_output_layer]),dtype= tf.float32)
}
b = {
    'b1': tf.Variable(tf.zeros([1,n_hidden_layer_1]), dtype= tf.float32),
    'b2': tf.Variable(tf.zeros([1,n_hidden_layer_2]), dtype= tf.float32),
    'b_out': tf.Variable(tf.zeros([1,n_output_layer]), dtype= tf.float32)
}

'''
3.前向传播
'''

def forward_propagation(_x, _w, _b):
    layer1 = tf.nn.sigmoid(tf.add(tf.matmul(_x, _w['w1']), _b['b1']))
    layer2 = tf.nn.sigmoid(tf.add(tf.matmul(layer1, _w['w2']), _b['b2']))
    return tf.add(tf.matmul(layer2, _w['w_out']), _b['b_out'])

'''
4.损失函数
'''
learn_rate = 0.01
y_predict = forward_propagation(x, w, b)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits= y_predict, labels= y))
optm = tf.train.GradientDescentOptimizer(learn_rate).minimize(loss)

'''
5.评价
'''
result = tf.equal(tf.arg_max(y_predict, 1), tf.arg_max(y, 1))
acc = tf.reduce_mean(tf.cast(result, dtype=tf.float32))

'''
6.训练
'''
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

iters = 100
batch_size = 100
for i in range(iters):
    cost = count = 0.0
    for batch_index in range(0, train_y.shape[0], batch_size):
        count += 1
        feed = {x: train_x[batch_index: batch_index + batch_size], y: train_y[batch_index: batch_index + batch_size]}
        sess.run(optm, feed_dict= feed)
        cost += sess.run(loss, feed_dict= feed)
    cost /= count
    feed_test = {x: test_x, y: test_y}
    accuracy = sess.run(acc, feed_dict= feed_test)
    #print (accuracy)
    print ('第 %d 次迭代, 损失值为 %.5f, 分类准确率为 %.5f' % (i + 1, cost, accuracy))

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值