-
[Machine Learning] Regression & Classification๐ณDev/Machine Learning 2022. 1. 10. 15:21
๋ชจ๋๋ฅผ ์ํ ๋ฅ๋ฌ๋ (๊น์ฑํ)
Colab_ML01-02
Colab_ML03
Colab_ML04
Colab_ML05
Colab_ML06
Regression
1. Linear Regression
ํ๋์ ๋ ๋ฆฝ๋ณ์ x์ ์ํด ์ข ์๋ณ์ y๊ฐ ๊ฒฐ์ ๋๋ ์ ํ ์๊ด๊ด๊ณ๋ฅผ ๊ฐ์ง๋ค.
- data
- Hypothesis: H(x) = Wx + b(W: weight, b: bias)
cost๊ฐ ๊ฐ์ฅ ์์ ๊ฐ์ค์ ์ ํ
cost function(loss function) = cost(W, b) = E( square(H(x) - y) ) - goal: Minimize cost
Code
#Gradient Desendent Algorithm import numpy as np import tensorflow as tf #1.dataSet ์ค์ x_train = [1, 2, 3, 4] y_train = [2, 3, 4, 5] #2.Model ๊ตฌ์ฑ tf.model = tf.keras.Sequential() # units == output shape, input_dim == input shape tf.model.add(tf.keras.layers.Dense(units=1, input_dim=1)) #3. Model ํ์ต๊ณผ์ ์ค์ # SGD == standard gradient descendent, lr == learning rate sgd = tf.keras.optimizers.SGD(lr=0.1) # mse == mean_squared_error tf.model.compile(loss='mse', optimizer=sgd) # prints summary of the model to the terminal tf.model.summary() #4. Model ํ์ต # fit() executes training # ํ ๋ฒ์ epoch์์ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ํ๋ฒ์ ๋ฃ์ ์ X, ๋ช ๋ฒ ๋๋์ด์ ์ฃผ๋๊ฐ๋ฅผ iteration, ๊ฐ iteration๋ง๋ค ์ฃผ๋ ๋ฐ์ดํฐ ์ฌ์ด์ฆ๋ฅผ batch size tf.model.fit(x_train, y_train, epochs = 100) #5. Model ์ฌ์ฉํ๊ธฐ # predict() returns predicted value y_predict = tf.model.predict(np.array([5, 4, 3])) print(y_predict)Result


