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
a1∗1+a2∗x+a3∗x2+a4∗x3
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的朋友评论指点,谢谢