필터 지우기
필터 지우기

Numerical answer to Partial derivative of Hopfield-style Energy in symbolic expression

조회 수: 2 (최근 30일)
Hello,
I'm implementing a gradient descent optimizer that uses Hopfield-style energy as function,
the hopfield-style energy is a function of p:
The hopfield energy equation can also be expressed as:
The gradient vector is defined as:
I'm attempting to calculate the gradient vector using the Symbolic Toolbox as follows:
r_mat = sym('r',[4,4]);
r_mat = r_mat(:);
p_mat = sym('p',[4*4,1]);
c_mat = sym('c',[4*4,1]);
Energy = -0.5*p_mat'*r_mat*p_mat' - c_mat'*p_mat;
%the first partial derivative:
q1 = -1*diff(testi1,p_mat(1,1));
which yields as the first term:
conj(c1) + r1_1*conj(p1) + (r1_2*conj(p5))/2 + (r1_3*conj(p9))/2 + (r2_1*conj(p2))/2 + (r1_4*conj(p13))/2 + (r2_2*conj(p6))/2 + (r2_3*conj(p10))/2 + (r3_1*conj(p3))/2 + (r2_4*conj(p14))/2 + (r3_2*conj(p7))/2 + (r3_3*conj(p11))/2 + (r4_1*conj(p4))/2 + (r3_4*conj(p15))/2 + (r4_2*conj(p8))/2 + (r4_3*conj(p12))/2 + (r4_4*conj(p16))/2
How can I replace the symbols with numbers to yield the q vector? I need the q-vector for the gradient descent
the algorithm I'm implementing: https://doi.org/10.1117/1.JEI.23.1.013007 (relevant part starts at Equation 5)

채택된 답변

Torsten
Torsten 2022년 7월 28일
편집: Torsten 2022년 7월 28일
p is (4x1), not (16x1).
And keep r_mat as matrix, then
E = -0.5*p.'*r_mat*p - c.'*p
( r_mat can be assumed symmetric ).
By the way:
dE/dp = -r_mat*p - c
assuming r_mat is symmetric.
To replace symbols by numerical values, use "subs".
  댓글 수: 5
Pseudoscientist
Pseudoscientist 2022년 8월 2일
It turned out symbolic expressions was extremely slow, I used symbolic to calculate the partial derivative and then used that to think how to write the equation in vector form and was evetually succesful and the optimizer is now very fast !
Torsten
Torsten 2022년 8월 2일
I used symbolic to calculate the partial derivative and then used that to think how to write the equation in vector form
That's the best you can do, I guess.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Assumptions에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by