Академический Документы
Профессиональный Документы
Культура Документы
PREV NEXT
⏮ ⏭
So max loader source code Visualization
🔎
Implementing a
five-layer neural
network
The following implementation increases the network complexity by
adding four layers before the softmax layer. To determine the appropriate
size of the network, that is, the number of hidden layers and the number
of neurons per layer, generally we rely on general empirical criteria, the
personal experience, or appropriate tests.
Number of Activation
Layer
neurons function
Third N = 60 sigmoid
https://www.safaribooksonline.com/library/view/deep-learning-with/9781786469786/9880603b-bba7-43cd-adae-346f8df48fb6.xhtml 1/4
12/8/2017 Implementing a five-layer neural network - Deep Learning with TensorFlow
Fourth O = 30 sigmoid
Fifth 10 softmax
The transfer function for the first four layers is the sigmoid function; the
last layer of the transfer function is always the softmax since the output
of the network must express a probability for the input digit. In general,
the number and the size of the intermediate layers greatly affect the
network performance:
In a positive way, because on these layers is based the ability of the net to
generalize and to detect peculiar characteristics of the input
import mnist_data
import tensorflow as tf
import math
logs_path = 'log_simple_stats_5_layers_relu_softmax'
batch_size = 100
learning_rate = 0.5
training_epochs = 10
We will then download images and labels and prepare the dataset:
mnist = mnist_data.read_data_sets("data")
Starting with the input layer, we'll now see how to build the network's
architecture.
The input layer is now a tensor of the shape [1×784], which represents the
image to classify:
The first layer receives the pixels of the input image to be classified
combined with the W1 weight connections and added to the respective
values of the B1 biases tensor:
⬆
The first layer sends its output to the second layer, through the sigmoid
activation function:
https://www.safaribooksonline.com/library/view/deep-learning-with/9781786469786/9880603b-bba7-43cd-adae-346f8df48fb6.xhtml 2/4
12/8/2017 Implementing a five-layer neural network - Deep Learning with TensorFlow
The second layer receives the Y1 output from the first layer and combines
it with the W2 weight connections and adds it to the respective values of
the B2 biases tensor:
The second layer sends its output to the third layer, through the sigmoid
activation function:
The third layer receives the Y2 output from the second layer and
combines it with the W3 weight connections and adds it to the respective
values of the B3 biases tensor:
The third layer sends its output to the fourth layer, through the sigmoid
activation function:
The fourth layer receives the Y3 output from the third layer and combines
it with the W4 weight connections and adds it to the respective values of
the B4 biases tensor:
It sends its output to the fifth layer, through the sigmoid activation
function:
The fifth layer will receive in input the O = 30 stimuli coming from the
fourth layer that will be converted in the respective classes of probability
for each number, through the softmax activation function:
Here, our loss function is the cross-entropy between the target and
the softmax activation function applied to the model's prediction:
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=Ylogits,
⬆
The tf.train.AdamOptimizer uses the Kingma and Ba's Adam
algorithm (https://arxiv.org/pdf/1412.6980v8.pdf) to
control the learning rate. AdamOptimizer offers several advantages
over the simple tf.train.GradientDescentOptimizer; in
https://www.safaribooksonline.com/library/view/deep-learning-with/9781786469786/9880603b-bba7-43cd-adae-346f8df48fb6.xhtml 3/4
12/8/2017 Implementing a five-layer neural network - Deep Learning with TensorFlow
fact, it uses a larger effective step size, and the algorithm will converge to
this step size without fine tuning:
learning_rate = 0.003
train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entro
The source code for the definition of the summaries and the running of
the session is almost identical to the previous. We can pass directly to
evaluate the implemented model. Running the model, we have the
following output. The final test set accuracy after running this code should
be approximately 97%:
>>>
Loading data/train-images-idx3-ubyte.mnist
Loading data/train-labels-idx1-ubyte.mnist
Loading data/t10k-images-idx3-ubyte.mnist
Loading data/t10k-labels-idx1-ubyte.mnist
Epoch: 0
Epoch: 1
Epoch: 2
Epoch: 3
Epoch: 4
Epoch: 5
Epoch: 6
Epoch: 7
Epoch: 8
Epoch: 9
Accuracy: 0.9744
done
>>>
Recommended / Queue / History / Topics / Tutorials / Settings / Get the App / Sign Out
© 2017 Safari. Terms of Service / Privacy Policy
PREV NEXT
⏮ ⏭
So max loader source code Visualization
https://www.safaribooksonline.com/library/view/deep-learning-with/9781786469786/9880603b-bba7-43cd-adae-346f8df48fb6.xhtml 4/4