Search code examples
tensorflowtensorboardtensorflow-datasetstensorflow-estimator

Evaluating Tensorflow Tensors


to get the gradients of the output with respect to the input, one can use

grads = tf.gradients(model.output, model.input)

where grads =

[<tf.Tensor 'gradients_81/dense/MatMul_grad/MatMul:0' shape=(?, 18) dtype=float32>]

This is a modell, where there are 18 continous inputs and 1 continous output.

I assume, this is a symbolic expression and that one needs a list of 18 entries to feed it to the tensor, such that it gives out the derivatives as floats.

I would use

Test =[1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0]
with tf.Session() as sess:
    alpha = sess.run(grads, feed_dict = {model.input : Test})
    print(alpha)

But I get the error

FailedPreconditionError (see above for traceback): Error while reading resource variable dense_2/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/dense_2/bias)
     [[Node: dense_2/BiasAdd/ReadVariableOp = ReadVariableOp[dtype=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](dense_2/bias)]]

What is wrong?

EDIT: This is, what has happened before:

def build_model():
    model = keras.Sequential([ 
            ...])
    optimizer = ...
    model.compile(loss='mse'... ) 
    return model 


model = build_model()
history= model.fit(data_train,train_labels,...)
loss, mae, mse = model.evaluate(data_eval,...)

Progress so far:

Test =[1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0]

with tf.Session() as sess:
    tf.keras.backend.set_session(sess)
    tf.initializers.variables(model.output)
    alpha = sess.run(grads, feed_dict = {model.input : Test})

is also not working, giving the error:

TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed. Use `if t is not None:` instead of `if t:` to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.

Solution

  • You're trying to use uninitialized variable. All you have to do is add

    sess.run(tf.global_variables_initializer()) 
    

    right after with tf.Session() as sess:

    Edit: You need to register session with Keras

    with tf.Session() as sess:
        tf.keras.backend.set_session(sess)
    

    And use tf.initializers.variables(var_list) instead of tf.global_variables_initializer()

    See https://blog.keras.io/keras-as-a-simplified-interface-to-tensorflow-tutorial.html

    Edit:

    Test = np.ones((1, 18), dtype=np.float32)
    
    inputs = layers.Input(shape=[18,])
    layer = layers.Dense(10, activation='sigmoid')(inputs)
    model = tf.keras.Model(inputs=inputs, outputs=layer)
    model.compile(optimizer='adam', loss='mse')
    checkpointer = tf.keras.callbacks.ModelCheckpoint(filepath='path/weights.hdf5')
    model.fit(Test, nb_epoch=1, batch_size=1, callbacks=[checkpointer])
    grads = tf.gradients(model.output, model.input)
    
    with tf.Session() as sess:
        tf.keras.backend.set_session(sess)
        sess.run(tf.global_variables_initializer())
        model.load_weights('path/weights.hdf5')
        alpha = sess.run(grads, feed_dict={model.input: Test})
        print(alpha)
    

    This shows consistent result