lua+torch实现数据多项式拟合(附代码、图像)

lua+torch实现数据多项式拟合(附代码、图像)

To approximate a nonlinear function by polynomial function. For example, use a polynomial to approximate tan ⁡ ( x ) \tan(x) tan(x), where
x ∈ [ − 1 , 1 ] x \in [-1, 1] x[1,1], and draw the fitted picture.

1. 获取数据

--prepare data
x=torch.rand(1,10)
y=-x
input=torch.cat(x,y)
--print(input)
tan_x=torch.tan(x)
tan_y=torch.tan(y)
output=torch.cat(tan_x,tan_y)
--usually, we use column vector
input=input:transpose(1,2)
output=output:transpose(1,2)

2. 代入公式

使用三次函数拟合, a 1 ∗ 1 + a 2 ∗ x + a 3 ∗ x 2 + a 4 ∗ x 3 a_1*1+a_2*x+a_3*x^2+a_4*x^3 a11+a2x+a3x2+a4x3
lua+torch矩阵转置https://blog.csdn.net/jiejinquanil/article/details/49617779
编写过程中出现
torch.inverse函数无法使用,解决方法Torch - 错误 getrf : Lapack library not found in compile time
上述博文操作过程中,又出现git错误,使用 Git 同步时出现gnutls_handshake() failed: Error in the pull function
注:make的过程可能时间比较长
按照安装过程时,luarocks install 提示 failed fetching manifest
解决错误 sh: 0: getcwd() failed: No such file or directory
https://blog.csdn.net/qq_36393978/article/details/117015031

3. torch命令参考链接

https://blog.csdn.net/c602273091/article/details/78940781
https://www.jianshu.com/p/d678c5e44a6b
https://github.com/torch/torch7/blob/master/doc/maths.md#torch.pow
https://blog.csdn.net/enjoyyl/article/details/48053291#torch%E5%8D%B8%E8%BD%BD
https://blog.csdn.net/hejunqing14/article/details/52162970
https://www.cnblogs.com/darkknightzh/p/5653864.html

4. 绘制图像

绘制图像使用gnuplot绘制,安装代码如下

sudo apt-get install gnuplot-x11

使用gnuplot可以遇到以下问题:解决Failed to load module canberra-gtk-module错误,参考https://blog.csdn.net/footrip/article/details/103639958

5. 完整代码

require('math')
require('torch')
require ('gnuplot')

--prepare data
x=torch.rand(1,10)
y=-x
input=torch.cat(x,y)

tan_x=torch.tan(x)
tan_y=torch.tan(y)
output=torch.cat(tan_x,tan_y)

--usually, we use column vector
input=input:transpose(1,2)
output=output:transpose(1,2)
--print(input)
---Use formula, here we use cubic function to fit the data
---a_1*1+a_2*x+a_3*x^2+a_4*x^3

column1=torch.ones(20)
--print(column1)
column2=input
column3=torch.pow(input,2)
column4=torch.pow(input,3)
--y = torch.pow(x, n)  element wise operation

A=torch.cat(column1,column2)
A=torch.cat(A,column3)
A=torch.cat(A,column4)
--print(A)

transpose_A=A:transpose(1,2)
--print(transpose_A)

ATA=torch.mm(transpose_A,A)
output1D=torch.squeeze(output)
--print(torch.mv(torch.mm(torch.inverse(ATA),transpose_A),output))
coefficient=torch.mv(torch.mm(torch.inverse(ATA),transpose_A),output1D)
--print(coefficient)

--Using torch.index_select, note that lua start from index 1
print(coefficient[1],coefficient[2],coefficient[3],coefficient[4])
--lua has broadcast
fit_result=coefficient[1]+coefficient[2]*input+coefficient[3]*torch.pow(input,2)+coefficient[4]*torch.pow(input,3)
print(fit_result)

data=torch.cat(input,fit_result)

结果

0.4188  0.4399
 0.6831  0.8254
 0.1083  0.1042
 0.8683  1.1845
 0.3162  0.3192
 0.1260  0.1215
 0.9125  1.2836
 0.8795  1.2091
 0.6082  0.7030
 0.1616  0.1567
-0.4188 -0.4399
-0.6831 -0.8254
-0.1083 -0.1042
-0.8683 -1.1845
-0.3162 -0.3192
-0.1260 -0.1215
-0.9125 -1.2836
-0.8795 -1.2091
-0.6082 -0.7030
-0.1616 -0.1567
[torch.DoubleTensor of size 20x2]


额外补充lua用法,lua读写txt文件,参考https://blog.csdn.net/forestsenlin/article/details/50776417
注:博主并未实现使用gnuplot进行绘图,目前已经有data数据,即上面结果,只需要一个作为x,一个作为y绘图即可,还希望会使用gnuplot的朋友评论指点,谢谢

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值