当前位置: 首页 > news >正文

Python和C++(CUDA)及Arduino雅可比矩阵导图

🎯要点

  1. 对比三种方式计算
  2. 读取二维和三维三角形四边形和六面体网格
  3. 运动学奇异点处理
  4. 医学图像成像组学分析
  5. 特征敏感度增强
  6. 机械臂路径规划和手臂空间操作变换
  7. 苹果手机物理稳定性中间轴定理
    在这里插入图片描述

Python雅可比矩阵

多变量向量值函数的雅可比矩阵推广了多变量标量值函数的梯度,而这又推广了单变量标量值函数的导数。换句话说,多变量标量值函数的雅可比矩阵是其梯度(的转置),而单变量标量值函数的梯度是其导数。

在函数可微的每个点,其雅可比矩阵也可以被认为是描述函数在该点附近局部施加的“拉伸”、“旋转”或“变换”量。例如,如果使用 ( x ′ , y ′ ) = f ( x , y ) \left(x^{\prime}, y^{\prime}\right)= f (x, y) (x,y)=f(x,y) 平滑变换图像,则雅可比矩阵 J f ( x , y ) J _{ f }( x, y) Jf(x,y),描述了 ( x , y ) (x, y) (x,y)邻域中的图像如何变换。如果函数在某点可微,其微分在坐标系中由雅可比矩阵给出。然而,函数不需要可微才能定义其雅可比矩阵,因为只需要存在其一阶偏导数。

考虑以下向量函数,该函数将 n n n 维向量 x ∈ R n x \in R ^n xRn 作为输入,并将该向量映射到 m m m 维向量:
f ( x ) = [ f 1 ( x 1 , x 2 , x 3 , … , x n ) f 2 ( x 1 , x 2 , x 3 , … , x n ) ⋮ f m ( x 1 , x 2 , x 3 , … , x n ) ] f ( x )=\left[\begin{array}{c} f_1\left(x_1, x_2, x_3, \ldots, x_n\right) \\ f_2\left(x_1, x_2, x_3, \ldots, x_n\right) \\ \vdots \\ f_m\left(x_1, x_2, x_3, \ldots, x_n\right) \end{array}\right] f(x)= f1(x1,x2,x3,,xn)f2(x1,x2,x3,,xn)fm(x1,x2,x3,,xn)

其中向量 x x x 定义为
x = [ x 1 x 2 ⋮ x n ] x =\left[\begin{array}{c} x_1 \\ x_2 \\ \vdots \\ x_n \end{array}\right] x= x1x2xn
非线性向量函数 f f f 产生 m m m 维向量
[ f 1 ( x 1 , x 2 , x 3 , … , x n ) f 2 ( x 1 , x 2 , x 3 , … , x n ) ⋮ f m ( x 1 , x 2 , x 3 , … , x n ) ] \left[\begin{array}{c} f_1\left(x_1, x_2, x_3, \ldots, x_n\right) \\ f_2\left(x_1, x_2, x_3, \ldots, x_n\right) \\ \vdots \\ f_m\left(x_1, x_2, x_3, \ldots, x_n\right) \end{array}\right] f1(x1,x2,x3,,xn)f2(x1,x2,x3,,xn)fm(x1,x2,x3,,xn)
其条目是 m m m 函数 f i , i = 1 , 2 , … , n f_i, i=1,2, \ldots, n fi,i=1,2,,n,将向量 x x x 的条目映射为标量数。

