AI学习记录 - 激活函数的作用
试验,通过在线性公式加入激活函数,可以拟合复杂的情况(使用js实现)
结论:1、线性函数的叠加,无论叠加多少次,都是线性的
如下图
示例代码
线性代码,使用y=kx+b的方式,叠加10个函数
const echartxianLine = useRef(null);
useEffect(() => {
// 基于准备好的dom,初始化echarts实例
echartxianLine.current = echarts.init(document.getElementById('xianlian'));
// echartNetwork.current = echarts.init(document.getElementById('network'));
})
useEffect(() => {
const getLineData = (initx) => {
// 定义10个函数的k和b值
const coefficients = [
{ k: 5, b: 0.5 },
{ k: 0.8, b: 0.6 },
{ k: 0.1, b: 0.4 },
{ k: 5, b: 0.8 },
{ k: 0.7, b: 0.2 },
{ k: 0.9, b: 0.1 },
{ k: 0.5, b: 0.2 },
{ k: 3, b: 0.1 },
{ k: 0.2, b: 0.5 },
{ k: 1, b: 0.7 }
];
let x = initx;
// 定义函数数组
coefficients.map(coeff => {
x = coeff.k * x + coeff.b
});
return x;
}
let objlist = []
for (let i = 0; i <= 50; i++) {
objlist.push({
x: i,
y: getLineData(i),
})
}
setLine(objlist);
const setxianEchartOptions = (objlist) => {
// 绘制图表
echartxianLine.current.setOption({
xAxis: {
type: 'category',
data: objlist.map((item, index) => item.x),
// data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
// data: [820, 932, 901, 934, 1290, 1330, 1320],
data: objlist.map((item, index) => item.y),
type: 'line',
smooth: true
}
]
});
}
setxianEchartOptions(objlist);
}, []);
非线性代码
非线性代码,使用y=kx+b的方式,在函数中间加入非线性函数sin,cos,sigmoid ,tanh,x2 .
const echartquine = useRef(null);
useEffect(() => {
// 基于准备好的dom,初始化echarts实例
echartquine.current = echarts.init(document.getElementById('quxian'));
// echartNetwork.current = echarts.init(document.getElementById('network'));
})
useEffect(() => {
const getLineData = (initx) => {
// 计算sin函数
function sin(degrees) {
return Math.sin(degrees);
}
function cos(degrees) {
return Math.cos(degrees);
}
const sigmoid = (x) => {
return 1 / (1 + Math.exp(-x));
}
const tanh = (x) => {
return Math.tanh(x);
}
const x2 = (x) => {
return x * x + 100
}
// 定义10个函数的k和b值
const coefficients = [
{ k: 5, b: 0.5, callbackLoss: sin },
{ k: -0.8, b: 0.6, callbackLoss: sigmoid },
{ k: 0.1, b: 0.4, callbackLoss: sigmoid },
{ k: -5, b: 0.8, callbackLoss: tanh },
{ k: 0.7, b: 0.2, callbackLoss: sigmoid },
{ k: -0.9, b: 0.1, callbackLoss: x2 },
{ k: 0.5, b: 0.2, callbackLoss: sin },
{ k: 3, b: 0.1, callbackLoss: sigmoid },
{ k: 0.2, b: 0.5, callbackLoss: x2 },
{ k: 1, b: 0.7, callbackLoss: x2 }
];
let x = initx;
let xmid = [];
// 定义函数数组
coefficients.map(coeff => {
if (coeff.callbackLoss) {
xmid.push(x);
x = coeff.callbackLoss(coeff.k * x + coeff.b)
} else {
x = coeff.k * x + coeff.b
}
});
return {
xmid: xmid,
x: x
};
}
let objlist = []
for (let i = 0; i <= 50; i++) {
objlist.push({
x: i,
y: ((getLineData(i).x * 1000000000) % 1).toFixed(2) * 10,
// y: getLineData(i).x,
xmid: getLineData(i).xmid
})
}
console.log("objlist", objlist)
setLine(objlist);
const setxianEchartOptions = (objlist) => {
console.log("quixan", objlist)
// 绘制图表
echartquine.current.setOption({
xAxis: {
type: 'category',
data: objlist.map((item, index) => item.x),
// data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
// data: [820, 932, 901, 934, 1290, 1330, 1320],
data: objlist.map((item, index) => item.y),
type: 'line',
smooth: true
}
]
});
}
setxianEchartOptions(objlist);
}, []);
原文地址:https://blog.csdn.net/weixin_43954090/article/details/140646673
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!