示例#1
0
    def print_hist_components(self):
        cdict = defaultdict(int)
        cdict_H = defaultdict(int)
        for c in self.components:
            cdict[tuple(c.alpha)] += 1
            cdict_H[tuple(c.alpha)] = c.entropy()

        # for c in sorted(cdict, key=cdict.get, reverse=True):
        wps = []
        for c in sorted(cdict):
            count = cdict[c]
            p = c[0]/sum(c)
            w = count/len(self.components)
            print(cdict[c], ':', ut.str_listfs(c), ':: H %.2f' % cdict_H[c])
            wps.append(w*p)
        # print('wp', sum(wps))
        return wps
示例#2
0
 def __str__(self):
     return 'alpha:' + ut.str_listfs(self.alpha)
示例#3
0
    def chat(self, ps=False, dateshift=0):
        """Chat between A and B.
        ps collects and outputs B's probabilities/language at each timestep.
        dateshift is for iterating multiple chats with same NNS/B, different A.
        """
        if VERBOSE > 0:
            print("ACC PARAM", self.s.accommodation)
            print('INIT AList: H %.4f  B: H %.4f' % (self.AListener.entropy(),
                                                     self.BLearner.entropy()),
                  'INIT A: p0 %.3f hbyB: (%s)' % (
                      self.AListener.pr_cat(0),
                      ut.prlints(self.AListener.heard_by_learner)))
            print("Learner Alphas A %s B %s" %
                  (ut.str_listfs(self.ALearner.alpha),
                   ut.str_listfs(self.BLearner.alpha)))
            self.AListener.print_hist_components()
            print('INIT B: H %.4f (%s)' % (self.BLearner.entropy(),
                                           self.BLearner))

        if ps:
            Bps = [self.BLearner.pr_cat(1)]

        # At each dialogue step, B then A speaks (+updates).
        for date in range(self.s.dialogue_length):
            if VERBOSE > 2:
                print('step', date, '+shift', date + dateshift)

            Bmsg = self.BLearner.production(self.s.message_length)
            self.ALearner.update(Bmsg)
            if self.AListener:
                self.AListener.update(Bmsg)

            if VERBOSE > 2:
                if self.AListener:
                    print('B msg %s  upA: p0 %.3f H %.4f Bheard(%s) Bsaid(%s)'
                          % (Bmsg,
                             self.AListener.pr_cat(0),
                             self.AListener.entropy(),
                             ut.prlints(self.AListener.heard_by_learner),
                             ut.prlints(self.AListener.heard_from_learner)))
                else:
                    print('B msg %s  upA: p0 %.3f H %.4f'
                          % (Bmsg,
                             self.ALearner.pr_cat(0),
                             self.ALearner.entropy()))

            if self.AListener:
                Amsg = self.AListener.production(self.s.message_length,
                                                 self.s.accommodation,
                                             ownlang=self.ALearner)
                self.AListener.update_heard_by_learner(Amsg)
            else:
                Amsg = self.ALearner.production(self.s.message_length)
            self.BLearner.update(Amsg)

            if VERBOSE > 2:
                print('A msg %s  upB: H %.4f (%s)' % (
                    Amsg, self.BLearner.entropy(), self.BLearner))

            self.update_Hs(date + dateshift)
            if ps:
                Bps.append(self.BLearner.pr_cat(1))
        if VERBOSE > 0:
            print('FINL A: H %.4f ALearner H %.4f Bheard(%s) Bsaid(%s) B: H %.4f'
                  % (self.AListener.entropy(),
                     self.ALearner.entropy(),
                     ut.prlints(self.AListener.heard_by_learner),
                     ut.prlints(self.AListener.heard_from_learner),
                     self.BLearner.entropy()))

        if ps:
            return (BH, Bps, AH)
示例#4
0
 def __init__(self, theta):
     assert len(theta) > 1, "Bad theta: %s" % ut.str_listfs(theta)
     self.theta = theta
     self.K = len(theta)