我的程序如下:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end; TBpnn = class
// 成员变量 // 输入层神经元个数 int input_n;
Input_n : integer; // 隐含层神经元个数 int hidden_n;
Hidden_n : integer; // 输出层神经元个数 int output_n;
Output_n : Integer; // 输入层神经元 double *input_units;
Intput_units : double; //这里改写成Input_units : array of
//double;那么Input_units就是指向动态
//数组地址的指针。那么对应的函数应该
//如何写他的返回指呢,返回的也是指针。
// 隐含层神经元 double *hidden_units;
Hidden_units : double; // 输出层神经元 double *output_units;
Output_units : double; // 隐含层误差 double *hidden_delta;
Hidden_delta : double; // 输出层误差 double *output_delta;
Output_delta : double; // 训练目标值 double *target;
Targer : double; // 输入层权值矩阵 double **input_weights;
Input_weights : double; // 隐含层权值矩阵 double **hidden_weights;
Hidden_weights: double; // 前输入层权值矩阵 double **input_prev_weights;
Input_prev_weights: double; // 前隐含层权值矩阵 double **hidden_prev_weights;
Hidden_prev_weights: double;
private
// 返回 0-1 的双精度随机数 double drnd(void);函数
function Drnd(x:integer): double; // 返回-1.0 - 1.0之间的双精度随机数 double dpn1(void);函数
function Dpn1(x:integer): double; // 作用函数,这里使用 S 函数 double squash(double x);
function Squash(x: double): double; // 申请1位双精度实数数组 double * alloc_1d_dbl(int n); 函数
function alloc_1d_dbl(n: integer): double; // 申请2位双精度实数数组 double ** alloc_2d_dbl(int m, int n); 函数
function alloc_2d_dbl(m,n: integer): double; // 随机初始化权值 void bpnn_randomize_weights(double ** w, int m, int n);过程
procedure Bpnn_randomize_weights(m,n: integer); // 0值化权值 void bpnn_zero_weights(double ** w, int m , int n); 过程
procedure Bpnn_zero_weights(m,n: integer); // 创建神经网络 void bpnn_internal_create(int n_in, int n_hidden, int n_out);过程
procedure Bpnn_internal_create(n_in,n_hidden,n_out: integer); // 输出称误差
procedure Bpnn_output_error(delta: double;target: double;output: double;nj:
integer;err: double); // 隐含层误差
procedure Bpnn_hidden_error(delta: double;nh: integer;delta_o: double;no:
integer;who:array of double;hidden: double; err: double); // 神经网络权值调整
procedure Bpnn_adjust_weights(delta: double;ndelta: integer;ly: double; nly:
integer;w: array of double;oldw: array of double;momentum: double); // 单层神经网络前向运动
procedure Bpnn_layerforward(l1: double;l2: double;Conn: array of integer;n1: integer;n2: integer); // 创建神经网络 void bpnn_create(int n_in, int n_hidden, int n_out);
procedure Bpnn_create(n_in,n_hidden,n_out: integer); // 训练BP神经网络
procedure Bpnn_train(eta, momentum: double;eo,eh: array of double); // 神经元前向运算 void bpnn_feedforward(void);
procedure Bpnn_feedforward(); public
// 传送输入参数给神经网络
procedure Bpnn_getelement(in_units: double;ta_units: double); //加入输入单元 void bpnn_SetInputElement(double * in_units);过程
procedure Bpnn_SetInputElement(in_units: double); // 取出输出单元 void bpnn_GetOutputElement(double * out_units);过程
procedure Bpnn_GetOutPutElement(out_units: double);
end;
var
Form1: TForm1;
Bp : Tbpnn;implementation{$R *.dfm}//返回0~1的双精度随机数
function Tbpnn.Drnd(x:integer): double;
begin
Result := random(x);
end;//返回-1~1的双精度随机数
function Tbpnn.Dpn1(x:integer): double;
begin
Result := Drnd(x)*2.0-1.0;
end;//作用函数,这里选用的是S型函数
//参数X是自变量
function Tbpnn.Squash(x: double): double;
begin
Result := 1.0/(1.0+exp(-X));
end;//申请1维的双精度实数数组
//参数:n为数组的元数个数;
function Tbpnn.alloc_1d_dbl(n: integer): double;
var
New1 : array of double;
begin
SetLength(New1,n);
if New1 = nil then
Result := 0
else
Result := New1[n]; //这个地方我如果我改成Result := New1;就是错误。而函数function
//Tbpnn.alloc_1d_dbl(n: integer): double;我如何让他返回的是个指针
//也就是一维数组的地址。
end;//申请2维的双精度实数数组
//参数:m为数组的行数
// n为数组的列数
function Tbpnn.alloc_2d_dbl(m,n: integer):double;
var
New1 : array of array of double;
I : integer;
begin
SetLength(New1,m,n);
if New1 = nil then
Result := 0
else
for i:= 0 to m do
New1[i,n] := alloc_1d_dbl(n);
Result := New1[m,n];
end;//随机初始化权值
//参数:w为保存权值的2维数组
// m为数组的行数
// n为数组的列数
procedure Tbpnn.Bpnn_randomize_weights(m,n: integer);
var
w: array of array of double;
i,j,x : integer;
begin
Setlength(w,m,n);
for i:= 0 to m do
for j:= 0 to n do
w[i,j] := Dpn1(x);
end;procedure Tbpnn.Bpnn_zero_weights(m,n: integer);
var
w : array of array of double;
i,j : integer;
begin
for i:= 0 to m do
for j:= 0 to n do
w[i,j] := 0.0;
end;//创建BPNN网络
//参数:n_in为输入层神经元个数
// n_hidden为隐含层神经元个数
// n_out为输入层神经元个数
procedure Tbpnn.Bpnn_internal_create(n_in,n_hidden,n_out: integer);
begin
Bp.Input_n := n_in;
Bp.Hidden_n := n_hidden;
Bp.Output_n := n_out; Bp.Intput_units := alloc_1d_dbl(n_in+1);
Bp.Hidden_units := alloc_1d_dbl(n_hidden+1);
Bp.Output_units := alloc_1d_dbl(n_out+1); Bp.Hidden_delta := alloc_1d_dbl(n_hidden+1);
Bp.Output_delta := alloc_1d_dbl(n_out+1);
Bp.Targer := alloc_1d_dbl(n_out+1); Bp.Input_weights := alloc_2d_dbl(n_in+1,n_hidden+1);
Bp.Hidden_weights := alloc_2d_dbl(n_hidden+1,n_out+1); Bp.Input_prev_weights := alloc_2d_dbl(n_in+1,n_hidden+1);
Bp.Hidden_prev_weights:= alloc_2d_dbl(n_hidden+1,n_out+1);end;procedure Bpnn_feedforward();
beginend;
procedure Tbpnn.Bpnn_output_error(delta: double;target: double;output: double;nj: integer;err: double);
beginend;procedure Tbpnn.Bpnn_hidden_error(delta: double;nh: integer;delta_o: double;
no: integer;who: array of double;hidden: double; err: double);
beginend;procedure Tbpnn.Bpnn_adjust_weights(delta: double;ndelta: integer;ly: double; nly: integer;w: array of double;oldw: array of double;momentum: double);
beginend;procedure Tbpnn.Bpnn_train(eta, momentum: double;eo,eh: array of double);
beginend;procedure Tbpnn.Bpnn_feedforward();
begin
end;procedure Tbpnn.Bpnn_layerforward(l1: double;l2: double;Conn: array of integer;n1: integer;n2: integer);
begin
end;procedure Tbpnn.Bpnn_create(n_in,n_hidden,n_out: integer);
beginend;procedure Tbpnn.Bpnn_getelement(in_units: double;ta_units: double);
beginend;procedure Tbpnn.Bpnn_SetInputElement(in_units: double);
beginend;procedure Tbpnn.Bpnn_GetOutPutElement(out_units: double);
beginend;
end.动态数组定义了之后,例如:Input_units : array of double;实际上这个Input_units指向的是动态数组的地址。那么在类的实例Bp中,我调用Input_units,对应的函数alloc_1d_dbl()我如何让他返回值也是数组的地址。我上面为什么把Input_units定义为double,是因为类型不匹配我没有办法(为了让程序通过而做的,实际上是错的)。这里主要的就是让函数alloc_1d_dbl()能跟Input_units类型匹配。
大哥们可以教教我吗?其他的想我应该可以举一反三的。谢谢了。
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end; TBpnn = class
// 成员变量 // 输入层神经元个数 int input_n;
Input_n : integer; // 隐含层神经元个数 int hidden_n;
Hidden_n : integer; // 输出层神经元个数 int output_n;
Output_n : Integer; // 输入层神经元 double *input_units;
Intput_units : double; //这里改写成Input_units : array of
//double;那么Input_units就是指向动态
//数组地址的指针。那么对应的函数应该
//如何写他的返回指呢,返回的也是指针。
// 隐含层神经元 double *hidden_units;
Hidden_units : double; // 输出层神经元 double *output_units;
Output_units : double; // 隐含层误差 double *hidden_delta;
Hidden_delta : double; // 输出层误差 double *output_delta;
Output_delta : double; // 训练目标值 double *target;
Targer : double; // 输入层权值矩阵 double **input_weights;
Input_weights : double; // 隐含层权值矩阵 double **hidden_weights;
Hidden_weights: double; // 前输入层权值矩阵 double **input_prev_weights;
Input_prev_weights: double; // 前隐含层权值矩阵 double **hidden_prev_weights;
Hidden_prev_weights: double;
private
// 返回 0-1 的双精度随机数 double drnd(void);函数
function Drnd(x:integer): double; // 返回-1.0 - 1.0之间的双精度随机数 double dpn1(void);函数
function Dpn1(x:integer): double; // 作用函数,这里使用 S 函数 double squash(double x);
function Squash(x: double): double; // 申请1位双精度实数数组 double * alloc_1d_dbl(int n); 函数
function alloc_1d_dbl(n: integer): double; // 申请2位双精度实数数组 double ** alloc_2d_dbl(int m, int n); 函数
function alloc_2d_dbl(m,n: integer): double; // 随机初始化权值 void bpnn_randomize_weights(double ** w, int m, int n);过程
procedure Bpnn_randomize_weights(m,n: integer); // 0值化权值 void bpnn_zero_weights(double ** w, int m , int n); 过程
procedure Bpnn_zero_weights(m,n: integer); // 创建神经网络 void bpnn_internal_create(int n_in, int n_hidden, int n_out);过程
procedure Bpnn_internal_create(n_in,n_hidden,n_out: integer); // 输出称误差
procedure Bpnn_output_error(delta: double;target: double;output: double;nj:
integer;err: double); // 隐含层误差
procedure Bpnn_hidden_error(delta: double;nh: integer;delta_o: double;no:
integer;who:array of double;hidden: double; err: double); // 神经网络权值调整
procedure Bpnn_adjust_weights(delta: double;ndelta: integer;ly: double; nly:
integer;w: array of double;oldw: array of double;momentum: double); // 单层神经网络前向运动
procedure Bpnn_layerforward(l1: double;l2: double;Conn: array of integer;n1: integer;n2: integer); // 创建神经网络 void bpnn_create(int n_in, int n_hidden, int n_out);
procedure Bpnn_create(n_in,n_hidden,n_out: integer); // 训练BP神经网络
procedure Bpnn_train(eta, momentum: double;eo,eh: array of double); // 神经元前向运算 void bpnn_feedforward(void);
procedure Bpnn_feedforward(); public
// 传送输入参数给神经网络
procedure Bpnn_getelement(in_units: double;ta_units: double); //加入输入单元 void bpnn_SetInputElement(double * in_units);过程
procedure Bpnn_SetInputElement(in_units: double); // 取出输出单元 void bpnn_GetOutputElement(double * out_units);过程
procedure Bpnn_GetOutPutElement(out_units: double);
end;
var
Form1: TForm1;
Bp : Tbpnn;implementation{$R *.dfm}//返回0~1的双精度随机数
function Tbpnn.Drnd(x:integer): double;
begin
Result := random(x);
end;//返回-1~1的双精度随机数
function Tbpnn.Dpn1(x:integer): double;
begin
Result := Drnd(x)*2.0-1.0;
end;//作用函数,这里选用的是S型函数
//参数X是自变量
function Tbpnn.Squash(x: double): double;
begin
Result := 1.0/(1.0+exp(-X));
end;//申请1维的双精度实数数组
//参数:n为数组的元数个数;
function Tbpnn.alloc_1d_dbl(n: integer): double;
var
New1 : array of double;
begin
SetLength(New1,n);
if New1 = nil then
Result := 0
else
Result := New1[n]; //这个地方我如果我改成Result := New1;就是错误。而函数function
//Tbpnn.alloc_1d_dbl(n: integer): double;我如何让他返回的是个指针
//也就是一维数组的地址。
end;//申请2维的双精度实数数组
//参数:m为数组的行数
// n为数组的列数
function Tbpnn.alloc_2d_dbl(m,n: integer):double;
var
New1 : array of array of double;
I : integer;
begin
SetLength(New1,m,n);
if New1 = nil then
Result := 0
else
for i:= 0 to m do
New1[i,n] := alloc_1d_dbl(n);
Result := New1[m,n];
end;//随机初始化权值
//参数:w为保存权值的2维数组
// m为数组的行数
// n为数组的列数
procedure Tbpnn.Bpnn_randomize_weights(m,n: integer);
var
w: array of array of double;
i,j,x : integer;
begin
Setlength(w,m,n);
for i:= 0 to m do
for j:= 0 to n do
w[i,j] := Dpn1(x);
end;procedure Tbpnn.Bpnn_zero_weights(m,n: integer);
var
w : array of array of double;
i,j : integer;
begin
for i:= 0 to m do
for j:= 0 to n do
w[i,j] := 0.0;
end;//创建BPNN网络
//参数:n_in为输入层神经元个数
// n_hidden为隐含层神经元个数
// n_out为输入层神经元个数
procedure Tbpnn.Bpnn_internal_create(n_in,n_hidden,n_out: integer);
begin
Bp.Input_n := n_in;
Bp.Hidden_n := n_hidden;
Bp.Output_n := n_out; Bp.Intput_units := alloc_1d_dbl(n_in+1);
Bp.Hidden_units := alloc_1d_dbl(n_hidden+1);
Bp.Output_units := alloc_1d_dbl(n_out+1); Bp.Hidden_delta := alloc_1d_dbl(n_hidden+1);
Bp.Output_delta := alloc_1d_dbl(n_out+1);
Bp.Targer := alloc_1d_dbl(n_out+1); Bp.Input_weights := alloc_2d_dbl(n_in+1,n_hidden+1);
Bp.Hidden_weights := alloc_2d_dbl(n_hidden+1,n_out+1); Bp.Input_prev_weights := alloc_2d_dbl(n_in+1,n_hidden+1);
Bp.Hidden_prev_weights:= alloc_2d_dbl(n_hidden+1,n_out+1);end;procedure Bpnn_feedforward();
beginend;
procedure Tbpnn.Bpnn_output_error(delta: double;target: double;output: double;nj: integer;err: double);
beginend;procedure Tbpnn.Bpnn_hidden_error(delta: double;nh: integer;delta_o: double;
no: integer;who: array of double;hidden: double; err: double);
beginend;procedure Tbpnn.Bpnn_adjust_weights(delta: double;ndelta: integer;ly: double; nly: integer;w: array of double;oldw: array of double;momentum: double);
beginend;procedure Tbpnn.Bpnn_train(eta, momentum: double;eo,eh: array of double);
beginend;procedure Tbpnn.Bpnn_feedforward();
begin
end;procedure Tbpnn.Bpnn_layerforward(l1: double;l2: double;Conn: array of integer;n1: integer;n2: integer);
begin
end;procedure Tbpnn.Bpnn_create(n_in,n_hidden,n_out: integer);
beginend;procedure Tbpnn.Bpnn_getelement(in_units: double;ta_units: double);
beginend;procedure Tbpnn.Bpnn_SetInputElement(in_units: double);
beginend;procedure Tbpnn.Bpnn_GetOutPutElement(out_units: double);
beginend;
end.动态数组定义了之后,例如:Input_units : array of double;实际上这个Input_units指向的是动态数组的地址。那么在类的实例Bp中,我调用Input_units,对应的函数alloc_1d_dbl()我如何让他返回值也是数组的地址。我上面为什么把Input_units定义为double,是因为类型不匹配我没有办法(为了让程序通过而做的,实际上是错的)。这里主要的就是让函数alloc_1d_dbl()能跟Input_units类型匹配。
大哥们可以教教我吗?其他的想我应该可以举一反三的。谢谢了。
解决方案 »
- 怎样激活已知句柄窗体! 在线等 谢谢!
- 能不能用sendmessage来去是勾选框不勾上啊
- 三层中打开表速度问题?
- 流的操作问题:为什么这段代码达不到目的? ----在线等待一个解答!
- 菜鸟问题,请各位大侠相救,我作了一个简单的DCOM组件,可用CreateRemoteComObject建立时,总是报告"没有注册类别",如何解决?
- 如何解决image画图的时候出现的闪烁问题?
- 我用InstallShield 做个安装盘,但是我添加一些代码后,报错,编译不错,运行出错
- 有关动态改变Tedit的大小。
- 谁知道三维立体算法!关于用红绿眼镜看的!
- 如何实现任意文件的复制?
- 请问关于MS Jet4.0 OLE DB 与 MS OLE DB Provider for ODBC Drivers 二者的区别?
- 紧急求助:关于网址过滤
var
var
arr:array of integer;
begin
setLength(arr,n);
asm
mov eax,esi //返回数组首地址
end
end;
mov eax,esi应该是mov eax,ebp才对
doubleArray=array of double;//一定要这样定义后,编译器才会把它作一个类型来处理.
double2D_Array=array of doubleArray;function alloc_1d_dbl(n: integer): doubleArray;
begin
SetLength(Result,n);
end;function alloc_2d_dbl(m,n:integer):double2D_Array;
var
i:Integer;
begin
SetLength(Result,m);
for i:= to n-1 do
begin
SetLength(Result[i],n);
end;end;