2.1-Tensorflow2-初学者教程-学习

1.教程链接

1.1官网

https://tensorflow.google.cn/overview

1.2 安装

豆瓣源飞起!!!(秒下!)
其他源暂时有点慢(一下午没下完。。)

python -m pip install tensorflow -i https://pypi.douban.com/simple --user

默认没有cuda
可以试试把tensorflow换为tensorflow-cpu

2.基本图像分类

官方英文教程

在这里插入图片描述

# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras

# Helper libraries
import numpy as np
import matplotlib.pyplot as plt

print(tf.__version__)

#Import the Fashion MNIST dataset
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

#查看数据,pycharm中需要加print()
train_images.shape
len(train_labels)
train_labels
test_images.shape
len(test_labels)

#Preprocess the data
plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()

train_images = train_images / 255.0
test_images = test_images / 255.0

plt.figure(figsize=(10,10))
for i in range(25):
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(train_images[i], cmap=plt.cm.binary)
    plt.xlabel(class_names[train_labels[i]])
plt.show()

#Build the model
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10)
])
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=10)

test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
print('\nTest accuracy:', test_acc)

#Make predictions
probability_model = tf.keras.Sequential([model, 
                                         tf.keras.layers.Softmax()])
predictions = probability_model.predict(test_images)        
predictions[0]      
np.argmax(predictions[0])      
test_labels[0]        

#Graph this to look at the full set of 10 class predictions.
def plot_image(i, predictions_array, true_label, img):
  true_label, img = true_label[i], img[i]
  plt.grid(False)
  plt.xticks([])
  plt.yticks([])
  plt.imshow(img, cmap=plt.cm.binary)
  predicted_label = np.argmax(predictions_array)
  if predicted_label == true_label:
    color = 'blue'
  else:
    color = 'red'
  plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                100*np.max(predictions_array),
                                class_names[true_label]),
                                color=color)
def plot_value_array(i, predictions_array, true_label):
  true_label = true_label[i]
  plt.grid(False)
  plt.xticks(range(10))
  plt.yticks([])
  thisplot = plt.bar(range(10), predictions_array, color="#777777")
  plt.ylim([0, 1])
  predicted_label = np.argmax(predictions_array)

  thisplot[predicted_label].set_color('red')
  thisplot[true_label].set_color('blue') 

#Verify predictions      
i = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions[i],  test_labels)
plt.show()  

i = 12
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions[i],  test_labels)
plt.show()    

#Let's plot several images with their predictions. Note that the model can be wrong even when very confident.
# Plot the first X test images, their predicted labels, and the true labels.
# Color correct predictions in blue and incorrect predictions in red.
num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
  plt.subplot(num_rows, 2*num_cols, 2*i+1)
  plot_image(i, predictions[i], test_labels, test_images)
  plt.subplot(num_rows, 2*num_cols, 2*i+2)
  plot_value_array(i, predictions[i], test_labels)
plt.tight_layout()
plt.show()

#Use the trained model
# Grab an image from the test dataset.
img = test_images[1]
print(img.shape)
# Add the image to a batch where it's the only member.
img = (np.expand_dims(img,0))
print(img.shape)

predictions_single = probability_model.predict(img)
print(predictions_single)
plot_value_array(1, predictions_single[0], test_labels)
_ = plt.xticks(range(10), class_names, rotation=45)
np.argmax(predictions_single[0])

2.2 TensorFlow 2 中文- fashion-MNIST 图像分类

# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras

# Helper libraries
import numpy as np

fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

#数据格式
train_images.shape # (60000, 28, 28)
len(train_labels) # 60000
train_labels # ([9, 0, 0, ..., 3, 0, 5], dtype=uint8)
test_images.shape # (10000, 28, 28)
len(test_labels) # 10000

#预处理
train_images = train_images / 255.0
test_images = test_images / 255.0

#搭建模型
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10, activation='softmax')
])
#编译模型
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
#训练模型
model.fit(train_images, train_labels, epochs=10)

#评估准确率
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('\nTest accuracy:', test_acc)
# 10000/10000 [========] - 0s 37us/sample - loss: 0.3610 - accuracy: 0.8777
# Test accuracy: 0.8777

#预测
predictions = model.predict(test_images)
predictions[0]
np.argmax(predictions[0]) # 9
test_labels[0] # 9

3.基本文本分类

电影评论文本分类

#导入包
import tensorflow as tf
from tensorflow import keras
import numpy as np

print(tf.__version__)#2.3.0

