Esempio n. 1
0
class NetworkUtil(object):
    '''
    Class to train and test a neural network 
    '''

    def __init__(self, nInputs=None, nHiddenLayers=None, bias=True, new=True, fileName=""):
        self.params = {}
        self.config = ConfigProvider().getNNTrainConfig()
        if new:
            self.params["nInputs"] = nInputs
            self.params["nHiddenLayers"] = nHiddenLayers
            self.params["bias"] = bias
            self.nn = NeuralNetwork().createNew(nInputs, nHiddenLayers, N_OUTPUT, bias)
        else:
            self.nn = NeuralNetwork().load(fileName)

    def train(self, trainData, convergence=False):
        start = time.time()
        print "start Training at " + str(datetime.fromtimestamp(start))
        if convergence:
            self.nn.trainConvergence(trainData, **self.config)
        else:
            self.nn.train(trainData, **self.config)
        print "Training Done in %.2fs" % (time.time() - start)
    
    def test(self, testData=None):
        self.nn.test(testData)
        print "Testing Done"

    def activate(self, testData):
        total = (len(testData) / 2) / 100.0
        matrix = np.array([[0, 0, 0], [0, 0, 0]])
        resArr = []
        for inpt, target in testData:
            res = self.nn.activate(inpt)
            clazz = self.nn._clazz(res)
            resArr.append([res, clazz, target])
            matrix[target[0]][clazz] += 1
        matrix[0, 2] = matrix[0, 0] / total
        matrix[1, 2] = matrix[1, 1] / total 
        return matrix, np.array(resArr)

    def save(self, name):
        self.nn.save(name)

    def __str__(self):
        self.params.update(self.config)
        return str(self.nn) + "\n" + str(self.params)
class TestNeuralNetwork(unittest.TestCase):


    def setUp(self):
        self.nn = NeuralNetwork()
        self.config = ConfigProvider().getNeuralNetworkConfig()
        self.nn.createNew(2, 4, 1, bias=True)

    def _createData(self, nInput, nTarget, values):
        ds = SupervisedDataSet(nInput, nTarget)
        for inp, target in values:
            ds.addSample(inp, target)
        
        return ds

    def createORData(self):
        values = [((0, 0), (0,)), ((0, 1), (1,)), ((1, 0), (1,)), ((1, 1), (1,))]
        return self._createData(2, 1, values)

    def createXORData(self):
        values = [((0, 0), (0,)), ((0, 1), (1,)), ((1, 0), (1,)), ((1, 1), (0,))]
        return self._createData(2, 1, values)

    def createANDData(self):
        values = [((0, 0), (0,)), ((0, 1), (0,)), ((1, 0), (0,)), ((1, 1), (1,))]
        return self._createData(2, 1, values)

    def removeFile(self):
        try:
            os.remove(self.nn.path + name + ".nn")
        except OSError as e:
            print e.message

    def test_xor(self):
        ds = self.createXORData()

        self.nn.train(ds, self.config["maxEpochs"], self.config["learningrate"], self.config["momentum"])

        #TODO may fail with delta 0.2
        for inpt, target in ds:
            self.assertAlmostEqual(self.nn.activate(inpt), target, delta=0.2)

    def test_and(self):
        ds = self.createANDData()
        self.nn.train(ds, self.config["maxEpochs"], self.config["learningrate"], self.config["momentum"])

        #TODO may fail with delta 0.2
        for inpt, target in ds:
            self.assertAlmostEqual(self.nn.activate(inpt), target, delta=0.2)

    def test_saveAndLoad(self):
        ds = self.createORData()
        self.nn.train(ds)
        self.nn.save(name)
        
        nn2 = NeuralNetwork()
        nn2.load(name)
        
        self.assertNotEqual(self.nn, nn2)
        
        self.assertTrue(sameEntries(self.nn.net.params, nn2.net.params))
        for inpt, _ in ds:
            self.assertEqual(self.nn.activate(inpt), nn2.activate(inpt))

        self.removeFile()

    def test_test(self):
        ds = self.createORData()
        self.nn.train(ds)
        self.nn.test(ds, False)

    def test_testBeforeTrain(self):
        with self.assertRaises(ValueError):
            self.nn.test(None, False)