Function 基于OCaml的Hopfield网络
我目前正在尝试使用Ocaml构建hopfield网络。我有困难,使一些功能,使这一切工作,不知道如何完成它们 第一个是能量函数 以下是我对该函数的了解:Function 基于OCaml的Hopfield网络,function,logic,ocaml,Function,Logic,Ocaml,我目前正在尝试使用Ocaml构建hopfield网络。我有困难,使一些功能,使这一切工作,不知道如何完成它们 第一个是能量函数 以下是我对该函数的了解: let rec energy(state, weightMatrix) = if weightMatrix == [] then 0.0 else (hd(state) *. hd(weightMatrix))/2.0 +. energy(tl(state),tl(weightMatrix));; Error: This expre
let rec energy(state, weightMatrix) =
if weightMatrix == [] then 0.0
else
(hd(state) *. hd(weightMatrix))/2.0 +. energy(tl(state),tl(weightMatrix));;
Error: This expression has type float but an expression was expected of type
int
状态是它所处的状态,权重矩阵是权重矩阵。它遵循以下等式:
这方面的一个例子是:
state = = [1.0; -1.0; 1.0; -1.0] and
weight = [[0.; -1.; 1.; -1.]; [-1.; 0.; -1.; 1.];[1.; -1.; 0.; -1.];[-1.; 1.; -1.; 0.]]
# energy(state,weight);;
- : float = -6.
运行函数时,函数的最后一行出现以下错误:
let rec energy(state, weightMatrix) =
if weightMatrix == [] then 0.0
else
(hd(state) *. hd(weightMatrix))/2.0 +. energy(tl(state),tl(weightMatrix));;
Error: This expression has type float but an expression was expected of type
int
编辑:修复上述错误后,我现在遇到了此错误的问题:
Error: This expression has type float list list
but an expression was expected of type float list
Type float list is not compatible with type float
我认为我的问题可以从以下几点看出:
I am getting: val energy : float list * float list -> float = <fun>
but want: val energy : float list * float list list -> float = <fun>
如果有人能帮我解决一些问题,那就太好了 第一个错误是由整数除法引起的。您可能需要
/。
。很容易忘记浮点运算符中的点
很抱歉,我对您的状态转换函数了解不够,无法给出任何建议。我的测试显示:
# let rec energy (state, weightMatrix) =
if weightMatrix == [] then 0.0
else
(hd(state) *. hd(weightMatrix))/. 2.0 +. energy(tl(state),tl(weightMatrix));;
val energy : float list * float list -> float = <fun>
# let st = [1.0; -1.0; 1.0; -1.0];;
val st : float list = [1.; -1.; 1.; -1.]
# let wg = [[0.; -1.; 1.; -1.]; [-1.; 0.; -1.; 1.];[1.; -1.; 0.; -1.];[-1.; 1.; -1.; 0.]];;
val wg : float list list =
[[0.; -1.; 1.; -1.]; [-1.; 0.; -1.; 1.]; [1.; -1.; 0.; -1.];
[-1.; 1.; -1.; 0.]]
# energy (st, wg);;
Error: This expression has type float list list
but an expression was expected of type float list
Type float list is not compatible with type float
#让rec能量(状态,权重矩阵)=
如果权重矩阵==[],则为0.0
其他的
(hd(州)*.hd(权重矩阵))/。2.0 +. 能量(tl(状态),tl(权重矩阵));;
val能量:浮动列表*浮动列表->浮动=
#设st=[1.0;-1.0;1.0;-1.0];;
val st:float list=[1..-1.-1.-1.]
#设wg=[-0.-1..-1.];[-1.-0.-1.];;
val wg:浮动列表=
[[0.; -1.; 1.; -1.]; [-1.; 0.; -1.; 1.]; [1.; -1.; 0.; -1.];
[-1.; 1.; -1.; 0.]]
#能源(st,wg);;;
错误:此表达式具有浮点列表类型
但应为float list类型的表达式
类型浮点列表与类型浮点不兼容
绝对正确。重量头是一个列表。你不能像那样把一个列表除以2.0
我不理解您要计算的公式,我只能说:您必须分解列表。@Jeffery Scofield当我在中的示例中输入数据时,我返回了此错误:错误:此表达式具有类型float list list,但应为类型float list的表达式类型float list与类型float list不兼容。有什么建议吗?@Jeffery Scofield如果你能看一下的话,我用我现在的问题编辑了我上面的问题。有几件事,1)通常你不会像在C中那样调用函数。
,
生成元组。可以用空格分隔参数。2) 通常OCaml的开发者使用模式匹配而不是直接使用hd/tl。3) 添加类型注释有助于调试。在函数参数上使用类似于(state:weightMatrix list)
的方法。您将看到,实际上您正在使用权重矩阵,就像它是一个浮动列表一样。4) 这些函数似乎更适合数组。@nlucaroni谢谢!我在哪里打电话像在C?那么我该如何使用weightMatrix作为浮点列表呢?函数let rec energy(state,weightMatrix)=
将是OCaml中的let rec energy state weightMatrix=
。@nlucaroni哦,是的,这很有意义!我没有意识到这一点。另外,你能给我一个例子,说明我是如何不让weightMatrix成为浮动列表的吗?(hd(state)*.hd(weightMatrix))
。由于hd(weightmatrix)
是一个浮点数(因为您将它与hd(state)
相乘),并且hd对列表进行操作,因此weightmatrix被推断为一个浮点数列表。嗯,这正是我得到的。你说的分解LSIT是什么意思?嗯,先小睡一会儿,盯着看太多也无济于事。然后比较st的开头和wg的开头。第一个有一个括号,第二个有两个括号。分解一个列表:可能取头中的头?但问题是怎么去一个,我不明白公式。与List.map、List.iter、List.fold\u left等交朋友。