2. Multi-Variable Linear Regression
Hypothesis and Cost
Cost(W,b) = E( square(H(X)-Y) ) (X = [x], Y = [y] : training set
์ด๋ ์ฌ๋ฌ H(x)์ค์์ cost๋ฅผ ์ต์๊ฐ์ผ๋ก ๊ฐ์ง๋ ๊ฐ์ค์ ์ ํํ๋ค.
(๊ทธ๋ํ์์ W๊ฐ์ด x์ถ, cost(W,b)๊ฐ y์ถ์ ์์น)Gradient Descent Algorithm
์์์ W์ ์์์ ๊ธฐ์ธ๊ธฐ๋ฅผ ํตํด, W๊ฐ์ ์ฆ๊ฐ/๊ฐ์๊ฐ ๊ฒฐ์ ๋๋ค. W๋ฅผ ์์ง์ฌ์ ๊ฐ์ฅ ๊ธฐ์ธ๊ธฐ๊ฐ ์์ ๊ณณ(0์ ์๋ ดํ๋ W๊ฐ)์ ์ฐพ์๊ฐ๋ ์๊ณ ๋ฆฌ์ฆ.

๊ธฐ์ธ๊ธฐ๊ฐ ์์์ด๋ฉด ๋ค์ W๊ฐ๋ณด๋ค ์์ W์ ์ผ์ชฝ์ผ๋ก ์ด๋ํ๊ณ , ๊ธฐ์ธ๊ธฐ๊ฐ ์์์ด๋ฉด ๋ค์ W๊ฐ๋ณด๋ค ํฐ W์ ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋ํ๋ค.
(W์์ ๋นผ๊ณ ์๋ ๊ฐ์ LearningRate(a) * Cost'(: Cost์ ๊ธฐ์ธ๊ธฐ))
(๊ณ์ฐ์ ํฐ ์ํฅ์ ์ฃผ์ง ์๋ ๊ฐ๋ค์ ๊ฐ๋ตํ๊ฒ ์ฒ๋ฆฌ)Gradient Descent Algorithm์ผ๋ก Linear Regression์ ์งํํ ๋,
Cost Function์ Convex Function(๋ณผ๋ก ํจ์)Code
##Graph ๊ทธ๋ฆฌ๊ธฐ import numpy as np import tensorflow as tf import matplotlib.pyplot as plt #1.data set ์ค์ x_train = [1, 2, 3, 4] y_train = [1, 2, 3, 4] #2.Model ๊ตฌ์ฑ tf.model = tf.keras.Sequential() # units == output shape, input_dim == input shape tf.model.add(tf.keras.layers.Dense(units=1, input_dim=1)) #3. Model ํ์ต๊ณผ์ ์ค์ # SGD == standard gradient descendent, lr == learning rate sgd = tf.keras.optimizers.SGD(lr=0.1) # mse == mean_squared_error, 1/m * sig (y'-y)^2 tf.model.compile(loss='mse', optimizer=sgd) # prints summary of the model to the terminal tf.model.summary() #4. Model ํ์ต # fit() trains the model and returns history of train history = tf.model.fit(x_train, y_train, epochs=100) #6. Model ์ฌ์ฉํ๊ธฐ # predict() returns predicted value y_predict = tf.model.predict(np.array([5, 4])) print(y_predict) # Plot training & validation loss values plt.plot(history.history['loss']) plt.title('Model loss') plt.ylabel('Loss') plt.xlabel('Epoch') plt.legend(['Train', 'Test'], loc='upper left') plt.show()Result


Multi-Variable Linear Regression
H(x) = Wx + b์ผ ๋๋ ๋ณ์๊ฐ ํ ๊ฐ, ๊ทธ๋ฌ๋ ๋ณ์๊ฐ ์ฌ๋ฌ๊ฐ์ธ ์ํฉ์์๋ Matrix์ ๊ณฑ์ ์ ์ด์ฉํ์ฌ ๊ฐ๋ตํํ ์ ์๋ค. ๋ฐ๋ผ์ ํ๋ ฌ์ ๊ณฑ์ ํน์ง์ ์ดํดํ๋ ๊ฒ์ด ์ข๋ค.

X ํ๋ ฌ์ [a, b]๋ผ๊ณ ํํํ ๋, a๋ instance์ ๊ฐ์, b๋ variable์ ๊ฐ์๋ฅผ ์๋ฏธํ๋ค.
W ํ๋ ฌ์ [b, c]๋ผ๊ณ ํํํ ๋, b๋ variable์ ๊ฐ์, c๋ ๊ฐ์ค์ X์ ๋ํ Y๊ฐ์ ๊ฐ์๋ฅผ ์๋ฏธํ๋ค.(Linear์์, c = 1)
X ํ๋ ฌ๊ณผ Y ํ๋ ฌ์ ๊ณฑ์ด๋ฏ๋ก, H ํ๋ ฌ์ [a, c]๋ผ๊ณ ํํํ ์ ์๋ค.
Matrix๋ฅผ ์ด์ฉํ๋ฉด, W์ X์ ์์๊ฐ ๋ฐ๋๋ ๊ฒ์ ๋ณผ ์ ์๋ค. ๋ฐ๋ผ์ H(X) = XW๋ผ๊ณ ํ๊ธฐํ๋ฉด, ํ๋ ฌ์ ์๋ฏธํ๋ค. ๊ฒฐ๋ก ์ ์ผ๋ก ๊ฐ์ค์ด๋ ๊ฐ์ ์๋ฏธ๋ฅผ ๊ฐ์ง๋ค.
Code
##multi-variable regression import tensorflow as tf import numpy as np #1.dataSet ์ค์ x_data = [[73., 80., 75.], [93., 88., 93.], [89., 91., 90.], [96., 98., 100.], [73., 66., 70.]] y_data = [[152.], [185.], [180.], [196.], [142.]] #2.Model ๊ตฌ์ฑ tf.model = tf.keras.Sequential() # input_dim=3 gives multi-variable regression tf.model.add(tf.keras.layers.Dense(units=1, input_dim=3)) # linear activation is default tf.model.add(tf.keras.layers.Activation('linear')) #3. Model ํ์ต๊ณผ์ ์ค์ tf.model.compile(loss='mse', optimizer=tf.keras.optimizers.SGD(lr=1e-5)) #4. Model ์ถ๋ ฅ tf.model.summary() #5. Model ํ์ต history = tf.model.fit(x_data, y_data, epochs=100) #6. Model ์ฌ์ฉํ๊ธฐ y_predict = tf.model.predict(np.array([[72., 93., 90.],[60., 80., 80]])) print(y_predict)Result


Logistic Classification
3. Binary Classification
์ด์ฐ์ ์ธ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ง๋ฉฐ, 0๊ณผ 1๋ก์ encoding์ด ํ์ํ๋ค.
spam detection: spam or ham, facebook feed์ show or hide

Hypothesis
.png)
.png)
-log(x) / -log(1-x)

