Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tf.Print() doesn't print the shape of the tensors?

I have written a simple classification program using Tensorflow and getting the output except I tried to print the shape of tensors for Model parameters, features & bias. The function definations:

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


def get_weights(n_features, n_labels):
#    Return weights
    return tf.Variable( tf.truncated_normal((n_features, n_labels)) )

def get_biases(n_labels):
    # Return biases
    return tf.Variable( tf.zeros(n_labels))

def linear(input, w, b):
    #  Linear Function (xW + b)
#     return np.dot(input,w) + b 
    return tf.add(tf.matmul(input,w), b)

def mnist_features_labels(n_labels):
    """Gets the first <n> labels from the MNIST dataset
    """
    mnist_features = []
    mnist_labels = []
    mnist = input_data.read_data_sets('dataset/mnist', one_hot=True)

    # In order to make quizzes run faster, we're only looking at 10000 images
    for mnist_feature, mnist_label in zip(*mnist.train.next_batch(10000)):

        # Add features and labels if it's for the first <n>th labels
        if mnist_label[:n_labels].any():
            mnist_features.append(mnist_feature)
            mnist_labels.append(mnist_label[:n_labels])

    return mnist_features, mnist_labels

The graph creation :

# Number of features (28*28 image is 784 features)
n_features = 784
# Number of labels
n_labels = 3

# Features and Labels
features = tf.placeholder(tf.float32)
labels = tf.placeholder(tf.float32)

# Weights and Biases
w = get_weights(n_features, n_labels)
b = get_biases(n_labels)

# Linear Function xW + b
logits = linear(features, w, b)

# Training data
train_features, train_labels = mnist_features_labels(n_labels)

print("Total {0} data points of Training Data, each having {1} features \n \
      Total {2} number of labels,each having 1-hot encoding {3}".format(len(train_features),len(train_features[0]),\
                                                                     len(train_labels),train_labels[0]
                                                                      )
     )

# global variables initialiser
init= tf.global_variables_initializer()

with tf.Session() as session:

    session.run(init)

The problem is here :

#            shapes =tf.Print ( tf.shape(features), [tf.shape(features),
#                                                     tf.shape(labels),
#                                                     tf.shape(w),
#                                                     tf.shape(b),
#                                                     tf.shape(logits)
#                                                     ], message= "The shapes are:" )
#         print("Verify shapes",shapes)
    logits = tf.Print(logits, [tf.shape(features),
                           tf.shape(labels),
                           tf.shape(w),
                           tf.shape(b),
                           tf.shape(logits)],
                  message= "The shapes are:")
    print(logits)

I looked at here, but didn't find much useful.

    # Softmax
    prediction = tf.nn.softmax(logits)

    # Cross entropy
    # This quantifies how far off the predictions were.
    # You'll learn more about this in future lessons.
    cross_entropy = -tf.reduce_sum(labels * tf.log(prediction), reduction_indices=1)

    # Training loss
    # You'll learn more about this in future lessons.
    loss = tf.reduce_mean(cross_entropy)

    # Rate at which the weights are changed
    # You'll learn more about this in future lessons.
    learning_rate = 0.08

    # Gradient Descent
    # This is the method used to train the model
    # You'll learn more about this in future lessons.
    optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)

    # Run optimizer and get loss
    _, l = session.run(
        [optimizer, loss],
        feed_dict={features: train_features, labels: train_labels})

# Print loss
print('Loss: {}'.format(l))

The output I am getting is :

Extracting dataset/mnist/train-images-idx3-ubyte.gz
Extracting dataset/mnist/train-labels-idx1-ubyte.gz
Extracting dataset/mnist/t10k-images-idx3-ubyte.gz
Extracting dataset/mnist/t10k-labels-idx1-ubyte.gz
Total 3118 data points of Training Data, each having 784 features 
       Total 3118 number of labels,each having 1-hot encoding [0. 1. 0.]
Tensor("Print_22:0", shape=(?, 3), dtype=float32)
Loss: 5.339271068572998

Could anyone help me understand, Why I am not able to see the shapes of the tensors?

like image 371
Anu Avatar asked Apr 11 '18 15:04

Anu


People also ask

Is it possible to print the shape of a tensor in Python?

Sign in to your account It has been a fairly frequent feature request to print the shape of a tensor when printing the tensor in Python with print (tensor) (and it was controversial when we stopped printing the shape awhile ago). the cost is asymmetric. I.e. printing is worse than not printing because you can't unprint.

How do I print in TensorFlow?

There are a couple of ways to get things to print out while writing TensorFlow code. Of course, there’s the classic Python built-in, print (Or the function print (), if we’re being Python 3 about it). And then there’s TensorFlow’s print function, tf.Print (notice the capital P ).

How to change the shape of a tensor in TF?

As you might guess, the tf.reshape () operation is used to change the shape of a tensor. The general definition of the operation is as follows: What this does is; given a tensor of initial shape, tf.reshape () returns a tensor with the same elements, in the same order, with the same datatype, but with a different arrangement (i.e. shape).

How to find the dimension of a tensor in TensorFlow?

We begin from the outermost list to find out the tensor’s shape. By subtracting the first dimension then subtracting the next, we arrive at the tensor’s shape. Afterwards, we repeat this process for the inner lists and find their next dimensions. How Do You Find The Dimension Of A Tensor In Tensorflow?


1 Answers

That is not how you use tf.Print. It is an op that does nothing on its own (simply returns the input) but prints the requested tensors as a side effect. You should do something like

logits = tf.Print(logits, [tf.shape(features),
                           tf.shape(labels),
                           tf.shape(w),
                           tf.shape(b),
                           tf.shape(logits)],
                  message= "The shapes are:")

Now, whenever logits is evaluated (as it will be for computing the loss/gradients), the shape information will be printed.

What you are doing right now is simply printing the return value of the tf.Print op, which is just its input (tf.shape(features)).

like image 87
xdurch0 Avatar answered Oct 21 '22 10:10

xdurch0