📄 main.~pas
字号:
test_file_name := PChar(OpenDialog2.FileName);
test_file_loaded := true;
Application.MessageBox(PChar('测试数据集'+test_file_name + ' successfully loaded!'),PChar('提示'), MB_OK);
end;
end;
procedure TForm1.N10Click(Sender: TObject);
begin
//if SaveDialog1.Execute then
// SaveDialog1.InitialDir := ExtractFilePath(Application.ExeName);
if SaveDialog1.Execute then
begin
model_file_name := PChar(SaveDialog1.FileName);
Application.MessageBox(PChar('设置训练模型保存在'+model_file_name),PChar('提示'), MB_OK);
end;
end;
procedure TForm1.N7Click(Sender: TObject);
var time1, time2 : int64;
begin
if input_file_loaded then
begin
time1 := GetTickCount;
gamma := estimate_gamma(input_file_name);
time2 := GetTickCount;
param.set_gamma(gamma);
time2 := time2 - time1;
ParamSet.gamma_text.Text := FloatToStr(sqrt(1/(2*gamma)));
Application.MessageBox(PChar('估计参数Sigma的值为' + FloatToStr(sqrt(1/(2*gamma)))
+ '(gamma = '+ FloatToStrF(gamma,ffGeneral,15,2) +')'+', 共用时间'+FloatToStr(1.0*time2/1000)+'秒'),PChar('提示'), MB_OK);
end
else
Application.MessageBox(PChar('请导入训练数据集'),PChar('提示'), MB_OK);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
input_file_loaded := false;
test_file_loaded := false;
model_file_loaded := false;
import_file_loaded := false;
output_file_loaded := false;
model_file_name := 'C:/default_model_file.txt';
predict_result_file := 'C:/default_result_file.txt';
param_group_file_name := 'C:/default_param_group_file_name';
model_created := false;
param := CreateParamObject;
end;
procedure TForm1.N8Click(Sender: TObject);
var time1, time2 : int64;
begin
if input_file_loaded then
begin
time1 := GetTickCount;
C := estimate_c(gamma, input_file_name);
param.set_c(C);
ParamSet.C_text.Text := FloatToStr(C);
time2 := GetTickCount;
time2 := time2 - time1;
Application.MessageBox(PChar('估计参数C的值为' + FloatToStr(C)
+ ', 共用时间'+FloatToStr(1.0*time2/1000)+'秒'),PChar('提示'), MB_OK);
end
else
Application.MessageBox(PChar('请导入训练数据集'),PChar('提示'), MB_OK);
end;
procedure TForm1.N12Click(Sender: TObject);
var res : integer;
time1, time2 : int64;
begin
result_file := 'default_result_file.txt';
if input_file_loaded then
begin
time1 := GetTickCount;
res := svm_train_param(param, input_file_name, model_file_name);
time2 := GetTickCount;
time2 := time2 - time1;
model_created := true;
Application.MessageBox(PChar('训练结束,训练总时间为'+FloatToStr(1.0*time2/1000)+'秒,模型文件保存在'+ model_file_name),PChar('提示'), MB_OK);
end else
Application.MessageBox(PChar('请导入训练数据集'),PChar('提示'), MB_OK);
end;
procedure TForm1.N13Click(Sender: TObject);
var accuracy : real;
begin
if (test_file_loaded and model_created)then
begin
accuracy := svm_test_show(param, test_file_name, model_file_name, predict_result_file);
if (param.svm_type = 0 ) then
begin
Application.MessageBox(PChar('训练模型' + model_file_name +'测试'
+test_file_name+'的精度为'+FloatToStr(accuracy)),PChar('提示'), MB_OK);
end
else if(param.svm_type = 3 ) then
begin
Application.MessageBox(PChar('训练模型' + model_file_name +'测试'
+test_file_name+'的平均平方误差为'+FloatToStr(accuracy)),PChar('提示'), MB_OK);
end;
end
else
Application.MessageBox(PChar('未导入需要测试的数据集或尚未生成模型'),PChar('提示'), MB_OK);
end;
procedure TForm1.N20Click(Sender: TObject);
var dense_spread_file : PChar;
begin
if input_file_loaded then
begin
PaintPanel.Visible := false;
RichEdit1.Visible := false;
PaintPicture(input_file_name);
end
else
Application.MessageBox(PChar('未导入需要绘图的二维数据'),PChar('提示'), MB_OK);
// ShowMessage('请导入训练数据......');
{dense_spread_file := 'D:/default_dense_spread_file';
if input_file_loaded then
begin
dense_spread(input_file_name,300,300,dense_spread_file);
showmessage('Convert to spread dense file succesfully!');
end; }
end;
procedure TForm1.N26Click(Sender: TObject);
begin
OpenDialog1.InitialDir := ExtractFilePath(Application.ExeName);
if OpenDialog1.Execute then
begin
model_file_name := PChar(OpenDialog1.FileName);
model_created := true;
end;
end;
procedure TForm1.N16Click(Sender: TObject);
var dense_spread_file, surface_file, dest_file : PChar;
begin
dense_spread_file := './default_dense_spread_file';
surface_file := './default_surface_file';
dest_file := './default_dest_file';
if (model_created and input_file_loaded) then
begin
RichEdit1.Visible := false;
PaintPanel.Visible := false;
dense_spread(input_file_name,Form1.PaintPanel.Width,Form1.PaintPanel.Height,dense_spread_file);
extract_surface_vectors(model_file_name, dense_spread_file, surface_file);
merge_file(input_file_name, surface_file, dest_file);
PaintPicture(dest_file);
end;
end;
procedure TForm1.N27Click(Sender: TObject);
var sv_file, dest_file : PChar;
begin
sv_file := './default_sv_file';
dest_file := './default_dest_file';
if (model_created and input_file_loaded) then
begin
RichEdit1.Visible := false;
PaintPanel.Visible := false;
// dense_spread(input_file_name,Form1.PaintPanel.Width,Form1.PaintPanel.Height,dense_spread_file);
//extract_surface_vectors(model_file_name, dense_spread_file, surface_file);
save_support_vectors(model_file_name,sv_file);
merge_file(input_file_name, sv_file,dest_file);
// merge_file(input_file_name, surface_file, dest_file);
PaintPicture(dest_file);
end
else
begin
ShowMessage('尚未有训练模型或未导入训练数据!');
end;
end;
procedure TForm1.N18Click(Sender: TObject);
begin
RichEdit1.Visible := true;
RichEdit1.Lines.LoadFromFile(predict_result_file);
end;
procedure TForm1.N28Click(Sender: TObject);
var sparse_file : PChar;
a : array[0..2] of integer;
begin
a[0] := 2;
a[1] := 20;
a[2] := 0;
sparse_file := 'C:/default_sparse_file.filter';
if input_file_loaded then
begin
filtering2file(input_file_name, sparse_file, a);
// dense2sparse(input_file_name,sparse_file,2);
// input_file_name := sparse_file;
end;
end;
procedure TForm1.N23Click(Sender: TObject);
var shuffle_file : PChar;
begin
shuffle_file := 'default_shuffle_file';
if input_file_loaded then
begin
shuffle(input_file_name, shuffle_file, 100);
end;
end;
procedure TForm1.N21Click(Sender: TObject);
var accuracy : real;
begin
if input_file_loaded then
begin
// cross_validation_groups_param(
accuracy := cross_validation(param, input_file_name, param.nr_fold, predict_result_file);
if (param.svm_type = 0 ) then
Application.MessageBox(PChar(IntToStr(param.nr_fold)+'倍交叉法验证在训练集' + input_file_name+ '上验证当前参数的精度为'+FloattoStr(accuracy)),PChar('提示'), MB_OK)
else if(param.svm_type =3) then
Application.MessageBox(PChar(IntToStr(param.nr_fold)+'倍交叉法验证在训练集' + input_file_name+
'上验证当前参数的平均方差为'+FloattoStr(accuracy)),PChar('提示'), MB_OK);
end;
end;
procedure TForm1.N19Click(Sender: TObject);
begin
if model_created then
begin
RichEdit1.Visible := true;
RichEdit1.Lines.LoadFromFile(model_file_name);
end
else
Application.MessageBox(PChar('尚未生成模型文件'),PChar('提示'), MB_OK);
end;
procedure TForm1.N5Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.N29Click(Sender: TObject);
begin
convert_form.Show;
end;
procedure TForm1.N3Click(Sender: TObject);
begin
//SaveDialog1.InitialDir := ExtractFilePath(Application.ExeName);
if SaveDialog2.Execute then
begin
predict_result_file := PChar(SaveDialog2.FileName);
Application.MessageBox(PChar('设置预测结果保存在'+predict_result_file),PChar('提示'), MB_OK);
end;
end;
procedure TForm1.N30Click(Sender: TObject);
begin
Form3.Show;
end;
procedure TForm1.N32Click(Sender: TObject);
begin
if OpenDialog3.Execute then
begin
param_group_file_name := PChar(OpenDialog3.FileName);
param_group_file_loaded := true;
Application.MessageBox(PChar('备选参数集'+param_group_file_name + ' successfully loaded!'),PChar('提示'), MB_OK);
end;
end;
procedure TForm1.N31Click(Sender: TObject);
var best_c, best_gamma : real;
best_accu : real;
begin
if param_group_file_loaded and input_file_loaded then
begin
best_accu := cross_validation_groups_param(param, param_group_file_name, input_file_name, param.nr_fold, predict_result_file, best_C, best_gamma);
Application.MessageBox(PChar('最优参数C的值为' + FloatToStr(best_c)
+ ', 最优参数Sigma的值为'+FloatToStr(best_gamma)+', 对应最优的交叉验证误差为 ' + FloatToStr(best_accu)+'。'),PChar('提示'), MB_OK);
ParamSet.C_text.Text := FloatToStr(best_c);
ParamSet.gamma_text.text := FloatToStr(best_gamma);
end
else
Application.MessageBox(PChar('尚未导入备选参数集或生成被选参数集'),PChar('提示'), MB_OK);
end;
procedure TForm1.N33Click(Sender: TObject);
begin
param_form.Show;
end;
procedure TForm1.N34Click(Sender: TObject);
var best_c, best_gamma : real;
best_accu : real;
begin
if (param_group_file_loaded and input_file_loaded and test_file_loaded) then
begin
best_accu := svm_train_test_batch(param, param_group_file_name, input_file_name,test_file_name, predict_result_file, best_C, best_gamma);
Application.MessageBox(PChar('最优参数C的值为' + FloatToStr(best_c)
+ ', 最优参数Sigma的值为'+FloatToStr(best_gamma)+', 对应最优的交叉验证误差为 ' + FloatToStr(best_accu)+'。'),PChar('提示'), MB_OK);
ParamSet.C_text.Text := FloatToStr(best_c);
ParamSet.gamma_text.text := FloatToStr(best_gamma);
end
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -