Simple TensorFlow Regression : MPG
We will see if we can predict the miles per gallon (MPG) for a car based on the car's weight, cylinders, engine size, and other features.
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
import pandas as pd
import io
import os
import requests
import numpy as np
from sklearn import metrics
df = pd.read_csv(
"https://data.heatonresearch.com/data/t81-558/auto-mpg.csv",
na_values=['NA', '?'])
cars = df['name']
# Handle missing value
df['horsepower'] = df['horsepower'].fillna(df['horsepower'].median())
# Pandas to Numpy
x = df[['cylinders', 'displacement', 'horsepower', 'weight',
'acceleration', 'year', 'origin']].values
y = df['mpg'].values # regression
# Build the neural network
model = Sequential()
model.add(Dense(25, input_dim=x.shape[1], activation='relu')) # Hidden 1
model.add(Dense(10, activation='relu')) # Hidden 2
model.add(Dense(1)) # Output
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(x,y,verbose=2,epochs=100)
Introduction to Neural Network Hyperparameters
Parameter vs Hyperparameter
- Parameter : 모델 내부에 존재하는 매개변수. 학습의 대상.
- Hyperparameter : 경험, 데이터의 특성 등에 근거하여 사용자가 설정하는 값. ex) learning rate
model.add(Dense(25, input_dim=x.shape[1], activation='relu')) # Hidden 1
- input_dim : the number of inputs the dataset has. The network needs one input neuron for every column in the data set (including dummy variables).
- x.shape[1] : the number of columns of dataset x
- 25 : the number of neurons in the hidden layer
- 몇 개의 hidden layer를 가질지, 각각의 layer에는 몇 개의 neuron이 필요할지 결정하는 것은 hyperparameter에 해당하기 때문에 정답이 없음.
model.add(Dense(1)) # Output
Regression neural network는 항상 1개의 output만을 가지므로 마지막 레이어는 이렇게 설정
model.compile(loss='mean_squared_error', optimizer='adam')
loss : regression neural network에서는 항상 'mean_squared_error' 사용
optimizer : 이 수업에서는 주로 'adam' 사용
model.fit(x,y,verbose=2,epochs=100)
verbose
- verbose=0 - No progress output (use with Jupyter if you do not want output).
- verbose=1 - Display progress bar, does not work well with Jupyter.
- verbose=2 - Summary progress output (use with Jupyter if you want to know the loss at each epoch).
epochs : neural network 트레인 횟수. 트레인이 진행될수록 loss가 줄어드는 것을 확인할 수 있음.
Simple TensorFlow Classification : Iris
import pandas as pd
import io
import requests
import numpy as np
from sklearn import metrics
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
from tensorflow.keras.callbacks import EarlyStopping
df = pd.read_csv(
"https://data.heatonresearch.com/data/t81-558/iris.csv",
na_values=['NA', '?'])
# Convert to numpy - Classification
x = df[['sepal_l', 'sepal_w', 'petal_l', 'petal_w']].values
dummies = pd.get_dummies(df['species']) # Classification
species = dummies.columns
y = dummies.values
# Build neural network
model = Sequential()
model.add(Dense(50, input_dim=x.shape[1], activation='relu')) # Hidden 1
model.add(Dense(25, activation='relu')) # Hidden 2
model.add(Dense(y.shape[1],activation='softmax')) # Output
model.compile(loss='categorical_crossentropy', optimizer='adam')
model.fit(x,y,verbose=2,epochs=100)
model.add(Dense(y.shape[1],activation='softmax')) # Output
category 수만큼 output 발생해야 하고, classification neural network이므로 activation='softmax'
model.compile(loss='categorical_crossentropy', optimizer='adam')
category가 2개 이상일 경우 loss='categorical_crossentropy' 사용