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__)
2.0.0
from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing()
# print(housing.DESCR)
# print(housing.data.shape)
# print(housing.target.shape)
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)
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)
该模型只是一个例子,用来展示多输入,多输出的实现。
# 多输入,拆分输入 wide有5个特征(前5个),deep有6个特征(后6个),一共8个变量,有交集。
# 多输出,针对多个任务,这个例子中仅仅是作为一个展示用的还是房价预测。
input_wide = keras.layers.Input(shape=[5])
input_deep = keras.layers.Input(shape=[6])
#deep model 的两个隐藏层
hidden1 = keras.layers.Dense(30,activation='relu')(input_deep)
hidden2 = keras.layers.Dense(30,activation='relu')(hidden1)
concat = keras.layers.concatenate([input_wide,hidden2]) # 拼接,输入和隐藏层
output = keras.layers.Dense(1)(concat)
output2 = keras.layers.Dense(1)(hidden2)
model = keras.models.Model(inputs = [input_wide,input_deep],outputs = [output,output2]) #固定模型
# input = [input_wide,input_deep],有两个输入
# outputs = [output,output2],有两个输出
model.summary()
model.compile(loss="mean_squared_error", optimizer="adam")
callbacks = [keras.callbacks.EarlyStopping(
patience=5, min_delta=1e-2)]
#拆分数据,前5个input wide ,后6个input deep。
x_train_scaled_wide = x_train_scaled[:,:5]
x_train_scaled_deep = x_train_scaled[:,2:]
x_valid_scaled_wide = x_valid_scaled[:,:5]
x_valid_scaled_deep = x_valid_scaled[:,2:]
x_test_scaled_wide = x_test_scaled[:,:5]
x_test_scaled_deep = x_test_scaled[:,2:]
model.fit 以列表形式按顺序放好多个输入和输出
#model.fit 以列表形式按顺序放好多个输入和输出
history = model.fit([x_train_scaled_wide,x_train_scaled_deep], [y_train,y_train],
validation_data = ([x_valid_scaled_wide,x_valid_scaled_deep],[y_valid,y_valid]),
epochs = 10,
callbacks = callbacks)
def plot_learning_curves(history):
pd.DataFrame(history.history).plot(figsize=(8, 5))
plt.grid(True)
plt.gca().set_ylim(0, 3)
plt.show()
plot_learning_curves(history)