04. Cost Function for Linear Regression

Cost function for linear regression

tensorflow

TOC

Cost(Loss) Function

  • To evaluate the quantity of error for hypothesis.
  • It is the goal of machine learning to find values of W and b minimizing the error.
  • Usually, MSE(Mean Square Error) or CEE(Cross Entropy Error) are used as a cost function.

Cost Function for Linear Regression

  • Cost function for linear regression is MSE

$$ cost(W, b) = \frac{1}{m} \sum_{i=1}^m (H(x_{i})-y_{i})^2 $$

  • Simplified Hypothesis will be used for further explanations

$$ H(x) = W * x $$

$$ cost(W) = \frac{1}{m} \sum_{i=1}^m ((W * x_{i})-y_{i})^2 $$

Mean Square Error(MSE)

  • As mentioned before, the goal of machine learning is to find values for W and b minimizing the error.
  • In mathematics, if the value of derivative at particular point is 0, that point is a candidate of minimum or maximum value.
  • MSE is 2-dimensional function, so its derivative has only one single minimum point.
import numpy as np
import matplotlib.pyplot as plt

# number of points
num_points = 300

# For the sample data
# Answer values
x_ans = [i * 0.01 for i in range(-200, 200)]
W_ans = 0.1

# Generate normal random values for input and output
x = [np.random.normal(0.0, 0.55) for i in range(num_points + 1)]
y = [W_ans * _x + np.random.normal(0.0, 0.03) for _x in x]

# Draw input, output and answer line
plt.plot(x, y, "ro")
plt.plot(x_ans, [W_ans * _x for _x in x_ans], label="W=0.1")
plt.xlabel("X")
plt.ylabel("Y")
plt.legend()
plt.title("y = 0.1 * x")
plt.show()

# For the Mean Square Error
# Weight from 0 to 0.2
W = [i * 0.001 for i in range(0, 201)]
# Cost
cost = []

for w in W:
    # hypothesis = W * x
    hypo = [w * _x for _x in x]

    diffSqrts = list(map(lambda _hypo, _answer : \
                                (_hypo - _answer) ** 2, hypo, y))
    sumDiffSqrt= sum(diffSqrts)

    cost.append(1 / (len(W)) * sumDiffSqrt)

# Draw cost function
plt.plot(W, cost)
plt.title("Cost Function")
plt.xlabel("W")
plt.ylabel("Cost(W)")
plt.xlim(0.00, 0.20)
plt.show()
Image 1. Sample data and answer line
Image 2. Cost function

COMMENTS

Name

0 weights,1,abstract class,1,active function,3,adam,2,Adapter,1,affine,2,argmax,1,back propagation,3,binary classification,3,blog,2,Bucket list,1,C++,11,Casting,1,cee,1,checkButton,1,cnn,3,col2im,1,columnspan,1,comboBox,1,concrete class,1,convolution,2,cost function,6,data preprocessing,2,data set,1,deep learning,31,Design Pattern,12,DIP,1,django,1,dnn,2,Don't Repeat Your code,1,drop out,2,ensemble,2,epoch,2,favicon,1,fcn,1,frame,1,gradient descent,5,gru,1,he,1,identify function,1,im2col,1,initialization,1,Lab,9,learning rate,2,LifeLog,1,linear regression,6,logistic function,1,logistic regression,3,logit,3,LSP,1,lstm,1,machine learning,31,matplotlib,1,menu,1,message box,1,mnist,3,mse,1,multinomial classification,3,mutli layer neural network,1,Non Virtual Interface,1,normalization,2,Note,21,numpy,4,one-hot encoding,3,OOP Principles,2,Open Close Principle,1,optimization,1,overfitting,1,padding,2,partial derivative,2,pooling,2,Prototype,1,pure virtual function,1,queue runner,1,radioButton,1,RBM,1,regularization,1,relu,2,reshape,1,restricted boltzmann machine,1,rnn,2,scrolledText,1,sigmoid,2,sigmoid function,1,single layer neural network,1,softmax,6,softmax classification,3,softmax cross entropy with logits,1,softmax function,2,softmax regression,3,softmax-with-loss,2,spinBox,1,SRP,1,standardization,1,sticky,1,stride,1,tab,1,Template Method,1,TensorFlow,31,testing data,1,this,2,tkinter,5,tooltip,1,Toplevel,1,training data,1,vanishing gradient,1,Virtual Copy Constructor,1,Virtual Destructor,1,Virtual Function,1,weight decay,1,xavier,2,xor,3,
ltr
item
Universe In Computer: 04. Cost Function for Linear Regression
04. Cost Function for Linear Regression
Cost function for linear regression
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiE9QfIQg9MqxmXv8wo1jRHrMgva3N0n9uaoJIHiM44Vt8k6nlufCwcOrXM4piATO-QqQmLgh_JEZUv2KXJVRIATvdu0xwckn-JPaRyfJpu9tFP929dbQgKHcd0zfVFfe9EjSkH18A4MxU4/s0/
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiE9QfIQg9MqxmXv8wo1jRHrMgva3N0n9uaoJIHiM44Vt8k6nlufCwcOrXM4piATO-QqQmLgh_JEZUv2KXJVRIATvdu0xwckn-JPaRyfJpu9tFP929dbQgKHcd0zfVFfe9EjSkH18A4MxU4/s72-c/
Universe In Computer
https://kunicom.blogspot.com/2017/06/04-cost-function-for-linear-regression.html
https://kunicom.blogspot.com/
https://kunicom.blogspot.com/
https://kunicom.blogspot.com/2017/06/04-cost-function-for-linear-regression.html
true
2543631451419919204
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy