File size: 1,721 Bytes
a64600b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import unittest
import torch
from suite.control import gate,masked_decode

class ControlTests(unittest.TestCase):
    def test_zero_support_is_untouched(self):
        s=torch.tensor([0.,.2,0.,1.])
        for soft in [True,False]:
            for p in [0.,.01,.1,.5,1.]:self.assertTrue(torch.equal(gate(s,p,.2,soft)[s==0],torch.zeros(2)))
    def test_soft_zero_endpoint_is_continuous(self):
        self.assertLess(float(gate(torch.tensor([1e-8]),0.,.1)),1e-6)
    def run_order(self,arm,alpha=0.,ref=None):
        if ref is None:ref=torch.arange(12).reshape(1,12)%3
        mask=torch.tensor([0.]*4+[1.]*4+[0.]*4);trace=[]
        def predict(ids):
            logits=torch.zeros(1,12,3);logits[:,:,0]=.3
            logits[:,:,1]+=((ids==1).float().sum()/12)
            return logits
        out,commit=masked_decode(predict,ref,mask,3,6,dict(seed=2,alpha=alpha,arm=arm),trace)
        return out,commit,trace
    def test_permutations_preserve_compute_counts(self):
        traces=[self.run_order(a)[2] for a in ['confidence','target_first','target_last','random']]
        self.assertTrue(all([t['newly_committed'] for t in ts]==[t['newly_committed'] for t in traces[0]] for ts in traces))
        for ts in traces:self.assertEqual(sum(t['newly_committed'] for t in ts),12)
    def test_alpha_zero_independent_of_reference(self):
        a=self.run_order('confidence',0.)[0];b=self.run_order('confidence',0.,torch.ones(1,12,dtype=torch.long)*2)[0]
        self.assertTrue(torch.equal(a,b))
    def test_commit_is_complete(self):
        _,c,tr=self.run_order('target_last',.5);self.assertTrue(bool((c>=0).all()));self.assertTrue(all(t['gate_outside_max']==0 for t in tr))

if __name__=='__main__':unittest.main()