用tensorflow求解吴恩达的机器学习练习题(ex1)
2018-03-04 23:01
295 查看
英语水平有限.....习题描述不准确的地方多担待本人系机器学习初学者,可能有的理解不够到位,希望大家批评指正
1.习题描述 你有一个ex1data1.txt的数据集,数据集的第一列是城市的人口,第二列是在城市***利润,负值表示损失。请应用单变量线性回归来通过人口预测利润。
,theta0同理。这里数学公式不太懂关系不是很大,tensorflow封装了这个梯度下降算法。然后通过循环不断调整theta的值就可以得到最终解。这里,为了将theta0与theta1用一个向量来表示,我们选择在原始数据的左边加一列1。
1.习题描述 你有一个ex1data1.txt的数据集,数据集的第一列是城市的人口,第二列是在城市***利润,负值表示损失。请应用单变量线性回归来通过人口预测利润。
In this part of this exercise, you will implement linear regression with one variable to predict prots for a food truck. Suppose you are the CEO of a restaurant franchise and are considering dierent cities for opening a new outlet. The chain already has trucks in various cities and you have data for prots and populations from the cities. 3 You would like to use this data to help you select which city to expand to next. The le ex1data1.txt contains the dataset for our linear regression prob- lem. The rst column is the population of a city and the second column is the prot of a food truck in that city. A negative value for prot indicates a loss. The ex1.m script has already been set up to load this data for you.(原文) 吴恩达.机器学习.ex12.数学基础 我们希望通过theta1*x+theta0来拟合当前的数据,这就需要找到一种方法来确定theta1,theta0的值。机器学习的思路大概是这样。先随便来一个theta1,theta0,在本文中初始化为0。然后计算h:=theta1*x+theta0的值。得到预测值,然后通过计算损失函数,loss:=1/(2*m)*∑((y-h)^2),然后运用梯度下降的算法来调整theta1,theta0。theta1:=theta1-alpha*
,theta0同理。这里数学公式不太懂关系不是很大,tensorflow封装了这个梯度下降算法。然后通过循环不断调整theta的值就可以得到最终解。这里,为了将theta0与theta1用一个向量来表示,我们选择在原始数据的左边加一列1。
# -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt import tensorflow as tf a=np.loadtxt('ex1data1.txt',delimiter=','); #载入txt数据 x=a[:,0] #x为数据的第一列 y=a[:,1] #y为数据的第二列 y=y.reshape(97,1) #这一步是确定y的列数,不然y的列数为空 m=y.shape[0] #确定行数 temp=np.ones((m,1)) x=np.c_[temp,x] #在自变量左侧增加一列1,这样就不用b了 theta=tf.Variable([[0.0],[0.0]]) #初始化theta的值,2行1列 x=x.astype(np.float32) #使x与theta的变量类型一致 h=tf.matmul(x,theta) #矩阵相乘 [97,2]*[2,1]=[97,1],即Wi*Xi+b loss=tf.reduce_mean(tf.square(h-y)/2) #计算损失值 optimizer=tf.train.GradientDescentOptimizer(0.015) #运用梯度下降算法,学习率为0.015 train=optimizer.minimize(loss) #最小化损失值 init=tf.initialize_all_variables() #初始化tensorflow变量 sess=tf.Session() #打开session sess.run(init) #初始化 for step in range(1000): #执行优化循环 sess.run(train) #训练 plt.plot(x[:,1],y,'rx') #原始数据点标记 plt.plot(x[:,1],sess.run(h)) #绘制优化曲线 plt.show() print(sess.run(loss)) #打印当前损失值数据下载:链接:链接:https://pan.baidu.com/s/1MA-u4YYU60fRtjQURh9C7Q 密码:nfdn
相关文章推荐
- 用tensorflow求解吴恩达的机器学习练习题(ex2)
- 吴恩达机器学习练习题ex1
- 【机器学习】tensorflow: GPU求解带核函数的SVM二分类支持向量机
- 【机器学习】tensorflow:HMM隐状态链的一种最优化求解方法
- 【tensorflow】通过机器学习求解二元一次方程的参数
- 吴恩达《深度学习-结构化机器学习项目》2--机器学习策略(2)
- 吴恩达《深度学习工程师》Part3.Week1 机器学习策略(一)
- 吴恩达《深度学习工程师》Part3.Week2 机器学习策略(二)
- 机器学习:如何在安卓上集成TensorFlow
- TensorFlow梯度求解tf.gradients
- 机器学习笔记7:TensorFlow进阶之利用CNN训练MNIST
- coursera_机器学习_吴恩达_week2
- 讲你肯定能懂的机器学习多维极值求解
- TensorFlow框架(5)之机器学习实践
- 机器学习速成课程MLCC(4)--使用TensorFlow的基本步骤
- Coursera-AndrewNg(吴恩达)机器学习第一周笔记
- 斯坦福大学(吴恩达) 机器学习课后习题详解 第三周 编程题 逻辑回归
- Coursea吴恩达《结构化机器学习》课程笔记(2)机器学习策略下
- 机器学习实战(用Scikit-learn和TensorFlow进行机器学习)(七)
- 机器学习--EM算法求解高斯混合模型