函数 f ( ⋅ ) f (\cdot) f() 的雅可比矩阵是 m m m × n n n 维偏导数矩阵,定义为
∂ f ∂ x = [ ∂ f 1 ∂ x 1 ∂ f 1 ∂ x 2 ⋯ ∂ f 1 ∂ x n ∂ f 2 ∂ x 1 ∂ f 2 ∂ x 2 ⋯ ∂ f 2 ∂ x n ⋮ ⋮ ⋮ ∂ f m ∂ x 1 ∂ f m ∂ x 2 … ∂ f m ∂ x n ] \frac{\partial f }{\partial x }=\left[\begin{array}{cccc} \frac{\partial f_1}{\partial x_1} & \frac{\partial f_1}{\partial x_2} & \cdots & \frac{\partial f_1}{\partial x_n} \\ \frac{\partial f_2}{\partial x_1} & \frac{\partial f_2}{\partial x_2} & \cdots & \frac{\partial f_2}{\partial x_n} \\ \vdots & \vdots & & \vdots \\ \frac{\partial f_m}{\partial x_1} & \frac{\partial f_m}{\partial x_2} & \ldots & \frac{\partial f_m}{\partial x_n} \end{array}\right] xf= x1f1x1f2x1fmx2f1x2f2x2fmxnf1xnf2xnfm
该矩阵的第一行由 f 1 ( ⋅ ) f_1(\cdot) f1() 分别相对于 x 1 、 x 2 、 … 、 x n x_1、x_2、\ldots、x_n x1x2xn 的偏导数组成。类似地,该矩阵的第二行由 f 2 ( ⋅ ) f_2(\cdot) f2() 分别相对于 x 1 、 x 2 、 … 、 x n x_1、x_2、\ldots、x_n x1x2xn 的偏导数组成。以同样的方式,我们构造雅可比矩阵的其他行。

在这里,我们展示了用于符号计算雅可比矩阵和创建 Python 函数的 Python 脚本,该函数将返回给定输入向量 x x x 的雅可比矩阵的数值。为了验证 Python 实现,让我们考虑以下测试用例函数
f = [ x 1 x 2 sin ⁡ ( x 1 ) cos ⁡ ( x 3 ) x 3 e x 4 ] f =\left[\begin{array}{c} x_1 x_2 \\ \sin \left(x_1\right) \\ \cos \left(x_3\right) \\ x_3 e^{x_4} \end{array}\right] f= x1x2sin(x1)cos(x3)x3ex4
其中 x x x
x = [ x 1 x 2 x 3 x 4 ] x =\left[\begin{array}{l} x_1 \\ x_2 \\ x_3 \\ x_4 \end{array}\right] x= x1x2x3x4

f 1 ( x 1 , x 2 , x 3 , x 4 ) = x 1 x 2 f 2 ( x 1 , x 2 , x 3 , x 4 ) = sin ⁡ ( x 1 ) f 3 ( x 1 , x 2 , x 3 , x 4 ) = cos ⁡ ( x 3 ) f 4 ( x 1 , x 2 , x 3 , x 4 ) = x 3 e x 4 \begin{aligned} & f_1\left(x_1, x_2, x_3, x_4\right)=x_1 x_2 \\ & f_2\left(x_1, x_2, x_3, x_4\right)=\sin \left(x_1\right) \\ & f_3\left(x_1, x_2, x_3, x_4\right)=\cos \left(x_3\right) \\ & f_4\left(x_1, x_2, x_3, x_4\right)=x_3 e^{x_4} \end{aligned} f1(x1,x2,x3,x4)=x1x2f2(x1,x2,x3,x4)=sin(x1)f3(x1,x2,x3,x4)=cos(x3)f4(x1,x2,x3,x4)=x3ex4
该函数的雅可比行列式是
∂ f ∂ x = [ ∂ f 1 ∂ x 1 ∂ f 1 ∂ x 2 ∂ f 1 ∂ x 3 ∂ f 1 ∂ x 4 ∂ f 2 ∂ x 1 ∂ f 2 ∂ x 2 ∂ f 2 ∂ x 3 ∂ f 2 ∂ x 4 ∂ f 3 ∂ x 1 ∂ f 3 ∂ x 2 ∂ f 3 ∂ x 3 ∂ f 3 ∂ x 4 ∂ f 4 ∂ x 1 ∂ f 4 ∂ x 2 ∂ f 4 ∂ x 3 ∂ f 4 ∂ x 4 ] \frac{\partial f }{\partial x }=\left[\begin{array}{llll} \frac{\partial f_1}{\partial x_1} & \frac{\partial f_1}{\partial x_2} & \frac{\partial f_1}{\partial x_3} & \frac{\partial f_1}{\partial x_4} \\ \frac{\partial f_2}{\partial x_1} & \frac{\partial f_2}{\partial x_2} & \frac{\partial f_2}{\partial x_3} & \frac{\partial f_2}{\partial x_4} \\ \frac{\partial f_3}{\partial x_1} & \frac{\partial f_3}{\partial x_2} & \frac{\partial f_3}{\partial x_3} & \frac{\partial f_3}{\partial x_4} \\ \frac{\partial f_4}{\partial x_1} & \frac{\partial f_4}{\partial x_2} & \frac{\partial f_4}{\partial x_3} & \frac{\partial f_4}{\partial x_4} \end{array}\right] xf= x1f1x1f2x1f3x1f4x2f1x2f2x2f3x2f4x3f1x3f2x3f3x3f4x4f1x4f2x4f3x4f4
通过计算这些偏导数,我们得到
∂ f ∂ x = [ x 2 x 1 0 0 cos ⁡ ( x 1 ) 0 0 0 0 0 − sin ⁡ ( x 3 ) 0 0 0 e x 4 x 3 e x 4 ] \frac{\partial f }{\partial x }=\left[\begin{array}{cccc} x_2 & x_1 & 0 & 0 \\ \cos \left(x_1\right) & 0 & 0 & 0 \\ 0 & 0 & -\sin \left(x_3\right) & 0 \\ 0 & 0 & e^{x_4} & x_3 e^{x_4} \end{array}\right] xf= x2cos(x1)00x100000sin(x3)ex4000x3ex4

