Tensorflow, py_func, or custom function
<p>I'm currently working on a quaternionic Neural Network using Tensorflow (I want to use GPUs). TensorFlow doesn't have support for quaternions, <em>but</em> you can represent than as a 4x4 real matrix, so it might be possible to build such a neural network in TensorFlow.</p>

<p>Is there a simple way to add a custom operation or to do a custom operation on tensors?</p>

<p>For example, I can write: </p>

<pre><code>output_activation = tf.nn.softmax(tf.matmul(hidden_activation, Weight_to_ouput))
</code></pre>

<p>...and that's pretty cool! All you have to do is add a loss function and then do backpropagation. However, I want to do the same thing but with quaternions, for example:</p>

<pre><code>output_activation = mySigmoid(myFunction(hidden_activation, Weight_to_output))
</code></pre>

<p>However, I need to transform the quaternions to and from tensors to optimize the GPU calculation. So I need to create a function that gets some tensors as parameters and returns the transformed tensors. 
I've looked at <code>py_func</code>, but it seems that you can't return tensors.</p>

<p>I tried the following, but it failed:</p>

<pre><code>def layerActivation(inputTensor,WeightTensor):
    newTensor = tf.matmul(inputTensor,WeightTensor)
    return newTensor
</code></pre>

<p>...and in <code>main()</code>:</p>

<pre><code>x = placeholder ...
W_to_hidden = tf.Variable
test = tf.py_func(layerActivation, [x,_W_to_hidden], [tf.float32])

with tf.Session() as sess:
    tf.initialize_all_variables().run()
    king_return = sess.run(test, feed_dict={x: qtrain})
</code></pre>

<p>Error : Unimplemented: Unsupported object type Tensor</p>

<p>Ideally I could use this <code>output_activation</code> in the standard backprop algorithm of TensorFlow but I don't know if it's possible.</p>