sigmoid
Binary Classification์ 0๋๋ 1์ ๊ฐ์ ๊ฐ์ง๊ธฐ ๋๋ฌธ์, ๊ฒฐ๊ณผ๊ฐ์ด 0~1์ธ Sigmoid(S์ํ๊ณก์ )์ ์ด์ฉํ ๊ฐ์ค์ ์ธ์ธ ์ ์๋ค. ์์ H(x)๊ฐ sigmoid ํจ์์ด๋ฉฐ, logistic cost ํจ์๋ผ๊ณ ๋ ํ๋ค. Cost ์ต์ํ๋ Gradient Descent Optimizer๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํํ๋ค.
Cost

H(x) = 1H(x) = 0Y = 1 0 INF Y = 0 INF 0 Code
##binary classification import tensorflow as tf #1.dataSet ์ค์ x_data = [[1, 2],[2, 3],[3, 1],[4, 3],[5, 3],[6, 2]] y_data = [[0], [0], [0], [1], [1], [1]] #2.Model ๊ตฌ์ฑ tf.model = tf.keras.Sequential() tf.model.add(tf.keras.layers.Dense(units=1, input_dim=2)) tf.model.add(tf.keras.layers.Activation('sigmoid')) #3. Model ํ์ต๊ณผ์ ์ค์ tf.model.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.SGD(lr=0.01), metrics=['accuracy']) #4. Model ์ถ๋ ฅ tf.model.summary() #5. Model ํ์ต history = tf.model.fit(x_data, y_data, epochs=100)#return np.array #6. Model ์ฌ์ฉํ๊ธฐ print("Accuracy: ", history.history['accuracy'][-1])Result


4. Multinomial Classification
์ฌ๋ฌ๊ฐ์ ์ ํ์ง ์ค ํ๋๋ฅผ ์ ํํ๋ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ง๋ค.

์์ ๊ฐ์ด ์ธ๊ฐ์ง๋ก ๋ถ๋ฅํ ์ ์๋ค๊ณ ๊ฐ์ ํ ๋, 3๊ฐ์ง Binary Classification์ ์งํํ์ฌ Multinomial Classification์ ๊ตฌํํ๋ค.
Hypothesis

