0

I have had some experience with creating Neural networks graphs with input as tensorflow placeholders . Until now , i used to believe that those graphs could be evaluated with something like sess.run() or more precisely as described here.

I was learning to see how a GAN works and came across this tutorial where the author creates a function(time 11:00 in the video) :

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import time

def make_discriminator_model():
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.Conv2D(7,(3,3) , padding = "same" , input_shape = (28,28,1)))
    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.LeakyReLU())
    model.add(tf.keras.layers.Dense(50,activation = 'relu'))
    model.add(tf.keras.layers.Dense(1))
    return model 

And then evaluates a forward pass as follows :

model_discriminator = make_discriminator_model()
model_discriminator(np.random.rand(1,28,28,1).astype("float32"))

He gets the following output :

<tf.Tensor: id=161  ,shape=(1,1) ,dtype=float32 , numpy=array([[0.01451516]],dtype = float32)>

The value numpy=array([[0.01451516]] is the output of the forward pass. On running the same code , i get a less infromative tensor which is :

<tf.Tensor 'sequential_5/dense_11/BiasAdd:0' shape=(1, 1) dtype=float32>

Is the difference due to difference version of tensorflow in the environment ? I am using tensorflow 1.14.0 , not sure about the one used in the video.

warrior_monk
  • 383
  • 2
  • 14
  • This is the default behaviour of TF 2.0. As long as they are not a part of a function decorated with `@tf.function`, Tensors and Numpy arrays are interchangeable. – Susmit Agrawal Mar 18 '20 at 12:30

1 Answers1

1

Yes, this is because of the different versions of the tensorflow.

In tensorflow version 1.14 the output on running your code is as below(have added the print of tf version),

tensorflow version: 1.14.0
<tf.Tensor 'sequential_3/dense_7/BiasAdd:0' shape=(1, 1) dtype=float32>

If you would like to extract the value in tensorflow version 1.14, then modify the code as below,

#!pip install tensorflow==1.14

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import time

print("tensorflow version:",tf.__version__)

def make_discriminator_model():
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.Conv2D(7,(3,3) , padding = "same" , input_shape = (28,28,1)))
    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.LeakyReLU())
    model.add(tf.keras.layers.Dense(50,activation = 'relu'))
    model.add(tf.keras.layers.Dense(1))
    return model 

model_discriminator = make_discriminator_model()

#initialize the variable
init_op = tf.initialize_all_variables()

#run the graph
with tf.Session() as sess:
    sess.run(init_op) #execute init_op
    print("Value of the model_discriminator function:",sess.run(model_discriminator(np.random.rand(1,28,28,1).astype("float32"))))

Output will be -

tensorflow version: 1.14.0
Value of the model_discriminator function: [[0.00674586]]

While in tensorflow version 2.1 the output on running your code is as below(have added the print of tf version),

tensorflow version: 2.1.0
<tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[-0.01521136]], dtype=float32)>
  • @warrior_monk - If you would like understand difference between normal tensor and symbolic tensor then you can view this link https://stackoverflow.com/questions/60338842/how-to-print-value-of-tensorflow-python-framework-ops-tensor-in-tensorflow-2-0/60564318#60564318 –  Mar 19 '20 at 04:18