Language Model Action Set
Training an RNN Acoustic Model
This example is not available for the CAS programming language.
Training an RNN Acoustic Model
This example is not available for the Lua programming language.
Training an RNN Acoustic Model
This section contains Python code. For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.
The following code shows how you can train an acoustic model that uses a recurrent neural network (RNN) in CAS:
# step 1: import the SAS Scripting Wrapper for Analytics Transfer library
from swat import *
# step 2: start a CAS session
s = CAS(host, port)
# step 3: load CAS action sets
s.loadactionset("audio")
s.loadactionset("searchAnalytics")
s.loadactionset("deepLearn")
# step 4: load audio data for training and development set
s.audio.loadaudio(path = "speech/train.listing",
casOut = {"name": "train_audio", "replace": True},
)
s.audio.loadaudio(path = "speech/dev.listing",
casOut = {"name": "dev_audio", "replace": True},
)
# step 5: load transcripts for training and development set
s.table.loadtable(path = "speech/train_trans.csv",
casOut = {"name": "train_trans", "replace": True},
importoptions = {"fileType": "CSV"}
)
s.table.loadtable(path = "speech/dev_trans.csv",
casOut = {"name": "dev_trans", "replace": True},
importoptions = {"fileType": "CSV"}
)
# step 6: join audio data with transcripts for training and development set
nChars = 530 # set maximum number of characters in a sentence
vars1 = [{"name": "_path_", "iskey": True}, {"name": "_audio_"}]
vars2 = [{"name": "_path_", "iskey": True}, {"name": "ylen"}]
for ci in range(nChars):
vars2.append({"name": "y%d" % (ci)})
s.searchanalytics.searchjoin(
casout = {"name": "train_computefeatures_input", "replace": True},
jointype = "left",
left = {"maps": vars1, "table": {"name": "train_audio"}},
right = {"maps": vars2, "table": {"name": "train_trans"}}
)
s.searchanalytics.searchjoin(
casout = {"name": "dev_computefeatures_input", "replace": True},
jointype = "left",
left = {"maps": vars1, "table": {"name": "dev_audio"}},
right = {"maps": vars2, "table": {"name": "dev_trans"}}
)
# step 7: extract MFCC features from audio files for training and development set
# Note: Values of the "frameExtractionOptions", "melBanksOptions", "mfccOptions"
# and "featureScalingMethod" parameters depend on your choices.
nFrames = 1500 # set maximum number of time frames in an audio file
vars_list = ["_path_", "ylen"]
for ci in range(nChars):
vars_list.append("y%d" % (ci))
s.audio.computefeatures(
table = "train_computefeatures_input",
casOut = {"name": "train_dltrain_input", "replace": True},
audioColumn = "_audio_",
copyvars = vars_list,
frameExtractionOptions = {"frameShift": 10, "frameLength": 25, "dither": 0},
melBanksOptions = {"nBins": 40},
mfccOptions = {"nCeps": 40},
featureScalingMethod = "STANDARDIZATION",
nOutputFrames = nFrames,
)
s.audio.computefeatures(
table = "dev_computefeatures_input",
casOut = {"name": "dev_dltrain_input", "replace": True},
audioColumn = "_audio_",
copyvars = vars_list,
frameExtractionOptions = {"frameShift": 10, "frameLength": 25, "dither": 0},
melBanksOptions = {"nBins": 40},
mfccOptions = {"nCeps": 40},
featureScalingMethod = "STANDARDIZATION",
nOutputFrames = nFrames,
)
# step 8: build an RNN model with one bidirectional LSTM layer
s.buildmodel(model = "asr", type = "RNN")
s.addlayer(model = "asr", name = "data", layer = {"type": "input"})
s.addlayer(
model = "asr",
name = "rnn11",
srclayers = ["data"],
layer = {"type": "recurrent", "n": 120, "dropout": 0.1,
"rnnType": "lstm", "init": "msra",
"outputType": "samelength", "reversed": False}
)
s.addlayer(
model = "asr",
name = "rnn12",
srclayers = ["data"],
layer = {"type": "recurrent", "n": 120, "dropout": 0.1,
"rnnType": "lstm", "init": "msra",
"outputType": "samelength", "reversed": True}
)
s.addlayer(
model = "asr",
name = "outlayer",
srclayers = ["rnn11", "rnn12"],
layer = {"type": "output", "act": "softmax", "error": "CTC"}
)
# step 9: train the RNN model
nToken = 40 # 40 is the number of features in each frame
inputs = []
for fi in range(nFrames):
for vi in range(nToken):
inputs.append("_f%d_v%d_" % (fi, vi))
targets = ["y%d" % i for i in range(nChars)]
s.dlTrain(
table = "train_dltrain_input",
validTable = "dev_dltrain_input",
model = "asr",
modelWeights = "trainedWeights",
bestWeights = "bestWeights",
targetSeq = targets,
inputs = inputs,
nominals = targets,
sequenceOpts = {"seqLen": "_num_frames_",
"tgtLen": "ylen",
"tokensize": nToken
},
optimizer = {"miniBatchSize": 16,
"logLevel": 3,
"maxEpochs": 1,
"snapshotFreq": 1,
"algorithm": {"method": "adam",
"clipGradMax": 10000,
"clipGradMin": -10000,
"learningRate": 0.001,
"lrpolicy": "step",
"stepsize": 30,
"gamma": 0.5
}
}
)
# step 10: save the RNN model
# Note: Each model has three tables - the model table, the weight table and the
# attribute table. Make sure all three are saved.
s.save(table = "asr",
name = "speech/RNN_model",
replace = True
)
s.save(table = "trainedWeights",
name = "speech/RNN_model_weights",
replace = True
)
s.table.attribute(task = "CONVERT", name = "trainedWeights")
s.save(table = "trainedWeights.attrs",
name = "speech/RNN_model_weights_attr",
replace = True
)
In this example, the RNN model has only one bidirectional long short-term memory (LSTM) layer. For better performance, you could try different ways to extract features from audio files or try different architectures for the acoustic model. In addition, you might want to experiment with different parameters when calling the dlTrain action.
Training an RNN Acoustic Model
This example is not available for the R programming language.