#下载并查看数据
imdb = keras.datasets.imdb
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)#参数 num_words=10000 保留了训练数据中最常出现的 10,000 个单词。
print("Training entries: {}, labels: {}".format(len(train_data), len(train_labels)))
print(train_data[0])
len(train_data[0]), len(train_data[1])

#将整数转换回单词
# 一个映射单词到整数索引的词典
word_index = imdb.get_word_index()

# 保留第一个索引
word_index = {k:(v+3) for k,v in word_index.items()}
word_index["<PAD>"] = 0
word_index["<START>"] = 1
word_index["<UNK>"] = 2  # unknown
word_index["<UNUSED>"] = 3
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])

#显示首条评论的文本:
def decode_review(text):
    return ' '.join([reverse_word_index.get(i, '?') for i in text])
decode_review(train_data[0])
#由于电影评论长度必须相同,我们将使用 pad_sequences 函数来使长度标准化:
train_data = keras.preprocessing.sequence.pad_sequences(train_data,
                                                        value=word_index["<PAD>"],
                                                        padding='post',
                                                        maxlen=256)

test_data = keras.preprocessing.sequence.pad_sequences(test_data,
                                                       value=word_index["<PAD>"],
                                                       padding='post',
                                                       maxlen=256)

len(train_data[0]), len(train_data[1])
#并检查一下首条评论(当前已经填充):
print(train_data[0])

#构建模型
# 输入形状是用于电影评论的词汇数目(10,000 词)
vocab_size = 10000

model = keras.Sequential()
model.add(keras.layers.Embedding(vocab_size, 16))
model.add(keras.layers.GlobalAveragePooling1D())
model.add(keras.layers.Dense(16, activation='relu'))
model.add(keras.layers.Dense(1, activation='sigmoid'))

model.summary()

#损失函数与优化器
model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])
#创建一个验证集              
x_val = train_data[:10000]
partial_x_train = train_data[10000:]

y_val = train_labels[:10000]
partial_y_train = train_labels[10000:]

#训练模型
history = model.fit(partial_x_train,
                    partial_y_train,
                    epochs=40,
                    batch_size=512,
                    validation_data=(x_val, y_val),
                    verbose=1)
#评估模型
results = model.evaluate(test_data,  test_labels, verbose=2)

#创建一个准确率(accuracy)和损失值(loss)随时间变化的图表
print(results)                
history_dict = history.history
history_dict.keys()    

import matplotlib.pyplot as plt

acc = history_dict['accuracy']
val_acc = history_dict['val_accuracy']
loss = history_dict['loss']
val_loss = history_dict['val_loss']

epochs = range(1, len(acc) + 1)

# “bo”代表 "蓝点"
plt.plot(epochs, loss, 'bo', label='Training loss')
# b代表“蓝色实线”
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()

plt.show()

plt.clf()   # 清除数字

plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()

plt.show()

4.回归模型

使用经典的 Auto MPG 数据集,构建了一个用来预测70年代末到80年代初汽车燃油效率的模型。

import pathlib

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
print(tf.__version__)
2.3.0
#获取数据
dataset_path = keras.utils.get_file("auto-mpg.data", "http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data")
print(dataset_path)
C:\Users\Administrator\.keras\datasets\auto-mpg.data
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(dataset.tail())
      MPG  Cylinders  Displacement  Horsepower  Weight  Acceleration  \
393  27.0          4         140.0        86.0  2790.0          15.6   
394  44.0          4          97.0        52.0  2130.0          24.6   
395  32.0          4         135.0        84.0  2295.0          11.6   
396  28.0          4         120.0        79.0  2625.0          18.6   
397  31.0          4         119.0        82.0  2720.0          19.4   

     Model Year  Origin  
393          82       1  
394          82       2  
395          82       1  
396          82       1  
397          82       1  
# 数据清洗
# 数据集中包括一些未知值。
dataset.isna().sum()
MPG             0
Cylinders       0
Displacement    0
Horsepower      6
Weight          0
Acceleration    0
Model Year      0
Origin          0
dtype: int64
dataset = dataset.dropna()#删除
origin = dataset.pop('Origin')#移除并返回
origin
0      1
1      1
2      1
3      1
4      1
      ..