์ธ๊ฐ์ง ๊ฐ์ค์ ํ๋ ฌ์ ์ฌ์ฉํ์ฌ ๊ฐ๋ตํํ๋ค. binary์ ๊ฒฝ์ฐ์๋ sigmoid ํจ์๋ฅผ ์ฌ์ฉํ์ง๋ง, ์ฌ๊ธฐ์๋ softmax ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์์ธก๊ฐ์ ๋ฐํํ๋ค. softmax๋ 0๋ถํฐ 1์ฌ์ด์ ๊ฐ์ ๋ฐํํ๋ฉฐ, ๋ชจ๋ ์ถ๋ ฅ๊ฐ์ ํฉ์ด 1์ด๋ค. One-Hot-Encoding์ ๋ฐฐ์ด ๋ด ๊ฐ์ฅ ํฐ ๊ฐ์ 1๋ก, ๋๋จธ์ง ๊ฐ๋ค์ 0์ผ๋ก ๋ฐํํ๋ค. ๊ฒฐ๊ตญ Y_data์ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์จ๋ค.
Cost

Cross-Entropy
: ๊ฒฐ๋ก ์ ์ผ๋ก Logistic cost function๊ณผ ๊ฒฐ๊ณผ๊ฐ ๊ฐ๋ค. Cost ์ต์ํ๋ Gradient Descent Optimizer๋ฅผ ์ฌ์ฉํ๋ค.Code
## multinominal classification import tensorflow as tf import numpy as np #1.dataSet ์ค์ x_raw = [[1, 2, 1, 1], [2, 1, 3, 2], [3, 1, 3, 4], [4, 1, 5, 5], [1, 7, 5, 5], [1, 2, 5, 6], [1, 6, 6, 6], [1, 7, 7, 7]] y_raw = [[0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 1, 0], [0, 1, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0]] #One Hot Encoding x_data = np.array(x_raw, dtype=np.float32) y_data = np.array(y_raw, dtype=np.float32) nb_classes = 3 #2. Model ๊ตฌ์ฑ tf.model = tf.keras.Sequential() tf.model.add(tf.keras.layers.Dense(input_dim=4, units=nb_classes, use_bias=True)) # use softmax activations: softmax = exp(logits) / reduce_sum(exp(logits), dim) tf.model.add(tf.keras.layers.Activation('softmax')) #3. Model ํ์ต๊ณผ์ ์ค์ # use loss == categorical_crossentropy tf.model.compile(loss='categorical_crossentropy', optimizer=tf.keras.optimizers.SGD(lr=0.1), metrics=['accuracy']) #4. Model ์ถ๋ ฅ tf.model.summary() #5. Model ํ์ต history = tf.model.fit(x_data, y_data, epochs=1000) #6. Model ์ฌ์ฉ print('--------------') #์ถ๋ ฅ : One-hot encoding # argmax : array์์ ๊ฐ์ฅ ํฐ๊ฐ์ ์ถ๋ ฅ a = tf.model.predict(np.array([[1, 11, 7, 9]])) print(a, tf.keras.backend.eval(tf.argmax(a, axis=1))) print('--------------') # or use argmax embedded method, predict_classes c = tf.model.predict(np.array([[1, 1, 0, 1]])) c_onehot = tf.model.predict_classes(np.array([[1, 1, 0, 1]])) print(c, c_onehot) print('--------------') all = tf.model.predict(np.array([[1, 11, 7, 9], [1, 3, 4, 3], [1, 1, 0, 1]])) all_onehot = tf.model.predict_classes(np.array([[1, 11, 7, 9], [1, 3, 4, 3], [1, 1, 0, 1]])) print(all, all_onehot)Result


'๐ณDev > Machine Learning' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Machine Learning] Basic Of Machine Learning (0) 2022.01.15 [Machine Learning] Learning Rate, Data Preprocessing, Overfitting and DataSet (0) 2022.01.12 [๊ธฐ๊ณํ์ต] 12. Dimensionality Reduction (0) 2022.01.01 [๊ธฐ๊ณํ์ต] 11. Kernel Method(Support Vector Machines) (0) 2021.12.26 [๊ธฐ๊ณํ์ต] 10. Deep Neural Networks (0) 2021.12.25