?? tests.py
字號:
# -*- coding: iso-8859-1 -*-## This file is a part of the Bayes Blocks library## Copyright (C) 2001-2006 Markus Harva, Antti Honkela, Alexander# Ilin, Tapani Raiko, Harri Valpola and Tomas 謘tman.## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2, or (at your option)# any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License (included in file License.txt in the# program package) for more details.## $Id: Tests.py 5 2006-10-26 09:44:54Z ah $#import unittestimport Numeric, RandomArrayimport MLabimport random, timeimport sysimport PyNet, Helpers, PickleHelpersfrom Label import Labelfrom Helpers import GetMean, GetMeanV, GetDiscrete, GetDiscreteV, GetVar, GetVarVdef path(a, b): node = a rootlabel = b.GetLabel() seq = [] while node.GetLabel() != rootlabel: seq.append(node.GetLabel()) c = node.GetChild(0) if c is None: return None node = c return seqclass PyNodeFactoryTestCase(unittest.TestCase): def setUp(self): self.net = PyNet.PyNet(1) self.nf = PyNet.PyNodeFactory(self.net) def tearDown(self): pass def test1(self): clst = [self.nf.GetConstant(Label("c", i), i) for i in range(10)] x = self.nf.MakeNodes("Gaussian", "x", 10, clst, clst[0]) for i in range(10): self.assertEqual(x[i].GetType(), "Gaussian") self.assertEqual(x[i].GetLabel(), Label("x", i)) self.assertEqual(x[i].GetParent(0).GetLabel(), clst[i].GetLabel()) self.assertEqual(x[i].GetParent(1).GetLabel(), clst[0].GetLabel()) def test2(self): clst = [self.nf.GetConstant(Label("c", i), i) for i in range(10)] x = self.nf.MakeNodes("Gaussian", ("x", 1, 2), 10, clst, clst) for i in range(10): self.assertEqual(x[i].GetType(), "Gaussian") self.assertEqual(x[i].GetLabel(), Label("x", 1, 2, i)) self.assertEqual(x[i].GetParent(0).GetLabel(), clst[i].GetLabel()) self.assertEqual(x[i].GetParent(1).GetLabel(), clst[i].GetLabel()) def test3(self): c0 = self.nf.GetConst0() summands = self.nf.MakeNodes("Gaussian", "s", 16, c0, c0) sum = self.nf.BuildSum2Tree(summands, "sumtree") for term in summands: self.assertEqual(len(path(term, sum)), 4) def test4(self): c0 = self.nf.GetConst0() s = self.nf.GetGaussian("s", c0, c0) x = self.nf.GetGaussian("x", s, c0) x.Clamp(1.0) self.nf.EvidenceNode(s, mean=0.5) class VariableGroupTestCase(unittest.TestCase): def setUp(self): self.net = PyNet.PyNet(1) self.nf = PyNet.PyNodeFactory(self.net) def tearDown(self): pass def test1(self): c0 = self.nf.GetConstant("c0", 0.0) a1 = self.nf.GetGaussian("ab1", c0, c0) a2 = self.nf.GetGaussian("ab2", c0, c0) b1 = self.nf.GetGaussian("ba1", c0, c0) b2 = self.nf.GetGaussian("ba2", c0, c0) self.net.DefineVariableGroup("a"); self.net.DefineVariableGroup("b"); groupa = [self.net.GetGroupVariable("a", i).GetLabel() for i in range(self.net.NumGroupVariables("a"))] groupa.sort() self.assertEqual(groupa, ["ab1", "ab2"]) groupb = [self.net.GetGroupVariable("b", i).GetLabel() for i in range(self.net.NumGroupVariables("b"))] groupb.sort() self.assertEqual(groupb, ["ba1", "ba2"]) def test2(self): c0 = self.nf.GetConstant("c0", 0.0) a1 = self.nf.GetGaussian("ab1", c0, c0) a2 = self.nf.GetGaussian("ab2", c0, c0) b1 = self.nf.GetGaussian("ba1", c0, c0) b2 = self.nf.GetGaussian("ba2", c0, c0) self.net.DefineVariableGroup("a"); self.net.DefineVariableGroup("b"); b1.Die() self.net.CleanUp() a3 = self.nf.GetGaussian("ab3", c0, c0) groupa = [self.net.GetGroupVariable("a", i).GetLabel() for i in range(self.net.NumGroupVariables("a"))] groupa.sort() self.assertEqual(groupa, ["ab1", "ab2", "ab3"]) groupb = [self.net.GetGroupVariable("b", i).GetLabel() for i in range(self.net.NumGroupVariables("b"))] groupb.sort() self.assertEqual(groupb, ["ba2"]) def test3(self): c0 = self.nf.GetConstant("c0", 0.0) foo1 = self.nf.GetGaussian("foo1", c0, c0) foo2 = self.nf.GetGaussian("foo2", c0, c0) foobar1 = self.nf.GetGaussian("foobar1", c0, c0) foobar2 = self.nf.GetGaussian("foobar2", c0, c0) self.net.DefineVariableGroup("foo"); self.net.DefineVariableGroup("foobar"); groupa = [self.net.GetGroupVariable("foo", i).GetLabel() for i in range(self.net.NumGroupVariables("foo"))] groupa.sort() self.assertEqual(groupa, ["foo1", "foo2", "foobar1", "foobar2"]) groupb = [self.net.GetGroupVariable("foobar", i).GetLabel() for i in range(self.net.NumGroupVariables("foobar"))] groupb.sort() self.assertEqual(groupb, ["foobar1", "foobar2"]) def setup4(self): data1 = RandomArray.normal(-1, Numeric.exp(-0.5*2), 100) data2 = RandomArray.normal(1, Numeric.exp(-0.5*2), 100) c0 = self.nf.GetConstant("c0", 0.0) m1 = self.nf.GetGaussian("am", c0, c0) m1.Clamp(0.0) m1.Unclamp() v1 = self.nf.GetGaussian("av", c0, c0) v1.Clamp(0.0) v1.Unclamp() m2 = self.nf.GetGaussian("bm", c0, c0) m2.Clamp(0.0) m2.Unclamp() v2 = self.nf.GetGaussian("bv", c0, c0) v2.Clamp(0.0) v2.Unclamp() x1 = [] for i in range(data1.shape[0]): x1.append(self.nf.GetGaussian(Label("x1", i), m1, v1)) x1[i].Clamp(data1[i]) x2 = [] for i in range(data1.shape[0]): x2.append(self.nf.GetGaussian(Label("x2", i), m2, v2)) x2[i].Clamp(data2[i]) self.m1 = m1 self.m2 = m2 self.v1 = v1 self.v2 = v2 def test4a(self): self.setup4() self.net.DefineVariableGroup("a") self.net.DefineVariableGroup("b") for i in range(5): self.net.UpdateAll() #print self.net.Cost() self.assert_(abs(GetMean(self.m1) + 1.0) < 0.5) self.assert_(abs(GetMean(self.m2) - 1.0) < 0.5) self.assert_(abs(GetMean(self.v1) - 2.0) < 0.5) self.assert_(abs(GetMean(self.v2) - 2.0) < 0.5) def test4b(self): self.setup4() self.net.DefineVariableGroup("a") for i in range(5): self.net.UpdateGroup("a") self.assert_(abs(GetMean(self.m1) + 1.0) < 0.5) self.assert_(abs(GetMean(self.v1) - 2.0) < 0.5)class PartialUpdateTestCase(unittest.TestCase): def setUp(self): pass def test1(self): tdim = 100 net = PyNet.PyNet(tdim) nf = PyNet.PyNodeFactory(net) indices = RandomArray.permutation(tdim)[:10].tolist() net.DefineTimeIndexGroup("mygroup", Helpers.Array2IntV(indices)) c0 = nf.GetConstant("c0", 0.0) c_5 = nf.GetConstant("c_5", -5.0) c5 = nf.GetConstant("c5", 5.0) s = nf.GetGaussianV("s", c0, c0) x = nf.GetGaussianV("x", s, c5) data = RandomArray.standard_normal(tdim) var = Numeric.ones(tdim) * 1e-8 x.Clamp(Helpers.Array2DV(data), Helpers.Array2DV(var)) net.UpdateAll() ms = GetMeanV(s) for t in range(tdim): self.assert_((data[t] - ms[t])**2 < 1e-2, "%f vs. %f" % (data[t], ms[t])) newdata = RandomArray.standard_normal(tdim) x.Clamp(Helpers.Array2DV(newdata), Helpers.Array2DV(var)) net.EnableTimeIndexGroup("mygroup") net.UpdateAll() ms = GetMeanV(s) for t in range(tdim): if t in indices: xt = newdata[t] else: xt = data[t] self.assert_((xt - ms[t])**2 < 1e-2, "%f vs. %f" % (xt, ms[t])) net.DisableTimeIndexGroup("mygroup") net.UpdateAll() ms = GetMeanV(s) for t in range(tdim): self.assert_((newdata[t] - ms[t])**2 < 1e-2, "%f vs. %f" % (newdata[t], ms[t]))class MoGTestCase(unittest.TestCase): def setUp(self): # N(-1, 0.5^2) data1 = Numeric.array( [-1.48372132, -0.74105301, -1.46452126, -0.74465525, -0.74137861, -0.81603923, -1.22201809, -1.40728462, -0.25976342, -1.07168696, -0.80576369, -1.28271085, -0.97642539, -1.36678675, -1.10264056, -0.53583777, -1.32091546, -0.74083391, -1.34777945, -0.87874509], Numeric.Float) # N(1, 0.1^2) data2 = Numeric.array( [ 0.8612323 , 1.05193536, 1.10445087, 0.89876609, 0.96364794, 1.14201061, 0.96772116, 0.8137229 , 0.9958501 , 0.99648816], Numeric.Float) self.data = Numeric.concatenate((data1, data2)) def buildnet1(self): net = PyNet.PyNet(self.data.shape[0]) nf = PyNet.PyNodeFactory(net) numComp = 2 prior = Helpers.Array2DV(Numeric.ones(numComp)) const = nf.GetConstantV("c_prior", prior) c = nf.GetDirichlet(Label("c"), const) d = nf.GetDiscreteDirichletV("d", c) s = nf.GetMoGV("s", d) c0 = nf.GetConstant("c0", 0.0) c_5 = nf.GetConstant("c_5", -5) m1 = nf.GetGaussian("m1", c0, c_5) v1 = nf.GetGaussian("v1", c0, c_5) m2 = nf.GetGaussian("m2", c0, c_5) v2 = nf.GetGaussian("v2", c0, c_5) s.AddComponent(m1, v1) s.AddComponent(m2, v2) vx = nf.GetConstant("vx", 9.21) x = nf.GetGaussianV("x", s, vx) x.Clamp(Helpers.Array2DV(self.data)) nf.EvidenceNode(m1, mean=-1.0) nf.EvidenceNode(m2, mean=1.0) return net def buildnet2(self): net = PyNet.PyNet(1) nf = PyNet.PyNodeFactory(net) numComp = 2 dim = self.data.shape[0] prior = Helpers.Array2DV(Numeric.ones(numComp)) const = nf.GetConstantV("c_prior", prior) c = nf.GetDirichlet(Label("c"), const) c0 = nf.GetConstant("c0", 0.0) c_5 = nf.GetConstant("c_5", -5) m1 = nf.GetGaussian("m1", c0, c_5) v1 = nf.GetGaussian("v1", c0, c_5)
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -