python pack_nbrs.py --max_nbrs=5 \ labeled_data.tfr \ unlabeled_data.tfr \ graph.tsv \ merged_examples.tfr
import neural_structured_learning as nsl# Create a custom model — sequential, functional, or subclass. base_model = tf.keras.Sequential(…)# Wrap the custom model with graph regularization. graph_config = nsl.configs.GraphRegConfig( neighbor_config=nsl.configs.GraphNeighborConfig(max_neighbors=1)) graph_model = nsl.keras.GraphRegularization(base_model, graph_config)# Compile, train, and evaluate. graph_model.compile(optimizer=’adam’, loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=[‘accuracy’]) graph_model.fit(train_dataset, epochs=5) graph_model.evaluate(test_dataset)
import neural_structured_learning as nsl# Create a base model — sequential, functional, or subclass. model = tf.keras.Sequential(…)# Wrap the model with adversarial regularization. adv_config = nsl.configs.make_adv_reg_config(multiplier=0.2, adv_step_size=0.05) adv_model = nsl.keras.AdversarialRegularization(model, adv_config)# Compile, train, and evaluate. adv_model.compile(optimizer=’adam’, loss=’sparse_categorical_crossentropy’, metrics=[‘accuracy’]) adv_model.fit({‘feature’: x_train, ‘label’: y_train}, epochs=5) adv_model.evaluate({‘feature’: x_test, ‘label’: y_test})