- Thank you received: 0
Matrix-Multiplication CoefficientFunction * NumPy
3 years 11 months ago #3237
by Tele
Matrix-Multiplication CoefficientFunction * NumPy was created 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
The Operator used in the NGSolve example to create the right ChaucyGreen tensor
The numpy matrix multiplication method
and tensorproduct method
The NGSolve
method, all resulting in various errors.
I have attached my trial file 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
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:
@
Code:
C = F.trans * F
Code:
numpy.matmul()
Code:
numpy.tensordot()
Code:
InnerProduct()
I have attached my trial file 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:
- christopher
- Offline
- Administrator
Less
More
- Thank you received: 101
3 years 11 months ago #3238
by christopher
Replied by christopher on topic Matrix-Multiplication CoefficientFunction * NumPy
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
Your symbolic form should be scalar though.
Best
Christopher
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
3 years 11 months ago #3239
by Tele
Replied by Tele on topic Matrix-Multiplication CoefficientFunction * NumPy
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
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
3 years 11 months ago - 3 years 11 months ago #3245
by Tele
Replied by Tele on topic Differentiation with Matrix-Multiplication CoefficientFunction * NumPy
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:
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
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 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
Attachments:
Last edit: 3 years 11 months ago by Tele.
3 years 11 months ago #3246
by joachim
Replied by joachim on topic Differentiation with Matrix-Multiplication CoefficientFunction * NumPy
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
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
3 years 11 months ago #3247
by Tele
Replied by Tele on topic Differentiation with Matrix-Multiplication CoefficientFunction * NumPy
Hi Joachim,
thanks for the fast reply I will install the nightly and be happy that everything works flawlessly.
Best
Til
thanks for the fast reply I will install the nightly and be happy that everything works flawlessly.
Best
Til
Time to create page: 0.119 seconds