import numpy as np
from sympy import *init_printing()x=MatrixSymbol('x',4,1)
f=Matrix([[x[0]*x[1]],[sin(x[0])],[cos(x[2])],[x[2]*E**(x[3])]])JacobianSymbolic=f.jacobian(x)
JacobianFunction=lambdify(x,JacobianSymbolic)
testCaseVector=np.array([[1],[1],[1],[1]])
JacobianNumerical=JacobianFunction(testCaseVector)

定义符号向量“x”如下

x=MatrixSymbol('x',4,1)

非线性向量函数“f”定义为

=Matrix([[x[0]*x[1]],[sin(x[0])],[cos(x[2])],[x[2]*E**(x[3])]])
JacobianSymbolic=f.jacobian(x)
JacobianFunction=lambdify(x,JacobianSymbolic)

测试向量处评估雅可比行列式。

testCaseVector=np.array([[1],[1],[1],[1]])
JacobianNumerical=JacobianFunction(testCaseVector)

存储在“JacobianNumerical”中的结果是一个 NumPy 数值数组(矩阵),可用于进一步计算。

示例:TensorFlow雅可比矩阵

%tensorflow_version 1.x
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
import numpy as np
import statsmodels.api as sm
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
from tqdm import tqdm
import tensorflow as tfnp.random.seed (245)
nobs =10000x1= np.random.normal(size=nobs ,scale=1)
x2= np.random.normal(size=nobs ,scale=1)
x3= np.random.normal(size=nobs ,scale=1)
x4= np.random.normal(size=nobs ,scale=1)
x5= np.random.normal(size=nobs ,scale=1)X= np.c_[np.ones((nobs ,1)),x1,x2,x3,x4,x5]y= np.cos(x1) + np.sin(x2) + 2*x3 + x4 + 0.01*x5 + np.random.normal(size=nobs , scale=0.01)LR=0.05Neuron_Out=1
Neuron_Hidden1=64
Neuron_Hidden2=32Activate_output='linear'
Activate_hidden='relu' Optimizer= SGD(lr=LR)
loss='mean_squared_error'from sklearn.model_selection import train_test_split
x_train , x_test , y_train , y_test = train_test_split(X, y, test_size =0.15, random_state =77)from tensorflow import set_random_seed
set_random_seed (245)sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())model_ANN= Sequential()model_ANN.add(Dense(Neuron_Hidden1, activation=Activate_hidden, input_shape=(6,), use_bias=True))
model_ANN.add(Dense(Neuron_Hidden2, activation=Activate_hidden, use_bias=True))model_ANN.add(Dense(Neuron_Out, activation=Activate_output,use_bias=True))
model_ANN.summary()model_ANN.compile(loss=loss, optimizer=Optimizer, metrics=['accuracy'])history_ANN=model_ANN.fit(
x_train, 
y_train, 
epochs=125)def jacobian_tensorflow(x):jacobian_matrix = []for m in range(Neuron_Out):grad_func = tf.gradients(model_ANN.output[:, m],model_ANN.input)gradients = sess.run(grad_func, feed_dict={model_ANN.input: x})  jacobian_matrix.append(gradients[0][0,:])return np.array(jacobian_matrix)jacobian_tensorflow(x_train)

