<jupyter_start><jupyter_text>一，内置fit方法<jupyter_code>tf.keras.backend.clear_session()
def create_model():
    
    model = models.Sequential()
    model.add(layers.Embedding(MAX_WORDS,7,input_length=MAX_LEN))
    model.add(layers.Conv1D(filters = 64,kernel_size = 5,activation = "relu"))
    model.add(layers.MaxPool1D(2))
    model.add(layers.Conv1D(filters = 32,kernel_size = 3,activation = "relu"))
    model.add(layers.MaxPool1D(2))
    model.add(layers.Flatten())
    model.add(layers.Dense(CAT_NUM,activation = "softmax"))
    return(model)

def compile_model(model):
    model.compile(optimizer=optimizers.Nadam(),
                loss=losses.SparseCategoricalCrossentropy(),
                metrics=[metrics.SparseCategoricalAccuracy(),metrics.SparseTopKCategoricalAccuracy(5)]) 
    return(model)
 
model = create_model()
model.summary()
model = compile_model(model)
history = model.fit(ds_train,validation_data = ds_test,epochs = 10)<jupyter_output>Train for 281 steps, validate for 71 steps
Epoch 1/10
281/281 [==============================] - 5s 19ms/step - loss: 1.9726 - sparse_categorical_accuracy: 0.4861 - sparse_top_k_categorical_accuracy: 0.7483 - val_loss: 1.6466 - val_sparse_categorical_accuracy: 0.5757 - val_sparse_top_k_categorical_accuracy: 0.7636
Epoch 2/10
281/281 [==============================] - 2s 7ms/step - loss: 1.4314 - sparse_categorical_accuracy: 0.6280 - sparse_top_k_categorical_accuracy: 0.8053 - val_loss: 1.5296 - val_sparse_categorical_accuracy: 0.6140 - val_sparse_top_k_categorical_accuracy: 0.7939
Epoch 3/10
281/281 [==============================] - 2s 7ms/step - loss: 1.1630 - sparse_categorical_accuracy: 0.6941 - sparse_top_k_categorical_accuracy: 0.8578 - val_loss: 1.5623 - val_sparse_categorical_accuracy: 0.6287 - val_sparse_top_k_categorical_accuracy: 0.8059
Epoch 4/10
281/281 [==============================] - 2s 6ms/step - loss: 0.8999 - sparse_categorical_accuracy: 0.7640 - sparse_top_k_catego[...]<jupyter_text>二，内置train_on_batch方法<jupyter_code>tf.keras.backend.clear_session()

def create_model():
    model = models.Sequential()

    model.add(layers.Embedding(MAX_WORDS,7,input_length=MAX_LEN))
    model.add(layers.Conv1D(filters = 64,kernel_size = 5,activation = "relu"))
    model.add(layers.MaxPool1D(2))
    model.add(layers.Conv1D(filters = 32,kernel_size = 3,activation = "relu"))
    model.add(layers.MaxPool1D(2))
    model.add(layers.Flatten())
    model.add(layers.Dense(CAT_NUM,activation = "softmax"))
    return(model)

def compile_model(model):
    model.compile(optimizer=optimizers.Nadam(),
                loss=losses.SparseCategoricalCrossentropy(),
                metrics=[metrics.SparseCategoricalAccuracy(),metrics.SparseTopKCategoricalAccuracy(5)]) 
    return(model)
 
model = create_model()
model.summary()
model = compile_model(model)

def train_model(model,ds_train,ds_valid,epoches):

    for epoch in tf.range(1,epoches+1):
        model.reset_metrics()
        
        # 在后期降低学习率
        if epoch == 5:
            model.optimizer.lr.assign(model.optimizer.lr/2.0)
            tf.print("Lowering optimizer Learning Rate...\n\n")
        
        for x, y in ds_train:
            train_result = model.train_on_batch(x, y)

        for x, y in ds_valid:
            valid_result = model.test_on_batch(x, y,reset_metrics=False)
            
        if epoch%1 ==0:
            printbar()
            tf.print("epoch = ",epoch)
            print("train:",dict(zip(model.metrics_names,train_result)))
            print("valid:",dict(zip(model.metrics_names,valid_result)))
            print("")
train_model(model,ds_train,ds_test,10)<jupyter_output>================================================================================13:44:01
epoch =  1
train: {'loss': 1.5183109, 'sparse_categorical_accuracy': 0.54545456, 'sparse_top_k_categorical_accuracy': 0.77272725}
valid: {'loss': 2.453667, 'sparse_categorical_accuracy': 0.5703473, 'sparse_top_k_categorical_accuracy': 0.7613535}

