Tensorflow 101 - regression

decal·2022년 12월 20일
0

study notes

목록 보기
1/12
post-thumbnail

Currently I'm halfway through the 'Tensorflow 101' playlist on 이선비's YouTube.

...

Basically there are 5 steps.

1: Library (import tensorflow and pandas)
2: Data (bring past data)
3: Model (build a model)
4: Fit (use data to teach the machine)
5: Predict (use the model)

1. Library

import tensorflow as tf
import pandas as pd

2. Data

  • get data from csv file (make variables like below)
  • print the names of the columns
  • print the first five rows of data from the csv file
file_directory = '/directory/filename.csv'
data_name = pd.read_csv(file)
print(data.columns)
data.head()
  • separate the dependent and independent variables
  • check the shape of the data
independent = data_name[['name_column1', 'name_column2', ... , 'name_columnN']]
dependent = data_name[['name_columnX', ... , 'name_columnZ']]
print(independent.shape, dependent.shape)

3. Model

X = tf.keras.layers.Input(shape=[12])
Y = tf.keras.layers.Dense(2)(X)
model = tf.keras.models.Model(X, Y)
model.compile(loss='mse')

In the example above,
the number '12' in the first line = number of independent variables
the number '2' in the second line = number of dependent variables

4. Fit

  • run test for 1000 times (but don't show the results)
  • run test for 10 times (and show the results)
model.fit(independent, dependent, epochs=1000, verbose=0)
model.fit(independent, dependent, epochs=10)

Loss = mean square error = mean of (|prediction - result|)^2
The smaller the number of the loss, the better it predicts.

5. Predict

  • select which rows to predict by using 'slicing' if needed: ex. print the first five)
model.predict(independent[0:5])
dependent[0:5]
  • print answers (data from dependent variable)
dependent[0:5]
  • find the equation
model.get_weights()

real equation:
y = w1x1 + w2x2 + ... + w12x12 + b

w = weight 가중치
b = bias 편향

0개의 댓글