Automatic differentiation
The user must provide the appropriate tensor symmetry information; otherwise, automatic differentiation may return unexpected values. In the following example, even with identical tensor values, the results vary depending on the Tensor
type.
julia> A = rand(Mat{3,3})
3×3 Tensor{Tuple{3, 3}, Float64, 2, 9}: 0.325977 0.894245 0.953125 0.549051 0.353112 0.795547 0.218587 0.394255 0.49425
julia> S = A * A' # `S` is symmetric but not of the type `SymmetricSecondOrderTensor`
3×3 Tensor{Tuple{3, 3}, Float64, 2, 9}: 1.81438 1.253 0.894897 1.253 1.05904 0.65243 0.894897 0.65243 0.4475
julia> gradient(identity, S) ≈ one(FourthOrderTensor{3})
true
julia> gradient(identity, symmetric(S)) ≈ one(SymmetricFourthOrderTensor{3})
true
Tensorial.gradient
— Functiongradient(f, x)
gradient(f, x, :all)
Compute the gradient of f
with respect to x
by the automatic differentiation. If pseudo keyword :all
is given, the value of f(x)
is also returned.
Examples
julia> x = rand(Mat{3,3})
3×3 Tensor{Tuple{3, 3}, Float64, 2, 9}:
0.325977 0.894245 0.953125
0.549051 0.353112 0.795547
0.218587 0.394255 0.49425
julia> gradient(tr, x)
3×3 Tensor{Tuple{3, 3}, Float64, 2, 9}:
1.0 0.0 0.0
0.0 1.0 0.0
0.0 0.0 1.0
julia> ∇f, f = gradient(tr, x, :all)
([1.0 0.0 0.0; 0.0 1.0 0.0; 0.0 0.0 1.0], 1.1733382401532275)
Tensorial.hessian
— Functionhessian(f, x)
hessian(f, x, :all)
Compute the hessian of f
with respect to x
by the automatic differentiation. If pseudo keyword :all
is given, the value of f(x)
is also returned.
Examples
julia> x = rand(Vec{3})
3-element Vec{3, Float64}:
0.32597672886359486
0.5490511363155669
0.21858665481883066
julia> hessian(norm, x)
3×3 Tensor{Tuple{3, 3}, Float64, 2, 9}:
1.13603 -0.582196 -0.231782
-0.582196 0.501079 -0.390397
-0.231782 -0.390397 1.32626
julia> ∇∇f, ∇f, f = hessian(norm, x, :all)
([1.1360324375454411 -0.5821964220304534 -0.23178236037013888; -0.5821964220304533 0.5010791569244991 -0.39039709608344814; -0.23178236037013886 -0.39039709608344814 1.3262640626479867], [0.4829957515506539, 0.8135223859352438, 0.3238771859304809], 0.6749059962060727)