资源简介
crf条件随机场模型--matlab源码
代码片段和文件信息
clear all
useMex = 1; % Set this to 1 to use mex files to speed things up
%% Generate Synthetic Data
% Notes:
% - X is categorical each element X(ij) contains the value of feature j for word i
% a value of ‘0‘ means ignore the feature for this training example
% - y is cateogircal each element y(i) contains the label for word i
% a value of ‘0‘ indicates the position between sentences
[Xy] = crfChain_genSynthetic;
nWords = size(X1);
nStates = max(y);
nFeatures = max(X);
%% Initialize parameters and data structures
[wv_startv_endv] = crfChain_initWeights(nFeaturesnStates‘randn‘);
featureStart = cumsum([1 nFeatures(1:end)]); % data structure which relates high-level ‘features‘ to elements of w
sentences = crfChain_initSentences(y);
nSentences = size(sentences1);
wv = [w(:);v_start(:);v_end(:);v(:)];
%% Set up training/testing indices
trainNdx = 1:floor(nSentences/2);
testNdx = floor(nSentences/2)+1:nSentences;
%% Example of making potentials and doing inference with first sentence
s = 1;
if useMex
[nodePotedgePot] = crfChain_makePotentialsC(XwvnFeaturesfeatureStartsentencessnStates);
[nodeBeledgeBellogZ] = crfChain_inferC(nodePotedgePot);
else
[nodePotedgePot]=crfChain_makePotentials(Xwv_startv_endvnFeaturesfeatureStartsentencess);
[nodeBeledgeBellogZ] = crfChain_infer(nodePotedgePot);
end
%% Compute Errors with random parameters
fprintf(‘Errors based on most likely sequence with random parameters:\n‘);
trainErr = crfChain_error(wv_startv_endvXynStatesnFeaturesfeatureStartsentences(trainNdx:)‘decode‘useMex)
testErr = crfChain_error(wv_startv_endvXynStatesnFeaturesfeatureStartsentences(testNdx:)‘decode‘useMex)
fprintf(‘Errors based on max marginals with random parameters:\n‘);
trainErr = crfChain_error(wv_startv_endvXynStatesnFeaturesfeatureStartsentences(trainNdx:)‘infer‘useMex)
testErr = crfChain_error(wv_startv_endvXynStatesnFeaturesfeatureStartsentences(testNdx:)‘infer‘useMex)
%% Training
% Compute objective function over training data
if useMex
maxSentenceLength = 1+max(sentences(:2)-sentences(:1));
crfChain_lossC2(wvXynStatesnFeaturesfeatureStartsentencesmaxSentenceLength);
else
crfChain_loss(wvXynStatesnFeaturesfeatureStartsentences);
end
% Optimize parameters
fprintf(‘Training...\n‘);
if useMex
[wv] = minFunc(@crfChain_lossC2[w(:);v_start;v_end;v(:)][]XynStatesnFeaturesfeatureStartsentences(trainNdx:)maxSentenceLength);
else
[wv] = minFunc(@crfChain_losswv[]XynStatesnFeaturesfeatureStartsentences(trainNdx:));
end
% Split up weights
[wv_startv_endv] = crfChain_splitWeights(wvfeatureStartnStates);
%% Decode/Infer/Sample based on first test example
s = testNdx(1);
fprintf(‘True Labels for first test sentence:\n‘);
y(sentences(s1):sentences(s2))‘
fprintf(‘Most likely sequen
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2009-04-13 20:34 crfChain\
目录 0 2009-04-13 20:36 crfChain\crfChain\
文件 621 2008-06-22 10:42 crfChain\crfChain\crfChain_decode.m
文件 915 2009-04-13 20:28 crfChain\crfChain\crfChain_error.m
文件 1090 2009-04-11 20:49 crfChain\crfChain\crfChain_genSynthetic.m
文件 1272 2008-06-22 10:41 crfChain\crfChain\crfChain_infer.m
文件 355 2008-06-22 19:29 crfChain\crfChain\crfChain_initSentences.m
文件 461 2008-06-22 19:29 crfChain\crfChain\crfChain_initWeights.m
文件 2636 2008-10-30 20:25 crfChain\crfChain\crfChain_loss.m
文件 1046 2008-09-13 15:06 crfChain\crfChain\crfChain_makePotentials.m
文件 772 2008-07-02 16:02 crfChain\crfChain\crfChain_sample.m
文件 411 2008-06-22 19:29 crfChain\crfChain\crfChain_splitWeights.m
目录 0 2009-04-13 20:46 crfChain\crfChain\mex\
文件 4230 2008-10-30 20:25 crfChain\crfChain\mex\crfChain_inferC.c
文件 12681 2009-04-13 20:46 crfChain\crfChain\mex\crfChain_inferC.mexglx
文件 9660 2009-01-07 17:35 crfChain\crfChain\mex\crfChain_inferC.mexmac
文件 8704 2008-10-30 20:25 crfChain\crfChain\mex\crfChain_inferC.mexw32
文件 12423 2008-10-30 20:25 crfChain\crfChain\mex\crfChain_lossC2.c
文件 12707 2009-04-13 20:46 crfChain\crfChain\mex\crfChain_lossC2.mexglx
文件 13820 2009-01-07 17:35 crfChain\crfChain\mex\crfChain_lossC2.mexmac
文件 12288 2008-10-30 20:25 crfChain\crfChain\mex\crfChain_lossC2.mexw32
文件 3238 2008-10-30 20:25 crfChain\crfChain\mex\crfChain_makePotentialsC.c
文件 8470 2009-04-13 20:46 crfChain\crfChain\mex\crfChain_makePotentialsC.mexglx
文件 9528 2009-01-07 17:35 crfChain\crfChain\mex\crfChain_makePotentialsC.mexmac
文件 7680 2008-10-30 20:25 crfChain\crfChain\mex\crfChain_makePotentialsC.mexw32
文件 4411 2009-04-13 20:36 crfChain\example_crfChain.m
目录 0 2009-04-13 20:34 crfChain\KPM\
文件 3965 2008-08-31 07:11 crfChain\KPM\repmatC.c
文件 7680 2008-08-31 07:11 crfChain\KPM\repmatC.dll
文件 20682 2008-08-31 07:11 crfChain\KPM\repmatC.mexglx
文件 10000 2008-08-31 07:11 crfChain\KPM\repmatC.mexmac
............此处省略13个文件信息
- 上一篇:Matlab余弦相似度算法判断图片相似度并识别源代码
- 下一篇:LLEMATLAB
评论
共有 条评论