Hi,
so I’m fairly new to Julia coming from Python.
I was playing around with the ForwardDiff package and stumbled upon the following ‘problem’:
A 2-D Gaussian from the Distributions package can take a matrix of size = (2, N) and outputs N probabilities.
Now I wanted to evaluate the gradient of multiple data points similar to PyTorch but apparently the gradient can only be evaluated for single data vectors and not data matrices.
Here is a MWE of my ‘problem’:
using Distributions
using Plots
using ForwardDiff
μ = [ 1. ; 2.]
Σ = [ 1. 0 ; 0 1]
dist = MvNormal(μ, Σ)
# samples = rand(dist, 2000)
# scatter(samples[1,:], samples[2,:]) for visualization purposes
matrix = [ 1 1 ; 2. 2]
vec = [ 1. ; 2.]
prob_vec = pdf(dist, vec) # evaluates to scalar value
prob_matrix = pdf(dist, matrix) # evaluate to array with two values like it should
grad_vec = ForwardDiff.gradient(x -> pdf(dist, x), vec) # evaluates to gradient with two 0's as it should bc of mean position
grad_matrix = ForwardDiff.gradient(x -> pdf(dist, x), matrix) # doesnt work
So is there any way to evaluate the gradient of multiple data points or do I really have to put in a for loop?
Or vectorize it with the dot operator although I haven’t figured out how to do that.
Thank you in advance for your time and effort! =)
PS: This was the error message
ERROR: MethodError: no method matching extract_gradient!(::Type{ForwardDiff.Tag{getfield(Main, Symbol(“##31#32”)),Float64}}, ::Array{Array{ForwardDiff.Dual{ForwardDiff.Tag{getfield(Main, Symbol(“##31#32”)),Float64},Float64,4},1},2}, ::Array{ForwardDiff.Dual{ForwardDiff.Tag{getfield(Main, Symbol(“##31#32”)),Float64},Float64,4},1})