Example #1
0
    def test_constructor(self):

        pattern = Pattern().random(shape=(20, 101))

        params = {
            'mag': MagT64,
            'hidden': 3,
            'max_iter': 1000,
            'seed': 135,
            'damping': .05,
            'accuracy': ('accurate', 'exact'),
            'randfact': .1,
            'epsil': .5,
            'protocol': 'pseudo_reinforcement',
            'size': 101,
            'nth': 2,
            'verbose': False
        }

        rfbp = rFBP(**params)
        assert params == rfbp.get_params()
        print(rfbp)

        # test wrong number of accuracy
        params['accuracy'] = ('none')
        with pytest.raises(TypeError):
            rfbp = rFBP(**params)
            assert params == rfbp.get_params()

        # test wrong accuracies
        params['accuracy'] = ('accurate', 'dummy')
        with pytest.raises(ValueError):
            rfbp = rFBP(**params)
            assert params == rfbp.get_params()

        # test wrong protocol
        params['accuracy'] = ('accurate', 'exact')
        params['protocol'] = 'none'
        with pytest.raises(ValueError):
            rfbp = rFBP(**params)
            assert params == rfbp.get_params()

        # test wrong magnetization
        params['protocol'] = 'pseudo_reinforcement'

        class Magnetization:
            pass

        params['mag'] = Magnetization
        with pytest.raises(TypeError):
            rfbp = rFBP(**params)
            assert params == rfbp.get_params()
Example #2
0
    def test_predict(self):

        params = {
            'mag': MagP64,
            'hidden': 3,
            'max_iter': 1,
            'seed': 135,
            'damping': .05,
            'accuracy': ('accurate', 'exact'),
            'randfact': .1,
            'epsil': .5,
            'protocol': 'pseudo_reinforcement',
            'size': 101,
            'nth': 2,
            'verbose': True
        }

        rfbp = rFBP(**params)

        Xtest = np.ones(shape=(10, params['size']), dtype=int)

        # check is fitted
        with pytest.raises(NotFittedError):
            pred_label = rfbp.predict(Xtest)
            assert pred_label is not None

        # train
        pattern = Pattern().random(shape=(20, 101))
        rfbp.fit(pattern)

        pred_label = rfbp.predict(pattern)

        predicted = rfbp.predict(pattern.data)

        assert np.allclose(pred_label, predicted)
Example #3
0
    def test_load_dump(self):

        params = {
            'mag': MagP64,
            'hidden': 3,
            'max_iter': 1,
            'seed': 135,
            'damping': .05,
            'accuracy': ('accurate', 'exact'),
            'randfact': .1,
            'epsil': .5,
            'protocol': 'pseudo_reinforcement',
            'size': 101,
            'nth': 2,
            'verbose': True
        }

        rfbp = rFBP(**params)

        with pytest.raises(NotFittedError):
            rfbp.save_weights('dummy.csv', delimiter=',', binary=False)

        pattern = Pattern().random(shape=(20, 101))
        rfbp.fit(pattern)

        W = rfbp.weights_

        rfbp.save_weights('dummy.csv', delimiter=',', binary=False)
        rfbp.load_weights('dummy.csv', delimiter=',', binary=False)

        os.remove('dummy.csv')

        assert np.allclose(W, rfbp.weights_)
        assert rfbp.hidden == params['hidden']

        W = rfbp.weights_
        rfbp.save_weights('dummy.bin', binary=True)
        rfbp.load_weights('dummy.bin', binary=True)

        os.remove('dummy.bin')

        assert np.allclose(W, rfbp.weights_)
        assert rfbp.hidden == params['hidden']
Example #4
0
def train ():
  args = parse_args()

  pattern = Pattern().load(filename=args.patterns, binary=args.bin, delimiter=args.delimiter)

  args.mag = MagP64 if args.mag == 0 else MagT64

  rfbp = rFBP(mag=args.mag,
              hidden=args.hidden,
              max_iter=args.iteration,
              seed=args.seed,
              damping=args.damping,
              accuracy=args.accuracy,
              randfact=args.randfact,
              epsil=args.epsilon,
              protocol=args.protocol,
              size=args.steps,
              nth=args.nth)

  rfbp.fit(pattern)
  rfbp.save_weights(args.outweights, args.delweights, args.binweights)
Example #5
0
def test():

    args = parse_args()

    pattern = Pattern().load(filename=args.patterns,
                             binary=args.bin,
                             delimiter=args.delimiter)

    rfbp = rFBP()
    rfbp.load_weights(args.weights)

    start_time = time.time()
    predicted_labels = rfbp.predict(pattern)
    elapsed_time = time.time() - start_time

    print('{0}: Predicted in {1:.2f} seconds'.format(args.patterns,
                                                     elapsed_time))

    if USE_SCORER:

        scorer = Scorer()
        scorer.evaluate(pattern.labels, predicted_labels)
        print(scorer)
