conditionalRandomFields

crfTrain

Description

The crfTrain action trains a Conditional Random Fields (CRF) model for sequence labeling. CRF is a statistical modeling method often applied in pattern recognition and machine learning for structured prediction. It is particularly suited for tasks like named entity recognition, part-of-speech tagging, and other sequence labeling tasks where the context of the sequence is important for making predictions.

conditionalRandomFields.crfTrain { model={...}, nloOpts={...}, table={...}, target="string", template="string" }
Settings
ParameterDescription
modelSpecifies the output modeling tables. This includes tables for attributes, features, labels, and the template used.
nloOptsSpecifies the nonlinear optimization parameters for the training process, such as the algorithm (LBFGS, SGD, etc.), tolerances, and iteration limits.
tableSpecifies the input CAS table that contains the data for training the model.
targetSpecifies the name of the variable in the input table that contains the labels (the predicted/hidden variable).
templateSpecifies the textual template that defines the features to be extracted from the input data for training the CRF model.
Data Preparation View data prep sheet
Data Creation

This example creates a sample dataset named 'my_cas_library.sequence_data'. This table contains sequences of tokens, with each token having associated features and a label. The '_start_', '_end_', and '_token_' columns define the structure of the sequences, which is essential for the CRF training process.

Copied!
1DATA my_cas_library.sequence_data;
2 INFILE DATALINES delimiter=',';
3 INPUT _start_ $ _end_ $ _token_ $ feature1 $ feature2 $ label $;
4 DATALINES;
5BEGIN,WORD,This,Cap,Len3,O
6WORD,WORD,is,Low,Len2,O
7WORD,END,a,Low,Len1,O
8BEGIN,WORD,SAS,Up,Len3,B-ORG
9WORD,END,Viya,Cap,Len4,I-ORG
10;
11RUN;

Examples

This example demonstrates a basic training of a Conditional Random Fields model. It uses the 'sequence_data' table as input, specifies 'label' as the target variable, and provides a simple feature template. The trained model components are saved into separate tables for labels, attributes, features, and the template itself.

SAS® / CAS Code Code awaiting community validation
Copied!
1PROC CAS;
2 conditionalRandomFields.crfTrain
3 TABLE={name='sequence_data', caslib='my_cas_library'},
4 target='label',
5 template='U00:%x[0,0]',
6 model={label={name='crf_labels', replace=true}, attr={name='crf_attrs', replace=true}, feature={name='crf_features', replace=true}, attrfeature={name='crf_attrfeatures', replace=true}, template={name='crf_template', replace=true}};
7RUN;

This example shows a more advanced training configuration. It uses the LBFGS optimization algorithm and specifies several options for the solver, including a regularization term (regL1) to prevent overfitting and a maximum of 50 iterations. The feature template is more complex, including unigrams and bigrams from the token and its surrounding context.

SAS® / CAS Code Code awaiting community validation
Copied!
1PROC CAS;
2 conditionalRandomFields.crfTrain
3 TABLE={name='sequence_data', caslib='my_cas_library'},
4 target='label',
5 template='U00:%x[0,0]
6U01:%x[-1,0]
7U02:%x[1,0]
8B01:%x[-1,0]/%x[0,0]',
9 nloOpts={
10 algorithm='LBFGS',
11 optmlOpt={regL1=0.5, maxIters=50},
12 printOpt={printLevel='PRINTDETAIL'}
13 },
14 model={
15 label={name='crf_labels_detailed', replace=true, caslib='my_cas_library'},
16 attr={name='crf_attrs_detailed', replace=true, caslib='my_cas_library'},
17 feature={name='crf_features_detailed', replace=true, caslib='my_cas_library'},
18 attrfeature={name='crf_attrfeatures_detailed', replace=true, caslib='my_cas_library'},
19 template={name='crf_template_detailed', replace=true, caslib='my_cas_library'}
20 };
21RUN;

FAQ

What is the purpose of the crfTrain action?
What are the required parameters for the crfTrain action?
What does the 'model' parameter specify?
What is the function of the 'target' parameter?
How do you specify the feature extraction logic for the crfTrain action?
What optimization algorithms can be used for training with the crfTrain action?
How can I apply regularization during model training?
What are the options for the LBFGS solver's line search method?