393    1
394    2
395    1
396    1
397    1
Name: Origin, Length: 392, dtype: int64
dataset['USA'] = (origin == 1)*1.0
dataset['Europe'] = (origin == 2)*1.0
dataset['Japan'] = (origin == 3)*1.0
dataset.tail()
MPGCylindersDisplacementHorsepowerWeightAccelerationModel YearUSAEuropeJapan
39327.04140.086.02790.015.6821.00.00.0
39444.0497.052.02130.024.6820.01.00.0
39532.04135.084.02295.011.6821.00.00.0
39628.04120.079.02625.018.6821.00.00.0
39731.04119.082.02720.019.4821.00.00.0
# 拆分训练数据集和测试数据集
# 现在需要将数据集拆分为一个训练数据集和一个测试数据集。
# 我们最后将使用测试数据集对模型进行评估。
train_dataset = dataset.sample(frac=0.8,random_state=0)
test_dataset = dataset.drop(train_dataset.index)
# 快速查看训练集中几对列的联合分布。
sns.pairplot(train_dataset[["MPG", "Cylinders", "Displacement", "Weight"]], diag_kind="kde")
<seaborn.axisgrid.PairGrid at 0x1ce0073a5f8>

在这里插入图片描述

# 也可以查看总体的数据统计:

train_stats = train_dataset.describe()
train_stats.pop("MPG")
train_stats = train_stats.transpose()
train_stats
countmeanstdmin25%50%75%max
Cylinders314.05.4777071.6997883.04.004.08.008.0
Displacement314.0195.318471104.33158968.0105.50151.0265.75455.0
Horsepower314.0104.86942738.09621446.076.2594.5128.00225.0
Weight314.02990.251592843.8985961649.02256.502822.53608.005140.0
Acceleration314.015.5592362.7892308.013.8015.517.2024.8
Model Year314.075.8980893.67564270.073.0076.079.0082.0
USA314.00.6242040.4851010.00.001.01.001.0
Europe314.00.1783440.3834130.00.000.00.001.0
Japan314.00.1974520.3987120.00.000.00.001.0
# 从标签中分离特征
# 将特征值从目标值或者"标签"中分离。 这个标签是你使用训练模型进行预测的值。

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)
# 构建模型
def build_model():
  model = keras.Sequential([
    layers.Dense(64, activation='relu', input_shape=[len(train_dataset.keys())]),
    layers.Dense(64, activation='relu'),
    layers.Dense(1)
  ])

  optimizer = tf.keras.optimizers.RMSprop(0.001)

  model.compile(loss='mse',
                optimizer=optimizer,
                metrics=['mae', 'mse'])
  return model
model = build_model()
model
<tensorflow.python.keras.engine.sequential.Sequential at 0x1ce0647fcc0>
# 检查模型
# 使用 .summary 方法来打印该模型的简单描述。

model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 64)                640       
_________________________________________________________________
dense_1 (Dense)              (None, 64)                4160      
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 65        
=================================================================
Total params: 4,865
Trainable params: 4,865
Non-trainable params: 0
_________________________________________________________________
# 现在试用下这个模型。从训练数据中批量获取‘10’条例子并对这些例子调用 model.predict 。

example_batch = normed_train_data[:10]
example_result = model.predict(example_batch)
example_result
array([[-0.13055828],
       [-0.10260607],
       [-0.30533665],
       [-0.0732219 ],
       [-0.02573733],
       [-0.17461824],
       [-0.07125732],
       [ 0.11656326],
       [-0.09466185],
       [-0.13080117]], dtype=float32)
# 训练模型
# 对模型进行1000个周期的训练,并在 history 对象中记录训练和验证的准确性。

# 通过为每个完成的时期打印一个点来显示训练进度
class PrintDot(keras.callbacks.Callback):
  def on_epoch_end(self, epoch, logs):
    if epoch % 100 == 0: print('')
    print('.', end='')


EPOCHS = 1000

history = model.fit(
  normed_train_data, train_labels,
  epochs=EPOCHS, validation_split = 0.2, verbose=0,
  callbacks=[PrintDot()])
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
....................................................................................................
# 使用 history 对象中存储的统计信息可视化模型的训练进度。

hist = pd.DataFrame(history.history)
hist['epoch'] = history.epoch
hist.tail()
lossmaemseval_lossval_maeval_mseepoch
9952.2759880.9222682.2759889.3313802.3120569.331380995
9962.1999290.9481212.1999298.9082142.3098358.908214996
9972.4014520.9570192.4014528.9751202.3749238.975120997
9982.2398550.9272122.2398559.2799712.3367659.279971998
9992.1949610.9185822.1949618.9883262.3429608.988326999
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['mae'],
           label='Train Error')
  plt.plot(hist['epoch'], hist['val_mae'],
           label = 'Val Error')
  plt.ylim([0,5])
  plt.legend()

  plt.figure()
  plt.xlabel('Epoch')
  plt.ylabel('Mean Square Error [$MPG^2$]')
  plt.plot(hist['epoch'], hist['mse'],
           label='Train Error')
  plt.plot(hist['epoch'], hist['val_mse'],
           label = 'Val Error')
  plt.ylim([0,20])
  plt.legend()
  plt.show()


