예제 #1
0
def training(d):
    """
        Builds a network and trains it.
        """
    print("Creating Neural Network")
    n = NNregression(d)
    print("Setting Up Neural Network")
    n.setupNN()
    print("Training Neural Network")
    n.runTraining()

    print("Returning Trainier")
    return n.Trainer
예제 #2
0
class NNMatcher(LinearMatcher):
    def __init__(self):
        LinearMatcher.__init__(self)

    def train(self, pairings):
        self.dataset = SupervisedDataSet(self.cheese_feat_len, self.wine_feat_len)
        for cheese, wine in pairings:
            cheese_desc = self.cheeses[cheese]
            wine_desc = self.wines[wine]
            self.dataset.addSample(cheese_desc, wine_desc)
        self.nn = NNregression(self.dataset, hidden=self.cheese_feat_len + self.wine_feat_len, maxepochs=35)
        self.nn.setupNN()
        self.nn.runTraining()

    def predict_feat(self, cheese_desc):
        return self.nn.Trainer.module.activate(cheese_desc)
예제 #3
0
 def train(self, pairings):
     self.dataset = SupervisedDataSet(self.cheese_feat_len, self.wine_feat_len)
     for cheese, wine in pairings:
         cheese_desc = self.cheeses[cheese]
         wine_desc = self.wines[wine]
         self.dataset.addSample(cheese_desc, wine_desc)
     self.nn = NNregression(self.dataset, hidden=self.cheese_feat_len + self.wine_feat_len, maxepochs=35)
     self.nn.setupNN()
     self.nn.runTraining()
def train_network(train,target):
    """
        Trains via linear regression from the target and train data
        Arguments:
        train : an array of training data
        target: an array of associated target data
        Returns:
        The trained model
    """
    print("Setting up Neural Network data with %s train features and %s targets sets" % (len(train[0]), len(target[0])))
    data = SupervisedDataSet(len(train[0]),len(target[0]))
    data.setField('input', train)
    data.setField('target', target)
    n = NNregression(data)
    n.setupNN()
    print("Training Neural Network on %s training sets" % len(data))
    n.runTraining()
    return n.Trainer.module
 def run(self, ds_train, ds_test):
     """
     This function evaluates the ANN using a test set and has the error-rate as an output.
     Args:
     :param ds_train (TweetClassificationDatasetFactory): the training dataset the neural network is trained with.
     :param ds_test (TweetClassificationDatasetFactory): the test dataset evaluated.
     :returns error (float): the percent error of the test dataset, tested on the neural network.
     """
     self.network = NNregression(ds_train)
     self.network.setupNN(hidden=self.hidden, verbose=True)
     self.network.Trainer.trainUntilConvergence(
         dataset=ds_train,
         maxEpochs=self.max_epochs,
         continueEpochs=self.con_epochs)
     error = self.test(ds_test)
     return error
    def run_with_crossvalidation(self, ds, iterations=5):
        """
        This function estimates the performance of the neural network using crossvalidation using a specified dataset.
        Args:
        :param ds (TweetRegressionDatasetFactory): the dataset used to crossvalidate the network.
        :param iterations (int, optional): number of iterations for the crossvalidation.
        :returns error (float): the average percent error of the dataset, tested on the network using crossvalidation.
        """
        x = ds['input']
        y = ds['target']
        n, m = x.shape
        errors = np.zeros(iterations)
        cv = cross_validation.KFold(n, iterations, shuffle=True)

        i = 0
        for train_index, test_index in cv:
            x_train = x[train_index, :]
            y_train = y[train_index, :]
            x_test = x[test_index, :]
            y_test = y[test_index, :]

            ds_train = TweetClassificationDatasetFactory.convert_to_ds(x_train, y_train)
            ds_test = TweetClassificationDatasetFactory.convert_to_ds(x_test, y_test)

            self.network = NNregression(ds_train)
            self.network.setupNN(hidden=self.hidden)
            self.network.Trainer.trainUntilConvergence(
                dataset=ds_train,
                maxEpochs=self.max_epochs,
                continueEpochs=self.con_epochs)
            tstresult = self.test(ds_test)
            errors[i] = tstresult[0]
            i += 1

        print "Simple Regression Neural Network cross-validation test errors: " % errors
        return np.average(errors)
