<commit_before>import pytest
import os
import numpy as np

import sys
myPath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(myPath, '..'))

import DBNlogic.nets as nets
from DBNlogic.nets import DBN, RBM
from DBNlogic.sets import DataSet
from DBNlogic.util import squared_error



def test_append():
    """A RBM can be appended to a DBN."""
    net = DBN();
    net.append(RBM(3, 4))
    net.append(RBM(4, 7))
    assert len(net) == 2

def test_init():
    """A DBN can be constructed from a list of RBMs."""
    net = DBN([RBM(4, 7), RBM(7, 3)], 'Test')
    assert len(net) == 2

def test_generationLength():
    """A DBN can generate a sample whose length
    is equal to the number of visible units of
    its first RBM."""
    net = DBN([RBM(4, 7), RBM(7, 3)])
    sample = net.evaluate([0, 1, 1, 0.4])
    assert len(sample) == 4

def test_save():
    """A DBN can be serialised and saved to a Pickle file."""
    net = DBN(name = 'Test')
    net.save()
    netFile = nets.full('Test.pkl')
    assert os.path.isfile(netFile)
    os.remove(netFile)

def test_load():
    """A DBN can be loaded from a Pickle file."""
    net = DBN([RBM(4, 5), RBM(5, 6)], 'Test')
    net.save()
    net = DBN.load('Test')
    assert len(net) == 2
    os.remove(nets.full('Test.pkl'))

def test_saveAndLoad():
    """A DBN serialised to a file is equivalent to
    a DBN loaded from the same file."""
    net_1 = DBN([RBM(4, 5), RBM(5, 6)], 'Test')
    net_1.save()
    net_2 = DBN.load('Test')
    assert [np.equal(net_1[i].W, net_2[i].W) for i in range(2)]
    os.remove(nets.full('Test.pkl'))

def test_learn():
    """A DBN can learn to generate samples from a dataset."""
    net = DBN([RBM(8, 15), RBM(15, 10)], 'Test')
    trainset = DataSet.fromWhatever('left_8')
    for train_info in net.learn(trainset):
        pass

def test_error():
    """The reconstruction error of a DBN is less than 1."""
    net = DBN([RBM(8, 15), RBM(15, 10)], 'Test')
    trainset = DataSet.fromWhatever('left_8')
    for train_info in net.learn(trainset):
        pass
    mean_err = squared_error(trainset[0], net.evaluate(trainset[0]))
    assert mean_err <= 1<commit_msg>Update training tests<commit_after>import pytest
import os
import numpy as np

import sys
myPath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(myPath, '..'))

import DBNlogic.nets as nets
from DBNlogic.nets import DBN, RBM
from DBNlogic.sets import DataSet
from DBNlogic.util import squared_error
from DBNlogic.train import CDTrainer



def test_append():
    """A RBM can be appended to a DBN."""
    net = DBN();
    net.append(RBM(3, 4))
    net.append(RBM(4, 7))
    assert len(net) == 2

def test_init():
    """A DBN can be constructed from a list of RBMs."""
    net = DBN([RBM(4, 7), RBM(7, 3)], 'Test')
    assert len(net) == 2

def test_generationLength():
    """A DBN can generate a sample whose length
    is equal to the number of visible units of
    its first RBM."""
    net = DBN([RBM(4, 7), RBM(7, 3)])
    sample = net.evaluate([0, 1, 1, 0.4])
    assert len(sample) == 4

def test_save():
    """A DBN can be serialised and saved to a Pickle file."""
    net = DBN(name = 'Test')
    net.save()
    netFile = nets.full('Test.pkl')
    assert os.path.isfile(netFile)
    os.remove(netFile)

def test_load():
    """A DBN can be loaded from a Pickle file."""
    net = DBN([RBM(4, 5), RBM(5, 6)], 'Test')
    net.save()
    net = DBN.load('Test')
    assert len(net) == 2
    os.remove(nets.full('Test.pkl'))

def test_saveAndLoad():
    """A DBN serialised to a file is equivalent to
    a DBN loaded from the same file."""
    net_1 = DBN([RBM(4, 5), RBM(5, 6)], 'Test')
    net_1.save()
    net_2 = DBN.load('Test')
    assert [np.equal(net_1[i].W, net_2[i].W) for i in range(2)]
    os.remove(nets.full('Test.pkl'))

def test_DBN_learn():
    """A DBN can learn to generate samples from a dataset."""
    net = DBN([RBM(8, 15), RBM(15, 10)], 'Test')
    trainset = DataSet.fromWhatever('left_8')
    for train_info in net.learn(trainset):
        pass

def test_DBN_error():
    """The reconstruction error of a DBN is less than 1."""
    net = DBN([RBM(8, 15), RBM(15, 10)], 'Test')
    trainset = DataSet.fromWhatever('left_8')
    for train_info in net.learn(trainset):
        pass
    mean_err = squared_error(trainset[0], net.evaluate(trainset[0]))
    assert mean_err <= 1

def test_RBM_learn():
    """A RBM can learn to generate samples from a dataset."""
    net = RBM(8, 15)
    trainset = DataSet.fromWhatever('left_8')
    trainer = CDTrainer(net)
    for err in trainer.run(trainset):
        pass

def test_RBM_error():
    """The reconstruction error of a RBM is less than 1."""
    net = RBM(8, 15)
    trainset = DataSet.fromWhatever('left_8')
    trainer = CDTrainer(net)
    for err in trainer.run(trainset):
        pass
    mean_err = squared_error(trainset[0], net.evaluate(trainset[0]))
    assert mean_err <= 1
{'fraction_non_alphanumeric': 0.1529482793318575, 'fraction_numerical': 0.024954719259408332, 'mean_word_length': 3.236999147485081, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4107382', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1801, 'n_words': 545}