2

I am a beginner of tensorflow. I would like to make the hidden layer size configurable. Let us say there is just one hidden layer, I would like

hidden1 = tf.layers.dense(X, n_hidden, activation=tf.nn.relu)

where 'n_hidden' is

n_hidden = tf.placeholder(tf.int64)

but I got a long error messages, at the end it is,

TypeError: int() argument must be a string or a number, not 'Tensor'

I searched for a while, most code examples have this output unit set by a constant or an int variable. How do I make it an configurable parameter of the program? Thanks.

Veydan Wu
  • 51
  • 1
  • 4
  • as mentioned [here](https://www.tensorflow.org/api_docs/python/tf/layers/dense), u need to sent a numeric value for 'units' attribute, instead of a tensor. Change your code to something like `n_hidden = 20` , and u would have a dense network with output dim 20. – skadoosh Nov 09 '17 at 10:32

2 Answers2

0

You can have it an input parameter of your program (as an int, like in this question) but for a given graph, this parameter is fixed so it can't be a placeholder or result of another computation. If you want to change the number of hidden units, you have to recreate the graph.

etarion
  • 16,935
  • 4
  • 43
  • 66
  • Probably not a command line input parameter, but a configurable parameter that can be changed at runtime. For example, I can have a loop, iterate over a set of possible layer size, run the graph with different layer size one at a time. – Veydan Wu Oct 29 '17 at 23:27
-2

According to the documentation,

enter image description here

where,

units: Integer or Long, dimensionality of the output space.

In your code, n_hidden should specify the number of units in the hidden layer. Therefore, it should be a number, not a tensor. Therefore, tf.layers.dense creates the hidden layer according to the number of units specified.

Nipun Wijerathne
  • 1,839
  • 11
  • 13