【水文】基于 MNN 训练能力解方程
简介
许久没上,水篇文章,事由同学想由房贷月还款额推测年利率:
月还款额=pow(年利率/12+1,期数)*(年利率/12)*贷款总额/(pow(年利率/12+1,期数)-1)
这个方程直接求解感觉比较麻烦,考虑使用梯度下降法。深度学习框架基本都支持求导和优化,MNN 也支持。所以安装 pymnn ,把方程用 MNN 的仿numpy接口实现一遍,然后写个 loss 训练就好了。文章来源:https://www.toymoban.com/news/detail-627258.html
基于 MNN 代码实现
import time
import MNN.numpy as np
import MNN
nn = MNN.nn
F = MNN.expr
# open lazy evaluation for train
F.lazy_eval(True)
# month_pay=pow(rate/12+1, times)*(rate/12)*total/(pow(rate/12+1,times)-1)
# Know month_pa, total, times, solve rate
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
one = np.array([0.001])
one.fix_as_trainable()
self.rate = one
def forward(self, times, total):
r12 = self.rate / 12.0
r12_1 = r12 + np.array([1.0])
total_rate = np.power(r12_1, times)
p0 = (total_rate * r12 * total) / (total_rate-np.array([1.0]))
return p0
model = Net()
opt = MNN.optim.SGD(model, 0.0000000001, 0.9, 0.0005)
for iter in range(0, 1000):
times = np.array([60.0])
month_pay = np.array([12439.12])
total = np.array([630000.0])
month_comp = model.forward(times, total)
diff = month_pay - month_comp
loss = diff * diff
opt.step(loss)
times = np.array([60.0])
month_pay = np.array([12439.12])
total = np.array([630000.0])
month_comp = model.forward(times, total)
print("rate:", model.rate, " ; month_comp: ", month_comp)
运行结果
hw.cpufamily: 458787763 , size = 4
The device support i8sdot:1, support fp16:1, support i8mm: 0
rate: array([0.06879799], dtype=float32) ; month_comp: array([12439.118], dtype=float32)文章来源地址https://www.toymoban.com/news/detail-627258.html
到了这里,关于【水文】基于 MNN 训练能力解方程的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!