テスト例でネットワークのトレーニング後に結果を受け取ることができません。 それはテンソルフローにおけるMNISTデータベースの大出力予測
examples_to_show = 5
y_result = sess.run(y_pred, feed_dict={x:mnist.test.images[:examples_to_show]})
print("y_result=",y_result)
私が代わりに[0 0 1 0 0 0 0 0 0 0のない不明瞭な数字を受け取る
は、私は、このような方法で結果を受信しようヘルプmultilayer_perceptron.pyから標準の例です]
In [20]:
'''
A Multilayer Perceptron implementation example using TensorFlow library.
This example is using the MNIST database of handwritten digits
Author: Aymeric Damien
'''
In [21]:
# Import MINST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
import tensorflow as tf
Extracting MNIST_data/train-images-idx3-ubyte.gz
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 [22]:
# Parameters
learning_rate = 0.001
training_epochs = 15
batch_size = 100
display_step = 1
# Network Parameters
n_hidden_1 = 256 # 1st layer number of features
n_hidden_2 = 256 # 2nd layer number of features
n_input = 784 # MNIST data input (img shape: 28*28)
n_classes = 10 # MNIST total classes (0-9 digits)
# tf Graph input
x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_classes])
In [23]:
# Create model
def multilayer_perceptron(x, weights, biases):
# Hidden layer with RELU activation
layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
layer_1 = tf.nn.relu(layer_1)
# Hidden layer with RELU activation
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
layer_2 = tf.nn.relu(layer_2)
# Output layer with linear activation
out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
return out_layer
In [24]:
# Store layers weight & bias
weights = {
'h1': tf.Variable(tf.random_normal([n_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, n_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([n_classes]))
}
# Construct model
pred = multilayer_perceptron(x, weights, biases)
# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
# Initializing the variables
init = tf.global_variables_initializer()
The designer for predictions!!!
In [25]:
# Prediction
y_pred = pred
In [30]:
# Launch the graph
with tf.Session() as sess:
sess.run(init)
# Training cycle
for epoch in range(training_epochs):
avg_cost = 0.
total_batch = int(mnist.train.num_examples/batch_size)
# Loop over all batches
for i in range(total_batch):
batch_x, batch_y = mnist.train.next_batch(batch_size)
# Run optimization op (backprop) and cost op (to get loss value)
_, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
y: batch_y})
# Compute average loss
avg_cost += c/total_batch
# Display logs per epoch step
if epoch % display_step == 0:
print("Epoch:", '%04d' % (epoch+1), "cost=", \
"{:.9f}".format(avg_cost))
print("Optimization Finished!")
# Test model
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
# Calculate accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))
# We will try to receive result of training!!!
examples_to_show = 5
y_result = sess.run(y_pred, feed_dict={x: mnist.test.images[:examples_to_show]})
print("y_result=",y_result)
Epoch: 0001 cost= 142.664078834
Epoch: 0002 cost= 37.176684845
Epoch: 0003 cost= 23.608409217
Epoch: 0004 cost= 16.678811304
Epoch: 0005 cost= 12.175642554
Epoch: 0006 cost= 9.083989911
Epoch: 0007 cost= 6.624555320
Epoch: 0008 cost= 4.970751049
Epoch: 0009 cost= 3.595181121
Epoch: 0010 cost= 2.671157273
Epoch: 0011 cost= 2.032964239
Epoch: 0012 cost= 1.588672840
Epoch: 0013 cost= 1.133152580
Epoch: 0014 cost= 0.805134769
Epoch: 0015 cost= 0.689760053
Optimization Finished!
Accuracy: 0.941
y_result= [
[ -203.50767517 -437.82525635 186.90861511 590.15588379
-471.18536377 -283.88424683 -1150.14709473 1022.75799561 -391.6159668
432.9206543 ]
[ -855.87487793 6.88715792 903.70776367 252.00227356
-1407.09313965 441.29104614 344.09405518 -1691.98535156
40.62039566 -1391.43688965]
[ -244.32698059 618.91705322 12.79210854 -36.14464951
-8.12554073 183.12348938 50.32661057 147.05378723 152.9332428
-210.40829468]
[ 1091.7199707 -919.26574707 -333.54571533 -953.7399292 -1072.82226562
73.99294281 305.2588501 -166.91053772 -985.14654541
452.14318848]
[ 200.62698364 89.34638214 -280.01904297 -342.19534302 1240.4128418
229.24633789 -424.91091919 298.81100464 -194.70623779
934.27703857]]
結果はy_result = [0 0 1 0 0 0 0 0 0 0]でなければならない! ??? ???なぜ ???
非常に良い!!!! 私は交換しました –
#予測#y_pred = pred y_pred = tf.nn.softmax(pred) 私はどのように機能を適用するか理解しています! –