0

I went thru the elementary example in Tensorflow of evaluating the trained model. Here is what it says:

accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

I did not follow this code, where is the trained 'model'? or is it tf.reduce_mean(....) ? checking the trained model.

Amir
  • 10,600
  • 9
  • 48
  • 75
Integration
  • 337
  • 1
  • 4
  • 15
  • 1
    ask your question clearly, clear your format. Learn how to ask questions. Your question does not make much sense. – Shubhashis Jan 07 '16 at 08:01
  • I to am interested in learning TensorFlow but was aware that I lacked the basic vocabulary and concepts for doing anything more than run the examples when I started. So to get the basics I turned to [Neural Networks and Deep Learning](http://neuralnetworksanddeeplearning.com/) and [Andrew Ng course videos](https://www.youtube.com/watch?v=UzxYlbK2c7E) – Guy Coder Jan 07 '16 at 13:47

1 Answers1

4

As "Guy Coder" says, maybe you should check other online resources or MOOCs before starting with tensorflow.

But anyway, maybe you will get a clearer picture with this...

There are two parts into training a model in tensorflow.

  1. First you declare the structure of your model, with the different layers and variables. Tensorflow will make a graph out of that, but there is no computation happened yet.
  2. Then you ask tensorflow to "run" and optimize the model. What you do here is tell tensorflow that you want to reduce the cross entropy, or whatever loss function you define, so you provide the input data and labels the graph needs to compute that.

After this you come up with a trained model. Maybe you will want to save the model and reuse it later, but that's another story.

So, during training, or after it is finished you may call
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})).

This is telling tensorflow to calculate the accuracy using the graph with the current value of the variables (maybe you are in the middle of training). And you are feeding this accuracy function the images and the labels. Tensorflow will take the x values and try to predict y_, and the accuracy will be the result of how well he did.

The connection to your trained model comes from the correct_prediction function which should compare the correct output with your model's prediction, i.e. y_ vs y

Hope this helps

EDIT

I will answer based on your comments, but be aware that your question is very poorly explained... as pointed out by S_kar

To save a model you do it like this:

# model declared before this line
with tf.Session() as sess:
    # Merge all the summaries and write them out to /tmp/tf
    merged = tf.merge_all_summaries()
    writer = tf.train.SummaryWriter("/tmp/tf", sess.graph_def)
    tf.initialize_all_variables().run()

    saver = tf.train.Saver()

    """
    train the model...
    """

    print "Model succesfuly trained"

    # now save the model in a subdirectory called "model"
    checkpoint_path = os.getcwd() + "/model/model.ckpt"
    saver.save(sess, checkpoint_path)
    print "Model saved"

To restore the model look into this question

Community
  • 1
  • 1
mathetes
  • 11,766
  • 7
  • 25
  • 32
  • I have followed the complete MNIST example of how it works, but from how I see we get data, create a model, train model & test the model. point is where is this model object in TensorFlow, how do I save it & use it in an application, which object is the trained model? – Integration Jan 07 '16 at 16:22
  • How is the Tensorflow model saved & used? – Integration Jan 07 '16 at 16:23
  • You need to require that the OP post a new question. I know new people are use to discussion boards but StackOverflow is not a discussion board. By posting new questions it allows people seeking answers to quickly find them. – Guy Coder Jan 07 '16 at 18:59
  • Thanks this really helped – Integration Jan 08 '16 at 01:12
  • 1
    Glad it helped! Just try to be more clear when formulating your next questions – mathetes Jan 08 '16 at 01:52