plot_history(history)

在这里插入图片描述

在这里插入图片描述

# 该图表显示在约100个 epochs 之后误差非但没有改进,反而出现恶化。
# 让我们更新 model.fit 调用,当验证值没有提高上是自动停止训练。 
# 我们将使用一个 EarlyStopping callback 来测试每个 epoch 的训练条件。
# 如果经过一定数量的 epochs 后没有改进,则自动停止训练。
model = build_model()

# patience 值用来检查改进 epochs 的数量
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)
..............................................................

在这里插入图片描述

在这里插入图片描述

# 让我们看看通过使用 测试集 来泛化模型的效果如何,我们在训练模型时没有使用测试集。这告诉我们,当我们在现实世界中使用这个模型时,我们可以期望它预测得有多好。
loss, mae, mse = model.evaluate(normed_test_data, test_labels, verbose=2)

print("Testing set Mean Abs Error: {:5.2f} MPG".format(mae))

3/3 - 0s - loss: 6.1160 - mae: 1.9173 - mse: 6.1160
Testing set Mean Abs Error:  1.92 MPG
# 使用测试集中的数据预测 MPG 值:
test_predictions = model.predict(normed_test_data).flatten()

plt.scatter(test_labels, test_predictions)
plt.xlabel('True Values [MPG]')
plt.ylabel('Predictions [MPG]')
plt.axis('equal')
plt.axis('square')
plt.xlim([0,plt.xlim()[1]])
plt.ylim([0,plt.ylim()[1]])
_ = plt.plot([-100, 100], [-100, 100])


在这里插入图片描述

# 这看起来我们的模型预测得相当好。我们来看下误差分布。

error = test_predictions - test_labels
plt.hist(error, bins = 25)
plt.xlabel("Prediction Error [MPG]")
_ = plt.ylabel("Count")

在这里插入图片描述

# 它不是完全的高斯分布,但我们可以推断出,这是因为样本的数量很小所导致的。
# 结论
# 本笔记本 (notebook) 介绍了一些处理回归问题的技术。

# 均方误差(MSE)是用于回归问题的常见损失函数(分类问题中使用不同的损失函数)。
# 类似的,用于回归的评估指标与分类不同。 常见的回归指标是平均绝对误差(MAE)。
# 当数字输入数据特征的值存在不同范围时,每个特征应独立缩放到相同范围。
# 如果训练数据不多,一种方法是选择隐藏层较少的小网络,以避免过度拟合。
# 早期停止是一种防止过度拟合的有效技术。

5.过拟合与欠拟合

  1. L2
l2_model = tf.keras.Sequential([
    layers.Dense(512, activation='elu',
                 kernel_regularizer=regularizers.l2(0.001),
                 input_shape=(FEATURES,)),
    layers.Dense(512, activation='elu',
                 kernel_regularizer=regularizers.l2(0.001)),
    layers.Dense(512, activation='elu',
                 kernel_regularizer=regularizers.l2(0.001)),
    layers.Dense(512, activation='elu',
                 kernel_regularizer=regularizers.l2(0.001)),
    layers.Dense(1)
])

regularizer_histories['l2'] = compile_and_fit(l2_model, "regularizers/l2")
  1. dropout
dropout_model = tf.keras.Sequential([
    layers.Dense(512, activation='elu', input_shape=(FEATURES,)),
    layers.Dropout(0.5),
    layers.Dense(512, activation='elu'),
    layers.Dropout(0.5),
    layers.Dense(512, activation='elu'),
    layers.Dropout(0.5),
    layers.Dense(512, activation='elu'),
    layers.Dropout(0.5),
    layers.Dense(1)
])

regularizer_histories['dropout'] = compile_and_fit(dropout_model, "regularizers/dropout")
  1. Combined L2 + dropout
combined_model = tf.keras.Sequential([
    layers.Dense(512, kernel_regularizer=regularizers.l2(0.0001),
                 activation='elu', input_shape=(FEATURES,)),
    layers.Dropout(0.5),
    layers.Dense(512, kernel_regularizer=regularizers.l2(0.0001),
                 activation='elu'),
    layers.Dropout(0.5),
    layers.Dense(512, kernel_regularizer=regularizers.l2(0.0001),
                 activation='elu'),
    layers.Dropout(0.5),
    layers.Dense(512, kernel_regularizer=regularizers.l2(0.0001),
                 activation='elu'),
    layers.Dropout(0.5),
    layers.Dense(1)
])

