import matplotlib as mpl
import matplotlib. pyplot as plt
% matplotlib inline
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import tensorflow as tf
from tensorflow import keras
from sklearn. datasets import fetch_california_housing
housing = fetch_california_housing( )
data = housing. data
target = housing. target
print ( "data.shape = " , housing. data. shape)
print ( "target.shape = " , housing. target. shape)
from sklearn. model_selection import train_test_split
x_train_all , x_test , y_train_all , y_test = train_test_split(
data, target, random_state= 7 , test_size= 0.25
)
print ( "x_train_all.shape = " , x_train_all. shape)
x_train , x_valid , y_train, y_valid = train_test_split(
x_train_all, y_train_all, random_state = 11 , test_size= 0.25
)
print ( "x_train.shape = " , x_train. shape)
data.shape = (20640, 8)
target.shape = (20640,)
x_train_all.shape = (15480, 8)
x_train.shape = (11610, 8)
from sklearn. preprocessing import StandardScaler
scalar = StandardScaler( )
x_train_scaled = scalar. fit_transform( x_train)
x_valid_scaled = scalar. transform( x_valid)
x_test_valid = scalar. transform( x_test)
class WideDeepModel ( keras. models. Model) :
def __init__ ( self) :
super ( WideDeepModel, self) . __init__( )
"""定義模型的層次"""
self. hidden1_layer = keras. layers. Dense( 30 , activation= "relu" )
self. hidden2_layer = keras. layers. Dense( 30 , activation= "relu" )
self. output_layer = keras. layers. Dense( 1 )
def call ( self, input ) :
"""完成模型的正向計算"""
hidden1 = self. hidden1_layer( input )
hidden2 = self. hidden2_layer( hidden1)
concat = keras. layers. concatenate( [ input , hidden2] )
output = self. output_layer( concat)
return output
model = WideDeepModel( )
model. build( input_shape= ( None , 8 ) )
model. summary( )
model. compile ( loss= "mean_squared_error" , optimizer= "sgd" )
Model: "wide_deep_model_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_6 (Dense) multiple 270
_________________________________________________________________
dense_7 (Dense) multiple 930
_________________________________________________________________
dense_8 (Dense) multiple 39
=================================================================
Total params: 1,239
Trainable params: 1,239
Non-trainable params: 0
_________________________________________________________________
history = model. fit(
x_train_scaled,
y_train,
validation_data= ( x_valid_scaled, y_valid) ,
epochs= 10 ,
)
Train on 11610 samples, validate on 3870 samples
Epoch 1/10
11610/11610 [==============================] - 1s 88us/sample - loss: 2.0550 - val_loss: 0.8995
Epoch 2/10
11610/11610 [==============================] - 1s 70us/sample - loss: 0.7359 - val_loss: 0.7515
Epoch 3/10
11610/11610 [==============================] - 1s 67us/sample - loss: 0.6680 - val_loss: 0.7048
Epoch 4/10
11610/11610 [==============================] - 1s 67us/sample - loss: 0.6313 - val_loss: 0.6697
Epoch 5/10
11610/11610 [==============================] - 1s 68us/sample - loss: 0.6018 - val_loss: 0.6402
Epoch 6/10
11610/11610 [==============================] - 1s 68us/sample - loss: 0.5788 - val_loss: 0.6162
Epoch 7/10
11610/11610 [==============================] - 1s 71us/sample - loss: 0.5592 - val_loss: 0.5960
Epoch 8/10
11610/11610 [==============================] - 1s 70us/sample - loss: 0.5424 - val_loss: 0.5790
Epoch 9/10
11610/11610 [==============================] - 1s 69us/sample - loss: 0.5285 - val_loss: 0.5645
Epoch 10/10
11610/11610 [==============================] - 1s 69us/sample - loss: 0.5156 - val_loss: 0.5502
def plot_learning_curves ( history) :
pd. DataFrame( history. history) . plot( figsize= ( 10 , 6 ) )
plt. grid( True )
plt. gca( ) . set_ylim( 0 , 1 )
plt. show( )
plot_learning_curves( history)
model. evaluate( x_test_valid, y_test)
5160/5160 [==============================] - 0s 36us/sample - loss: 0.5292
0.5292306062787078