2017-03-09 55 views
3

我現在正在用python 3.5.2學習tensorflow 1.0。我嘗試了在github上找到的下面的代碼,但是我得到錯誤沒有名爲'tensorflowvisu'的模塊。如果我刪除導入tensorflowvisu我得到錯誤我= tensorflowvisu.tf_format_mnist_images(X,Ypred,Y_)#默認彙編10x10圖像0​​NameError:名稱'tensorflowvisu'未定義 我該怎麼做才能使此代碼工作?有沒有人有與tensorflow 1.0和python 3.5,我可以按照學習mnist工作代碼?任何迴應讚賞。 https://github.com/martin-gorner/tensorflow-mnist-tutorial/blob/master/mnist_1.0_softmax.py張量流1.0錯誤代碼

import tensorflow as tf 
import tensorflowvisu 
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets 
tf.set_random_seed(0) 

# neural network with 1 layer of 10 softmax neurons 
# 
# · · · · · · · · · ·  (input data, flattened pixels)  X [batch, 784]  # 784 = 28 * 28 
# \x/x\x/x\x/x\x/x\x/ -- fully connected layer (softmax)  W [784, 10]  b[10] 
# · · · · · · · ·            Y [batch, 10] 

# The model is: 
# 
# Y = softmax(X * W + b) 
#    X: matrix for 100 grayscale images of 28x28 pixels, flattened (there are 100 images in a mini-batch) 
#    W: weight matrix with 784 lines and 10 columns 
#    b: bias vector with 10 dimensions 
#    +: add with broadcasting: adds the vector to each line of the matrix (numpy) 
#    softmax(matrix) applies softmax on each line 
#    softmax(line) applies an exp to each value then divides by the norm of the resulting line 
#    Y: output matrix with 100 lines and 10 columns 

# Download images and labels into mnist.test (10K images+labels) and mnist.train (60K images+labels) 
mnist = read_data_sets("data", one_hot=True, reshape=False, validation_size=0) 

# input X: 28x28 grayscale images, the first dimension (None) will index the images in the mini-batch 
X = tf.placeholder(tf.float32, [None, 28, 28, 1]) 
# correct answers will go here 
Y_ = tf.placeholder(tf.float32, [None, 10]) 
# weights W[784, 10] 784=28*28 
W = tf.Variable(tf.zeros([784, 10])) 
# biases b[10] 
b = tf.Variable(tf.zeros([10])) 

# flatten the images into a single line of pixels 
# -1 in the shape definition means "the only possible dimension that will preserve the number of elements" 
XX = tf.reshape(X, [-1, 784]) 

# The model 
Y = tf.nn.softmax(tf.matmul(XX, W) + b) 

# loss function: cross-entropy = - sum(Y_i * log(Yi)) 
#       Y: the computed output vector 
#       Y_: the desired output vector 

# cross-entropy 
# log takes the log of each element, * multiplies the tensors element by element 
# reduce_mean will add all the components in the tensor 
# so here we end up with the total cross-entropy for all images in the batch 
cross_entropy = -tf.reduce_mean(Y_ * tf.log(Y)) * 1000.0 # normalized for batches of 100 images, 
                  # *10 because "mean" included an unwanted division by 10 

# accuracy of the trained model, between 0 (worst) and 1 (best) 
correct_prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1)) 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 

# training, learning rate = 0.005 
train_step = tf.train.GradientDescentOptimizer(0.005).minimize(cross_entropy) 

# matplotlib visualisation 
allweights = tf.reshape(W, [-1]) 
allbiases = tf.reshape(b, [-1]) 
I = tensorflowvisu.tf_format_mnist_images(X, Y, Y_) # assembles 10x10 images by default 
It = tensorflowvisu.tf_format_mnist_images(X, Y, Y_, 1000, lines=25) # 1000 images on 25 lines 
datavis = tensorflowvisu.MnistDataVis() 

# init 
init = tf.global_variables_initializer() 
sess = tf.Session() 
sess.run(init) 


# You can call this function in a loop to train the model, 100 images at a time 
def training_step(i, update_test_data, update_train_data): 

    # training on batches of 100 images with 100 labels 
    batch_X, batch_Y = mnist.train.next_batch(100) 

    # compute training values for visualisation 
    if update_train_data: 
     a, c, im, w, b = sess.run([accuracy, cross_entropy, I, allweights, allbiases], feed_dict={X: batch_X, Y_: batch_Y}) 
     datavis.append_training_curves_data(i, a, c) 
     datavis.append_data_histograms(i, w, b) 
     datavis.update_image1(im) 
     print(str(i) + ": accuracy:" + str(a) + " loss: " + str(c)) 

    # compute test values for visualisation 
    if update_test_data: 
     a, c, im = sess.run([accuracy, cross_entropy, It], feed_dict={X: mnist.test.images, Y_: mnist.test.labels}) 
     datavis.append_test_curves_data(i, a, c) 
     datavis.update_image2(im) 
     print(str(i) + ": ********* epoch " + str(i*100//mnist.train.images.shape[0]+1) + " ********* test accuracy:" + str(a) + " test loss: " + str(c)) 

    # the backpropagation training step 
    sess.run(train_step, feed_dict={X: batch_X, Y_: batch_Y}) 


datavis.animate(training_step, iterations=2000+1, train_data_update_freq=10, test_data_update_freq=50, more_tests_at_start=True) 

# to save the animation as a movie, add save_movie=True as an argument to datavis.animate 
# to disable the visualisation use the following line instead of the datavis.animate line 
# for i in range(2000+1): training_step(i, i % 50 == 0, i % 10 == 0) 

print("max test accuracy: " + str(datavis.get_max_test_accuracy())) 

# final max test accuracy = 0.9268 (10K iterations). Accuracy should peak above 0.92 in the first 2000 iterations. 
+0

您是否在'tensorflowvisu.py'所在的文件夾中運行腳本?這與tensorflow沒有任何關係。這是一個python問題。 – user3813674

回答

6

我有同樣的問題。解決方案是從所有代碼所在的文件夾中運行代碼。不要將mnist_1.0_softmax.py代碼複製到您的IDE並運行它。下載或鏈接克隆低於

https://github.com/martin-gorner/tensorflow-mnist-tutorial.git 

一旦克隆整個回購,你會看到該文件夾​​中有一個名爲tensorflowvisu.py文件。所以這不是您從conda或pip安裝的模塊。這只是作者在這種情況下作爲模塊使用的文件。去那個dir其中所有這些代碼坐鎮通過命令行,並從那裏運行

python mnist_1.0_softmax.py 

現在,它應該工作。您應該會看到一個彈出窗口,其中包含6個實時更新的圖表。

如果你想從你的IDE運行它,然後打開你的IDE(在我的情況下是Atom),然後去文件>打開文件夾>點擊確定>選擇你的文件mnist_1.0_softmax.py並按下Ctrl + Shift + B. 應該出現相同的彈出窗口。 最重要的是從作者提供的目錄內打開文件。

+0

kuatroka非常感謝您的回覆......我真的很感謝您的幫助! –