<jupyter_start><jupyter_text>第3个tensorflow的例子，使用keras api对70年代末和80年代初汽车的燃油效率进行预测，这是一个回归问题。 一、导入module。<jupyter_code># import modules
import pathlib

import tensorflow as tf
from tensorflow import keras

import pandas as pd
import numpy as np

import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns

# 查看各个module的版本
print('tensorflow version: ', tf.__version__)
print('numpy version: ', np.__version__)
print('matplot version: ', mpl.__version__)
print('seaborn version: ', sns.__version__)<jupyter_output>tensorflow version:  1.13.0-rc1
numpy version:  1.14.6
matplot version:  3.0.2
seaborn version:  0.7.1<jupyter_text>二、导入数据。<jupyter_code># 从网上下载auto mpg开放的数据集
dataset_path = keras.utils.get_file("auto-mpg.data", "https://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data")
print('dataset_path: ', dataset_path)
# 使用pandas进行数据导入
column_names = ['MPG','Cylinders','Displacement','Horsepower','Weight','Acceleration','Model Year','Origin'] 
raw_dataset = pd.read_csv(dataset_path, names = column_names, na_values = '?', comment='\t', sep=' ', skipinitialspace=True)
# 复制一份原始数据是个好习惯
dataset = raw_dataset.copy()
# 查看样例数据
print('data examples:')
dataset.tail()<jupyter_output>Downloading data from https://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data
32768/30286 [================================] - 0s 1us/step
dataset_path:  /root/.keras/datasets/auto-mpg.data
data examples:<jupyter_text>三、数据预处理，对数据进行清洗，正则化等。<jupyter_code># 统计na值
dataset.isna().sum()
# 丢弃异常行
dataset = dataset.dropna()
# 对标称型的数据进行one-hot编码
origin = dataset['Origin']
dataset['USA'] = (origin == 1) * 1.0
dataset['Europe'] = (origin == 2) * 1.0
dataset['Japan'] = (origin == 3) * 1.0
# 查看样例
dataset.head()
# 将数据分割成训练集、测试集
# 随机抽取80%的数据作为训练集，0为随机种子
train_dataset = dataset.sample(frac=0.8, random_state = 0)
# 剩下的为测试信
test_dataset = dataset.drop(train_dataset.index)
# 查看数据
sns.pairplot(train_dataset, vars = ['MPG', 'Cylinders', 'Displacement', 'Weight'], diag_kind = 'kde')
# 使用不同颜色区分不同国家的数据
sns.pairplot(train_dataset, vars = ['MPG', 'Cylinders', 'Displacement', 'Weight'], diag_kind = 'kde', hue="Origin", palette="husl")
# 查看统计数据，分析数据是否合理
train_stats = train_dataset.describe()
# MPG不是特征项
train_stats.pop("MPG")
# 将表格转置，便于后面进行正则化
train_stats = train_stats.transpose()
train_stats
# 分割特征与标签
train_labels = train_dataset.pop('MPG')
test_labels = test_dataset.pop('MPG')
# 对数据进行正则化
def norm(x):
  return (x - train_stats['mean']) / train_stats['std']
normed_train_data = norm(train_dataset)
normed_test_data = norm(test_dataset)<jupyter_output><empty_output><jupyter_text>四、构建模型<jupyter_code># 定义一个函数构建模型
def build_model():
  model = keras.Sequential([
      # input_shape表示输入是一个尺寸为 (*, 16) 的数组
      # 64表示输出数组的尺寸为 (*, 64)
      keras.layers.Dense(64, activation=tf.nn.relu, input_shape=[len(train_dataset.keys())]),
      keras.layers.Dense(64, activation=tf.nn.relu),
      keras.layers.Dense(1)
  ])
  model.compile(loss='mse', optimizer=tf.train.RMSPropOptimizer(0.001), metrics=['mae', 'mse'])
  return model

model = build_model()
# 打印模型概述信息
model.summary()<jupyter_output>WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/losses_utils.py:170: to_float (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 64)                704       
_________________________________________________________________
dense_1 (Dense)              (None, 64)                4160      
_____________________________________________[...]<jupyter_text>五、训练模型<jupyter_code>normed_train_data.shape
normed_train_data.head()
EPOCHS = 1000
# 由于1000较大，因此定义一个打印函数，每个周期打印一个点，100个点换一行
class PrintDot(keras.callbacks.Callback):
  def on_epoch_end(self, epoch, logs):
    if epoch % 100 == 0: print('')
    print('.', end='')

model = build_model()
# verbose: 0, 1 或 2。日志显示模式。 0 = 安静模式, 1 = 进度条, 2 = 每轮一行。
history = model.fit(normed_train_data, train_labels, epochs=EPOCHS, validation_split = 0.2, verbose=0, callbacks=[PrintDot()])
# 查看训练过程
hist = pd.DataFrame(history.history)
hist['epoch'] = history.epoch
hist.tail()

# 画图
def plot_history(history):
  hist = pd.DataFrame(history.history)
  hist['epoch'] = history.epoch
  
  plt.figure()
  plt.xlabel('Epoch')
  plt.ylabel('Mean Abs Error [MPG]')
  plt.plot(hist['epoch'], hist['mean_absolute_error'],
           label='Train Error')
  plt.plot(hist['epoch'], hist['val_mean_absolute_error'],
           label = 'Val Error')
  plt.legend()
  plt.ylim([0,5])
  
  plt.figure()
  plt.xlabel('Epoch')
  plt.ylabel('Mean Square Error [$MPG^2$]')
  plt.plot(hist['epoch'], hist['mean_squared_error'],
           label='Train Error')
  plt.plot(hist['epoch'], hist['val_mean_squared_error'],
           label = 'Val Error')
  plt.legend()
  plt.ylim([0,20])

plot_history(history)
# 从上图可以看到100个周期后收敛效果已经不明显，且在验证集上出现了误差上升的情况，说明模型已经过拟合，因此需要提前结束训练，可使用回调函数实现此功能。
model = build_model()

# patience参数用于检查误差上升次数
early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=10)

history = model.fit(normed_train_data, train_labels, epochs=EPOCHS, validation_split = 0.2, verbose=0, callbacks=[early_stop, PrintDot()])

plot_history(history)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15543684301223504, 'fraction_numerical': 0.021023608478373257, 'mean_word_length': 5.6331428571428575, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '294137', 'license': 'Apache-2.0', 'n_tokens_mistral': 2370, 'n_tokens_neox': 2382, 'n_words': 359, 'path': 'fuel_regression.ipynb', 'repo_name': 'hedgehog-zowie/tf-study', 'size': '5803'}