[theano-users] Complex if/else operations in a custom loss function on 3-dimensional tensors

2017-01-13 Thread Corey Nolet
I am currently implementing the fully convolutional regression network 
which is outlined in detail in the paper "Synthetic Data for Text 
Localisation in Natural Images" by Ankush Gupta et al. 

I've got the network model compiled using the Keras API and I'm trying to 
implement the custom loss layer. This network outputs a tensor which is 
7x16x16 and each of the 7 output values for the 16x16 output feature maps 
represents a different regressed value that ultimately helps to predict the 
possibility, size, and location of text in each convolutional cell. The 
part that's confusing me is implementing the special operations that need 
to be performed in the loss function. One of the 7 regressed output values, 
let's call it "c", represents the probability of a cell to contain text. 
This is used to help determine whether or not the loss of the other 6 
output values even get back propagated. The rules in the custom loss 
function are as follows:

1. If the value 'c' is 0 (meaning there is no text in the current cell), 
the loss of the other 6 values is ignored completely (I'm assuming this 
means it's set to 0 and nothing is actually back-propagated, right?) and 
the MSE of the 'c' value is back-propagated.
2. If the value 'c' is 0, the MSE loss of the 'c' value is discounted. At 
the beginning of training, only 1% of the loss is back-propagated and this 
value is steadily increased throughout training until 100% of the loss is 
back propagated.

So, since my output tensor is 7x16x16, I believe this means I have 7 
different 16x16 matrices (feature maps) by which to specify the loss. I 
have an easier time thinking about a tensor that's 16x16x7 (which means 
each cell in the 16x16 matrix has 7 values) and I'm not exactly sure how to 
approach coding up the 2 rules that I specified because it looks like I 
need to specify them as lazily evaluated theano tensor functions instead of 
being able to loop through the tensors myself to apply the if/then/else 
logic. 

It would be helpful to be pointed in the right direction. I've looked at 
the documentation for the theano tensor functions already and I don't think 
I'm understanding exactly what a sequence of those functions may look like 
to help solve my problem. I can see there being an IfElse function in there 
but I'm not exactly sure how I would do the logic for the two steps I 
outlined above using this method. 

Thank you in advance for any examples, suggestions, or insight you are able 
to provide. 

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to theano-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [theano-users] Optimize Sum(Elemwise)

2017-01-13 Thread Frédéric Bastien
Hi,

We have this in mind for a long time, but we didn't had the time to do it.

The simplest, but not the most versatile version is to make a new op with
new c code. Then in it, you suppose the input is c contiguous. If not, you
call a numpy function that make it c contiguous or raise an error.

Do you need arbitrary elemwise support? If not, it make it event easier to
implement.

We don't have time for this now. Do you want to try that simpler version?
We have good tutorial how to make a new op in Theano with c code.

Fred

Le jeu. 12 janv. 2017 16:37, Adrian Seyboldt  a
écrit :

> Hallo,
>
> I've been using theano through pymc3 a lot recently and came across a
> hopefully realistic and useful optimization that theano does not seem
> to do. Most theano graphs of models I've seen contain the combination
> of an Elemwise Node and after that a Sum node. Often a lot of time
> is spend in the Sum nodes (40% of the total run time in some of my real
> world models). As far as I understand it, this means that theano stores
> an array of the result of the Elemwise operation, and then computes the
> sum of the elements in that array. It would save a lot of time and
> memory bandwidth to compute the sum on the fly, possibly in parallel
> with an openmp reduce operation.
>
> This notebook illustrates the problem:
>
> https://nbviewer.jupyter.org/gist/aseyboldt/777fc6e73f6444da14da03818352c96f
>
> I did not find a way to do this faster as it is, did I miss something?
>
> I thought about writing an op like SumElemwise (or more general
> CAReduceElemwise) and an optimization that changes `Elemwise -> Sum`
> into `SumElemwise`. I guess SumElemwise could be pretty similar to
> Elemwise, at least for cases where we sum over all dimensions. But
> while reading the code of Elemwise I realized that I really don't know
> enough about the internals of theano to pull this off myself. If anyone
> feels like spending time on this, they would have my eternal
> gratitude. ;-)
>
> -- Adrian
>
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "theano-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to theano-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to theano-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.