H* 自然数 x を2進展開で長さnの2進列に変換する *L binarySeq@x_, n_D := Module@8binaryExp, length<, binaryExp = IntegerDigits@x, 2D; length = Length@binaryExpD; If@length ã n, binaryExp, If@length < n, Join@Table@0, 8n - length<D, binaryExpDD DD H* 自然数 x から定まる長さn の2進列の H-1Lµエネルギー S h_i x_i +S w_8ij< x_ix_j を求める関数。 edgesは辺の集合、q1=param@@1DDはしきい値 h_i, q2=param@@2DDは重み w_8ij< が 8i,j<œ edges の順序で並んでいる。*L negEnergy@x_, n_, edges_, param_D := Module@8q1, q2, xseq, xprod<, q1 = param@@1DD; q2 = param@@2DD; xseq = binarySeq@x, nD; xprod = Function@Times üü xseq@@ÒDDD êü edges; [email protected] + q2.xprodD D H* 規格化定数 Z の計算 *L normConst@n_, edges_, param_D := 2n -1 ‚ Exp@negEnergy@x, n, edges, paramDD x=0 H* 期待値パラメータ, h=8h1,h2<, h1=8E@X_iD<, h2=8E@X_i X_jD< *L h1@n_, edges_, param_D := Module@8xseq, result<, result = Table@0, 8n<D; For@x = 0, x < 2n , x ++, xseq = binarySeq@x, nD; result = result + xseq * Exp@negEnergy@x, n, edges, paramDD D; result = result ê normConst@n, edges, paramD D h1@3, 881, 2<, 82, 3<<, 881, 2, 3<, 82, - 1<<D 80.940039, 0.94341, 0.884859< h@3, 881, 2<, 82, 3<<, 881, 2, 3<, 82, - 1<<D 880.940039, 0.94341, 0.884859<, 80.898668, 0.830953<< h2@n_, edges_, param_D := Module@8xseq, xprod, result<, result = Table@0, 8Length@edgesD<D; For@x = 0, x < 2n , x ++, xseq = binarySeq@x, nD; xprod = Function@Times üü xseq@@ÒDDD êü edges; result = result + xprod * Exp@negEnergy@x, n, edges, paramDD D; result = result ê normConst@n, edges, paramD D h@n_, edges_, param_D := Module@8xseq, xprod, result<, result = List@Table@0, 8n<D, Table@0, 8Length@edgesD<DD; For@x = 0, x < 2n , x ++, xseq = binarySeq@x, nD; xprod = Function@Times üü xseq@@ÒDDD êü edges; result = result + List@xseq, xprodD * Exp@negEnergy@x, n, edges, paramDD D; result = result ê normConst@n, edges, paramD D 2 handout140214-1.nb H* 完全グラフ生成関数 *L genCompGraph@n_D := Module@8result<, result = 8<; For@i = 1, i < n, i ++, For@j = i + 1, j § n, j ++, result = Append@result, 8i, j<D DD; resultD genCompGraph@3D 881, 2<, 81, 3<, 82, 3<< H* 勾配法でhからqを求めるためのパラペータ更新 *L updateParam@n_, edges_, param_, hTarget_, e_D := param + e * HhTarget - h@n, edges, paramDL h@3, 881, 2<, 82, 3<<, 881, 2, 3<, 8- 1, - 2<<D 880.635848, 0.412064, 0.861296<, 80.206032, 0.301243<< a=% 880.635848, 0.412064, 0.861296<, 80.206032, 0.301243<< updateParam@3, 881, 2<, 82, 3<<, 880, 0, 0<, 80, 0<<, a, 0.1D 880.0135848, - 0.00879362, 0.0361296<, 8- 0.00439681, 0.00512428<< theta = % 880.0135848, - 0.00879362, 0.0361296<, 8- 0.00439681, 0.00512428<< theta = updateParam@3, 881, 2<, 82, 3<<, theta, a, 0.1D 880.0268847, - 0.0173774, 0.0712922<, 8- 0.00880308, 0.00984003<< Do@theta = updateParam@3, 881, 2<, 82, 3<<, theta, a, 0.1D, 8100<D theta 880.598405, - 0.187704, 1.46911<, 8- 0.315115, - 0.0572541<< Do@theta = updateParam@3, 881, 2<, 82, 3<<, theta, a, 0.1D, 81000<D theta 880.935393, 1.28898, 2.4733<, 8- 0.8845, - 1.31962<< Do@theta = updateParam@3, 881, 2<, 82, 3<<, theta, a, 0.1D, 81000<D theta 880.979735, 1.71802, 2.77749<, 8- 0.964196, - 1.72518<< Do@theta = updateParam@3, 881, 2<, 82, 3<<, theta, a, 0.1D, 81000<D theta 880.992044, 1.87865, 2.90229<, 8- 0.985987, - 1.8811<< Do@theta = updateParam@3, 881, 2<, 82, 3<<, theta, a, 0.1D, 81000<D theta 880.996596, 1.94611, 2.95627<, 8- 0.994012, - 1.9471<< Do@theta = updateParam@3, 881, 2<, 82, 3<<, theta, a, 0.1D, 81000<D theta 880.998493, 1.97575, 2.98026<, 8- 0.99735, - 1.97618<< H* 真値 881,2,3<,8-1,-2<< にかなり近づいている。 *L
© Copyright 2024 ExpyDoc