regularizer_histories['combined'] = compile_and_fit(combined_model, "regularizers/combined")
Conclusions
To recap: here are the most common ways to prevent overfitting in neural networks:

 1. Get more training data.
 2. Reduce the capacity of the network.
 3. Add weight regularization.
 4. Add dropout.

Two important approaches not covered in this guide are:
 1. data-augmentation
 2. batch normalization
Remember that each method can help on its own, but often combining them can be even more effective.

6.模型保存与加载

import os

import tensorflow as tf
from tensorflow import keras

print(tf.version.VERSION)
2.3.0
# #构建数据(网络无反应)
# (train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()

path = r"\data\mnist.npz"
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data(r"C:\Users\hjz\python-project\project\02_lianxi\06_tf2\data\mnist.npz")

train_labels = train_labels[:1000]
test_labels = test_labels[:1000]

train_images = train_images[:1000].reshape(-1, 28 * 28) / 255.0
test_images = test_images[:1000].reshape(-1, 28 * 28) / 255.0
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 11s 1us/step
#定义模型
# 首先构建一个简单的序列(sequential)模型:
# 定义一个简单的序列模型
def create_model():
  model = tf.keras.models.Sequential([
    keras.layers.Dense(512, activation='relu', input_shape=(784,)),
    keras.layers.Dropout(0.2),
    keras.layers.Dense(10)
  ])

  model.compile(optimizer='adam',
                loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
                metrics=['accuracy'])

  return model

# 创建一个基本的模型实例
model = create_model()

# 显示模型的结构
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 512)               401920    
_________________________________________________________________
dropout (Dropout)            (None, 512)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 10)                5130      
=================================================================
Total params: 407,050
Trainable params: 407,050
Non-trainable params: 0
_________________________________________________________________
# 在训练期间保存模型(以 checkpoints 形式保存)
# 您可以使用训练好的模型而无需从头开始重新训练,或在您打断的地方开始训练,以防止训练过程没有保存。 tf.keras.callbacks.ModelCheckpoint 允许在训练的过程中和结束时回调保存的模型。

# Checkpoint 回调用法
# 创建一个只在训练期间保存权重的 tf.keras.callbacks.ModelCheckpoint 回调:
checkpoint_path = "training_1/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)

# 创建一个保存模型权重的回调
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
                                                 save_weights_only=True,
                                                 verbose=1)

# 使用新的回调训练模型
model.fit(train_images, 
          train_labels,  
          epochs=10,
          validation_data=(test_images,test_labels),
          callbacks=[cp_callback])  # 通过回调训练

# 这可能会生成与保存优化程序状态相关的警告。
# 这些警告(以及整个笔记本中的类似警告)
# 是防止过时使用,可以忽略。
Epoch 1/10
24/32 [=====================>........] - ETA: 0s - loss: 1.3054 - accuracy: 0.6237
Epoch 00001: saving model to training_1\cp.ckpt
32/32 [==============================] - 1s 22ms/step - loss: 1.1673 - accuracy: 0.6620 - val_loss: 0.7207 - val_accuracy: 0.7910
Epoch 2/10
29/32 [==========================>...] - ETA: 0s - loss: 0.4227 - accuracy: 0.8793
Epoch 00002: saving model to training_1\cp.ckpt
32/32 [==============================] - 0s 11ms/step - loss: 0.4275 - accuracy: 0.8800 - val_loss: 0.5399 - val_accuracy: 0.8350
Epoch 3/10
31/32 [============================>.] - ETA: 0s - loss: 0.2973 - accuracy: 0.9254
Epoch 00003: saving model to training_1\cp.ckpt
32/32 [==============================] - 1s 17ms/step - loss: 0.2964 - accuracy: 0.9260 - val_loss: 0.4623 - val_accuracy: 0.8550
Epoch 4/10
30/32 [===========================>..] - ETA: 0s - loss: 0.1973 - accuracy: 0.9531
Epoch 00004: saving model to training_1\cp.ckpt
32/32 [==============================] - 0s 11ms/step - loss: 0.2039 - accuracy: 0.9520 - val_loss: 0.4527 - val_accuracy: 0.8530
Epoch 5/10
25/32 [======================>.......] - ETA: 0s - loss: 0.1580 - accuracy: 0.9688
Epoch 00005: saving model to training_1\cp.ckpt
32/32 [==============================] - 0s 12ms/step - loss: 0.1583 - accuracy: 0.9660 - val_loss: 0.4015 - val_accuracy: 0.8750
Epoch 6/10
26/32 [=======================>......] - ETA: 0s - loss: 0.1202 - accuracy: 0.9868
Epoch 00006: saving model to training_1\cp.ckpt
32/32 [==============================] - 0s 11ms/step - loss: 0.1167 - accuracy: 0.9860 - val_loss: 0.4332 - val_accuracy: 0.8550
Epoch 7/10
27/32 [========================>.....] - ETA: 0s - loss: 0.0855 - accuracy: 0.9884
Epoch 00007: saving model to training_1\cp.ckpt
32/32 [==============================] - 0s 10ms/step - loss: 0.0866 - accuracy: 0.9860 - val_loss: 0.4270 - val_accuracy: 0.8670
Epoch 8/10
22/32 [===================>..........] - ETA: 0s - loss: 0.0683 - accuracy: 0.9943
Epoch 00008: saving model to training_1\cp.ckpt
32/32 [==============================] - 0s 11ms/step - loss: 0.0641 - accuracy: 0.9960 - val_loss: 0.4077 - val_accuracy: 0.8690
Epoch 9/10
27/32 [========================>.....] - ETA: 0s - loss: 0.0462 - accuracy: 0.9977
Epoch 00009: saving model to training_1\cp.ckpt
32/32 [==============================] - 0s 11ms/step - loss: 0.0460 - accuracy: 0.9980 - val_loss: 0.4205 - val_accuracy: 0.8680
Epoch 10/10
29/32 [==========================>...] - ETA: 0s - loss: 0.0359 - accuracy: 0.9989
Epoch 00010: saving model to training_1\cp.ckpt
32/32 [==============================] - 0s 10ms/step - loss: 0.0356 - accuracy: 0.9990 - val_loss: 0.3956 - val_accuracy: 0.8740





