03Lecture noteLinear and Logistic Regression.docx

上传人:b****2 文档编号:2247586 上传时间:2022-10-28 格式:DOCX 页数:18 大小:214.86KB
下载 相关 举报
03Lecture noteLinear and Logistic Regression.docx_第1页
第1页 / 共18页
03Lecture noteLinear and Logistic Regression.docx_第2页
第2页 / 共18页
03Lecture noteLinear and Logistic Regression.docx_第3页
第3页 / 共18页
03Lecture noteLinear and Logistic Regression.docx_第4页
第4页 / 共18页
03Lecture noteLinear and Logistic Regression.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

03Lecture noteLinear and Logistic Regression.docx

《03Lecture noteLinear and Logistic Regression.docx》由会员分享,可在线阅读,更多相关《03Lecture noteLinear and Logistic Regression.docx(18页珍藏版)》请在冰豆网上搜索。

03Lecture noteLinear and Logistic Regression.docx

03LecturenoteLinearandLogisticRegression

Lecturenote3:

LinearandLogisticRegressioninTensorFlow

CS20:

TensorFlowforDeepLearningResearch(cs20.stanford.edu)

PreparedbyChipHuyen(chiphuyen@cs.stanford.edu)

ThanksLaurenceMoroney,AkshayAgrawalforreviewingthenote

Phew,thelastlecturewaslong.Goodnewsisthatwe’redonewithallthealphabeticallearningandcannowbeofftobuildingthings.Beforestartingtheadventures,let’smakesurethatwe’refamiliarwithallthekeyconcepts.Ifyou’reunsureaboutanyofthese,youshouldgobacktotheprevioustwolectures.

Graphsandsessions

TFOps:

constants,variables,functions

TensorBoard

Lazyloading

LinearRegression:

Predictlifeexpectancyfrombirthrate

Let’sstartwithasimplelinearregressionexample.Ihopeyouallarealreadyfamiliarwithlinearregression.Ifnot,youcanreadaboutitonWikipedia.Basically,we’llbebuildingaverysimpleneuralnetworkconsistingofonelayertoinferthelinearrelationshipbetweenoneexplanatoryvariableXandonedependentvariableY.

Problem

Irecentlycameacrossthevisualizationoftherelationshipbetweenbirthratesandlifeexpectanciesofdifferentcountriesaroundtheworldandfoundthatfascinating.Basically,itlookslikethemorechildrenyouhave,theyoungeryouaregoingtodie!

YoucanplaythevisualizationcreatedbyGooglebasedonthedatacollectedbytheWorldBankhere.

Myquestionis,canwequantifythatrelationship?

Inotherwords,ifthebirthrateofacountryisanditslifeexpectancyis,canwefindalinearfunctionfsuchthat?

Ifweknowthatrelationship,giventhebirthrateofacountry,wecanpredictthelifeexpectancyofthatcountry.

Forthisproblem,wewillbeusingasubsetoftheWorldDevelopmentIndicatorsdatasetcollectedbytheWorldBank.Forsimplicity,wewillbeusingdatafromtheyear2010only.Youcandownloadthedatafromclass’sGitHubfolderhere.

DatasetDescription

Name:

Birthrate-lifeexpectancyin2010

X=birthrate.Type:

float.

Y=lifeexpectancy.Type:

foat.

Numberofdatapoints:

190

Approach

First,assumethattherelationshipbetweenthebirthrateandthelifeexpectancyislinear,whichmeansthatwecanfindwandbsuchthat.

Tofindwandb(inthiscase,theyarebothscalars),wewillusebackpropagationthroughaonelayerneuralnetwork.Forthelossfunction,wewillbeusingmeansquarederror.Aftereachepoch,wemeasurethemeansquareddifferencebetweentheactualvalueYsandthepredictedvaluesofYs.

Youcandownloadthefileexamples/03_linreg_starter.pyfromtheclass’sGitHubrepotogiveitashotyourself.Afteryou’redone,youcancomparewiththesolutionbelow.Youcanalsovisitexamples/03_linreg_placeholder.pyonGitHubfortheexecutablescript.

importtensorflowastf

importutils

DATA_FILE="data/birth_life_2010.txt"

#Step1:

readindatafromthe.txtfile

#dataisanumpyarrayofshape(190,2),eachrowisadatapoint

data,n_samples=utils.read_birth_life_data(DATA_FILE)

#Step2:

createplaceholdersforX(birthrate)andY(lifeexpectancy)

X=tf.placeholder(tf.float32,name='X')

Y=tf.placeholder(tf.float32,name='Y')

#Step3:

createweightandbias,initializedto0

w=tf.get_variable('weights',initializer=tf.constant(0.0))

b=tf.get_variable('bias',initializer=tf.constant(0.0))

#Step4:

constructmodeltopredictY(lifeexpectancyfrombirthrate)

Y_predicted=w*X+b

#Step5:

usethesquareerrorasthelossfunction

loss=tf.square(Y-Y_predicted,name='loss')

#Step6:

usinggradientdescentwithlearningrateof0.01tominimizeloss

optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss)

withtf.Session()assess:

#Step7:

initializethenecessaryvariables,inthiscase,wandb

sess.run(tf.global_variables_initializer())

#Step8:

trainthemodel

foriinrange(100):

#run100epochs

forx,yindata:

#Sessionrunstrain_optominimizeloss

sess.run(optimizer,feed_dict={X:

x,Y:

y})

#Step9:

outputthevaluesofwandb

w_out,b_out=sess.run([w,b])

Aftertrainingfor100epochs,wegottheaveragesquarelosstobe30.04withw=-6.07,b=84.93.Itconfirmsourbeliefthatthere’sanegativecorrelationbetweenthebirthrateandthelifeexpectancyofacountry.Andno,itdoesn’tmeanthathavingachildtakesoff6yearsofyourlife.

YoucanmakeotherassumptionsabouttherelationshipbetweenXandY.Forexample,ifwehaveaquadraticfunction:

Tofindw,u,andbforthismodel,weonlyhavetoaddanothervariableuandchangetheformulaforY_predicted.

#Step3:

createvariables:

weights_1,weights_2,bias.Allareinitializedto0

w=tf.get_variable('weights_1',initializer=tf.constant(0.0))

u=tf.get_variable('weights_2',initializer=tf.constant(0.0))

b=tf.get_variable('bias',

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1