How can I add an optional input to a graph in TensorFlow?

Lenar Hoyt

I basically want to have the option to feed input to the middle of the graph and compute the output going from there. One idea I had is to use tf.placeholder_with_default that defaults to a zero tensor. Then I could mix the optional inputs using addition, however addition on a large shape this seems to be a lot of unnecessary computation. Are there better ways of accomplishing that?

input_enabled = tf.placeholder_with_default(tf.constant(1.), [1])

input_shape = [None, in_size]
input = tf.placeholder_with_default(tf.zeros(input_shape), input_shape)
// ...
bottleneck_shape = [None, bottleneck_size]
bottleneck = input_enabled * f(prev_layer) + tf.placeholder_with_default(tf.zeros(bottleneck_shape), bottleneck_shape)
// ...

// Using graph with input at first layer:
sess.run([output], feed_dict={input: x})

// Using graph with input at bottleneck layer:
sess.run([output], feed_dict={bottleneck: b, input_enabled: 0.})
Olivier Moindrot

I understand better thanks to your code.

Basically the schema is:

       input       <- you can feed here
         |        
     (encoder)
         |
     bottleneck    <- you can also feed here instead
         |
     (decoder)
         |
       output

You want two use cases:

  1. train: feed an image into input, compute the output
  2. test: feed a code into the bottleneck, compute the output

You don't need to create a placeholder for bottleneck, because sess.run() allows you to feed values to non placeholders in the Graph:

input_shape = [None, in_size]
input = tf.placeholder(tf.float32, input_shape)
# ...

bottleneck = f(prev_layer)  # of shape [None, bottleneck_size]
# ...

# Using graph with input at first layer:
sess.run([output], feed_dict={input: x})

# Using graph with input at bottleneck layer:
sess.run([output], feed_dict={bottleneck: b})

From the documentation of sess.run():

The optional feed_dict argument allows the caller to override the value of tensors in the graph. Each key in feed_dict can be one of the following types:

If the key is a Tensor, the value may be a Python scalar, string, list, or numpy ndarray that can be converted to the same dtype as that tensor. Additionally, if the key is a placeholder, the shape of the value will be checked for compatibility with the placeholder.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I create an optional DateTime parameter?

From Dev

How can I add optional callbacks to a typescript function?

From Dev

How can I add optional named parameters to a TypeScript function parameter?

From Dev

How can I make part of regex optional?

From Dev

How can I add the 'required' attribute to input on change of textarea?

From Dev

How can I map an optional into a primitive optional?

From Dev

How can i add variables to my List by reading the User Input?

From Dev

How can I document optional parameters

From Dev

How can I add an unremovable postfix to an HTML input field?

From Dev

Can I query for an optional field in the Facebook Graph API?

From Dev

How can I execute a TensorFlow graph from a protobuf in C++?

From Dev

How to add if condition in a TensorFlow graph?

From Dev

Tensorflow : how to insert custom input to existing graph?

From Dev

How can I add array of object to html list by input form?

From Dev

How can I map Optional to another Optional if not present?

From Dev

How can i add a While loop to detect errors in the input in python

From Dev

How can I add attribute inside an input tag using jQuery?

From Dev

How can I add optional named parameters to a TypeScript function parameter?

From Dev

How can I add to the end of the string that already exists in input field?

From Dev

how can i add dynamic labels and text input field in xcode

From Dev

How can I add edges to my graph in a for loop?

From Dev

How can I add text to the end of user input?

From Dev

How I can add attribute to certain type of input

From Dev

Alamofire prints "Optional(data)", how can I get rid of "Optional"

From Dev

OxyPlot: How can I add text to a graph?

From Dev

How can I add input field and select dynamically?

From Dev

How to build a reusable graph with input and output variables in Tensorflow?

From Dev

How can I save and restore a graph that uses Tensorflow Hub Module

From Dev

How can I add a fonticon after an input field?

Related Related

  1. 1

    How can I create an optional DateTime parameter?

  2. 2

    How can I add optional callbacks to a typescript function?

  3. 3

    How can I add optional named parameters to a TypeScript function parameter?

  4. 4

    How can I make part of regex optional?

  5. 5

    How can I add the 'required' attribute to input on change of textarea?

  6. 6

    How can I map an optional into a primitive optional?

  7. 7

    How can i add variables to my List by reading the User Input?

  8. 8

    How can I document optional parameters

  9. 9

    How can I add an unremovable postfix to an HTML input field?

  10. 10

    Can I query for an optional field in the Facebook Graph API?

  11. 11

    How can I execute a TensorFlow graph from a protobuf in C++?

  12. 12

    How to add if condition in a TensorFlow graph?

  13. 13

    Tensorflow : how to insert custom input to existing graph?

  14. 14

    How can I add array of object to html list by input form?

  15. 15

    How can I map Optional to another Optional if not present?

  16. 16

    How can i add a While loop to detect errors in the input in python

  17. 17

    How can I add attribute inside an input tag using jQuery?

  18. 18

    How can I add optional named parameters to a TypeScript function parameter?

  19. 19

    How can I add to the end of the string that already exists in input field?

  20. 20

    how can i add dynamic labels and text input field in xcode

  21. 21

    How can I add edges to my graph in a for loop?

  22. 22

    How can I add text to the end of user input?

  23. 23

    How I can add attribute to certain type of input

  24. 24

    Alamofire prints "Optional(data)", how can I get rid of "Optional"

  25. 25

    OxyPlot: How can I add text to a graph?

  26. 26

    How can I add input field and select dynamically?

  27. 27

    How to build a reusable graph with input and output variables in Tensorflow?

  28. 28

    How can I save and restore a graph that uses Tensorflow Hub Module

  29. 29

    How can I add a fonticon after an input field?

HotTag

Archive