flexible transmitter 中sin信号模拟python代码(基于随机梯度下降思想)

接着上一篇文章,这次使用随机梯度下降法调整参数完成反向传播

flexible transmitter (南大周志华团队Shao-Qun Zhang, Zhi-Hua Zhou)中sin信号模拟python代码

import numpy as np
import matplotlib.pyplot as plt # 导入 matplotlib 包的 pyplot 模块,并简写为 plt 

# 生成数据
x=np.array(range(300))
# x
y=np.sin(x*2/3*np.pi)
# y

# 设置初始参数
w=-1.142
v=-0.1929
m=-0.25

#初始化记忆单元m,以便于后面查看m随时间的变化情况
hm=np.zeros(len(y))
# 迭代次数计数
j=0
# 初始化损失
totolloss=10000
# 当损失小于70或者迭代超过1000次时停止迭代
while((totolloss>70)&(j<1000)):
    # 遍历数据集
    for i in range(len(y)-1):
        # 随机梯度下降
        # 前向传播
        pre[i+1]=np.tanh(w*y[i]-v*m)
        real[i+1]=np.tanh(w*y[i]-v*m)
        # 计算损失
        loss = np.square(pre[i+1]-y[i+1])/2
        # 计算梯度
        dw=(pre[i+1]-y[i+1])*(1-np.square(np.tanh(real[i+1])))*y[i]
        dv=(pre[i+1]-y[i+1])*(1-np.square(np.tanh(real[i+1])))*(-m)
        # 更新参数
        w=w-0.001*dw
        v=v-0.001*dv
        m=np.tanh(w*y[i]+v*m)
        hm[i+1]=m
    # 迭代次数计数
    j=j+1
    # 每迭代100次输出一次总损失和参数值
    if j%100==0:
        totolloss=np.sum(np.square(y-pre))
        print(totolloss,w,v)

绘制拟合效果图

plt.plot(x[:60],y[:60])
plt.plot(x[:60],pre[:60])
plt.show()

在这里插入图片描述
绘制记忆单元m和真实信号的时序图

plt.plot(x[:60],y[:60])
plt.plot(x[:60],hm[:60])
plt.show()

在这里插入图片描述

文章链接如下
[1]: https://arxiv.org/pdf/2004.03839.pdf
[2]:flexible transmitter (南大周志华团队Shao-Qun Zhang, Zhi-Hua Zhou)中sin信号模拟python代码

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值