# -*- coding: utf-8 -*- """ Created on Tue Mar 13 20:49:03 2018 @author: """ import numpy as np from scipy import stats import matplotlib.pyplot as plt ##产生训练数据,生成模型为2*x+5+random.randint(50) x=np.arange(0.,10.,0.2) m=len(x) print(m) x0=np.full(m,1.0) input_data=np.vstack([x0,x]).T target_data=2*input_data[:,1]+5*input_data[:,0]+np.random.randn(m) loop_max=100000 #设置最大训练次数,防止程序死循环 epsilon=1e-3 # 设置训练模型容许误差 np.random.seed(0) #设置随机产生种子,让每次生成随机数一致 theta=np.random.randn(2) # 初始化训练模型的权重 alpha=0.001 #训练速度(太大容易导致欠拟合,太小容易导致模型不收敛) diff=0. error = np.zeros(2) #初始化模型误差 count=0 #统计模型循环次数 finish=0 #模型训练截止标志