Example #6
0
    def test_fit(self):

        K = 1
        M = 3
        N = 101
        data1 = np.ones(shape=(M, N), dtype=int)
        labels1 = np.ones(shape=(M, ), dtype=int)
        data2 = -data1
        labels2 = -labels1

        # + / +
        pattern1 = Pattern(X=data1, y=labels1)
        # + / -
        pattern2 = Pattern(X=data1, y=labels2)
        # - / +
        pattern3 = Pattern(X=data2, y=labels1)
        # - / -
        pattern4 = Pattern(X=data2, y=labels2)

        for seed in np.random.randint(1, 99, 10):
            params = {
                'mag': MagP64,
                'hidden': K,
                'max_iter': 2,
                'seed': seed,
                'damping': .05,
                'accuracy': ('exact', 'exact'),
                'randfact': .1,
                'epsil': .9,
                'protocol': 'pseudo_reinforcement',
                'size': 2,
                'nth': 2,
                'verbose': True
            }

            rfbp = rFBP(**params)
            # train

            rfbp.fit(pattern1)
            assert np.sum(rfbp.weights_) > 0

            rfbp.fit(pattern2)
            assert np.sum(rfbp.weights_) < 0

            rfbp.fit(pattern3)
            assert np.sum(rfbp.weights_) < 0

            rfbp.fit(pattern4)
            assert np.sum(rfbp.weights_) > 0

            params['mag'] = MagT64

            rfbp = rFBP(**params)
            # train

            rfbp.fit(pattern1)
            assert np.sum(rfbp.weights_) > 0

            rfbp.fit(pattern2)
            assert np.sum(rfbp.weights_) < 0

            rfbp.fit(pattern3)
            assert np.sum(rfbp.weights_) < 0

            rfbp.fit(pattern4)
            assert np.sum(rfbp.weights_) > 0

            params['mag'] = MagP64
            params['accuracy'] = ('accurate', 'accurate')

            rfbp = rFBP(**params)
            # train

            rfbp.fit(pattern1)
            assert np.sum(rfbp.weights_) > 0

            rfbp.fit(pattern2)
            assert np.sum(rfbp.weights_) < 0

            rfbp.fit(pattern3)
            assert np.sum(rfbp.weights_) < 0

            rfbp.fit(pattern4)
            assert np.sum(rfbp.weights_) > 0

            params['mag'] = MagT64

            rfbp = rFBP(**params)
            # train

            rfbp.fit(pattern1)
            assert np.sum(rfbp.weights_) > 0

            rfbp.fit(pattern2)
            assert np.sum(rfbp.weights_) < 0

            rfbp.fit(pattern3)
            assert np.sum(rfbp.weights_) < 0

            rfbp.fit(pattern4)
            assert np.sum(rfbp.weights_) > 0

            params['mag'] = MagP64
            params['accuracy'] = ('none', 'none')

            rfbp = rFBP(**params)
            # train

            rfbp.fit(pattern1)
            assert np.sum(rfbp.weights_) > 0

            rfbp.fit(pattern2)
            assert np.sum(rfbp.weights_) < 0

            rfbp.fit(pattern3)
            assert np.sum(rfbp.weights_) < 0

            rfbp.fit(pattern4)
            assert np.sum(rfbp.weights_) > 0

            params['mag'] = MagT64

            rfbp = rFBP(**params)
            # train

            rfbp.fit(pattern1)
            assert np.sum(rfbp.weights_) > 0

            rfbp.fit(pattern2)
            assert np.sum(rfbp.weights_) < 0

            rfbp.fit(pattern3)
            assert np.sum(rfbp.weights_) < 0

            rfbp.fit(pattern4)
            assert np.sum(rfbp.weights_) > 0
Example #7
0
    USE_SCORER = False

__author__ = ["Nico Curti", "Daniele Dall'Olio"]
__email__ = ['*****@*****.**', '*****@*****.**']

if __name__ == '__main__':

    pattern = np.random.choice([-1, 1], p=[.5, .5], size=(20, 101))
    label = np.random.choice([-1, 1], p=[.5, .5], size=(20, ))

    rfbp = rFBP(mag=MagT64,
                hidden=3,
                max_iter=1000,
                seed=135,
                damping=0.5,
                accuracy=['accurate', 'exact'],
                randfact=0.1,
                epsil=0.5,
                protocol='pseudo_reinforcement',
                size=101,
                nth=2)

    start_time = time.time()
    rfbp.fit(pattern, label)

    elapsed_time = time.time() - start_time

    pattern = Pattern(X=pattern, y=label)
    print('{0}: Training completed in {1:.2f} seconds'.format(
        pattern, elapsed_time))