Hello. I'm DVM who is talk about deep learning for 5minutes using keras
Today I'm going to talk about how to make linear regression
wiki pedia saids linear regression is
the relationships are modeled using linear predictor functions
whose unknown model parameters are estimated from the data
Let's make model
Code is very simple.
define the sequential model and add the layer.
you could adjust the number of the learning parameters.
Linear regression is learning the weight and bias like equation untill cost function is minimized.
we need to adjust learning rate.
If learning rate is big, cost funtion become divergence
if learning rate is too small, cost function fall into local minimum which is not optimization.
we adjust learning rate untill cost values is small.
It is very easy.
Let`s practice coding using keras
Let's define library¶
- Tensorflow include keras
In [0]:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras import optimizers
Let's define train datas and test datas
- we have to train using train datas
- we evaluate using test datas
In [0]:
x_train = [1,2,3,4]
y_train = [2,3,4,5]
x_test = [5,6,7]
y_test = [6,7,8]
Let's make a single model¶
In [0]:
model = Sequential()
model.add(Dense(1,input_dim=1))
Let's learn using stochastic gradient decent¶
- epochs means how many times train the datas
In [0]:
sgd = optimizers.SGD(lr=0.1)
model.compile(loss='mse',optimizer=sgd)
model.summary()
model.fit(x_train,y_train,epochs=30)
Let's check the predicted datas¶
In [0]:
y_predict = model.predict(x_test)
print('prediction:\n',y_predict)
Let's evaluate using test datas¶
In [0]:
score = model.evaluate(x_test,y_test)
print('cost',score)
Total codes¶
In [23]:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras import optimizers
import numpy as np
x_train = [1,2,3,4]
y_train = [2,3,4,5]
x_test = [5,6,7]
y_test = [6,7,8]
model = Sequential()
model.add(Dense(1,input_dim=1))
sgd = optimizers.SGD(lr = 0.1)
model.compile(loss='mse',optimizer =sgd)
model.summary()
model.fit(x_train,y_train,epochs=30)
y_predict = model.predict(x_test)
print('Prediction : \n',y_predict)
score = model.evaluate(x_test,y_test)
print('cost',score)