Пример #1
0
def NN_data(ts, max_lag):
    '''Function for creating a normalized dataset suitable for training 
    PyBrain's neural networks from pandas Series object.
    Returns: dataset suitable for neural net training, max value of 
    dataset for denormalization purposes'''
    ds = SupervisedDataSet(max_lag, 1)
    times = ts.index
    prices = [item for item in normalize(ts.values)[0]]
    target = list()
    
    for item in prices:
        target.append(item)
        input_cols = list()
        for i in range(1, max_lag+1):
            col = prices[:-i]
            while len(col) < len(prices):
                col = ['nan'] + list(col)
            input_cols.append(col)
    #convert input columns to input rows
    input_rows = zip(*input_cols)
    #Remove rows containing 'nan'
    input_rows  = input_rows[max_lag:]
    target = target[max_lag:]
    for i in range(0, len(target)):
        ds.appendLinked(input_rows[i], target[i])
        
    return ds, normalize(ts.values)[1]
Пример #2
0
def train_net(data_set, n, epochs=1):
    num_inputs = len(data_set[0][0][n])
    ds = SupervisedDataSet(num_inputs, 2)
    for i in range(len(data_set)):
        try:
            ds.appendLinked(data_set[i][0][n],
                            (data_set[i][1], data_set[i][2]))
        except:
            continue
    print str(len(ds)) + ' points successfully aquired'

    net = FeedForwardNetwork()
    net.addInputModule(LinearLayer(num_inputs, name='input'))
    net.addInputModule(BiasUnit(name='bias'))
    net.addOutputModule(LinearLayer(2, name='output'))
    net.addModule(SigmoidLayer(int((num_inputs + 2) / 2.), name='sigmoid'))
    net.addModule(TanhLayer(10, name='tanh'))
    net.addConnection(FullConnection(net['bias'], net['sigmoid']))
    net.addConnection(FullConnection(net['bias'], net['tanh']))
    net.addConnection(FullConnection(net['input'], net['sigmoid']))
    net.addConnection(FullConnection(net['sigmoid'], net['tanh']))
    net.addConnection(FullConnection(net['tanh'], net['output']))
    net.sortModules()

    trainer = BackpropTrainer(net,
                              learningrate=0.01,
                              momentum=0.1,
                              verbose=True)

    trainer.trainOnDataset(ds)
    trainer.trainEpochs(epochs)

    return net
Пример #3
0
def train_separate_nets(data_set,
                        test_data,
                        arousal_net,
                        valence_net,
                        epochs=1):
    num_inputs = 4
    arousal_ds = SupervisedDataSet(num_inputs, 1)
    valence_ds = SupervisedDataSet(num_inputs, 1)
    for i in range(len(data_set)):
        try:
            arousal_ds.appendLinked(data_set[i], (data_set[i][1]))
            valence_ds.appendLinked(data_set[i], (data_set[i][2]))
        except:
            continue
    print str(
        len(arousal_ds)) + ' points successfully aquired for arousal analysis'
    print str(
        len(valence_ds)) + ' points successfully aquired for valence analysis'

    arousal_trainer = BackpropTrainer(arousal_net,
                                      learningrate=0.05,
                                      momentum=0.08,
                                      verbose=True)
    valence_trainer = BackpropTrainer(valence_net,
                                      learningrate=0.01,
                                      momentum=0.05,
                                      verbose=True)

    arousal_trainer.trainOnDataset(arousal_ds)
    valence_trainer.trainOnDataset(valence_ds)
    mean_internal_errors = []
    mean_errors = []

    for j in range(epochs / 50):
        arousal_trainer.trainEpochs(50)
        valence_trainer.trainEpochs(50)
        print str((j + 1) * 50) + '/' + str(epochs) + ' complete'
        sq_arousal_errors = [(arousal_net.activate(datum[0]) - datum[1])**2
                             for datum in test_data]
        sq_valence_errors = [(valence_net.activate(datum[0]) - datum[2])**2
                             for datum in test_data]
        errors = [
            sqrt(sq_arousal_errors[i] + sq_valence_errors[i])
            for i in range(len(sq_arousal_errors))
        ]
        mean_errors.append(np.mean(errors))

        sq_arousal_errors = [
            (arousal_net.activate(data_set[i][0]) - data_set[i][1])**2
            for i in range(len(data_set))
        ]
        sq_valence_errors = [
            (valence_net.activate(data_set[i][0]) - data_set[i][2])**2
            for i in range(len(data_set))
        ]
        errors = [
            sqrt(sq_arousal_errors[i] + sq_valence_errors[i])
            for i in range(len(sq_arousal_errors))
        ]
        mean_internal_errors.append(np.mean(errors))

    return arousal_net, valence_net, mean_errors, mean_internal_errors
trainingSet = SupervisedDataSet(len(championdictionary),1)

#Takes each game and turns it into a vector. -1 is stored if the champion is on the opposing team, 1 if the champion is on the player's team
#and 0 if it wasn't played. The vector is then fed into the Neural Network's Training Set
print "Adding Games to NN"
for game in aramdata.readlines():
 
    aramgame = json.loads(game)
    teammates,opponents, gameWin = DatabaseActions.GetResults(aramgame)
    
    #writes a vector of which champions were on which team followed by the result
    gamevector = [0]*len(championdictionary)
    for champion in teammates:
        gamevector[championdictionary[str(champion)]["Id"]-1] = 1
    for champion in opponents:
        gamevector[championdictionary[str(champion)]["Id"]-1] = -1
    
    #Feeds that result into our Neural Network's training set
    trainingSet.appendLinked(gamevector,int(gameWin))
    
#Creates a Backpropagation trainer and proceeds to train on our set. This step can take a while due to the volume of our data.
print "Training NN"
trainer = BackpropTrainer(predictionNet,trainingSet)
trainer.trainUntilConvergence(dataset = trainingSet, maxEpochs = 300, verbose = True, continueEpochs = 10, validationProportion=0.1)
    
#Saves our trained Network by exporting it to an XML file
NetworkWriter.writeToFile(predictionNet, "TrainedNetwork.xml")

aramdata.close()
Пример #5
0
import matplotlib.pyplot as plt

frame, _wea = load_full()
# frame = pandas.DataFrame(fnl)
# frame = (frame - frame.mean()) / (frame.max() - frame.min())
frame = (frame - frame.mean()) / (frame.var())
for k in [30]:
  for i in frame.columns:

    fnn = buildNetwork(k,10,1)

    DS = SupervisedDataSet(k, 1)
    dta = frame[i][:5000]
    for j in xrange(0, len(dta) - (k+1)):
      DS.appendLinked(dta[j:j+k], [dta[j+k+1]])

    test = frame[i][5000:]
    testDS = SupervisedDataSet(k, 1)
    for j in xrange(0, len(test) - (k+1)):
      testDS.appendLinked(test[j:j+k], [test[5000+i+k+1]])

    trainer = BackpropTrainer(fnn, dataset=DS, momentum=0.1, verbose=False, weightdecay=0.01)

    # for ep in range(0, 5):
      # trainer.trainEpochs()

    trainer.trainUntilConvergence(maxEpochs=20)

    res = fnn.activateOnDataset(testDS)