欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

wide & deep 模型

程序员文章站 2022-07-13 13:10:36
...

我们可以把一些比较常见的特征直接把他记录下来作为wide模型,把一些需要泛化的特征让他进行深度学习,形成deep模型,所以显而易见,wide模型使用多类型的叉乘特征变换,记忆特定的特征组合,但它的限制就是难以归纳以前没出现过的组合,那这就需要人工提供特征。而deep模型虽然较为复杂,但是可以提高整体模型的泛化水平,这也就是线性模型+DNN的组合。
wide & deep 模型

import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
import numpy as np
import random
import pandas as pd
import os 
import sys
#导入数据集
from sklearn.datasets import fecth_california_housing
housing = fectch_california_housing()
#用sklearn进行数据集的划分
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_vaild,y_train,y_vaild = train_test_split(
	x_train_all,y_train_all,random.state = 11 ,test_size = 0.2
)

#对数据集归一化
from sklearn.preprocessing import StandardScaler
#创建一个类
scaler = StandardScaler()
x_train_scaler = scaler.fit_transform(x_train)
y_vaild_scaler = scaler.transform(x_vaild)
x_test_scaler = scaler.transform(x_test)

#搭建模型
#deep model
input = keras.layers.Input (shape = x_train.shape[1:])
hidden1 = keras.layers.Dense(30,activation = 'relu')(input)
hidden2 = keras.layers.Dense(30,activation = 'relu')(hidden1)
#将deep模型的输出和wide模型的输入拼接起来(假设deep和wide的input相同)
concat = keras.layers.concatenate([input,hidden2])
output = keras.layers.Dense(1)(concat)

model = keras.models.Model(inputs = [input],
			   outputs = [output])
#编译并且训练模型
model.compile(loss = 'mse' , optimizer = 'adam')
model.fit(x_train_scaler,y_train,
	  validation_data =(x_valid_scaler,y_valid),
	  epochs = 10)
mode1.evaluate(x_test_scaler,y_test)