<p>As I said, in the loss function I used the scores (output values of network) for different inputs, and I compare them using the cross_entropy function.</p>

<pre><code>tf.nn.softmax_cross_entropy_with_logits(logits, labels, name=None)
</code></pre>

<p>This function computs the softmax of the logits, but naturally it doesn't compute them for the labels, so, in order to properly compare the scores of different inputs I need to wrap the "labels" with tf.nn.softmax() function, like this: (notice the last term)</p>

<pre><code>loss_function = tf.reduce_sum(alpha1 * weights_ll tf.nn.softmax_cross_entropy_with_logits(logits=g(input1), labels=tf.nn.softmax(g(input))
</code></pre>

<p>In case you are curious why I need to do something like this, and you are interested in Deep Learning, I invite you to read this interesting paper on Neural Graph Machines: <a href="https://arxiv.org/abs/1703.04818" rel="nofollow noreferrer">https://arxiv.org/abs/1703.04818</a></p>

<p>Also, there is another big problem with this model, that I don't share variables, so in practice with this loss function it's like training multiple networks for each term of the loss_function. </p>

<p>Luckily, there is a tutorial in the TensorFlow website that addresses Sharing Variables: <a href="https://www.tensorflow.org/programmers_guide/variable_scope" rel="nofollow noreferrer">https://www.tensorflow.org/programmers_guide/variable_scope</a> </p>
Tensorflow, placement of shared variable definitions
<p>I have a doubt about the definition of TensorFlow models. 
I am implementing a rather peculiar neural network model, where I need to access to the values of the output level for different inputs to compute the loss function... </p>

<p>So, I defined the layers of the neural network in a function like this:</p>

<pre><code>def g(input_x,....):
###...Convolutional and Fully Connected Layers...###
# Last FC Layer
with tf.name_scope("output"):
    W = tf.Variable(tf.truncated_normal([num_hidden_units, num_classes], stddev=0.05), name="W")
    b = tf.Variable(tf.constant(0.1, shape=[num_classes]), name="b")
    scores = tf.nn.xw_plus_b(fc_2_output, W, b, name="output")
return scores
</code></pre>

<p>And then in my model I have something like this:</p>

<pre><code>with tf.Graph().as_default():
    session_conf = tf.ConfigProto(
        allow_soft_placement=FLAGS.allow_soft_placement,
        log_device_placement=FLAGS.log_device_placement)
    sess = tf.Session(config=session_conf)
    with sess.as_default():
         ###definition of other variables, constants and placeholders###

         ###weird loss function with scores from different inputs###
         loss_function = tf.reduce_mean(alpha1 * weights_ll * tf.nn.softmax_cross_entropy_with_logits(logits=g(input1), labels=labels1) \
                        + cu1 * tf.nn.softmax_cross_entropy_with_logits(logits=g(input2), labels=g(input1) \
                        + cv1 * tf.nn.softmax_cross_entropy_with_logits(logits=g(input1), labels=labels2))) \
                        + ... + ...
        optimizer = tf.train.AdamOptimizer(1e-3).minimize(loss_function, global_step=global_step)
           ##...training steps and such...##
</code></pre>

<p>The training is working, but without running it for too long I am getting strange results, and I wonder if the weights defined in the g functions are being trained or they are somewhat out of scope of the optimizer.</p>

<p>Unfortunately, I am still learning a lot about tensorflow and I have no TensorBoard results to show you right now. </p>

<p>I just need to know from someone with a bit more experience if it is legal to define a model like this, using a python function for outputs.</p>

<p>Thank you very much for reading this far</p>