從零基礎入門Tensorflow2.0 ----三、7.自定義損失函數

every blog every motto: Let bygones be bygones.

0. 前言

本節自定義損失函數

1. 代碼部分

1. 導入模塊

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 time
import tensorflow as tf
from tensorflow import keras

print(tf.__version__)
print(sys.version_info)
for module in mpl,np,pd,sklearn,tf,keras:
    print(module.__name__,module.__version__)

2. 讀取數據

from sklearn.datasets import fetch_california_housing

# 房價預測
housing = fetch_california_housing()
print(housing.DESCR)
print(housing.data.shape)
print(housing.target.shape)

3. 劃分樣本

# 劃分樣本
from sklearn.model_selection import train_test_split

x_train_all,x_test,y_train_all,y_test = train_test_split(housing.data,housing.target,random_state=7)
x_train,x_valid,y_train,y_valid = train_test_split(x_train_all,y_train_all,random_state=11)

print(x_train.shape,y_train.shape)
print(x_valid.shape,y_valid.shape)
print(x_test.shape,y_test.shape)

4. 數據歸一化

# 歸一化
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
x_train_scaled = scaler.fit_transform(x_train)
x_valid_scaled = scaler.transform(x_valid)
x_test_scaled = scaler.transform(x_test)

5. 自定義損失函數

# 自定義損失函數
def customied_mse(y_true,y_pred):
    return tf.reduce_mean(tf.square(y_pred-y_true))

6. 搭建模型

# 搭建模型
model = keras.models.Sequential([
    keras.layers.Dense(30,activation='relu',input_shape=x_train.shape[1:]),
    keras.layers.Dense(1),
    
])

# 打印model信息
model.summary()
# 編譯
model.compile(loss=customied_mse,optimizer="sgd",metrics=['mean_squared_error'])
# 回調函數
callbacks = [keras.callbacks.EarlyStopping(patience=5,min_delta=1e-3)]

7. 訓練

#訓練
history = model.fit(x_train_scaled,y_train,validation_data=(x_valid_scaled,y_valid),epochs=100,callbacks=callbacks)

8. 學習曲線

# 學習曲線
def plot_learning_curves(history):
    pd.DataFrame(history.history).plot(figsize=(8,5))
    plt.grid(True)
    plt.gca().set_ylim(0,1)
    plt.show()
plot_learning_curves(history)

9. 測試集上

model.evaluate(x_test_scaled,y_test)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章