神经网络搭建八股
使用tf.keras
六步法搭建模型
1.import
2.train, test 指定输入特征/标签
3.model = tf.keras.model.Sequential
在Squential,搭建神经网络
4.model.compile
配置训练方法,选择哪种优化器、损失函数、评测指标
5.model.fit 执行训练过程,告知训练集输入特征,batch,epoch
6.model.summary打印网络结构和参数统计
model = tf.keras.model.Sequential
Sequential是个容器,封装了网络结构
网络结构例子:
拉直层:tf.keras.layers.Flatten()
全连接层:tf.keras.layers.Dense(神经元个数,activetion="激活函数",kernel_regularizer=那种正则化)
卷积层:
tf.keras.layers.Conv2D(filters= 卷积核个数,kernel_size=卷积核尺寸,strides=卷积步长,padding="valid"or"same"
LSTM层:
tf.keras.layers.LSTM()
model.compile
model.compile(optimizer=优化器,loss=损失函数,metrics=["准确率"]
后期可通过tensorflow官网查询函数的具体用法,调节超参数
有些网络输出经过softmax概率分布输出,有些不经过概率分布输出
当网络评测指标和蒙的概率一样,例如十分类概率为.1/10.可能概率分布错了
独热码y_和y是[010]网络输出则为[0.xx, 0.xx, 0.xx]
第三种方法 y_= [1] y =[0.2xx,0xx,0xx]
model.fit
model.fit(训练集的输入特征,训练集的标签,batch_size, epochs=,
validation_data=(测试集的输入特征,标签),
validation_split=从训练集划分多少比例给测试集,
validation_freq=多少次epoch测试一次)
model.summary
重构Iris分类
import tensorflow as tf
from sklearn import datasets
import numpy as npx_train = datasets.load_iris().data
y_train = datasets.load_iris().targetnp.random.seed(116)
np.random.shuffle(x_train)
np.random.seed(116)
np.random.shuffle(y_train)
tf.random.set_seed(116)model = tf.keras.models.Sequential([tf.keras.layers.Dense(3, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2())
])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()
自定义搭建模型
swquential可以搭建上层输出就是下层输入的网络结构,但是无法搭建带有跳连特征的非顺序网络结构
class MyModel(Model)
def __init__(self):
super(MyModel, self) __init()
定义网络结构块
def call(self, x): #写出前向传播
调用网络结构块,实现前向传播
return y
model = MyModel
__init__定义出积木
call调用积木,实现前向传播
import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras import Model
from sklearn import datasets
import numpy as npx_train = datasets.load_iris().data
y_train = datasets.load_iris().targetnp.random.seed(116)
np.random.shuffle(x_train)
np.random.seed(116)
np.random.shuffle(y_train)
tf.random.set_seed(116)class IrisModel(Model):def __init__(self):super(IrisModel, self).__init__()self.d1 = Dense(3, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2())def call(self, x):y = self.d1(x)return ymodel = IrisModel()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()
每循环一次train,计算一次test的测试指标
MNIST数据集
1.导入MNIST数据集
mnist=tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data(
2.作为输入特征,输入神经网络时,将数据拉伸成一维数组:
tf.keras.layers.Flatten()
把784个像素点的灰度值作为输入特征放入神经网络
plt.imshow(x_train[0], cmap='gray')#绘制灰度图
plt.show()
0表示纯黑色255表示纯白色
需要对测试集和数据集进行归一化处理,把数值变小,更适合神经网络吸收,使用sequental训练模型,由于输入特征为数组,输出为概率分布,所以我们选择sparse_categorical_accuracy
import tensorflow as tfmnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0model = 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()
准确率是测试集的准确率
自定义Model实现 __init__中定义cell函数中用到的层
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras import Modelmnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0class MnistModel(Model):def __init__(self):super(MnistModel, self).__init__()self.flatten = Flatten()self.d1 = Dense(128, 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 ymodel = MnistModel()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()
FASHION数据集
import tensorflow as tffashion = tf.keras.datasets.fashion_mnist
(x_train, y_train),(x_test, y_test) = fashion.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0model = 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()