class SimpleRegressionNeuralNetwork(AI):
    def __init__(self, hid_cnt=10, max_epochs=50, con_epochs=4):
        self.hidden = hid_cnt
        self.network = None
        self.con_epochs = con_epochs
        self.max_epochs = max_epochs

    def run(self, ds_train, ds_test):
        """
        This function evaluates the ANN using a test set and has the error-rate as an output.
        Args:
        :param ds_train (TweetClassificationDatasetFactory): the training dataset the neural network is trained with.
        :param ds_test (TweetClassificationDatasetFactory): the test dataset evaluated.
        :returns error (float): the percent error of the test dataset, tested on the neural network.
        """
        self.network = NNregression(ds_train)
        self.network.setupNN(hidden=self.hidden, verbose=True)
        self.network.Trainer.trainUntilConvergence(
            dataset=ds_train,
            maxEpochs=self.max_epochs,
            continueEpochs=self.con_epochs)
        error = self.test(ds_test)
        return error

    def test(self, ds_test):
        """
        This function evaluates the ANN using a test set and has the error-rate as an output.
        Args:
        :param ds_test (TweetRegressionDatasetFactory): the test dataset evaluated.
        :returns error (float): the percent error of the test dataset, tested on the network.
        """
        result = self.network.Trainer.module.activateOnDataset(ds_test)
        error = mean_squared_error(ds_test['target'], result)
        return error

    def run_with_crossvalidation(self, ds, iterations=5):
        """
        This function estimates the performance of the neural network using crossvalidation using a specified dataset.
        Args:
        :param ds (TweetRegressionDatasetFactory): the dataset used to crossvalidate the network.
        :param iterations (int, optional): number of iterations for the crossvalidation.
        :returns error (float): the average percent error of the dataset, tested on the network using crossvalidation.
        """
        x = ds['input']
        y = ds['target']
        n, m = x.shape
        errors = np.zeros(iterations)
        cv = cross_validation.KFold(n, iterations, shuffle=True)

        i = 0
        for train_index, test_index in cv:
            x_train = x[train_index, :]
            y_train = y[train_index, :]
            x_test = x[test_index, :]
            y_test = y[test_index, :]

            ds_train = TweetClassificationDatasetFactory.convert_to_ds(x_train, y_train)
            ds_test = TweetClassificationDatasetFactory.convert_to_ds(x_test, y_test)

            self.network = NNregression(ds_train)
            self.network.setupNN(hidden=self.hidden)
            self.network.Trainer.trainUntilConvergence(
                dataset=ds_train,
                maxEpochs=self.max_epochs,
                continueEpochs=self.con_epochs)
            tstresult = self.test(ds_test)
            errors[i] = tstresult[0]
            i += 1

        print "Simple Regression Neural Network cross-validation test errors: " % errors
        return np.average(errors)

    def __call__(self, ds_train, ds_test):
        return self.run(ds_train, ds_test)

    def save(self, path):
        """
        This function saves the neural network.
        Args:
        :param path (String): the path where the network is going to be saved.
        """
        file_object = open(path, 'w')
        pickle.dump(self.network, file_object)
        file_object.close()

    def load(self, path):
        """
        This function loads the neural network.
        Args:
        :param path (String): the path where the neural network is going to be loaded from.
        """
        file_object = open(path, 'r')
        self.network = pickle.load(file_object)

    def get_type(self):
        """
        This function returns the type of problem and type of neural network.
        :returns:
        ProblemTypeEnum.Regression (enum): the type of problem.
        AIEnum.SimpleRegressionNeuralNetwork (enum): the type of artificial intelligence.
        """
        return ProblemTypeEnum.Regression, AIEnum.SimpleRegressionNeuralNetwork

    def fill_with_predicted_data(self, ds, data):
        """
        This function fills a dataset with the real and predicted values using a specified database.
        Args:
        :param ds (TweetRegressionDatasetFactory): the dataset used to
        fill the dictionary with the real and predicted values.
        :param data (dictionary): dataset gets filled with the real and the predicted values.
        """
        target = ds['target']
        out = self.network.Trainer.module.activateOnDataset(ds)
        results = np.ravel(np.argmax(out, 1))
        self.fill_data_regression(target, results, data)
예제 #8
0
''' Now lets build a neural network and train it using a back propagation network on the training data (trndata).

'''

fnn = buildNetwork(trndata.indim, 5, trndata.outdim, recurrent=False)
trainer = BackpropTrainer(fnn,
                          dataset=trndata,
                          momentum=0.1,
                          verbose=True,
                          weightdecay=0.01)
''' Originally I figured the best way to train was to do it myself, having looked at my code, and that in the doc, I think that it’s best to use the built-in functions:
'''

from pybrain.tools.neuralnets import NNregression, Trainer
# Create you dataset - as above
nn = NNregression(alldata)
nn.setupNN()
nn.runTraining()
''' Train the network for n epochs '''

# I am not sure about this, I don't think my production code is implemented like this
modval = ModuleValidator()
for i in range(1000):
    trainer.trainEpochs(1)
    trainer.trainOnDataset(dataset=trndata)
    cv = CrossValidator(trainer, trndata, n_folds=5, valfunc=modval.MSE)
    print "MSE %f @ %i" % (cv.validate(), i)
''' Now predict the test data again – this should really be a HOT set.

 '''
예제 #9
0
print trndata['input'][0], trndata['target'][0], trndata['class'][0]

''' Now lets build a neural network and train it using a back propagation network on the training data (trndata).

'''

fnn = buildNetwork( trndata.indim, 5, trndata.outdim, recurrent=False )
trainer = BackpropTrainer( fnn, dataset=trndata, momentum=0.1, verbose=True, weightdecay=0.01 )

''' Originally I figured the best way to train was to do it myself, having looked at my code, and that in the doc, I think that it’s best to use the built-in functions:
'''


from pybrain.tools.neuralnets import NNregression, Trainer
# Create you dataset - as above
nn = NNregression(alldata)
nn.setupNN()
nn.runTraining()

''' Train the network for n epochs '''

# I am not sure about this, I don't think my production code is implemented like this
modval = ModuleValidator()
for i in range(1000):
      trainer.trainEpochs(1)
      trainer.trainOnDataset(dataset=trndata)
      cv = CrossValidator( trainer, trndata, n_folds=5, valfunc=modval.MSE )
      print "MSE %f @ %i" %( cv.validate(), i )

''' Now predict the test data again – this should really be a HOT set.