def log_sum_exp_index(x, axis, index): index = T.cast(index, dtype="float32") # 2D, time*batch index_bc = index.dimshuffle(*(range(index.ndim) + ['x'] * (x.ndim - index.ndim))) assert index_bc.ndim == x.ndim x_max = max_filtered(x, axis=axis, index=index) # we ignore the out-of-index frames x_max_bc = T.makeKeepDims(x, x_max, axis=axis) assert x.ndim == x_max_bc.ndim x_shift = (x - x_max_bc) * index_bc # filter out out-of-index. exp() could be inf otherwise return T.log(T.sum(T.exp(x_shift) * index_bc, axis=axis)) + x_max
def log_sum_exp_index(x, axis, index): index = T.cast(index, dtype="float32") # 2D, time*batch index_bc = index.dimshuffle(*(range(index.ndim) + ['x'] * (x.ndim - index.ndim))) assert index_bc.ndim == x.ndim x_max = max_filtered(x, axis=axis, index=index) # we ignore the out-of-index frames x_max_bc = T.makeKeepDims(x, x_max, axis=axis) assert x.ndim == x_max_bc.ndim x_shift = ( x - x_max_bc ) * index_bc # filter out out-of-index. exp() could be inf otherwise return T.log(T.sum(T.exp(x_shift) * index_bc, axis=axis)) + x_max
def log_sum_exp(x, axis): x_max = T.max(x, axis=axis) x_max_bc = T.makeKeepDims(x, x_max, axis=axis) return T.log(T.sum(T.exp(x - x_max_bc), axis=axis)) + x_max