user1218191
user1218191

Reputation:

executing function in TensorFlow

I have some questions regarding this Code - neural networks in TensorFlow.

#!/usr/bin/env python

import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data


def init_weights(shape):
    return tf.Variable(tf.random_normal(shape, stddev=0.01))


def model(X, w_h, w_o):
    h = tf.nn.sigmoid(tf.matmul(X, w_h)) # this is a basic mlp, think 2 stacked logistic regressions
    return tf.matmul(h, w_o) # note that we dont take the softmax at the end because our cost fn does that for us


mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels

X = tf.placeholder("float", [None, 784])
Y = tf.placeholder("float", [None, 10])

w_h = init_weights([784, 625]) # create symbolic variables
w_o = init_weights([625, 10])

py_x = model(X, w_h, w_o)

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y)) # compute costs
train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost) # construct an optimizer
predict_op = tf.argmax(py_x, 1)

# Launch the graph in a session
with tf.Session() as sess:
    # you need to initialize all variables
    tf.global_variables_initializer().run()

    for i in range(100):
        for start, end in zip(range(0, len(trX), 128), range(128, len(trX)+1, 128)):
            sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end]})
        print(i, np.mean(np.argmax(teY, axis=1) ==
                         sess.run(predict_op, feed_dict={X: teX})))

Thanks in advance. I am new to tensorFlow :)

Upvotes: 1

Views: 4908

Answers (2)

jugi
jugi

Reputation: 702

Regarding the second question:

  • Similarly, how to print the values of h inside the model() function ?

With the function tf.Print

def model(X, w_h, w_o):
    print_h = tf.nn.sigmoid(tf.matmul(X, w_h)) # this is a basic mlp, think 2 stacked logistic regressions
    h = tf.Print(print_h,[print_h]) # add a node to the execution graph that prints h when ever h is needed and executed in the graph
    return tf.matmul(h, w_o) # note that we dont take the softmax at the end because our cost fn does that for us

Good explanation can be found here: https://towardsdatascience.com/using-tf-print-in-tensorflow-aa26e1cff11e

Upvotes: 0

RuDevel
RuDevel

Reputation: 714

The feed_dict translates the placeholders into real value(s). So provide a single entry for feed_dicts and evaluate py_x.

The following should work:

For the result (px_y):

print(sess.run(py_x, feed_dict={X: [yoursample]}))

For h it's (almost) the same. But as in the linked code h is a private member of model() you'll need a reference to h in order to evaluate it. The easiest way most probably is to replace lines:

(14) return tf.matmul(h, w_o)
with
(14) return (tf.matmul(h, w_o), h)

(26) py_x = model(X, w_h, w_o)
with
(26) py_x, h = model(X, w_h, w_o)

and use:

print(sess.run(h, feed_dict={X: [yoursample]}))

or alternatively (evaluate multiple variables):

py_val, h_val = sess.run([py_x, h], feed_dict={X: [yoursample]})
print(py_val, h)

Explanation: By the way we told Tensorflow how our Network is constructed we did not need an explicit reference to the (inner/hidden) variable h. But in order to evaluate it we do need the reference to define what exactly to evaluate.

There are other ways to get the variables out of the guts of Tensorflow, but as we explicitly create this variable a few lines above I'd avoid dropping something into a black box and ask the very same black box later on to give it back.

Upvotes: 3

Related Questions