機器學習筆記part1,係數優化
阿新 • • 發佈:2021-02-02
機器學習筆記part1
1,epochs(時代/學習週期)→模型通過不斷地執行(學習)→不斷的更新coefficient(係數)→更好地擬合數據
即b=b-learningrate* error * x
2把所有地epoch(學習週期,注意,每個epoch裡會有相應的訓練集)進行loop(迴圈迭代)
3每一次係數(coefficient)迴圈都會進行係數調優
error=prediction-expected
// An highlighted block
from Ay_hat import make_prediction#模組化程式設計,輕量化程式碼
def using_sgd_method_to_calculate_coefficient (traing_dataset,learning_rate,n_times_epoch):
coefficients=[0.0 for i in range(len(traing_dataset[0]))]
for epoch in range(n_times_epoch):
the_sum_of_error = 0#用於計數
for row in traing_dataset:
y_hat=make_prediction(row,coefficients)
error = y_hat - row[-1]#error=prediction-expected
the_sum_of_error += error**2#用平方避免負數的情況出現
coefficients[0] = coefficients[0] - learning_rate*error#b=b-learningrate*errorx
for i in range(len(row)-1):
coefficients[i+1] = coefficients[i+1] - learning_rate*error*row[i]
print("This is epoch :",epoch,"the learning_rate we are using is :",learning_rate, "the error is :",the_sum_of_error)
return coefficients
your_training_dataset=[[1,1],[2,3],[4,3],[3,2],[5,5]]
test_coefficients = [0.4,0.8]
your_model_learning_rate = 0.01
your_n_epoch = 50
your_coefficient = using_sgd_method_to_calculate_coefficient(your_training_dataset,your_model_learning_rate,your_n_epoch)