试验,通过在线性公式加入激活函数,可以拟合复杂的情况(使用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);}, []);