2017-05-10 7 views
-2

TensorFlow tutorialコードのテンソルフロー単純ネット予測(ネットワーク予測のヒストグラム)の結果を表示する必要があります。numf.ndarrayへのtf.Tensorデータの書式設定

問題は、予測の結果をこの目的のために判読可能な形式で表示できないことです(numpy.ndarrayが理想的です)。予測 'y'は<tf.Tensor 'Softmax_3:0' shape=(?, 10) dtype=float32>となっていますが、これまではこの形式を変更する方法が見つかりませんでした。

誰かがそれを行う方法を知っていれば、それについて私に助言してもらえますか?

私が試したnp.array(y)(まだ配列の内部のテンソル形式を保持し、これに関心がある場合y = tf.Variable([y], expected_shape = [55000,10])が(コードがそのようなエラーTypeError: __init__() got an unexpected keyword argument 'expected_shape'

をスロー:

import tensorflow as tf 
from tensorflow.examples.tutorials.mnist import input_data 
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) 

x = tf.placeholder(tf.float32, [None, 784]) 

W = tf.Variable(tf.zeros([784, 10])) 
b = tf.Variable(tf.zeros([10])) 
y = tf.nn.softmax(tf.matmul(x, W) + b) 

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.GradientDescentOptimizer(0.5).minimize(cross_entropy) 
sess = tf.InteractiveSession() 
tf.initialize_all_variables().run() 

for _ in range(1000): 
    batch_xs, batch_ys = mnist.train.next_batch(100) 
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) 

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})) 
+0

[TensorFlowでTensorオブジェクトの値を印刷するにはどうすればいいですか?](http://stackoverflow.com/questions/33633370/how-to-print-the-value-of-a-tensor-objectテンソル流) –

答えて

2

。 Python配列で取得するには、ターゲット変数でセッションを実行する必要があります(train_stepと精度と同じです)。 これを実行します。

pred_np = sess.run(y,feed_dict={x: mnist.test.images, y_: mnist.test.labels}) 

ここで、pred_npはnumpyの配列です。印刷/保存/表示することができます。お役に立てれば。

関連する問題