core api linear regression
play

Core API : linear regression IN TR OD U C TION TO TE N SOR FL OW - PowerPoint PPT Presentation

Core API : linear regression IN TR OD U C TION TO TE N SOR FL OW IN R Colleen Bobbie Instr u ctor TensorFlo w APIs INTRODUCTION TO TENSORFLOW IN R TensorFlo w APIs INTRODUCTION TO TENSORFLOW IN R TensorFlo w APIs 1 h ps :// tensor o


  1. Core API : linear regression IN TR OD U C TION TO TE N SOR FL OW IN R Colleen Bobbie Instr u ctor

  2. TensorFlo w APIs INTRODUCTION TO TENSORFLOW IN R

  3. TensorFlo w APIs INTRODUCTION TO TENSORFLOW IN R

  4. TensorFlo w APIs 1 h � ps :// tensor � o w. rst u dio . com / INTRODUCTION TO TENSORFLOW IN R

  5. Introd u ction to o u r first dataset For this simple linear regression e x ample : Research q u estion : Can w e predict ho w m u ch beer is cons u med in a u ni v ersit y to w n on a gi v en da y, u sing Amo u nt of Dail y Precipitation ( mm ) O u r dependent v ariable , amo u nt of beer cons u med , w ill be meas u red in liters ( L ). Note : w e ' ll be adding on more v ariables w hen w e cond u ct a m u ltiple linear regression later in this chapter ! INTRODUCTION TO TENSORFLOW IN R

  6. Creating testing and training datasets We � rst m u st separate o u r dataset into training and testing datasets . For the p u rposes of this lesson , w e ' ll u se an 80/20 split . First , select randoml y 80% of the t u ples in o u r dataset . beerrows <- sample(1:nrow(beer_consumption), size = 0.8 * nrow(beer_consumption)) Then , u se the selected ro w s to create a training and testing dataset . beer_consumption_train <- beer_consumption[beerrows,] beer_consumption_test <-beer_consumption[-beerrows,] Consider pa u sing to complete Datacamp ' s Introd u ction to Machine Learning if y o u need a refresh . INTRODUCTION TO TENSORFLOW IN R

  7. Parsing o u t dependent and independent v ariables Ne x t step : De � ne x-v ariable : x_actual <- beer_consumption_train$precip De � ne y-v ariable : y_actual <- beer_consumption_train$beer_consumed INTRODUCTION TO TENSORFLOW IN R

  8. Defining w, b , and y v ariables Remember the eq u ation for a straight line ? y = wx+b w here w is the slope ( aka ' Weights ') and b is the intercept ( aka ' Bias '). O u r ne x t step is to de � ne o u r w , b , and y v ariables . w <- tf$Variable(tf$random_uniform(shape(1L), -1, 1)) b <- tf$Variable(tf$zeros(shape(1L))) y_predict <- w * x_data + b INTRODUCTION TO TENSORFLOW IN R

  9. Let ' s practice ! IN TR OD U C TION TO TE N SOR FL OW IN R

  10. Core API : linear regression part II IN TR OD U C TION TO TE N SOR FL OW IN R Colleen Bobbie Instr u ctor

  11. Defining the cost f u nction Cost f u nction : meas u re of ho w w rong a model is also kno w n as loss or error t y picall y compare predicted and kno w n v al u es of Y Here , loss = Mean Sq u ared Error ( MSE ): loss <- tf$reduce_mean((y_predict-y_actual)^2) mean sq u ared di � erences bet w een predicted and act u al Y v al u es INTRODUCTION TO TENSORFLOW IN R

  12. Defining the optimi z er f u nction Ne x t , w e ' ll de � ne o u r optimi z er . Gradient Descent Optimi z er : model learns direction ( gradient ) sho u ld take to red u ce errors Use tf$train$GradientDescentOptimizer() w ith the learning rate in brackets : optimizer <- tf$train$GradientDescentOptimizer(0.001) INTRODUCTION TO TENSORFLOW IN R

  13. Minimi z ing MSE loss O u r � nal step is to minimi z e the MSE loss , w hich w e ' ll de � ne in a v ariable called train . train <- optimizer$minimize(loss) and la u nch the session and initiali z e o u r v ariables sess <- tf$Session() sess$run(tf$global_variables_initializer()) INTRODUCTION TO TENSORFLOW IN R

  14. Let ' s practice ! IN TR OD U C TION TO TE N SOR FL OW IN R

  15. Core API : linear regression part III IN TR OD U C TION TO TE N SOR FL OW IN R Colleen Bobbie Yes , still y o u r instr u ctor

  16. Training y o u r linear regression model Remember in o u r last lesson , w e de � ned : train <- optimizer$minimize(loss) w hich w ill minimi z e o u r MSE loss . No w w e ' ll train o u r model u sing this strateg y and 2000 epochs . for (step in 1:2000) { sess$run(train) if (step %% 500 == 0) cat("Step = ", step, "Estimate w = ", sess$run(w), "Estimate b =", sess$run(b)) } INTRODUCTION TO TENSORFLOW IN R

  17. E v al u ating y o u r linear regression model Yo u r � nal w eight and bias from y o u r training step can be accessed u sing : sess$run(w) and sess$run(b) From here , w e can plot the di � erence bet w een the act u al v al u es and o u r predicted v al u es . beer_actualconsumption <- beer_consumption_test$beer_consumed beer_predictedconsumption <- sess$run(w)*beer_consumption_test$precip+sess$run(b) plot(beer_actualconsumption, beer_predictedconsumption) lines(beer_actualconsumption, beer_actualconsumption) INTRODUCTION TO TENSORFLOW IN R

  18. E v al u ating y o u r linear regression model ...w hich w ill gi v e y o u an o u tp u t of something like this : INTRODUCTION TO TENSORFLOW IN R

  19. Calc u late the prediction acc u rac y of y o u r model We can calc u late the prediction acc u rac y dependent on a correlation bet w een o u r act u al and predicted v al u es : meandiff <- data.frame(cbind(beer_actualconsumption, beer_predictedconsumption)) correlation_accuracy <-cor(meandiff) correlation_accuracy beer_actualconsumption beer_predictedconsumption beer_actualconsumption 1.0000000 0.6018578 beer_predictedconsumption 0.6018578 1.0000000 INTRODUCTION TO TENSORFLOW IN R

  20. Let ' s practice ! IN TR OD U C TION TO TE N SOR FL OW IN R

  21. Estimators API : m u ltiple linear regression IN TR OD U C TION TO TE N SOR FL OW IN R Colleen Bobbie Instr u ctor

  22. Adding a fe w more v ariables to o u r dataset In o u r last lesson , w e concl u ded that one v ariable likel y isn ' t eno u gh to predict beer cons u mption in o u r u ni v ersit y to w n . Let ' s add a fe w more predictor v ariables , incl u ding : Minim u m Dail y Temperat u re Ma x im u m Dail y Temperat u re Is da y a w eekend ( Y / N ) INTRODUCTION TO TENSORFLOW IN R

  23. Defining feat u re col u mns The � rst step in creating a canned model w ith the Estimators API is to de � ne feat u re col u mns . These are y o u r independent a � rib u tes that w ill be u sed to predict y o u r dependent v ariable . There are 10 canned t y pes incl u ding : numeric_column ( for integers ) categorical_column_with_identity ( for categorical col u mns , s u ch as o u r weekend v ariable ) INTRODUCTION TO TENSORFLOW IN R

  24. Defining feat u re col u mns We can de � ne feat u re col u mns u sing : ftr_colns <- feature_columns( tf$feature_column$numeric_column("numericcolumnname"), tf$feature_column$categorical_column_with_identity( "categoricalcolumnname", NumCategories) ) INTRODUCTION TO TENSORFLOW IN R

  25. Choosing y o u r model There are si x canned models to choose from in Estimators : linear_regressor or linear_classifier dnn_regressor or dnn_classifier dnn_linear_combined_regressor or dnn_linear_combined_classifier To choose a model : nameofyourmodel <- linear_regressor(feature_columns = ftr_colns) INTRODUCTION TO TENSORFLOW IN R

  26. Defining inp u t f u nction Estimators also needs an inp u t f u nction , w hich de � nes y o u r dataset , y o u r feat u res v ariables , and y o u r response v ariable . This looks like : nameofyourinputfunction = function(data){ input_fn(data, features = c("feature1", "feature2"...), response = "responsevariable") } Note that data is a constant and w ill be inp u t later w hen w e r u n the f u nction . INTRODUCTION TO TENSORFLOW IN R

  27. Training and e v al u ating y o u r model To train y o u r model , simpl y inp u t y o u r model name , the inp u t f u nction y o u de � ned earlier , and the name of y o u r training dataset . train(nameofyourmodel, nameofyourinputfunction(trainingdatasetname)) To e v al u ate y o u r model , replace the train abo v e , w ith evaluate , make s u re to specif y y o u r testing dataset and sa v e as a ne w v ariable : modeleval <- evaluate(nameofyourmodel, nameofyourinputfunction(testingdatasetname)) modeleval INTRODUCTION TO TENSORFLOW IN R

  28. Let ' s practice ! IN TR OD U C TION TO TE N SOR FL OW IN R

Recommend


More recommend