Forum Message

 

 

We have moved the forum to https://forum.ngsolve.org . This is an archived version of the topics until 05/05/23. All the topics were moved to the new forum and conversations can be continued there. This forum is just kept as legacy to not invalidate old links. If you want to continue a conversation just look for the topic in the new forum.

Notice

The forum is in read only mode.

Matrix-Multiplication CoefficientFunction * NumPy

More
3 years 6 months ago #3237 by Tele
Hi,

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
Code:
@
The Operator used in the NGSolve example to create the right ChaucyGreen tensor
Code:
C = F.trans * F
The numpy matrix multiplication method
Code:
numpy.matmul()
and tensorproduct method
Code:
numpy.tensordot()
The NGSolve
Code:
InnerProduct()
method, all resulting in various errors.

I have attached my trial file

File Attachment:

File Name: trials.py
File Size:3 KB
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!!

Best,
Til
Attachments:
More
3 years 6 months ago #3238 by christopher
Hi,
Vector and matrix valued CoefficientFunctions can be created using python tuples. If you use lists, domainwise coefficients are created, see:
ngsolve.org/docu/nightly/i-tutorials/uni...ficientfunction.html
Code:
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.

Best
Christopher
The following user(s) said Thank You: Tele
More
3 years 6 months ago #3239 by Tele
Hi Christopher,

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.

Best,
Til
More
3 years 6 months ago - 3 years 6 months ago #3245 by Tele
Hi,

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:
Code:
File "trials_ngForum.py", line 94, in <module> 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
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 :)

Best,
Til

File Attachment:

File Name: trials_ngForum.py
File Size:3 KB
Last edit: 3 years 6 months ago by Tele.
More
3 years 6 months ago #3246 by joachim
Hi Til,

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.

Joachim
The following user(s) said Thank You: Tele
More
3 years 6 months ago #3247 by Tele
Hi Joachim,

thanks for the fast reply :) I will install the nightly and be happy that everything works flawlessly.

Best
Til
Time to create page: 0.195 seconds