一.建模流程
通常用TensorFlow实现机器学习模型,尤其常用于实现神经网络模型,但为简洁起见,一般推荐使用TensorFlow的高层次keras接口来实现神经网络模型
使用TensorFlow实现神经网络模型的一般流程为:准备数据 → 定义模型 → 训练模型 → 评估模型 → 使用模型 → 保存模型
1.准备数据
df = pd.read_csv('./data.csv')
2.定义模型
使用keras接口有三种方式构建模型
①使用Sequential按层顺序构建模型:最简单
model = models.Sequential()
model.add(layers.Dense(20,activation = 'relu'))
...
model.summary()
②使用函数式API构建任意结构模型
inputs = layers.Input(shape=(32,32,3))
x = layers.Conv2D(32,kernel_size=(3,3))(inputs)
x = layers.MaxPool2D()(x)
...
outputs = layers.Dense(1,activation = 'sigmoid')(x)
model = models.Model(inputs = inputs,outputs = outputs)
model.summary()
③继承Model基类构建自定义模型
class CnnModel(models.Model):
def __init__(self):
super(CnnModel,self).__init__()
def build(self,input_shape):
self.embedding = layers.Embedding(MAX_WORDS,7,input_length = MAX_LEN)
self.conv_1 = layers.Conv1D(16,kernel_size = 5,name = "conv_1",activation = "relu")
self.pool = layers.MaxPool1D()
self.conv_2 = layers.Conv1D(128,kernel_size = 2,name = "conv_2",activation = "relu")
self.flatten = layers.Flatten()
self.dense = layers.Dense(1,activation = "sigmoid")
super(CnnModel,self).build(input_shape)
def call(self,x):
x = self.embedding(x)
x = self.conv_1(x)
x = self.pool(x)
x = self.conv_2(x)
x = self.pool(x)
x = self.flatten(x)
x = self.dense(x)
return(x)
model = CnnModel()
model.build(input_shape = (None,MAX_LEN))
model.summary()
3.训练模型
训练模型通常有三种方法
①内置fit方法:最简单
#二分类问题选择二元交叉熵损失函数
model.compile(optimizer = 'adam',
loss = 'binary_crossentropy',
metrics = ['AUC'])
history = model.fit(x_train,y_train,
batch_size = 64,
epochs = 30,
validation_split = 0.2 #分割一部分训练数据用于验证
)
②内置train_on_batch方法
③自定义训练循环
4.评估模型
def plot_metric(history,metric):
train_metrics = history.history[metric]
val_metrics = history.history['val_' + metric]
epochs = range(1,len(train_metrics) + 1)
plt.plot(epochs,train_metrics,'bo--'