<tensorflow.python.keras.callbacks.History at 0x1bf6d7d1630>
# 创建一个新的未经训练的模型。并在测试集上进行评估。未经训练的模型将在机会水平(chance levels)上执行(准确度约为10%):
# 创建一个基本模型实例
model = create_model()

# 评估模型
loss, acc = model.evaluate(test_images,  test_labels, verbose=2)
print("Untrained model, accuracy: {:5.2f}%".format(100*acc))
32/32 - 0s - loss: 2.3580 - accuracy: 0.0740
Untrained model, accuracy:  7.40%
# 然后从 checkpoint 加载权重并重新评估:
# 加载权重
model.load_weights(checkpoint_path)

# 重新评估模型
loss,acc = model.evaluate(test_images,  test_labels, verbose=2)
print("Restored model, accuracy: {:5.2f}%".format(100*acc))
32/32 - 0s - loss: 0.3956 - accuracy: 0.8740
Restored model, accuracy: 87.40%
# checkpoint 回调选项
# 回调提供了几个选项,为 checkpoint 提供唯一名称并调整 checkpoint 频率。

# 训练一个新模型,每五个 epochs 保存一次唯一命名的 checkpoint :
# 在文件名中包含 epoch (使用 `str.format`)
checkpoint_path = "training_2/cp-{epoch:04d}.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)

# 创建一个回调,每 5 个 epochs 保存模型的权重
cp_callback = tf.keras.callbacks.ModelCheckpoint(
    filepath=checkpoint_path, 
    verbose=1, 
    save_weights_only=True,
    period=5)

# 创建一个新的模型实例
model = create_model()

# 使用 `checkpoint_path` 格式保存权重
model.save_weights(checkpoint_path.format(epoch=0))

# 使用新的回调训练模型
model.fit(train_images, 
          train_labels,
          epochs=50, 
          callbacks=[cp_callback],
          validation_data=(test_images,test_labels),
          verbose=0)
WARNING:tensorflow:`period` argument is deprecated. Please use `save_freq` to specify the frequency in number of batches seen.

Epoch 00005: saving model to training_2\cp-0005.ckpt

Epoch 00010: saving model to training_2\cp-0010.ckpt

Epoch 00015: saving model to training_2\cp-0015.ckpt

Epoch 00020: saving model to training_2\cp-0020.ckpt

Epoch 00025: saving model to training_2\cp-0025.ckpt

Epoch 00030: saving model to training_2\cp-0030.ckpt

Epoch 00035: saving model to training_2\cp-0035.ckpt

Epoch 00040: saving model to training_2\cp-0040.ckpt

Epoch 00045: saving model to training_2\cp-0045.ckpt

Epoch 00050: saving model to training_2\cp-0050.ckpt





