07. Multi-variables for Linear Regression

Multi-variables for linear regression

tensorflow

TOC

3 Equations for Linear Regression

  • Hypothesis

$$ H(x) = Wx + b $$

  • Cost function

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

  • Gradient descent

$$ W_new = W - \alpha {\frac{1}{m} \sum_{i=1}^m ((W * x_{i}) - y_{i}) * x_i} $$

Multi-variable for Linear Regression

  • If one data set consists of multiple inputs, how do we care all of them?

  • If there are multiple data sets, what should we do for them?

  • Example:

    • We will predict the last test score from previous 3 test scores.
    • There are 6 students, and we know all scores for 5 students and 3 previous scores for target student.
Student test1 test2 test3 final
A 70 80 75 75
B 90 90 90 95
C 50 70 80 70
D 80 90 85 95
E 90 95 90 95
F 80 80 80 ?
Table 1. Scores of Students

Hypothesis & Cost Function for Multi-variable

  • Hypothesis

$$ H(x_1, x_2, x_3, ..., x_n) = w_1x_1 + w_2x_2 + w_3x_3 + ... + w_nx_n b $$

  • Cost function

$$ cost(W, b) = \frac{1}{m} \sum_{i=1}^m (H(x_{i1}, x_{i2}, x_{i3}, ..., x_{in}) - y_i)^ 2 $$

  • Gradient descent equation is difficult to show itself as simple equation, because it derives partially n weight parameters.

Matrix

  • A rectangular array of numbers, symbols, or expressions, arranged in row and columns.1
Matrix
Image 1. Matrix
  • Matrix is a good mathematical expression for multi-variables.

  • For multi-variables, matrix can expand columns.

  • Matrix expression example for single data set with multi-variables.

$$ w_!x_1 + w_2x_2 + w_3x_3 + ... + w_nx_n $$

$$ \begin{bmatrix} x_1 & x_2 & x_3 \end{bmatrix} \cdot \begin{bmatrix} w_1 \\ w_2 \\ w_3 \end{bmatrix} = \begin{bmatrix} x_1w_1 + x_2w_2 + x_3w_3 \end{bmatrix} $$

  • For multiple data sets(instances), matrix can expand rows.
  • Matrix expression example for multi-instances with multi-variables.

$$ \begin{bmatrix} x_{11} & x_{12} & x_{13} \\ x_{21} & x_{22} & x_{23} \\ x_{31} & x_{32} & x_{33} \\ x_{41} & x_{42} & x_{43} \end{bmatrix} \cdot \begin{bmatrix} w_1 \\ w_2 \\ w_3 \end{bmatrix} = \begin{bmatrix} x_{11}w_1 + x_{12}w_2 + x_{13}w_3 \\ x_{21}w_1 + x_{22}w_2 + x_{23}w_3 \\ x_{31}w_1 + x_{32}w_2 + x_{33}w_3 \\ x_{41}w_1 + x_{42}w_2 + x_{43}w_3 \end{bmatrix} $$

  • These expressions are so complex, so we use simplified matrix expression.

$$ H(X) = XW + B $$

  • Usually, capital letter means matrix.
  • Previously, we used Wx, but here is XW because of follwing matrix rule.

Determine the Size of Weight and Bias

  • For Deep Learning, inputs and answer are given, but we need to design weight and bias.
  • From the input and answer, the size od weight and bias are determined.
  • At the previous examples, input is 4 x 3 matrix, and answer is 4 x 1 matrix.
  • In this case, weight is 3 * 1. The number of rows should be the same as the number of input's column, and the number of column should be the same as the number of answer's column. This is matrix multiplication's rule.2 If they are not the same, we could not calculate the matrix multiplication.
  • For bias, its row size is the same as input's row, and its column size is 1. Because bias is a constant value to be added, so it just needs to be the same number of instances.
multiplication
Image 2. Matrix multiplication
import numpy as np
import matplotlib.pyplot as plt

nr_student = 5

def calc_cost(W):
    # hypothesis = X * W
    hypo = np.dot(X, W)

    _mse = list(map(\
            lambda _hypo, _answer : (_hypo[0] - _answer[0]) ** 2, hypo, Y))

    sumMse = sum(_mse)

    # 1 / m * sum(X * W - Y)^2
    return 1 / nr_student * sumMse

def calc_gradient(W):
    # Euler method to calculate derivative
    # h is delta of W
    h = 1e-4

    # Defines the same size matrix with W
    grad = np.zeros_like(W)

    # For partial derivatives
    for i in range(W.size):
        tmp_val = W[i]

        # Calculate forward values
        W[i] = tmp_val + h
        fxh1 = calc_cost(W)

        # Calculate backward values
        W[i] = tmp_val - h
        fxh2 = calc_cost(W)

        # Calculate the diff
        grad[i] = (fxh1 - fxh2) / (2*h)

        W[i] = tmp_val

    return grad

# Input
X = np.array([ [70, 80, 75], \
               [90, 90, 90], \
               [50, 70, 80], \
               [80, 90, 85], \
               [90, 95, 90] ])

# Answer
Y = np.array([ [75], \
               [95], \
               [70], \
               [95], \
               [95] ])

# Weight
# W = np.full((3, 1), np.random.normal(0, 10))
# In this test, we have only 5 instances.
# Therefore, if W is started from too far, the result will be weird.
# So, I just picked closed value for the pre-trained W.
W = np.array([ [0.4], \
               [0.15], \
               [0.5] ])

# Learning rate
learning_rate = 0.0001

costs = []
steps = []

nb_train = 10
# Training
for i in range(nb_train):
    # Calculate cost
    _cost = calc_cost(W)

    # Calculate gradient descent
    gradients = calc_gradient(W)

    # Value for descent
    DV = learning_rate / nr_student * gradients

    # Update W
    W = W - DV

    steps.append(i)
    costs.append(_cost)

# Test
x = np.array([80, 80, 80])
y = np.dot(x, W)
print("Answer: {0}".format(y))

plt.plot(steps, costs, label="Costs")
plt.xlabel("trial")
plt.ylabel("Cost(W)")
plt.grid()
plt.show()
Answer:  [ 84.86409948]
output_1_1
Image 3. Costs

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: 07. Multi-variables for Linear Regression
07. Multi-variables for Linear Regression
Multi-variables 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/07-multi-variables-for-linear-regression.html
https://kunicom.blogspot.com/
https://kunicom.blogspot.com/
https://kunicom.blogspot.com/2017/06/07-multi-variables-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