神经网络与深度学习(邱锡鹏)编程练习 2 题目解析
作者:互联网
线性回归的参数优化:
- 先完成最小二乘法的优化 (参考书中第二章 2.3中的公式)
- 再完成梯度下降法的优化 (参考书中第二章 2.3中的公式)
不同的基函数实现:
- 多项式基函数
- 高斯基函数
函数:
- 载入数据:load_data(filename)
- 基函数: identity_basis(x) multinomial_basis(x, feature_num=10) gaussian_basis x, feature_num=10)
- 训练模型:main(x_train, y_train) 返回从x到y的映射
- 评估模型:evaluate(ys, ys_pred)
主程序:
# 程序主入口(建议不要改动以下函数的接口)
if __name__ == '__main__':
train_file = 'train.txt'
test_file = 'test.txt'
# 载入数据
x_train, y_train = load_data(train_file)
x_test, y_test = load_data(test_file)
print(x_train.shape)
print(x_test.shape)
# 使用线性回归训练模型,返回一个函数f()使得y = f(x)
f = main(x_train, y_train)
y_train_pred = f(x_train)
std = evaluate(y_train, y_train_pred)
print('训练集预测值与真实值的标准差:{:.1f}'.format(std))
# 计算预测的输出值
y_test_pred = f(x_test)
# 使用测试集评估模型
std = evaluate(y_test, y_test_pred)
print('预测值与真实值的标准差:{:.1f}'.format(std))
#显示结果
plt.plot(x_train, y_train, 'ro', markersize=3)
plt.plot(x_test, y_test, 'k')
plt.plot(x_test, y_test_pred, 'k')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Linear Regression')
plt.legend(['train', 'test', 'pred'])
plt.show()</code></pre>
标签:__,plt,函数,pred,编程,神经网络,train,test,邱锡鹏 来源: https://www.cnblogs.com/hbuwyg/p/16326163.html