赞
踩
本文将介绍:
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)
# 复合函数: f(x) = h(g(x))
concat = keras.layers.concatenate([input, hidden2])
output = keras.layers.Dense(1)(concat)
model = keras.models.Model(inputs = [input], outputs = [output])
model.summary()
model.compile(loss="mean_squared_error",optimizer = keras.optimizers.SGD(0.001))
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np import sklearn import pandas as pd import os import sys import time import tensorflow as tf from tensorflow import keras # 1,打印使用的python库的版本信息 print(tf.__version__) print(sys.version_info) for module in mpl, np, pd, sklearn, tf, keras: print(module.__name__, module.__version__) # 2,下载并使用sklearn中的“fetch_california_housing”数据集 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,构建wide_deep回归模型、模型层级图、编译模型(添加损失函数、优化器)、添加回调函数 # 函数式API 功能API 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) # 复合函数: f(x) = h(g(x)) concat = keras.layers.concatenate([input, hidden2]) output = keras.layers.Dense(1)(concat) model = keras.models.Model(inputs = [input], outputs = [output]) model.summary() model.compile(loss="mean_squared_error",optimizer = keras.optimizers.SGD(0.001)) # 定义 callbacks logdir = './dnn-bn-callbacks' if not os.path.exists(logdir): os.mkdir(logdir) output_model_file = os.path.join(logdir,"fashion_mnist_model.h5") callbacks = [ keras.callbacks.TensorBoard(logdir), keras.callbacks.ModelCheckpoint(output_model_file,save_best_only = True), keras.callbacks.EarlyStopping(patience=5, min_delta=1e-3)] # 6,训练构建的模型 history = model.fit(x_train_scaled, y_train, validation_data = (x_valid_scaled, y_valid), epochs = 200, callbacks = callbacks) # 7,得到训练曲线图 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) # 8,调用估计器 print(model.evaluate(x_test_scaled, y_test, verbose=0))
# 子类API 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 = keras.models.Sequential([ WideDeepModel(), ]) model.build(input_shape=(None, 8)) model.summary() model.compile(loss="mean_squared_error", optimizer = keras.optimizers.SGD(0.001)) callbacks = [keras.callbacks.EarlyStopping( patience=5, min_delta=1e-2)]
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np import sklearn import pandas as pd import os import sys import time import tensorflow as tf from tensorflow import keras # 1,打印使用的python库的版本信息 print(tf.__version__) print(sys.version_info) for module in mpl, np, pd, sklearn, tf, keras: print(module.__name__, module.__version__) # 2,下载并使用sklearn中的“fetch_california_housing”数据集 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,构建wide_deep 子类API 回归模型、模型层级图、编译模型(添加损失函数、优化器)、添加回调函数 # 子类API 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 = keras.models.Sequential([ WideDeepModel(), ]) model.build(input_shape=(None, 8)) model.summary() model.compile(loss="mean_squared_error", optimizer = keras.optimizers.SGD(0.002)) callbacks = [keras.callbacks.EarlyStopping( patience=5, min_delta=1e-4)] # 6,训练构建的模型 history = model.fit(x_train_scaled, y_train, validation_data = (x_valid_scaled, y_valid), epochs = 200, callbacks = callbacks) # 7,得到训练曲线图 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) # 8,调用估计器 model.evaluate(x_test_scaled, y_test, verbose=0)
# 5,构建wide_deep 子类API 回归模型、模型层级图、编译模型(添加损失函数、优化器)、添加回调函数 # 多输入 input_wide = keras.layers.Input(shape=[5]) input_deep = keras.layers.Input(shape=[6]) 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) model = keras.models.Model(inputs = [input_wide, input_deep], outputs = [output]) model.compile(loss="mean_squared_error", optimizer="sgd") callbacks = [keras.callbacks.EarlyStopping(patience=5, min_delta=1e-3)] model.summary() # 6,训练构建的模型 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:] history = model.fit(x = [x_train_scaled_wide, x_train_scaled_deep], y = y_train, validation_data = ( [x_valid_scaled_wide, x_valid_scaled_deep], y_valid), epochs = 100, callbacks = callbacks)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np import sklearn import pandas as pd import os import sys import time import tensorflow as tf from tensorflow import keras # 1,打印使用的python库的版本信息 print(tf.__version__) print(sys.version_info) for module in mpl, np, pd, sklearn, tf, keras: print(module.__name__, module.__version__) # 2,下载并使用sklearn中的“fetch_california_housing”数据集 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,构建wide_deep 子类API 回归模型、模型层级图、编译模型(添加损失函数、优化器)、添加回调函数 # 多输入 input_wide = keras.layers.Input(shape=[5]) input_deep = keras.layers.Input(shape=[6]) 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) model = keras.models.Model(inputs = [input_wide, input_deep], outputs = [output]) model.compile(loss="mean_squared_error", optimizer="sgd") callbacks = [keras.callbacks.EarlyStopping(patience=5, min_delta=1e-3)] model.summary() # 6,训练构建的模型 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:] history = model.fit(x = [x_train_scaled_wide, x_train_scaled_deep], y = y_train, validation_data = ( [x_valid_scaled_wide, x_valid_scaled_deep], y_valid), epochs = 100, callbacks = callbacks) # 7,得到训练曲线图 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) # 8,调用估计器 print(model.evaluate([x_test_scaled_wide, x_test_scaled_deep], y_test, verbose=0))
# 多输出
input_wide = keras.layers.Input(shape=[5])
input_deep = keras.layers.Input(shape=[6])
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])
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np import sklearn import pandas as pd import os import sys import time import tensorflow as tf from tensorflow import keras # 1,打印使用的python库的版本信息 print(tf.__version__) print(sys.version_info) for module in mpl, np, pd, sklearn, tf, keras: print(module.__name__, module.__version__) # 2,下载并使用sklearn中的“fetch_california_housing”数据集 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,构建wide_deep 子类API 回归模型、模型层级图、编译模型(添加损失函数、优化器)、添加回调函数 # 多输出 input_wide = keras.layers.Input(shape=[5]) input_deep = keras.layers.Input(shape=[6]) 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]) model.compile(loss="mean_squared_error", optimizer="sgd") callbacks = [keras.callbacks.EarlyStopping( patience=5, min_delta=1e-2)] model.summary() # 6,训练构建的模型 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:] 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 = 100, callbacks = callbacks) # 7,得到训练曲线图 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) # 8,调用估计器 print(model.evaluate([x_test_scaled_wide, x_test_scaled_deep], [y_test, y_test], verbose=0))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。