資源簡介
google-tensorflow官方樣例,簡單的BP神經網絡解決mnist問題.
代碼片段和文件信息
“““?Neural?Network.
A?2-Hidden?layers?Fully?Connected?Neural?Network?(a.k.a?Multilayer?Perceptron)
implementation?with?TensorFlow.?This?example?is?using?the?MNIST?database
of?handwritten?digits?(http://yann.lecun.com/exdb/mnist/).
links:
????[MNIST?Dataset](http://yann.lecun.com/exdb/mnist/).
Author:?Aymeric?Damien
Project:?https://github.com/aymericdamien/TensorFlow-Examples/
“““
from?__future__?import?print_function
#?Import?MNIST?data
from?tensorflow.examples.tutorials.mnist?import?input_data
mnist?=?input_data.read_data_sets(“/tmp/data/“?one_hot=True)
import?tensorflow?as?tf
#?Parameters
learning_rate?=?0.1
num_steps?=?500
batch_size?=?128
display_step?=?100
#?Network?Parameters
n_hidden_1?=?256?#?1st?layer?number?of?neurons
n_hidden_2?=?256?#?2nd?layer?number?of?neurons
num_input?=?784?#?MNIST?data?input?(img?shape:?28*28)
num_classes?=?10?#?MNIST?total?classes?(0-9?digits)
#?tf?Graph?input
X?=?tf.placeholder(“float“?[None?num_input])
Y?=?tf.placeholder(“float“?[None?num_classes])
#?Store?layers?weight?&?bias
weights?=?{
????‘h1‘:?tf.Variable(tf.random_normal([num_input?n_hidden_1]))
????‘h2‘:?tf.Variable(tf.random_normal([n_hidden_1?n_hidden_2]))
????‘out‘:?tf.Variable(tf.random_normal([n_hidden_2?num_classes]))
}
biases?=?{
????‘b1‘:?tf.Variable(tf.random_normal([n_hidden_1]))
????‘b2‘:?tf.Variable(tf.random_normal([n_hidden_2]))
????‘out‘:?tf.Variable(tf.random_normal([num_classes]))
}
#?Create?model
def?neural_net(x):
????#?Hidden?fully?connected?layer?with?256?neurons
????layer_1?=?tf.add(tf.matmul(x?weights[‘h1‘])?biases[‘b1‘])
????#?Hidden?fully?connected?layer?with?256?neurons
????layer_2?=?tf.add(tf.matmul(layer_1?weights[‘
評論
共有 條評論