You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
At the core of the neurodiffeq library is the diff(x, t) function, which computes the partial derivative ∂x/∂t evaluated at t. Usually, both tensor t and x have shapes of (n_samples, 1). When either x.shape or t.shape is malformed, however, there are cases where things could go wrong due to broadcasting. Such cases are so subtle that they have gone unnoticed for a long time.
All our generators (as defined in neurodiffeq.generator) currently return tensors with shapes (n_samples,) instead of (n_samples, 1). Efforts should be put into unifying the tensor shapes everywhere.
Here are two simple cases for review.
Case 1: Shapes don't matter
In this case, we try different combinations of x.shape and t.shape and check the shape of the output ∂x/∂t, namely:
[n, 1] and [n] --> [n]
[n] and [n]--> [n]
[n, 1] and [n, 1]--> [n,1]
[n] and [n, 1]--> [n,1]
To see this, run the following code. Note that d1, d2, d3, and d4, while having different shapes, hold the same values. This is the reason why we incorrectly believed in the soundness of the diff() function.
In this second case, we examine two new operators – div and curl in spherical coordinates – and show that only when x.shape and t.shape are both (n, 1) will the vector identity div(curl(...)) == 0 hold.
We then test the vector identity div(curl(q)) == 0 for q
n=10# create r, theta, and phi with shape (n, 1)r=torch.rand(n, 1, requires_grad=True) +0.1theta=torch.rand(n, 1, requires_grad=True) *np.piphi=torch.rand(n, 1, requires_grad=True) *np.pi*2q_r, q_theta, q_phi=compute_q(r, theta, phi)
# bind the operators to the r, theta, phi created abovediv=lambdau_r, u_theta, u_phi: spherical_div(u_r, u_theta, u_phi, r, theta, phi)
curl=lambdau_r, u_theta, u_phi: spherical_curl(u_r, u_theta, u_phi, r, theta, phi)
div_curl_q1=div(*curl(q_r.reshape(-1, 1), q_theta.reshape(-1, 1), q_phi.reshape(-1, 1)))
div_curl_q2=div(*curl(q_r.reshape(-1), q_theta.reshape(-1), q_phi.reshape(-1)))
# create r, theta, and phi with shape (n,)r=r.reshape(-1)
theta=r.reshape(-1)
phi=r.reshape(-1)
q_r, q_theta, q_phi=compute_q(r, theta, phi)
# bind the operators to the r, theta, phi created abovediv=lambdau_r, u_theta, u_phi: spherical_div(u_r, u_theta, u_phi, r, theta, phi)
curl=lambdau_r, u_theta, u_phi: spherical_curl(u_r, u_theta, u_phi, r, theta, phi)
div_curl_q3=div(*curl(q_r.reshape(-1, 1), q_theta.reshape(-1, 1), q_phi.reshape(-1, 1)))
div_curl_q4=div(*curl(q_r.reshape(-1), q_theta.reshape(-1), q_phi.reshape(-1)))
print(div_curl_q1, div_curl_q2, div_curl_q3, div_curl_q4, sep="\n")
Printing all four div_curl_qs will show that, only div_curl_q1 is (approximately) equal to 0, which means both the dependent and independent variables must have shape (n, 1) for the differentiation to go correctly.
The text was updated successfully, but these errors were encountered:
Rethink the
diff
functionAt the core of the
neurodiffeq
library is thediff(x, t)
function, which computes the partial derivative∂x/∂t
evaluated att
. Usually, both tensort
andx
have shapes of(n_samples, 1)
. When eitherx.shape
ort.shape
is malformed, however, there are cases where things could go wrong due to broadcasting. Such cases are so subtle that they have gone unnoticed for a long time.All our generators (as defined in
neurodiffeq.generator
) currently return tensors with shapes(n_samples,)
instead of(n_samples, 1)
. Efforts should be put into unifying the tensor shapes everywhere.Here are two simple cases for review.
Case 1: Shapes don't matter
In this case, we try different combinations of
x.shape
andt.shape
and check the shape of the output∂x/∂t
, namely:[n, 1]
and[n]
-->[n]
[n]
and[n]
-->[n]
[n, 1]
and[n, 1]
-->[n,1]
[n]
and[n, 1]
-->[n,1]
To see this, run the following code. Note that
d1
,d2
,d3
, andd4
, while having different shapes, hold the same values. This is the reason why we incorrectly believed in the soundness of thediff()
function.Case 2: Shapes matter
In this second case, we examine two new operators –
div
andcurl
in spherical coordinates – and show that only whenx.shape
andt.shape
are both(n, 1)
will the vector identitydiv(curl(...)) == 0
hold.Here is the definition of curl and divergence in spherical coordinates
Here we define a vector field
q
by specifying the rule to computeq
given coordinates(r, theta, phi)
We then test the vector identity
div(curl(q)) == 0
for qPrinting all four
div_curl_q
s will show that, onlydiv_curl_q1
is (approximately) equal to 0, which means both the dependent and independent variables must have shape(n, 1)
for the differentiation to go correctly.The text was updated successfully, but these errors were encountered: