user2129910 user2129910 - 3 years ago 81
Python Question

Tensorflow define a operation that builds the product of all tensor components

I want to define a operation in tensorflow that calculates something like:

enter image description here

x is provided by a tensor. Finally the operation should be compared to a known value and parameters alpha, beta i and b should be learned.

(I guess) The product of all inputs causes trouble. This is one version that I tried to deploy, with no success.
# input
X = tf.placeholder(tf.float32, [None, 2], name="X")
Y = tf.placeholder(tf.float32, [None, 1], name="Y")

# hidden
beta = tf.get_variable("beta", shape=[2], initializer=tf.contrib.layers.xavier_initializer())
powered = tf.pow(X,beta)
productLayer = tf.contrib.keras.layers.multiply(powered) # bad line

# output
w_o = tf.get_variable("w_o", shape=[1], initializer=tf.contrib.layers.xavier_initializer())
b_o = tf.get_variable("bias", shape=[1], initializer=tf.zeros([1]))

output = tf.add(tf.matmul(productLayer,w_o), b_o)
loss = tf.reduce_sum(tf.square(output - Y)) # tf.nn.l2_loss(yhat - Y)


Running the full script from gist
https://gist.github.com/anonymous/c17d45b4e997bfccb5275dffa44512d6
is resulting in the error message:


File "h2o_test_opti.py", line 13, in
productLayer = tf.contrib.keras.layers.multiply(powered) ValueError: A merge layer should be called on a list of inputs.


I thought the functionality describtion of tf.contrib.keras.layers.multiply fits to my needs. I also tried to find a naive way like a for-loop to calculate the product of all incoming tensor-elements, but with no success, as I couldn't imagine a way to access the tensor in a right way. Choosing the correct indicies isn't(?) possible, as I don't know the current step and therefore the right tensor to be treated?

I want to test this as an "activation-function" (properly more as optimization/fitting procedure)

Please let me know if there is more information required to help with this problem.

Answer Source

I found an working solution to my idea by changing:

    productLayer = tf.contrib.keras.layers.multiply(powered) # bad line

to:

    productLayer = tf.reshape(tf.reduce_prod(X,1), (-1,1))

It should work. Maybe someone can use this too.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download