步骤:
model = tf.keras.models.Sequential([网络结构])
用于描述各层网络
用Sequential可以搭建出上层输出就是下层输入的顺序网络结构
网络结构举例:
拉直层:tf.keras.layers.Flatten(),这一层不做计算,只是将数据拉成一维数组
全连接层:tf.keras.layers.Dense(神经元个数,activation=“激活函数”,kernel_regularizer=哪种正则化)
其中,activation可选参数有,relu,softmax,sigmoid,tanh
kernel_regularizer可选:tf.keras.regularizers.l1(),tf.keras.regularizers.l2()
卷积层:tf.keras.layers.Conv2D(filters=卷积核个数,kernel_size=卷积核尺寸,strides=卷积步长,padding=“valid” or “same”)
LSTM层:tf.keras.layers.LSTM()
model.compile(optimizer=优化器,loss=损失函数,metrics=[“准确率”])
optimizer可选:
loss可选:
Metrics可选:
model.fit(训练集输入特征,训练集标签,batch_size=,epochs=,validation_data=(测试集输入特征,测试集标签),validation_split=从训练集划分多少比例给测试集,validation_freq=多少次epoch测试一次准确率)
打印出网络的结构和参数统计
import tensorflow as tf
from sklearn import datasets
import numpy as np
# 导入数据集
x_train = datasets.load_iris().data
y_train = datasets.load_iris().target
# 打乱数据集顺序
np.random.seed(seed=116)
np.random.shuffle(x_train)
np.random.seed(seed=116)
np.random.shuffle(y_train)
np.random.seed(seed=116)
# 搭建网络结构
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(
3,
activation="softmax",
kernel_regularizer=tf.keras.regularizers.l2()
)
])
# 优化
# 因为在搭建网络结构时,使用了softmax进行概率分布,所以此处的from_logits=False
# 对于metrics参数,因为数据集的标签值为数值,而预测结果为ont_hot编码形式,所以选择sparse_categorical_accuracy
model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.1),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=["sparse_categorical_accuracy"])
# 训练
model.fit(x_train, y_train, batch_size=32, epochs=500, validation_split=0.2, validation_freq=20)
# 打印网络结构和参数统计
model.summary()
结果展示:
Sequential可以搭建顺序网络结构但无法写出一些带有跳连的非顺序网络结构,这个时候我们可以选择用类class搭建神经网络。只需将Sequential搭建网络结构的部分换成以下类即可,其余不变
class MyModel(Model):
def __init__(self):
# 继承父类属性
super(MyModel,self).__init__()
定义网络结构块
def call(self.x):
调用网络结构块,实现向前传播
return y
model = MyModel()
import numpy as np
import tensorflow as tf
from sklearn import datasets
from tensorflow.keras import Model
from tensorflow.keras.layers import Dense
# 导入数据集
x_train = datasets.load_iris().data
y_train = datasets.load_iris().target
# 打乱数据集顺序
np.random.seed(seed=116)
np.random.shuffle(x_train)
np.random.seed(seed=116)
np.random.shuffle(y_train)
np.random.seed(seed=116)
# 搭建网络结构
class IrisModel(Model):
def __init__(self):
super(IrisModel,self).__init__()
# 定义网络结构
self.d1 = Dense(3,activation="sigmoid",kernel_regularizer=tf.keras.regularizers.l2())
def call(self, x):
y = self.d1(x)
return y
model = IrisModel()
# 配置训练方法
# 因为在搭建网络结构时,使用了softmax进行概率分布,所以此处的from_logits=False
# 对于metrics参数,因为数据集的标签值为数值,而预测结果为ont_hot编码形式,所以选择sparse_categorical_accuracy
model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.1),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=["sparse_categorical_accuracy"])
# 训练
model.fit(x_train, y_train, batch_size=32, epochs=500, validation_split=0.2, validation_freq=20)
# 打印网络结构和参数统计
model.summary()
在Tensorflow中,MNIST数据集提供了6万张28*28像素点的09手写数字图片和标签,用于训练;提供了1万张28*28像素点的09手写数字图片和标签,用于测试。
import tensorflow as tf
import matplotlib.pyplot as plt
# 导入数据
mnist = tf.keras.datasets.mnist
(x_train,y_train), (x_test,y_test) = mnist.load_data()
# 对数据归一化,即将数据转换为0~1之间的数,把输入特征的数值变小更适合神经网络吸收
x_train, x_test = x_train/255.0, x_test/255.0
# 定义网络结构
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(), # 将数据集拉成一维数组
tf.keras.layers.Dense(128, activation="relu"), # 搭建第一层网络
tf.keras.layers.Dense(10, activation="softmax") # 构建第二层网络
])
# 配置训练方法
model.compile(
optimizer="adam",
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=["sparse_categorical_accuracy"]
)
# 训练
model.fit(x_train,y_train, batch_size=32, epochs=5, validation_data=(x_test,y_test), validation_freq=1)
# 打印网络结构并统计信息
model.summary()
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras import Model
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Flatten
# 导入数据
mnist = tf.keras.datasets.mnist
(x_train,y_train), (x_test,y_test) = mnist.load_data()
# 对数据归一化,即将数据转换为0~1之间的数,把输入特征的数值变小更适合神经网络吸收
x_train, x_test = x_train/255.0, x_test/255.0
# 定义网络结构
class MnistMode(Model):
def __init__(self):
super(MnistMode, self).__init__()
self.flatten = Flatten()
self.d1 = Dense(120, activation="relu")
self.d2 = Dense(10, activation="softmax")
def call(self,x):
x = self.flatten(x)
x = self.d1(x)
y = self.d2(x)
return y
model = MnistMode()
# 配置训练方法
model.compile(
optimizer="adam",
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=["sparse_categorical_accuracy"]
)
# 训练
model.fit(x_train,y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1)
# 打印网络结构并统计信息
model.summary()
提供6万张28*28像素点的衣裤等图片和标签,用于训练。
提供1万张28*28像素点的衣裤等图片和标签,用于测试
共分为10类,有T恤,裤子,套头衫,连衣裙,外套,凉鞋,衬衫,运动鞋, 包,靴子
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import fashion_mnist
# 读取数据
(x_train,y_train), (x_test,y_test) = fashion_mnist.load_data()
# 数据归一化
x_train, x_test = x_train/255, x_test/255
# 定义网络结构
model = tf.keras.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(120, activation="relu"),
tf.keras.layers.Dense(10, activation="softmax")
])
# 配置训练方法
model.compile(
optimizer=tf.keras.optimizers.Adam(),
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=["sparse_categorical_accuracy"]
)
# 训练
model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1)
# 查看网络结构及数据统计
model.summary()
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import fashion_mnist
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
# 读取数据
(x_train,y_train), (x_test,y_test) = fashion_mnist.load_data()
# 数据归一化
x_train, x_test = x_train/255, x_test/255
# 定义网络结构
class FashionModel(Model):
def __init__(self):
super(FashionModel, self).__init__()
self.flatten = Flatten()
self.d1 = Dense(120, activation="relu")
self.d2 = Dense(10, activation="softmax")
def call(self,x):
x = self.flatten(x)
x = self.d1(x)
y = self.d2(x)
return y
model = FashionModel()
# 配置训练方法
model.compile(
optimizer=tf.keras.optimizers.Adam(),
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=["sparse_categorical_accuracy"]
)
# 训练
model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1)
# 查看网络结构及数据统计
model.summary()