================================================================================13:44:02
epoch =  2
train: {'loss': 1.0983586, 'sparse_categorical_accuracy': 0.72727275, 'sparse_top_k_categorical_accuracy': 0.8636364}
valid: {'loss': 2.19265, 'sparse_categorical_accuracy': 0.6148709, 'sparse_top_k_categorical_accuracy': 0.7934105}

================================================================================13:44:04
epoch =  3
train: {'loss': 0.8068676, 'sparse_categorical_accuracy': 0.8181818, 'sparse_top_k_categorical_accuracy': 0.90909094}
valid: {'loss': 1.7307795, 'sparse_categorical_accuracy': 0.63401604, 'sparse_top_k_categorical_accuracy': 0.800[...]<jupyter_text>三，自定义训练循环<jupyter_code>tf.keras.backend.clear_session()

def create_model():
    
    model = models.Sequential()

    model.add(layers.Embedding(MAX_WORDS,7,input_length=MAX_LEN))
    model.add(layers.Conv1D(filters = 64,kernel_size = 5,activation = "relu"))
    model.add(layers.MaxPool1D(2))
    model.add(layers.Conv1D(filters = 32,kernel_size = 3,activation = "relu"))
    model.add(layers.MaxPool1D(2))
    model.add(layers.Flatten())
    model.add(layers.Dense(CAT_NUM,activation = "softmax"))
    return(model)

model = create_model()
model.summary()
optimizer = optimizers.Nadam()
loss_func = losses.SparseCategoricalCrossentropy()

train_loss = metrics.Mean(name='train_loss')
train_metric = metrics.SparseCategoricalAccuracy(name='train_accuracy')

valid_loss = metrics.Mean(name='valid_loss')
valid_metric = metrics.SparseCategoricalAccuracy(name='valid_accuracy')

@tf.function
def train_step(model, features, labels):
    with tf.GradientTape() as tape:
        predictions = model(features,training = True)
        loss = loss_func(labels, predictions)
    gradients = tape.gradient(loss, model.trainable_variables)
    optimizer.apply_gradients(zip(gradients, model.trainable_variables))

    train_loss.update_state(loss)
    train_metric.update_state(labels, predictions)
    

@tf.function
def valid_step(model, features, labels):
    predictions = model(features)
    batch_loss = loss_func(labels, predictions)
    valid_loss.update_state(batch_loss)
    valid_metric.update_state(labels, predictions)
    

def train_model(model,ds_train,ds_valid,epochs):
    for epoch in tf.range(1,epochs+1):
        
        for features, labels in ds_train:
            train_step(model,features,labels)

        for features, labels in ds_valid:
            valid_step(model,features,labels)

        logs = 'Epoch={},Loss:{},Accuracy:{},Valid Loss:{},Valid Accuracy:{}'
        
        if epoch%1 ==0:
            printbar()
            tf.print(tf.strings.format(logs,
            (epoch,train_loss.result(),train_metric.result(),valid_loss.result(),valid_metric.result())))
            tf.print("")
            
        train_loss.reset_states()
        valid_loss.reset_states()
        train_metric.reset_states()
        valid_metric.reset_states()

train_model(model,ds_train,ds_test,10)<jupyter_output>================================================================================13:50:57
Epoch=1,Loss:2.04555941,Accuracy:0.459029168,Valid Loss:1.74900675,Valid Accuracy:0.553873539

================================================================================13:50:58
Epoch=2,Loss:1.52538884,Accuracy:0.603985727,Valid Loss:1.55278289,Valid Accuracy:0.607301891

================================================================================13:51:00
Epoch=3,Loss:1.22206712,Accuracy:0.675462,Valid Loss:1.49989986,Valid Accuracy:0.641585052

================================================================================13:51:01
Epoch=4,Loss:0.895000041,Accuracy:0.765865088,Valid Loss:1.59119236,Valid Accuracy:0.651825488

================================================================================13:51:02
Epoch=5,Loss:0.611382246,Accuracy:0.842908,Valid Loss:1.80687654,Valid Accuracy:0.650935

================================================================================13:51:0[...]
{'fraction_non_alphanumeric': 0.2210960960960961, 'fraction_numerical': 0.07432432432432433, 'mean_word_length': 5.101526717557252, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '531642', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 3089, 'n_tokens_neox': 2666, 'n_words': 349, 'path': 'Computer Science/eat_tensorflow2_in_30_days/practice/6_high level API/2_three ways to fit model/main.ipynb', 'repo_name': 'YunyaoCheng/Books', 'size': '7992'}