Combining scalars and vectors in Theano to calculate Hessian

I am trying to use Theano to calculate a hessian function with respect to a vector, as well as a couple of scalars (edit: that is, in fact, I want scalars to be added to the vector, which I calculate hessian with respect to). Here is a minimal example:

import theano
import theano.tensor as T
A = T.vector('A')
b,c = T.scalars('b','c')
y = T.sum(A)*b*c

My first attempt:

hy = T.hessian(y,[A,b,c])

What fails with AssertionError: tensor.hessian expects a (list of) 1 dimensional variable as 'wrt'

My second attempt was to combine A, b, and c with:

wrt = T.concatenate([A,T.stack(b,c)])
hy = T.hessian(y,[wrt])

What fails with DisconnectedInputError: grad method was asked to compute the gradient with respect to a variable that is not part of the computational graph of the cost, or is used only by a non-differentiable operator: Join.0

What is the correct way to calculate hessian in this case?

Update. To clarify what I'm looking for, suppose A is a 2-element vector. Then the Hessian will be:

[[d2y/d2A1, d2y/dA1dA2, d2y/dA1dB, d2y/dA1dC],
[d2y/dA2dA1, d2y/d2A2, d2y/dA2dB, d2y/dA2dC],
[d2y/dBdA1, d2y/dBdA2, d2y/d2B, d2y/dABdC],
[d2y/dCdA1, d2y/dCdA2, d2y/dCdB, d2y/d2C]]

which for example function yshould be:

[[0, 0, C, B],
[0, 0, C, B],
[C, C, 0, A1+A2],
[B, B, A1+A2, 0]]

So, if we defined a function:

f = theano.function([A,b,c], hy)

then, assuming that we could successfully calculate hy, we expect the result:

f([1,1], 4, 5) = 
    [[0, 0, 5, 4],
    [0, 0, 5, 4],
    [5, 5, 0, 2],
    [4, 4, 2, 0]]

A 25 y , .

+4
2

b,c , . hessian 1D . , , , , .

, stack non-endnode , . .

:

import theano.tensor as T
A = T.vector('A')
b,c = T.vectors('b','c')
y = T.sum(A)*b[0]*c[0]

hy = T.hessian(y,[A,b,c])
+1

@eickenberg numpy, :

import theano
import theano.tensor as T

A,temp = T.vectors('A','T')
b,c = T.scalars('b','c')

y = T.sum(A)*b*c
y2 = theano.clone(y,{A:temp[:-2],b:temp[-2],c:temp[-1]})

hy = T.hessian(y2,[temp])
f = theano.function([temp], hy)

f([1,1,4,5])

:

> [array([[ 0.,  0.,  5.,  4.],
>         [ 0.,  0.,  5.,  4.],
>         [ 5.,  5.,  0.,  2.],
>         [ 4.,  4.,  2.,  0.]])]

, , - ( ) , , .

+1

Source: https://habr.com/ru/post/1619735/


All Articles