👉更新:亚图跨际

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 大语言模型的超参数含义: Top-P 采样; Top-P 采样;logit_bias:
  • 浏览器播放RTSP流,支持H264、H265等格式,支持IE、Chrome等浏览器
  • 六、vue进阶知识点
  • 安防监控/软硬一体/视频汇聚网关EasyCVR硬件启动崩溃是什么原因?
  • Centos7.x安装grafana11
  • Spring框架;Spring中IOC简介及搭建;Spring中AOP简介;
  • Unity 3D学习资料集合
  • 英文缩写大全(IT领域和电子行业制造领域)
  • AI游戏革命!谷歌推出GameNGen,实时生成游戏画面,每秒20帧实时模拟
  • 浅谈通信协议设计
  • 线性约束最小方差准则(LCMV)波束形成算法及MATLAB深入仿真分析
  • 9.1写论文
  • JavaSE-递归法解决二分查找、快速排序
  • Qt:玩转QPainter后转之太极图
  • 蓝色炫酷碎粒子HTML5导航源码
  • @jsonView过滤属性
  • 4. 路由到控制器 - Laravel从零开始教程
  • java 多线程基础, 我觉得还是有必要看看的
  • Js基础知识(一) - 变量
  • JS题目及答案整理
  • Magento 1.x 中文订单打印乱码
  • MySQL主从复制读写分离及奇怪的问题
  • Netty+SpringBoot+FastDFS+Html5实现聊天App(六)
  • PaddlePaddle-GitHub的正确打开姿势
  • Redash本地开发环境搭建
  • Vue UI框架库开发介绍
  • Yeoman_Bower_Grunt
  • 彻底搞懂浏览器Event-loop
  • 工程优化暨babel升级小记
  • 开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
  • 什么软件可以提取视频中的音频制作成手机铃声
  • 腾讯优测优分享 | Android碎片化问题小结——关于闪光灯的那些事儿
  • 限制Java线程池运行线程以及等待线程数量的策略
  • 自制字幕遮挡器
  • CMake 入门1/5:基于阿里云 ECS搭建体验环境
  • 如何在招聘中考核.NET架构师
  • ​补​充​经​纬​恒​润​一​面​
  • ​虚拟化系列介绍(十)
  • # centos7下FFmpeg环境部署记录
  • ## 基础知识
  • #07【面试问题整理】嵌入式软件工程师
  • #NOIP 2014#day.2 T1 无限网络发射器选址
  • #QT(一种朴素的计算器实现方法)
  • ()、[]、{}、(())、[[]]等各种括号的使用
  • (1)(1.11) SiK Radio v2(一)
  • (1)(1.9) MSP (version 4.2)
  • (1)安装hadoop之虚拟机准备(配置IP与主机名)
  • (3)llvm ir转换过程
  • (31)对象的克隆
  • (6)【Python/机器学习/深度学习】Machine-Learning模型与算法应用—使用Adaboost建模及工作环境下的数据分析整理
  • (搬运以学习)flask 上下文的实现
  • (六)Hibernate的二级缓存
  • (南京观海微电子)——示波器使用介绍
  • (图)IntelliTrace Tools 跟踪云端程序
  • (一)python发送HTTP 请求的两种方式(get和post )