<tensorflow.python.keras.callbacks.History at 0x1bf6d9b73c8>
latest = tf.train.latest_checkpoint(checkpoint_dir)
latest
'training_2\\cp-0050.ckpt'
# 注意: 默认的 tensorflow 格式仅保存最近的5个 checkpoint 。

# 如果要进行测试,请重置模型并加载最新的 checkpoint :
# 创建一个新的模型实例
model = create_model()

# 加载以前保存的权重
model.load_weights(latest)

# 重新评估模型
loss, acc = model.evaluate(test_images,  test_labels, verbose=1)
print("Restored model, accuracy: {:5.2f}%".format(100*acc))
32/32 [==============================] - 0s 1ms/step - loss: 0.4902 - accuracy: 0.8740
Restored model, accuracy: 87.40%
# Checkpoints 包含:
# 一个或多个包含模型权重的分片。
# 索引文件,指示哪些权重存储在哪个分片中。
# 手动保存权重
# 您将了解如何将权重加载到模型中。使用 Model.save_weights 方法手动保存它们同样简单。
# 默认情况下, tf.keras 和 save_weights 特别使用 TensorFlow checkpoints 格式 .ckpt 扩展名和 ( 保存在 HDF5 扩展名为 .h5 保存并序列化模型 ):
# 保存权重
model.save_weights('./checkpoints/my_checkpoint')

# 创建模型实例
model = create_model()

# 恢复权重
model.load_weights('./checkpoints/my_checkpoint')

# 评估模型
loss,acc = model.evaluate(test_images,  test_labels, verbose=1)
print("Restored model, accuracy: {:5.2f}%".format(100*acc))
32/32 [==============================] - 0s 1ms/step - loss: 0.4902 - accuracy: 0.8740
Restored model, accuracy: 87.40%
# 保存整个模型
# 调用 model.save 将保存模型的结构,权重和训练配置保存在单个文件/文件夹中。
# 整个模型可以以两种不同的文件格式(SavedModel 和 HDF5)进行保存。
# 需要注意的是 TensorFlow 的 SavedModel 格式是 TF2.x. 中的默认文件格式。
# 但是,模型仍可以以 HDF5 格式保存。下面介绍了以两种文件格式保存整个模型的更多详细信息。
# 保存完整模型会非常有用——您可以在 TensorFlow.js(Saved Model, HDF5)加载它们,
# 然后在 web 浏览器中训练和运行它们,
# 或者使用 TensorFlow Lite 将它们转换为在移动设备上运行(Saved Model, HDF5)
# SavedModel 格式
# SavedModel 格式是序列化模型的另一种方法。
# 以这种格式保存的模型,可以使用 tf.keras.models.load_model 还原,并且模型与 TensorFlow Serving 兼容。
# 创建并训练一个新的模型实例。
model = create_model()
model.fit(train_images, train_labels, epochs=5,verbose=1)

# 将整个模型另存为 SavedModel。
!mkdir -p saved_model
model.save('saved_model/my_model') 
Epoch 1/5
32/32 [==============================] - 0s 3ms/step - loss: 1.1490 - accuracy: 0.6780
Epoch 2/5
32/32 [==============================] - 0s 3ms/step - loss: 0.4154 - accuracy: 0.8830
Epoch 3/5
32/32 [==============================] - 0s 3ms/step - loss: 0.2929 - accuracy: 0.9240
Epoch 4/5
32/32 [==============================] - 0s 3ms/step - loss: 0.2106 - accuracy: 0.9410
Epoch 5/5
32/32 [==============================] - 0s 3ms/step - loss: 0.1514 - accuracy: 0.9670


子目录或文件 -p 已经存在。
处理: -p 时出错。
子目录或文件 saved_model 已经存在。
处理: saved_model 时出错。


INFO:tensorflow:Assets written to: saved_model/my_model\assets
# 从保存的模型重新加载新的 Keras 模型:
new_model = tf.keras.models.load_model('saved_model/my_model')

# 检查其架构
new_model.summary()
Model: "sequential_14"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_28 (Dense)             (None, 512)               401920    
_________________________________________________________________
dropout_14 (Dropout)         (None, 512)               0         
_________________________________________________________________
dense_29 (Dense)             (None, 10)                5130      
=================================================================
Total params: 407,050
Trainable params: 407,050
Non-trainable params: 0
_________________________________________________________________
# 还原的模型使用与原始模型相同的参数进行编译。 尝试使用加载的模型运行评估和预测:
# 评估还原的模型
loss, acc = new_model.evaluate(test_images,  test_labels, verbose=1)
print('Restored model, accuracy: {:5.2f}%'.format(100*acc))

