多层感知机--tensorflow

本文详细介绍了如何使用TensorFlow构建并训练一个多层感知机模型,从搭建网络结构到设置优化器和损失函数,再到模型的训练与评估,深入理解深度学习的基础操作。
摘要由CSDN通过智能技术生成
#多层感知机
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
mnist = input_data.read_data_sets("MNIST_data",one_hot = True)
sess = tf.InteractiveSession()
WARNING: Logging before flag parsing goes to stderr.
W0815 13:04:20.904257  5308 deprecation.py:323] From <ipython-input-2-51754e844915>:1: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
W0815 13:04:20.904257  5308 deprecation.py:323] From C:\ProgramData\Anaconda3\envs\tensorflow1.14\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Please write your own downloading logic.
W0815 13:04:20.904257  5308 deprecation.py:323] From C:\ProgramData\Anaconda3\envs\tensorflow1.14\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.


Extracting MNIST_data\train-images-idx3-ubyte.gz


W0815 13:04:21.148457  5308 deprecation.py:323] From C:\ProgramData\Anaconda3\envs\tensorflow1.14\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
W0815 13:04:21.148457  5308 deprecation.py:323] From C:\ProgramData\Anaconda3\envs\tensorflow1.14\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.one_hot on tensors.
W0815 13:04:21.210657  5308 deprecation.py:323] From C:\ProgramData\Anaconda3\envs\tensorflow1.14\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.


Extracting MNIST_data\train-labels-idx1-ubyte.gz
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
#设置隐层参数并初始化
in_units = 784#输入节点数
h1_units = 300#隐层节点数
#使用truncated_normal()函数实现初始值为0,权重分布为截断的正态分布
w1 = tf.Variable(tf.truncated_normal([in_units,h1_units],stddev = 0.1))
b1 = tf.Variable(tf.zeros([h1_units]))
w2 = tf.Variable(tf.zeros([h1_units,10]))
b2 = tf.Variable(tf.zeros([10]))
#定义输入的x
x = tf.placeholder(tf.float32,[None,in_units])
keep_prop = tf.placeholder(tf.float32)#Dropout的比率keep_prop即保留节点的概率
###定义模型结构###
#定义一个隐含层--forwardc传播
hidden1 = tf.nn.relu(tf.matmul(x,w1) + b1)
hidden1_drop = tf.nn.dropout(hidden1,rate = 1 - keep_prop)
y = tf.nn.softmax(tf.matmul(hidden1_drop,w2)+b2)
#定义一个loss function 和 优化函数
y_ = tf.placeholder(tf.float32,[None,10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y),reduction_indices=[1]))
train_step = tf.train.AdagradOptimizer(0.3).minimize(cross_entropy)
W0815 13:04:21.548257  5308 deprecation.py:506] From C:\ProgramData\Anaconda3\envs\tensorflow1.14\lib\site-packages\tensorflow\python\training\adagrad.py:76: calling Constant.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Call initializer instance with the dtype argument instead of passing it to the constructor
#开始训练
#初始化全局参数
tf.global_variables_initializer().run()
for i in range(3000):
    batch_xs,batch_ys = mnist.train.next_batch(100)
    train_step.run({x:batch_xs,y_:batch_ys,keep_prop:0.75})
print("训练结束")
训练结束
#判断模型准确性
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
print(accuracy.eval({x:mnist.test.images,y_:mnist.test.labels,keep_prop:1.0}))
0.9813

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值