梯度下降函數實現

def descent(data,theta,batchSize,stopType,stopType,thresh,alpha):
	init_time = time.time()
	i=0
	k=0
	X,y = shuffleData(data)
	grad = np.zeros(theta.shape)
	costs = [cost(X,y,theta)]
	
	while True:
		grad = gradient(X[k:k+batchSize],y[k:k+batchSize],theta)
		k +=batchSize
		if k >=n:
			k=0
			X,y = shuffleData(Data)
		theta = theta - alpha*grad
		costs.append(cost(X,y,theta))
		i +=1
		
		if stopType == STOP_ITER:		value=i
		elif stopType == STOP_COST:		value=costs
		elif stopType == STOP_GRAD:		value = grad
		if stopCriterion(stopType,value,thresh): break
	return theta,i-1,costs,grad,time.time()-init_time

                                                        

 

其他子函數請閱本博主其他相關博客

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章