currently I am trying to integrate some TensorFlow neural Networks into NGSolve models for nonlinear mechanics. Basically I want to remodel the Nonlinear Elasticity Example but instead of using some closed formula I want to use my trained Neural Network to compute the Potential (W) from the material deformation (The Green-Langrangian Deformation tensor E).
Now I figured I cannot use my Network directly to create a function, that NGSolve can compile, but instead I need to remodel my Network using the weight matrizes and activation functions. The problem I now have is the twofold:
I want to multiply a vector (lets say size 6) and a weight matrix (lets say size 6*32) to return a vector of size 32. The vector is a CoefficientFunction and the matrix is a (constant) numpy array. I tried using the python (numpy?) operator for matrix multiplication
@ The Operator used in the NGSolve example to create the right ChaucyGreen tensor
C = F.trans * F The numpy matrix multiplication method
numpy.matmul() and tensorproduct method
numpy.tensordot() The NGSolve
InnerProduct() method, all resulting in various errors.
I have attached my trial file
https://ngsolve.org/media/kunena/attachments/1454/trials.py so you can better understand, what I want to do.
I hope you can give me some hints how to solve my problem.
Thanks a lot!!
Vector and matrix valued CoefficientFunctions can be created using python tuples. If you use lists, domainwise coefficients are created, see:
E_star = CF(tuple(E_dummy), dims=(1,6))
test = np.random.random((6,32))
testCF = CF(tuple(test.flatten()), dims=(6,32))
Potential = E_star * testCFz
Your symbolic form should be scalar though.
that helped me a lot, thank you for your quick answer!
I just tried to get it working on a first multiplication, so my symbolic form is getting scalar in the end.
I am sorry that I have to ask some questions again…
While all the Computation itself works perfectly fine, I encountered some problems when trying to compute the stresses as derivatives of the Potential, when trying to compute the derivative of the Potential computed by Matrix-Multiplication I get the following error:
File “trials_ngForum.py”, line 94, in
P = PK_stress(gfu)
File “trials_ngForum.py”, line 45, in PK_stress
P = W.Diff(F)
NgException: Matrix dimensions don’t fit: m1 is 1 x 32, m2 is 1 x 32[/code]
I tried to look further into this, but W seems to get computed just fine from the GridFunction, the point where it starts to make problems is the differentiation and trying to get into the Diff function in debuging does not work. I am also not really getting ahead of the error message since at no point in the computation itself two matrizes with these dimensions get multiplied (what would be a problem) they just get summed up (what would be no problem (i guess?)).
I attached a sample file with some dummy values for the matrices so its easier to send those values, but the error stays the same no matter what are the values in the matrices.
So in the end my question would be what did I do wrong to cause this error?
I hope this is not just some rather obvious mistake I made on the computation itself and I am more than grateful for all the help I got here so far
it was a bug in the differentiation of the vectorized exp(.) function (and others).
It’s fixed in the coming nightly.
Thanks for reporting the issue.
thanks for the fast reply I will install the nightly and be happy that everything works flawlessly.
I am sorry I have to bring up this topic again, but everything worked flawlessly besides some minor issues I encountered with the stresses around the undeformed state. Here the stresses computed from NGSolve are somewhat different compared to the results I obtain by using the same Matrizes using the origninal tensorflow network. However this is only the case around the undeformed state for larger deformations everything works fine again. I have attached an Image to illustrate the behaviour the blue and orange lines can be ignored they are some test data I use from other sources. Important are the red (NGSolve Simulation) and the green (Tensorflow Prediciton) data. I made sure this is no issue on the meshing by refining the mesh in size, order and a combination thereof. All result in the same data. Then I tried refining the steps I use to plot the data to see wether the linearization might result in some issues, but this also resulted in no changes. Lastly I wanted to see whether the Energy Density computed is the issue or whether the differentiation of said Energy Density.
Now I tried to evaluate the Energy Density function I am using to solve the Problem by generating a CoefficientFunction and evaluating it on the net, just I have done it with the stresses. However, this results in the error
File "<string>", line 91, in <module>
netgen.libngpy._meshing.NgException: MultMatMatCF:: scalar evaluate for matrix called
This error is not reproducable with the NeoHookean example in the WTA section of the Documentation, so I believe it to be an issue with the Matrix-Multiplication I am using, I sadly didn’t find any further details on how the error is happening, but I attached a file wich reproduces the error.
I am using
NGSolve-6.2.2008-78-ga51abb5db in order to enable differentiation of vector-valued functions as mentioned above.
Thank you again for all your help, should you need anything more from me, just let me know!
replace line 91 by
we distinguish between scalars and 1x1 matrices.
I also fixed the cf(eval_point) to allow vectors and matrices of dimension 1.
It’s difficult to help with the difference between direct simulation and the tensorflow approximation without having the example.
thanks a lot for the quick answer.
I am left with just one minor question: If I need to distinguish between scalars and 1x1 matrices the “cleaner” way to return the energy desinty would be to write
return W[0,0][/code] instead of [code]return W
in line 37 of my example file?
Regarding the differences between the NGSolve simulation and my Tensorflow model I am still investigating where exactly the error is sourced. The Problem I encountered while doing this was the problem with the evaluation of 1x1 matrizes. I was able to compare the energy density of both approaches now and recognized, that the tensorflow model does not result in an energy density of 0 at an undeformed state, which is most likely the reason behind this inconsistencies between the two approaches. I am sorry if I made the impression there would be something wrong with NGSolve in this case. I just wanted to give some background on how I encountered the error.
Thank you again!
Stay healthy and best regards,