If I use the 2 x 10 array then I already have 20 x 20 matrix! Can I put the result in the other dimension?
Because my neural network will be very slow as I use hessian in the loss function
The result you got is correct. sum computes the sum of all elements, so your function x -> sum(x.^3) is indeed defined as \mathbb{R}^4\rightarrow\mathbb{R} and hence the hessian is a 4\times 4 matrix. If you feed to your function a 2\times 10 matrix, the function is \mathbb{R}^{20}\rightarrow\mathbb{R} and hence the hessian will be 20\times 20. In general if your function is \mathbb{R}^n\rightarrow\mathbb{R}, the hessian will be a n\times n matrix.
This makes me think you actually have a \mathbb{R}^2\rightarrow\mathbb{R} function and you want to evaluate the hessian at n distinct points (e.g if your array is (2,10) at 10 points) is this what you want to do?
A possible way I can think of is the following (there might be better ones)