import tensorflow as tf
from tensorflow import keras
from tensorflow.python.keras.layers import Dense
from tensorflow.python.keras.layers import DepthwiseConv2D
from tensorflow.python.keras.layers import Dot
from tensorflow.python.keras.layers import Dropout
from tensorflow.python.keras.layers import ELU
from tensorflow.python.keras.layers import Embedding
from tensorflow.python.keras.layers import Flatten
from tensorflow.python.keras.layers import GRU
from tensorflow.python.keras.layers import GRUCell
from tensorflow.python.keras.layers import LSTMCell
...
...
...
【layers构建网络的没一层】
tf.keras.Sequential构建类似管道的模型
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dense(10, activation=tf.nn.softmax)
])
示例代码:
import tensorflow as tf
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten
def main():
# (784, )--->(28, 28)
model = Sequential([
Flatten(input_shape=(28, 28)),
Dense(128, activation=tf.nn.relu),
Dense(10, activation=tf.nn.softmax)
])
print(model)
if __name__ == '__main__':
main()
from keras.models import Model
from keras.layers import Input, Dense
data = Input(shape=(784,))
out = Dense(32)(data)
model = Model(input=data, output=out)
示例代码:
import tensorflow as tf
from tensorflow.python.keras.models import Sequential, Model
from tensorflow.python.keras.layers import Dense, Flatten, Input
def main():
# (784, )--->(28, 28)
model_1 = Sequential([
Flatten(input_shape=(28, 28)),
Dense(128, activation=tf.nn.relu),
Dense(10, activation=tf.nn.softmax)
])
print(model_1)
# 通过Model建立模型
data = Input(shape=(784, ))
print(data)
out = Dense(64)(data)
print(out)
model_2 = Model(inputs=data, outputs=out)
print(model_2)
if __name__ == '__main__':
main()
运行结果:
Tensor("input_1:0", shape=(None, 784), dtype=float32)
Tensor("dense_2/BiasAdd:0", shape=(None, 64), dtype=float32)
model.layers
:获取模型结构列表print(model.layers)
[, , , ]
model.inputs
是模型的输入张量列表print(model.inputs)
[]
model.outputs
是模型的输出张量列表print(model.outputs)
[]
model.summary()
打印模型的摘要表示Layer (type) Output Shape Param #
=================================================================
flatten (Flatten) (None, 784) 0
_________________________________________________________________
dense (Dense) (None, 64) 50240
_________________________________________________________________
dense_1 (Dense) (None, 128) 8320
_________________________________________________________________
dense_2 (Dense) (None, 10) 1290
=================================================================
Total params: 59,850
Trainable params: 59,850
Non-trainable params: 0
示例代码:
import tensorflow as tf
from tensorflow.python.keras.models import Sequential, Model
from tensorflow.python.keras.layers import Dense, Flatten, Input
def main():
# (784, )--->(28, 28)
model_1 = Sequential([
Flatten(input_shape=(28, 28)),
Dense(128, activation=tf.nn.relu),
Dense(10, activation=tf.nn.softmax)
])
print(model_1)
# 通过Model建立模型
data = Input(shape=(784, ))
print(data)
out = Dense(64)(data)
print(out)
model_2 = Model(inputs=data, outputs=out)
print(model_2)
print('#' * 100)
# 获取模型结构列表
print(model_1.layers)
# 模型的输入张量列表
print(model_1.inputs)
# 模型的输出张量列表
print(model_1.outputs)
# 打印模型的摘要表示
print(model_1.summary())
if __name__ == '__main__':
main()
运行效果:
Tensor("input_1:0", shape=(None, 784), dtype=float32)
Tensor("dense_2/BiasAdd:0", shape=(None, 64), dtype=float32)
####################################################################################################
[, , ]
[]
[]
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten (Flatten) (None, 784) 0
_________________________________________________________________
dense (Dense) (None, 128) 100480
_________________________________________________________________
dense_1 (Dense) (None, 10) 1290
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
_________________________________________________________________
None
通过调用model的 compile
方法去配置该模型所需要的训练参数以及评估方法。
model.compile(optimizer,loss=None,metrics=None, 准确率衡):配置训练相关参数
from tensorflow.python.keras.optimizers import Adadelta
from tensorflow.python.keras.optimizers import Adagrad
from tensorflow.python.keras.optimizers import Adam
from tensorflow.python.keras.optimizers import Adamax
from tensorflow.python.keras.optimizers import Nadam
from tensorflow.python.keras.optimizers import Optimizer
from tensorflow.python.keras.optimizers import RMSprop
from tensorflow.python.keras.optimizers import SGD
from tensorflow.python.keras.optimizers import deserialize
from tensorflow.python.keras.optimizers import get
from tensorflow.python.keras.optimizers import serialize
from tensorflow.python.keras.optimizers import AdamOptimizer()
from tensorflow.python.keras.losses import KLD
from tensorflow.python.keras.losses import KLD as kld
from tensorflow.python.keras.losses import KLD as kullback_leibler_divergence
from tensorflow.python.keras.losses import MAE
from tensorflow.python.keras.losses import MAE as mae
from tensorflow.python.keras.losses import MAE as mean_absolute_error
from tensorflow.python.keras.losses import MAPE
from tensorflow.python.keras.losses import MAPE as mape
from tensorflow.python.keras.losses import MAPE as mean_absolute_percentage_error
from tensorflow.python.keras.losses import MSE
from tensorflow.python.keras.losses import MSE as mean_squared_error
from tensorflow.python.keras.losses import MSE as mse
from tensorflow.python.keras.losses import MSLE
from tensorflow.python.keras.losses import MSLE as mean_squared_logarithmic_error
from tensorflow.python.keras.losses import MSLE as msle
from tensorflow.python.keras.losses import binary_crossentropy
from tensorflow.python.keras.losses import categorical_crossentropy
from tensorflow.python.keras.losses import categorical_hinge
from tensorflow.python.keras.losses import cosine
from tensorflow.python.keras.losses import cosine as cosine_proximity
from tensorflow.python.keras.losses import deserialize
from tensorflow.python.keras.losses import get
from tensorflow.python.keras.losses import hinge
from tensorflow.python.keras.losses import logcosh
from tensorflow.python.keras.losses import poisson
from tensorflow.python.keras.losses import serialize
from tensorflow.python.keras.losses import sparse_categorical_crossentropy
from tensorflow.python.keras.losses import squared_hinge
model.compile(optimizer=tf.keras.optimizers.Adam(),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
sparse_categorical_crossentropy:对于目标值是整型的进行交叉熵损失计算 【即为没有进行one-hot编码等,不是01数】
categorical_crossentropy:对于两个output tensor and a target tensor进行交叉熵损失计算 【即为已经是编码后的数了】
model.fit():进行训练
(x=None,y=None, batch_size=None,epochs=1,callbacks=None)
x:特征值:
1、Numpy array (or array-like), or a list of arrays
2、A TensorFlow tensor, or a list of tensors
3、`tf.data` dataset or a dataset iterator. Should return a tuple of either `(inputs, targets)` or `(inputs, targets, sample_weights)`.
4、A generator or `keras.utils.Sequence` returning `(inputs, targets)` or `(inputs, targets, sample weights)`.
y:目标值
batch_size=None:批次大小
epochs=1:训练迭代次数
callbacks=None:添加回调列表(用于如tensorboard显示等)
model.fit(train_images, train_labels, epochs=5, batch_size=32)
model.evaluate(test, test_label)
预测model.predict(test):
其它方法:
model.save_weights(filepath)
将模型的权重保存为HDF5文件或者ckpt文件model.load_weights(filepath, by_name=False)
从HDF5文件(由其创建save_weights
)加载模型的权重。默认情况下,架构预计不会更改。要将权重加载到不同的体系结构(具有一些共同的层),请使用by_name=True
仅加载具有相同名称的那些层。70000 张灰度图像,涵盖 10 个类别。以下图像显示了单件服饰在较低分辨率(28x28 像素)下的效果:
标签 | 类别 |
---|---|
0 | T 恤衫/上衣 |
1 | 裤子 |
2 | 套衫 |
3 | 裙子 |
4 | 外套 |
5 | 凉鞋 |
6 | 衬衫 |
7 | 运动鞋 |
8 | 包包 |
class SingleNN(object):
def __init__(self):
(self.train, self.train_label), (self.test, self.test_label) = keras.datasets.fashion_mnist.load_data()
class SingleNN(object):
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dense(10, activation=tf.nn.softmax)
])
这里我们model只是放在类中,作为类的固定模型属性, 这里的激活函数暂时使用tf.nn.relu函数
这里选择我们SGD优化器
keras.optimizers.SGD(lr=0.01)
loss:tf.keras.losses.sparse_categorical_crossentropy
metrics:accuracy
def compile(self):
SingleNN.model.compile(optimizer=keras.optimizers.SGD(lr=0.01),
loss=tf.keras.losses.sparse_categorical_crossentropy,
metrics=['accuracy'])
return None
def fit(self):
SingleNN.model.fit(self.train, self.train_label, epochs=5, batch_size=32)
return None
def evaluate(self):
test_loss, test_acc = SingleNN.model.evaluate(self.test, self.test_label)
print(test_loss, test_acc)
return None
示例代码:
import tensorflow as tf
from keras import datasets
from keras.models import Sequential
from keras.layers import Flatten, Dense
from keras.optimizers import SGD
from keras.losses import sparse_categorical_crossentropy
class SingleNN(object):
"""
构建双层神经网络进行时装模型训练与预测
"""
# 建立神经网络模型
model = Sequential([
Flatten(input_shape=(28, 28)), # 将输入数据的形状进行修改为神经网络要求的数据形状
Dense(128, activation=tf.nn.relu), # 定义隐藏层,128个神经元的网络层
Dense(10, activation=tf.nn.softmax) # 10个类别的分类问题,输出的神经元个数必须跟总类别数量相同
])
def __init__(self):
# 读取数据集,返回两个元组
# x_train:(1875, 784),y_train:(1875, 1)
(self.x_train, self.y_train), (self.x_test, self.y_test) = datasets.fashion_mnist.load_data()
def compile(self):
"""
编译模型优化器、损失和准确率
:return:
"""
SingleNN.model.compile(optimizer=SGD(lr=0.01), loss=sparse_categorical_crossentropy, metrics=['accuracy'])
return None
def fit(self):
"""
进行fit训练
:return:
"""
# 训练样本的特征值和目标值
SingleNN.model.fit(self.x_train, self.y_train, epochs=5)
return None
def evaluate(self):
"""
评估模型测试效果
:return:
"""
test_loss, test_acc = SingleNN.model.evaluate(self.x_test, self.y_test)
print(test_loss, test_acc)
return None
if __name__ == '__main__':
snn = SingleNN()
snn.compile()
snn.fit()
snn.evaluate()
运行结果:
Epoch 1/5
1875/1875 [==============================] - 1s 579us/step - loss: 171.3367 - accuracy: 0.1105
Epoch 2/5
1875/1875 [==============================] - 1s 578us/step - loss: 2.2984 - accuracy: 0.1015
Epoch 3/5
1875/1875 [==============================] - 1s 597us/step - loss: 2.3026 - accuracy: 0.0987
Epoch 4/5
1875/1875 [==============================] - 1s 639us/step - loss: 2.3025 - accuracy: 0.0962
Epoch 5/5
1875/1875 [==============================] - 1s 635us/step - loss: 2.3025 - accuracy: 0.0983
313/313 [==============================] - 0s 551us/step - loss: 2.3027 - accuracy: 0.1000
2.3027026653289795 0.10000000149011612
# 做特征值的归一化
# 所有像素值范围0~255,
self.train = self.train / 255.0
self.test = self.test / 255.0
示例代码:
import tensorflow as tf
from keras import datasets
from keras.models import Sequential
from keras.layers import Flatten, Dense
from keras.optimizers import SGD
from keras.losses import sparse_categorical_crossentropy
class SingleNN(object):
"""
构建双层神经网络进行时装模型训练与预测
"""
# 建立神经网络模型
model = Sequential([
Flatten(input_shape=(28, 28)), # 将输入数据的形状进行修改为神经网络要求的数据形状
Dense(128, activation=tf.nn.relu), # 定义隐藏层,128个神经元的网络层
Dense(10, activation=tf.nn.softmax) # 10个类别的分类问题,输出的神经元个数必须跟总类别数量相同
])
def __init__(self):
# 读取数据集,返回两个元组
# x_train:(1875, 784),y_train:(1875, 1)
(self.x_train, self.y_train), (self.x_test, self.y_test) = datasets.fashion_mnist.load_data()
# 进行数据的归一化,注意:目标值不需要归一化
self.x_train = self.x_train / 255.0
self.x_test = self.x_test / 255.0
def compile(self):
"""
编译模型优化器、损失和准确率
:return:
"""
SingleNN.model.compile(optimizer=SGD(lr=0.01), loss=sparse_categorical_crossentropy, metrics=['accuracy'])
return None
def fit(self):
"""
进行fit训练
:return:
"""
# 训练样本的特征值和目标值
SingleNN.model.fit(self.x_train, self.y_train, epochs=5)
return None
def evaluate(self):
"""
评估模型测试效果
:return:
"""
test_loss, test_acc = SingleNN.model.evaluate(self.x_test, self.y_test)
print(test_loss, test_acc)
return None
if __name__ == '__main__':
snn = SingleNN()
snn.compile()
snn.fit()
snn.evaluate()
运行结果:
Epoch 1/5
1875/1875 [==============================] - 1s 590us/step - loss: 0.7474 - accuracy: 0.7579
Epoch 2/5
1875/1875 [==============================] - 1s 580us/step - loss: 0.5147 - accuracy: 0.8258
Epoch 3/5
1875/1875 [==============================] - 1s 575us/step - loss: 0.4706 - accuracy: 0.8378
Epoch 4/5
1875/1875 [==============================] - 1s 573us/step - loss: 0.4477 - accuracy: 0.8450
Epoch 5/5
1875/1875 [==============================] - 1s 567us/step - loss: 0.4304 - accuracy: 0.8515
313/313 [==============================] - 0s 502us/step - loss: 0.4560 - accuracy: 0.8411
0.45601874589920044 0.8410999774932861
这儿的效果明显好了很多!!!
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten (Flatten) (None, 784) 0
_________________________________________________________________
dense (Dense) (None, 128) 100480
_________________________________________________________________
dense_1 (Dense) (None, 10) 1290
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
SingleNN.model.save_weights("./ckpt/SingleNN")
def predict(self):
# 直接使用训练过后的权重测试
if os.path.exists("./ckpt/checkpoint"):
SingleNN.model.load_weights("./ckpt/SingleNN")
predictions = SingleNN.model.predict(self.test)
# 对预测结果进行处理
print(np.argmax(predictions, 1))
return
示例代码:
import os
import numpy as np
import tensorflow as tf
from keras import datasets
from keras.models import Sequential
from keras.layers import Flatten, Dense
from keras.optimizers import SGD
from keras.losses import sparse_categorical_crossentropy
class SingleNN(object):
"""
构建双层神经网络进行时装模型训练与预测
"""
# 建立神经网络模型
model = Sequential([
Flatten(input_shape=(28, 28)), # 将输入数据的形状进行修改为神经网络要求的数据形状
Dense(128, activation=tf.nn.relu), # 定义隐藏层,128个神经元的网络层
Dense(10, activation=tf.nn.softmax) # 10个类别的分类问题,输出的神经元个数必须跟总类别数量相同
])
# print(model.summary())
def __init__(self):
# 读取数据集,返回两个元组
# x_train:(1875, 784),y_train:(1875, 1)
(self.x_train, self.y_train), (self.x_test, self.y_test) = datasets.fashion_mnist.load_data()
# 进行数据的归一化,注意:目标值不需要归一化
self.x_train = self.x_train / 255.0
self.x_test = self.x_test / 255.0
def compile(self):
"""
编译模型优化器、损失和准确率
:return:
"""
SingleNN.model.compile(optimizer=SGD(lr=0.01), loss=sparse_categorical_crossentropy, metrics=['accuracy'])
return None
def fit(self):
"""
进行fit训练
:return:
"""
# 训练样本的特征值和目标值
SingleNN.model.fit(self.x_train, self.y_train, epochs=5)
return None
def evaluate(self):
"""
评估模型测试效果
:return:
"""
test_loss, test_acc = SingleNN.model.evaluate(self.x_test, self.y_test)
print(test_loss, test_acc)
return None
def predict(self):
"""
预测结果
:return:
"""
# 首先加载模型
if os.path.exists("./tmp/ckpt/checkpoint"):
SingleNN.model.load_weights("./tmp/ckpt/SingleNN")
predictions = SingleNN.model.predict(self.x_test)
return predictions
if __name__ == '__main__':
snn = SingleNN()
# snn.compile()
# snn.fit()
# snn.evaluate()
# # 保存模型
# SingleNN.model.save_weights("./tmp/ckpt/SingleNN")
# 进行模型预测
predictions = snn.predict()
print(predictions)
print(np.argmax(predictions, axis=1))
运行结果:
[[9.6156173e-06 6.1552450e-06 2.5047178e-05 ... 2.4859291e-01
5.6075659e-03 6.3287145e-01]
[2.0993820e-04 5.5472728e-06 9.6353877e-01 ... 8.4127844e-11
1.1876404e-04 6.0094135e-10]
[5.0108505e-05 9.9980801e-01 1.5537924e-05 ... 2.3488226e-07
5.4975771e-07 5.3934581e-08]
...
[2.8350599e-02 7.1193126e-06 2.2114406e-03 ... 3.6337989e-04
8.3771253e-01 1.2190870e-05]
[5.7001798e-05 9.9706584e-01 6.9393049e-05 ... 1.9720854e-05
4.7423578e-06 2.9315692e-05]
[3.8735327e-04 3.1307348e-04 8.3889149e-04 ... 9.6547395e-02
2.1333354e-02 8.7000076e-03]]
[9 2 1 ... 8 1 5]
SingleNN.model.save_weights("./ckpt/SingleNN.h5")
def predict(self):
# 直接使用训练过后的权重测试
if os.path.exists("./ckpt/checkpoint"):
SingleNN.model.load_weights("./ckpt/SingleNN.h5")
predictions = SingleNN.model.predict(self.test)
print(np.argmax(predictions, 1))
return
回调是在训练过程的给定阶段应用的一组函数。可以使用回调来获取培训期间内部状态和模型统计信息的视图。您可以将回调列表(作为关键字参数callbacks
)传递给或类的fit()
方法。然后将在训练的每个阶段调用回调的相关方法。
from tensorflow.python.keras.callbacks import ModelCheckpoint
check = ModelCheckpoint('./ckpt/singlenn_{epoch:02d}-{val_acc:.2f}.h5',
monitor='val_acc',
save_best_only=True,
save_weights_only=True,
mode='auto',
period=1)
SingleNN.model.fit(self.train, self.train_label, epochs=5, callbacks=[check])
histogram_freq
必须大于0# 添加tensoboard观察
tensorboard = keras.callbacks.TensorBoard(log_dir='./graph', histogram_freq=1,
write_graph=True, write_images=True)
SingleNN.model.fit(self.train, self.train_label, epochs=5, callbacks=[tensorboard])
示例代码:
import os
import numpy as np
import tensorflow as tf
from keras import datasets
from keras.models import Sequential
from keras.layers import Flatten, Dense
from keras.optimizers import SGD
from keras.callbacks import ModelCheckpoint, TensorBoard
from keras.losses import sparse_categorical_crossentropy
class SingleNN(object):
"""
构建双层神经网络进行时装模型训练与预测
"""
# 建立神经网络模型
model = Sequential([
Flatten(input_shape=(28, 28)), # 将输入数据的形状进行修改为神经网络要求的数据形状
Dense(128, activation=tf.nn.relu), # 定义隐藏层,128个神经元的网络层
Dense(10, activation=tf.nn.softmax) # 10个类别的分类问题,输出的神经元个数必须跟总类别数量相同
])
# print(model.summary())
def __init__(self):
# 读取数据集,返回两个元组
# x_train:(1875, 784),y_train:(1875, 1)
(self.x_train, self.y_train), (self.x_test, self.y_test) = datasets.fashion_mnist.load_data()
# 进行数据的归一化,注意:目标值不需要归一化
self.x_train = self.x_train / 255.0
self.x_test = self.x_test / 255.0
def compile(self):
"""
编译模型优化器、损失和准确率
:return:
"""
SingleNN.model.compile(optimizer=SGD(lr=0.01), loss=sparse_categorical_crossentropy, metrics=['accuracy'])
return None
def fit(self):
"""
进行fit训练
:return:
"""
# fit当中添加回调函数,记录训练模型过程
# modelcheck = ModelCheckpoint(
# filepath="./tmp/ckpt/singleNN_{epoch:02d}-{val_loss:.2f}.h5",
# monitor='val_loss', # 保存损失还是准确率
# save_best_only=True,
# save_weights_only=True,
# mode='auto',
# period=1
# )
# 调用tensorboard回调函数
board = TensorBoard(log_dir="./tmp/graph/", histogram_freq=1, write_graph=True, write_images=True)
# 训练样本的特征值和目标值
SingleNN.model.fit(self.x_train, self.y_train, epochs=5, callbacks=[board])
return None
def evaluate(self):
"""
评估模型测试效果
:return:
"""
test_loss, test_acc = SingleNN.model.evaluate(self.x_test, self.y_test)
print(test_loss, test_acc)
return None
def predict(self):
"""
预测结果
:return:
"""
# 首先加载模型
# if os.path.exists("./tmp/ckpt/checkpoint"):
SingleNN.model.load_weights("./tmp/ckpt/SingleNN.h5")
predictions = SingleNN.model.predict(self.x_test)
return predictions
if __name__ == '__main__':
snn = SingleNN()
snn.compile()
snn.fit()
snn.evaluate()
# 保存模型
# SingleNN.model.save_weights("./tmp/ckpt/SingleNN.h5")
# 进行模型预测
# predictions = snn.predict()
# print(predictions)
# print(np.argmax(predictions, axis=1))