print(new_model.predict(test_images).shape)
32/32 [==============================] - 0s 1ms/step - loss: 0.4324 - accuracy: 0.0860
Restored model, accuracy:  8.60%
(1000, 10)
# HDF5 格式
# Keras使用 HDF5 标准提供了一种基本的保存格式。
# 创建并训练一个新的模型实例
model = create_model()
model.fit(train_images, train_labels, epochs=5)

# 将整个模型保存为 HDF5 文件。
# '.h5' 扩展名指示应将模型保存到 HDF5。
model.save('my_model.h5') 
Epoch 1/5
32/32 [==============================] - 0s 3ms/step - loss: 1.1613 - accuracy: 0.6700
Epoch 2/5
32/32 [==============================] - 0s 3ms/step - loss: 0.4077 - accuracy: 0.8860
Epoch 3/5
32/32 [==============================] - 0s 3ms/step - loss: 0.2821 - accuracy: 0.9330
Epoch 4/5
32/32 [==============================] - 0s 3ms/step - loss: 0.2195 - accuracy: 0.9440
Epoch 5/5
32/32 [==============================] - 0s 3ms/step - loss: 0.1593 - accuracy: 0.9650
# 现在,从该文件重新创建模型:
# 重新创建完全相同的模型,包括其权重和优化程序
new_model = tf.keras.models.load_model('my_model.h5')

# 显示网络结构
new_model.summary()
Model: "sequential_15"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_30 (Dense)             (None, 512)               401920    
_________________________________________________________________
dropout_15 (Dropout)         (None, 512)               0         
_________________________________________________________________
dense_31 (Dense)             (None, 10)                5130      
=================================================================
Total params: 407,050
Trainable params: 407,050
Non-trainable params: 0
_________________________________________________________________
# 检查其准确率(accuracy):
loss, acc = new_model.evaluate(test_images,  test_labels, verbose=2)
print('Restored model, accuracy: {:5.2f}%'.format(100*acc))
32/32 - 0s - loss: 0.4227 - accuracy: 0.0820
Restored model, accuracy:  8.20%

7.使用keras tuner调整超参数

import tensorflow as tf
from tensorflow import keras
# !conda install -n tf2 -c conda-forge keras-tuner
# !pip install -q -U keras-tuner
import kerastuner as kt
#包已存在仍无法导入。。

(img_train, label_train), (img_test, label_test) = keras.datasets.fashion_mnist.load_data()
# Normalize pixel values between 0 and 1
img_train = img_train.astype('float32') / 255.0
img_test = img_test.astype('float32') / 255.0

def model_builder(hp):
  model = keras.Sequential()
  model.add(keras.layers.Flatten(input_shape=(28, 28)))
  
  # Tune the number of units in the first Dense layer
  # Choose an optimal value between 32-512
  hp_units = hp.Int('units', min_value = 32, max_value = 512, step = 32)
  model.add(keras.layers.Dense(units = hp_units, activation = 'relu'))
  model.add(keras.layers.Dense(10))

  # Tune the learning rate for the optimizer 
  # Choose an optimal value from 0.01, 0.001, or 0.0001
  hp_learning_rate = hp.Choice('learning_rate', values = [1e-2, 1e-3, 1e-4]) 
  
  model.compile(optimizer = keras.optimizers.Adam(learning_rate = hp_learning_rate),
                loss = keras.losses.SparseCategoricalCrossentropy(from_logits = True), 
                metrics = ['accuracy'])
  
  return model
# Instantiate the tuner and perform hypertuning实例化调优器并执行超调优
tuner = kt.Hyperband(model_builder,
                     objective = 'val_accuracy', 
                     max_epochs = 10,
                     factor = 3,
                     directory = 'my_dir',
                     project_name = 'intro_to_kt') 
# Before running the hyperparameter search, define a callback to clear the training outputs at the end of every training step.
tuner.search(img_train, label_train, epochs = 10, validation_data = (img_test, label_test), callbacks = [ClearTrainingOutput()])

# Get the optimal hyperparameters
best_hps = tuner.get_best_hyperparameters(num_trials = 1)[0]

print(f"""
The hyperparameter search is complete. The optimal number of units in the first densely-connected
layer is {best_hps.get('units')} and the optimal learning rate for the optimizer
is {best_hps.get('learning_rate')}.
""")  

# Build the model with the optimal hyperparameters and train it on the data
model = tuner.hypermodel.build(best_hps)
model.fit(img_train, label_train, epochs = 10, validation_data = (img_test, label_test))# To finish this tutorial, retrain the model with the optimal hyperparameters from the search.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值