• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python structure.RecurrentNetwork类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中pybrain.structure.RecurrentNetwork的典型用法代码示例。如果您正苦于以下问题:Python RecurrentNetwork类的具体用法?Python RecurrentNetwork怎么用?Python RecurrentNetwork使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了RecurrentNetwork类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: __init__

    def __init__(self, nin, nout):
        singleton.append(self)

        self.inn = nin
        self.outn = nout

        self.n = buildNetwork(nin, 20, nout, bias=False, recurrent=True)
        self.n = RecurrentNetwork()
        self.n.addInputModule(LinearLayer(nin, name='in'))
        self.n.addOutputModule(LinearLayer(nout, name='out'))

        self.n.addModule(SigmoidLayer(8, name='hidden2'))
        self.n.addModule(TanhLayer(nin+nout/2, name='hidden1'))
        self.n.addModule(BiasUnit(name='bias'))
        self.n.addModule(LSTMLayer(5, name='memory'))

        self.n.addConnection(FullConnection(self.n['in'], self.n['hidden1']))
        self.n.addConnection(FullConnection(self.n['bias'], self.n['hidden1']))
        self.n.addConnection(FullConnection(self.n['hidden1'], self.n['hidden2']))
        self.n.addConnection(FullConnection(self.n['hidden2'], self.n['out']))
        self.n.addConnection(FullConnection(self.n['hidden1'], self.n['memory']))
        self.n.addConnection(FullConnection(self.n['memory'], self.n['hidden2']))
        self.n.addConnection(FullConnection(self.n['in'], self.n['hidden2']))
        self.n.addConnection(FullConnection(self.n['hidden2'], self.n['out']))
        
        self.n.addRecurrentConnection(FullConnection(self.n['hidden1'], self.n['hidden1']))
        self.n.addRecurrentConnection(FullConnection(self.n['memory'], self.n['hidden1']))
        self.n.sortModules()
开发者ID:wikii122,项目名称:nemi,代码行数:28,代码来源:rnn.py


示例2: __init__

    def __init__(self):
        self.n = RecurrentNetwork()
        inLayer = LinearLayer(8)
        hiddenLayer = SigmoidLayer(4)
        self.numInputs = 8
        outLayer = LinearLayer(4)
        self.n.addInputModule(inLayer)
        self.n.addModule(hiddenLayer)
        self.n.addOutputModule(outLayer)

        in_to_hidden = FullConnection(inLayer, hiddenLayer)
        hidden_to_out = FullConnection(hiddenLayer, outLayer)

        self.n.addConnection(in_to_hidden)
        self.n.addConnection(hidden_to_out)

        self.n.sortModules()
        self.ds = SupervisedDataSet(8, 4) 
        self.trainer = BackpropTrainer(self.n, self.ds)
开发者ID:SlightlyCyborg,项目名称:pybot,代码行数:19,代码来源:brain.py


示例3: initialize

	def initialize(self, verbose):
		print("Initializing language learner...")
		self.verbose = verbose

		# Create network and modules
		self.net = RecurrentNetwork()
		inp = LinearLayer(self.inputs, name="in")
		hiddenModules = []
		for i in range(0, self.hiddenLayers):
			hiddenModules.append(LSTMLayer(self.hiddenNodes, name=("hidden-" + str(i + 1))))
		outp = LinearLayer(self.outputs, name="out")

		# Add modules to the network with recurrence
		self.net.addOutputModule(outp)
		self.net.addInputModule(inp)
		
		for module in hiddenModules:
			self.net.addModule(module)

		# Create connections

		self.net.addConnection(FullConnection(self.net["in"], self.net["hidden-1"]))
		for i in range(0, len(hiddenModules) - 1):
			self.net.addConnection(FullConnection(self.net["hidden-" + str(i + 1)], self.net["hidden-" + str(i + 2)]))
			self.net.addRecurrentConnection(FullConnection(self.net["hidden-" + str(i + 1)], self.net["hidden-" + str(i + 1)]))
		self.net.addRecurrentConnection(FullConnection(self.net["hidden-" + str(len(hiddenModules))],
			self.net["hidden-" + str(len(hiddenModules))]))
		self.net.addConnection(FullConnection(self.net["hidden-" + str(len(hiddenModules))], self.net["out"]))
		self.net.sortModules()

		self.trainingSet = SequentialDataSet(self.inputs, self.outputs)
		for x, y in zip(self.dataIn, self.dataOut):
			self.trainingSet.newSequence()
			self.trainingSet.appendLinked([x], [y])

		self.net.randomize()

		print("Neural network initialzed with structure:")
		print(self.net)

		self.trainer = BackpropTrainer(self.net, self.trainingSet, verbose=verbose)
		self.__initialized = True
		print("Successfully initialized network.")
开发者ID:sl,项目名称:babble,代码行数:43,代码来源:languagelearner.py


示例4: __init__

    def __init__(self, name, dataset, trained, store):
        self.name = name
        self.store = store
        self.trained = trained
        self.dataset = dataset

        self.net = RecurrentNetwork()
        self.net.addInputModule(LinearLayer(2, name='in'))
        self.net.addModule(SigmoidLayer(3, name='hidden'))
        self.net.addOutputModule(LinearLayer(2, name='out'))
        self.net.addConnection(FullConnection(self.net['in'], self.net['out'], name='c1'))
        self.net.addConnection(FullConnection(self.net['hidden'], self.net['out'], name='c2'))
        self.net.addRecurrentConnection(FullConnection(self.net['hidden'], self.net['hidden'], name='c3'))
        self.net.sortModules()
        '''
        self.net = buildNetwork(2, 3, 2)
        '''
        if not self.trained:
            self.train()

        return
开发者ID:cagatay,项目名称:Evolution-9,代码行数:21,代码来源:NN.py


示例5: __init__

class BrainController:

    indim = 2
    outdim = 2

    def __init__(self, trained_net = None):
        if trained_net == None:
            self.net = RecurrentNetwork()
            self.init_network(self.net)
        else:
            self.net = trained_net

    def init_network(self, net):
        net.addInputModule(LinearLayer(2, 'in'))
        net.addModule(SigmoidLayer(3, 'hidden'))
        net.addOutputModule(LinearLayer(2, 'out'))
        net.addModule(BiasUnit(name='bias'))
        net.addConnection(FullConnection(net['in'], net['hidden']))
        net.addConnection(FullConnection(net['hidden'], net['out']))
        net.sortModules()

    def train(self, data):
        ds = SupervisedDataSet(2, 2)
        for i in range(0, len(data)):
            input, target = data[i]
            ds.addSample(input, target)

        trainer = BackpropTrainer(self.net, ds, learningrate=0.01, momentum=0.99,
                verbose=True)

        max_error = 1e-5
        error = 1
        while abs(error) >= max_error:
            error = trainer.train()

        #self.validate_net()
        f = open('neuro.net', 'w')
        pickle.dump(self.net, f)
        f.close()

    def validate_net(self):
        print self.net.activate([0, 0])
        print self.net.activate([0, 1])
        print self.net.activate([0, 2])
        print self.net.activate([1, 0])
        print self.net.activate([1, 1])
        print self.net.activate([1, 2])
开发者ID:xjie0403,项目名称:communication-swarm-intelligence,代码行数:47,代码来源:brain_controller.py


示例6: buildMinimalLSTMNetwork

def buildMinimalLSTMNetwork():
    N = RecurrentNetwork('simpleLstmNet')  
    i = LinearLayer(4, name='i')
    h = LSTMLayer(1, peepholes=True, name='lstm')
    o = LinearLayer(1, name='o')
    N.addInputModule(i)
    N.addModule(h)
    N.addOutputModule(o)
    N.addConnection(IdentityConnection(i, h))
    N.addConnection(IdentityConnection(h, o))
    N.sortModules()
    return N
开发者ID:ZachPhillipsGary,项目名称:CS200-NLP-ANNsProject,代码行数:12,代码来源:test_peephole_lstm.py


示例7: testTraining

def testTraining():
    # the AnBnCn dataset (sequential)
    d = AnBnCnDataSet()
    
    # build a recurrent network to be trained
    hsize = 2
    n = RecurrentNetwork()
    n.addModule(TanhLayer(hsize, name = 'h'))
    n.addModule(BiasUnit(name = 'bias'))
    n.addOutputModule(LinearLayer(1, name = 'out'))
    n.addConnection(FullConnection(n['bias'], n['h']))
    n.addConnection(FullConnection(n['h'], n['out']))
    n.addRecurrentConnection(FullConnection(n['h'], n['h']))
    n.sortModules()

    # initialize the backprop trainer and train
    t = BackpropTrainer(n, learningrate = 0.1, momentum = 0.0, verbose = True)
    t.trainOnDataset(d, 200)
    
    # the resulting weights are in the network:
    print 'Final weights:', n.params
开发者ID:HKou,项目名称:pybrain,代码行数:21,代码来源:backpropanbncn.py


示例8: RecurrentNetwork

all weights of the network at once. """

print hidden2out.params
print n.params

""" The former are the last slice of the latter. """

print n.params[-3:] == hidden2out.params

""" Ok, after having covered the basics, let's move on to some additional concepts.
First of all, we encourage you to name all modules, or connections you create, because that gives you
more readable printouts, and a very concise way of accessing them.

We now build an equivalent network to the one before, but with a more concise syntax:
"""
n2 = RecurrentNetwork(name='net2')
n2.addInputModule(LinearLayer(2, name='in'))
n2.addModule(SigmoidLayer(3, name='h'))
n2.addOutputModule(LinearLayer(1, name='out'))
n2.addConnection(FullConnection(n2['in'], n2['h'], name='c1'))
n2.addConnection(FullConnection(n2['h'], n2['out'], name='c2'))
n2.sortModules()

""" Printouts look more concise and readable: """
print n2

""" There is an even quicker way to build networks though, as long as their structure is nothing
more fancy than a stack of fully connected layers: """

n3 = buildNetwork(2, 3, 1, bias=False)
开发者ID:Boblogic07,项目名称:pybrain,代码行数:30,代码来源:networks.py


示例9: buildParityNet

def buildParityNet():
    net = RecurrentNetwork()
    net.addInputModule(LinearLayer(1, name = 'i'))
    net.addModule(TanhLayer(2, name = 'h'))
    net.addModule(BiasUnit('bias'))
    net.addOutputModule(TanhLayer(1, name = 'o'))
    net.addConnection(FullConnection(net['i'], net['h']))
    net.addConnection(FullConnection(net['bias'], net['h']))
    net.addConnection(FullConnection(net['bias'], net['o']))
    net.addConnection(FullConnection(net['h'], net['o']))
    net.addRecurrentConnection(FullConnection(net['o'], net['h']))
    net.sortModules()

    p = net.params
    p[:] = [-0.5, -1.5, 1, 1, -1, 1, 1, -1, 1]
    p *= 10.

    return net
开发者ID:Angeliqe,项目名称:pybrain,代码行数:18,代码来源:parityrnn.py


示例10: trainedRNN

def trainedRNN():
    n = RecurrentNetwork()

    n.addInputModule(LinearLayer(4, name='in'))
    n.addModule(SigmoidLayer(6, name='hidden'))
    n.addOutputModule(LinearLayer(2, name='out'))
    n.addConnection(FullConnection(n['in'], n['hidden'], name='c1'))
    n.addConnection(FullConnection(n['hidden'], n['out'], name='c2'))

    n.addRecurrentConnection(NMConnection(n['out'], n['out'], name='nmc'))
    # n.addRecurrentConnection(FullConnection(n['out'], n['hidden'], inSliceFrom = 0, inSliceTo = 1, outSliceFrom = 0, outSliceTo = 3))
    n.sortModules()

    draw_connections(n)
    d = getDatasetFromFile(root.path()+"/res/dataSet")
    t = BackpropTrainer(n, d, learningrate=0.001, momentum=0.75)
    t.trainOnDataset(d)

    count = 0
    while True:
        globErr = t.train()
        print globErr
        if globErr < 0.01:
            break
        count += 1
        if count == 50:
            return trainedRNN()
    # exportRNN(n)
    draw_connections(n)

    return n
开发者ID:DianaShatunova,项目名称:NEUCOGAR,代码行数:31,代码来源:main.py


示例11: trained_cat_dog_RFCNN

def trained_cat_dog_RFCNN():
    n = RecurrentNetwork()

    d = get_cat_dog_trainset()
    input_size = d.getDimension('input')
    n.addInputModule(LinearLayer(input_size, name='in'))
    n.addModule(SigmoidLayer(input_size+1500, name='hidden'))
    n.addOutputModule(LinearLayer(2, name='out'))
    n.addConnection(FullConnection(n['in'], n['hidden'], name='c1'))
    n.addConnection(FullConnection(n['hidden'], n['out'], name='c2'))
    n.addRecurrentConnection(FullConnection(n['out'], n['hidden'], name='nmc'))
    n.sortModules()

    t = BackpropTrainer(n, d, learningrate=0.0001)#, momentum=0.75)

    count = 0
    while True:
        globErr = t.train()
        print globErr
        count += 1
        if globErr < 0.01:
            break
        if count == 30:
            break

    exportCatDogRFCNN(n)
    return n
开发者ID:DianaShatunova,项目名称:NEUCOGAR,代码行数:27,代码来源:main.py


示例12: main

def main():
    numIterations=200
    terminal_EMA_SharpeRatio=[0 for i in range(numIterations)]
    numTrades=[0 for i in range(numIterations)]
    sharpe_first_half=[0 for i in range(numIterations)]
    sharpe_sec_half=[0 for i in range(numIterations)]
    sharpe_ratio_total=[0 for i in range(numIterations)]

    for i in range(numIterations):
        env=RWEnvironment(2000)
        task = MaximizeReturnTask(env)
        numIn=min(env.worldState.shape)

        net=RecurrentNetwork()
        net.addInputModule(BiasUnit(name='bias'))
        net.addOutputModule((SignLayer(1,name='out')))
        net.addRecurrentConnection(FullConnection(net['out'], net['out'], name='c3'))
        net.addInputModule(LinearLayer(numIn,name='in'))
        net.addConnection(FullConnection(net['in'],net['out'],name='c1'))
        net.addConnection((FullConnection(net['bias'],net['out'],name='c2')))
        net.sortModules()

        ts=env.ts
        learner = RRL(numIn+2,ts) # ENAC() #Q_LinFA(2,1)
        agent = LearningAgent(net,learner)
        exp = ContinuousExperiment(task,agent)

        #performance tracking

        exp.doInteractionsAndLearn(len(ts)-1)
            #print(net._params)
        terminal_EMA_SharpeRatio[i]=learner.ema_sharpeRatio[-1]
        rs=pE.calculateTradingReturn(env.actionHistory,ts)
        sharpe_first_half[i]=pE.annualisedSharpe(rs[:(len(ts)/2)])
        sharpe_sec_half[i]=pE.annualisedSharpe(rs[len(ts)/2:])
        sharpe_ratio_total[i]=pE.annualisedSharpe(rs)
        numTrades[i]=learner.numTrades



    print(net._params)
    print("average number of trades per 1000 observations is {}".format(mean(numTrades)/2))
    print("mean Sharpe ratios are {} with standard errors {}, and {} with standard errors {}".format(mean(sharpe_first_half),st.sem(sharpe_first_half),mean(sharpe_sec_half),st.sem(sharpe_sec_half)))
    print("average sharpe ratio for each entire epoche is {} with standard error {}".format(mean(sharpe_ratio_total),st.sem(sharpe_ratio_total)))
    fig,ax= plt.subplots(nrows=2,ncols=1,sharex=True,sharey=True)
    l1=ax[0].hist(sharpe_first_half,bins=20)
    ax[0].set_title('Annualised Sharpe Ratio (t=0:1000)')
    l2=ax[1].hist(sharpe_sec_half,bins=20)
    ax[1].set_title('Annualised Sharpe Ratio (t=1001:2000)')
    plt.show()


    #plt.hist(numTrades,bins=20)


    #plt.plot(terminal_EMA_SharpeRatio)
    #plt.show()

    actionHist=env.actionHistory
    ts=[t/100 for t in ts]
    cum_log_r=cumsum([log(1+ts[i]) for i in range(len(ts))])
    cum_log_R=cumsum([log(1+(actionHist[i]*ts[i])) for i in range(len(ts))])



    fix, axes = plt.subplots(3, sharex=True)
    ln1=axes[0].plot(cum_log_r,label='Buy and Hold')
    ln2=axes[0].plot(cum_log_R,label='Trading Agent')
    lns=ln1+ln2
    labs=[l.get_label() for l in lns]
    axes[0].legend(lns,labs,loc='upper left')
    axes[0].set_ylabel("Cumulative Log Returns")
    ax[0].set_title("Artificial Series")
    ln3=axes[1].plot(actionHist,'r',label='Trades')
    axes[1].set_ylabel("F(t)")
    axes[2].plot(learner.ema_sharpeRatio)
    axes[2].set_ylabel("EMA Sharpe Ratio")
    plt.show()
开发者ID:samstern,项目名称:MSc-Project,代码行数:78,代码来源:autoRegRRL.py


示例13: buildMixedNestedNetwork

def buildMixedNestedNetwork():
    """ build a nested network with the inner one being a ffn and the outer one being recurrent. """
    N = RecurrentNetwork('outer')
    a = LinearLayer(1, name = 'a')
    b = LinearLayer(2, name = 'b')
    c = buildNetwork(2, 3, 1)
    c.name = 'inner'
    N.addInputModule(a)
    N.addModule(c)
    N.addOutputModule(b)
    N.addConnection(FullConnection(a,b))
    N.addConnection(FullConnection(b,c))
    N.addRecurrentConnection(FullConnection(c,c))
    N.sortModules()
    return N
开发者ID:HKou,项目名称:pybrain,代码行数:15,代码来源:test_nested_ffn_and_rnn.py


示例14: epochs

class LanguageLearner:

	__OUTPUT = "Sample at {0} epochs (prompt=\"{1}\", length={2}): {3}"

	def __init__(self, trainingText, hiddenLayers, hiddenNodes):
		self.__initialized = False
		with open(trainingText) as f:
			self.raw = f.read()
		self.characters = list(self.raw)
		self.rawData = list(map(ord, self.characters))
		print("Creating alphabet mapping...")
		self.mapping = []
		for charCode in self.rawData:
			if charCode not in self.mapping:
				self.mapping.append(charCode)
		print("Mapping of " + str(len(self.mapping)) + " created.")
		print(str(self.mapping))
		print("Converting data to mapping...")
		self.data = []
		for charCode in self.rawData:
			self.data.append(self.mapping.index(charCode))
		print("Done.")
		self.dataIn = self.data[:-1:]
		self.dataOut = self.data[1::]
		self.inputs = 1
		self.hiddenLayers = hiddenLayers
		self.hiddenNodes = hiddenNodes
		self.outputs = 1

	def initialize(self, verbose):
		print("Initializing language learner...")
		self.verbose = verbose

		# Create network and modules
		self.net = RecurrentNetwork()
		inp = LinearLayer(self.inputs, name="in")
		hiddenModules = []
		for i in range(0, self.hiddenLayers):
			hiddenModules.append(LSTMLayer(self.hiddenNodes, name=("hidden-" + str(i + 1))))
		outp = LinearLayer(self.outputs, name="out")

		# Add modules to the network with recurrence
		self.net.addOutputModule(outp)
		self.net.addInputModule(inp)
		
		for module in hiddenModules:
			self.net.addModule(module)

		# Create connections

		self.net.addConnection(FullConnection(self.net["in"], self.net["hidden-1"]))
		for i in range(0, len(hiddenModules) - 1):
			self.net.addConnection(FullConnection(self.net["hidden-" + str(i + 1)], self.net["hidden-" + str(i + 2)]))
			self.net.addRecurrentConnection(FullConnection(self.net["hidden-" + str(i + 1)], self.net["hidden-" + str(i + 1)]))
		self.net.addRecurrentConnection(FullConnection(self.net["hidden-" + str(len(hiddenModules))],
			self.net["hidden-" + str(len(hiddenModules))]))
		self.net.addConnection(FullConnection(self.net["hidden-" + str(len(hiddenModules))], self.net["out"]))
		self.net.sortModules()

		self.trainingSet = SequentialDataSet(self.inputs, self.outputs)
		for x, y in zip(self.dataIn, self.dataOut):
			self.trainingSet.newSequence()
			self.trainingSet.appendLinked([x], [y])

		self.net.randomize()

		print("Neural network initialzed with structure:")
		print(self.net)

		self.trainer = BackpropTrainer(self.net, self.trainingSet, verbose=verbose)
		self.__initialized = True
		print("Successfully initialized network.")

	def train(self, epochs, frequency, prompt, length):
		if not self.__initialized:
			raise Exception("Attempted to train uninitialized LanguageLearner")
		print ("Beginning training for " + str(epochs) + " epochs...")
		if frequency >= 0:
			print(LanguageLearner.__OUTPUT.format(0, prompt, length, self.sample(prompt, length)))
		for i in range(1, epochs):
			print("Error at " + str(i) + " epochs: " + str(self.trainer.train()))
			if i % frequency == 0:
				print(LanguageLearner.__OUTPUT.format(i, prompt, length, self.sample(prompt, length)))
		print("Completed training.")

	def sample(self, prompt, length):
		self.net.reset()
		if prompt == None:
			prompt = chr(random.choice(self.mapping))
		output = prompt
		charCode = ord(prompt)
		for i in range(0, length):
			sampledResult = self.net.activate([charCode])
			charCode = int(round(sampledResult[0]))
			if charCode < 0 or charCode >= len(self.mapping):
				return output + "#TERMINATED_SAMPLE(reason: learner guessed invalid character)"
			output += chr(self.mapping[charCode])
		return output
开发者ID:sl,项目名称:babble,代码行数:98,代码来源:languagelearner.py


示例15: LinearLayer

#inLayer = LinearLayer(ds.indim)
#hiddenLayer = SigmoidLayer(5)
#outLayer = SoftmaxLayer(ds.outdim)
#net.addInputModule(inLayer)
#net.addModule(hiddenLayer)
#net.addOutputModule(outLayer)
#from pybrain.structure import FullConnection
#in_to_hidden = FullConnection(inLayer, hiddenLayer)
#hidden_to_out = FullConnection(hiddenLayer, outLayer)
#net.addConnection(in_to_hidden)
#net.addConnection(hidden_to_out)
#net.sortModules()
#net = buildNetwork(ds.indim, 2, ds.outdim, outclass=SoftmaxLayer)

from pybrain.structure import RecurrentNetwork
net = RecurrentNetwork()
net.addInputModule(LinearLayer(ds.indim, name='inLayer'))
net.addModule(SigmoidLayer(ds.indim, name='hiddenLayer'))
net.addOutputModule(SoftmaxLayer(ds.outdim, name='outLayer'))
net.addConnection(FullConnection(net['inLayer'], net['hiddenLayer'], name='in_to_hidden'))
net.addConnection(FullConnection(net['hiddenLayer'], net['outLayer'], name='hidden_to_out'))
net.addRecurrentConnection(FullConnection(net['hiddenLayer'], net['hiddenLayer'], name='hidden_to_hidden'))
net.sortModules()

#Train net
from pybrain.supervised.trainers import BackpropTrainer
trainer = BackpropTrainer(net, ds, momentum=0.1, verbose=True, weightdecay=0.01)

#for i in range(10):
#    if i%20==0:
#        print i
开发者ID:bilykigor,项目名称:qimb,代码行数:31,代码来源:QimbWorkbook.py


示例16: RecurrentNetwork

from pybrain.structure import FeedForwardNetwork, RecurrentNetwork, LinearLayer, SigmoidLayer, FullConnection

####
#RECURRENT NETWORK
####

n = RecurrentNetwork()

inLayer = LinearLayer(inputVector, name='inputLayer')
hiddenLayerA = SigmoidLayer(hiddenVector, name='hiddenLayerA')
hiddenLayerB = SigmoidLayer(hiddenVector, name='hiddenLayerB')
outputLayer = LinearLayer(outputVector, name='outputLayer')

n.addInputModule(inLayer)
n.addModule(hiddenLayerA)
n.addModule(hiddenLayerB)
n.addOutputModule(outputLayer)

n.addConnection(FullConnection(n['inputLayer'], n['hiddenLayerA'], name='c1'))
n.addConnection(FullConnection(n['hiddenLayerA'], n['hiddenLayerB'], name='c2'))
n.addConnection(FullConnection(n['hiddenLayerB'], n['outputLayer'], name='c3'))

n.addRecurrentConnection(FullConnection(n['hiddenLayerA'], n['hiddenLayerB'], name='rec3'))

n.sortModules()
print 'Network One (Recurrent)' + str(n.activate([1,2,3]))
print 'Network One (Recurrent)' + str(n.activate([1,2,3]))

####
#FEED FORWARD NETWORK
####
开发者ID:ElDonClaudio,项目名称:NNET,代码行数:31,代码来源:tst.py


示例17: trainNetwork

def trainNetwork(dirname):

    numFeatures = 2000

    ds = SequentialDataSet(numFeatures, 1)

    tracks = glob.glob(os.path.join(dirname, "*.csv"))
    for t in tracks:
        track = os.path.splitext(t)[0]
        # load training data
        print "Reading %s..." % t
        data = numpy.genfromtxt(t, delimiter=",")
        numData = data.shape[0]

        # add the input to the dataset
        print "Adding to dataset..."
        ds.newSequence()
        for i in range(numData):
            # ds.addSample(data[i], (labels[i],))
            input = data[i]
            label = input[numFeatures]
            if label > 0:
                label = midi_util.frequencyToMidi(label)
            ds.addSample(input[0:numFeatures], (label,))

    # initialize the neural network
    print "Initializing neural network..."
    # net = buildNetwork(numFeatures, 50, 1,
    #                   hiddenclass=LSTMLayer, bias=True, recurrent=True)

    # manual network building
    net = RecurrentNetwork()
    inlayer = LinearLayer(numFeatures)
    # h1 = LSTMLayer(70)
    # h2 = SigmoidLayer(50)
    octaveLayer = LSTMLayer(5)
    noteLayer = LSTMLayer(12)
    combinedLayer = SigmoidLayer(60)
    outlayer = LinearLayer(1)

    net.addInputModule(inlayer)
    net.addOutputModule(outlayer)
    # net.addModule(h1)
    # net.addModule(h2)
    net.addModule(octaveLayer)
    net.addModule(noteLayer)
    net.addModule(combinedLayer)

    # net.addConnection(FullConnection(inlayer, h1))
    # net.addConnection(FullConnection(h1, h2))
    # net.addConnection(FullConnection(h2, outlayer))

    net.addConnection(FullConnection(inlayer, octaveLayer))
    net.addConnection(FullConnection(inlayer, noteLayer))
    # net.addConnection(FullConnection(octaveLayer,combinedLayer))
    for i in range(5):
        net.addConnection(
            FullConnection(
                octaveLayer, combinedLayer, inSliceFrom=i, inSliceTo=i + 1, outSliceFrom=i * 12, outSliceTo=(i + 1) * 12
            )
        )
    net.addConnection(FullConnection(noteLayer, combinedLayer))
    net.addConnection(FullConnection(combinedLayer, outlayer))

    net.sortModules()

    # train the network on the dataset
    print "Training neural net"
    trainer = RPropMinusTrainer(net, dataset=ds)
    ##    trainer.trainUntilConvergence(maxEpochs=50, verbose=True, validationProportion=0.1)
    error = -1
    for i in range(150):
        new_error = trainer.train()
        print "error: " + str(new_error)
        if abs(error - new_error) < 0.005:
            break
        error = new_error

    # save the network
    print "Saving neural network..."
    NetworkWriter.writeToFile(net, os.path.basename(dirname) + "designnet")
开发者ID:tediris,项目名称:MusicML,代码行数:81,代码来源:trainer2.py


示例18: exec_algo

def  exec_algo(xml_file, output_location):
        rootObj=ml.parse(xml_file)
        file_name=rootObj.MachineLearning.prediction.datafile
        file=open(file_name)
        var_input=rootObj.MachineLearning.prediction.input
        var_output=rootObj.MachineLearning.prediction.output
        var_classes=rootObj.MachineLearning.prediction.classes

        DS=ClassificationDataSet(var_input,var_output,nb_classes=var_classes)
        #DS1=ClassificationDataSet(13,1,nb_classes=10)

        for line in file.readlines():
                data=[float(x) for x in line.strip().split(',') if x != '']
                inp=tuple(data[:var_input])
                output=tuple(data[var_input:])
                DS.addSample(inp,output)

        tstdata,trndata=DS.splitWithProportion(0)
        #trndatatest,tstdatatest=DS1.splitWithProportion(0)

        trdata=ClassificationDataSet(trndata.indim,1,nb_classes=10)
        #tsdata=ClassificationDataSet(DS1.indim,1,nb_classes=10)
        #tsdata1=ClassificationDataSet(DS1.indim,1,nb_classes=10)

        for i in xrange(trndata.getLength()):
                if (trndata.getSample(i)[1][0]!=100):
                        trdata.addSample(trndata.getSample(i)[0],trndata.getSample(i)[1])

        trdata._convertToOneOfMany()
        #tsdata._convertToOneOfMany()
        #tsdata1._convertToOneOfMany()
        print "%d" % (trdata.getLength())

        rnn=RecurrentNetwork()
        inputLayer=LinearLayer(trdata.indim)

        hiddenLayer=rootObj.MachineLearning.prediction.algorithm.RecurrentNeuralNetwork.hiddenLayerActivation
        hiddenNeurons=rootObj.MachineLearning.prediction.algorithm.RecurrentNeuralNetwork.hiddenNeurons

        if  hiddenLayer=='Sigmoid':
                hiddenLayer=SigmoidLayer(hiddenNeurons)
        elif hiddenLayer=='Softmax':
                hiddenLayer=SoftmaxLayer(hiddenNeurons)
        else:
                hiddenLayer=LinearLayer(hiddenNeurons)

        outputLayer=rootObj.MachineLearning.prediction.algorithm.RecurrentNeuralNetwork.outputLayerActivation

        if  outputLayer=='Sigmoid':
               outputLayer=SigmoidLayer(trdata.outdim)
        elif outputLayer=='Softmax':
                outputLayer=SoftmaxLayer(trdata.outdim)
        else:
                outputLayer=LinearLayer(trdata.outdim)

        rnn.addInputModule(inputLayer)
        rnn.addModule(hiddenLayer)
        rnn.addOutputModule(outputLayer)
        rnn_type=rootObj.MachineLearning.prediction.algorithm.RecurrentNeuralNetwork.RNN_Type
        in_to_hidden=FullConnection(inputLayer,hiddenLayer)
        hidden_to_outputLayer=FullConnection(hiddenLayer,outputLayer)
        rnn.addConnection(in_to_hidden)
        rnn.addConnection(hidden_to_outputLayer)

        if rnn_type=='Elman':
                hidden_to_hidden=FullConnection(hiddenLayer,hiddenLayer, name='c3')
                rnn.addRecurrentConnection(hidden_to_hidden)
        #hidden_to_hidden=FullConnection(hiddenLayer,hiddenLayer, name='c3')

        if rnn_type=='Jordan':
                output_to_hidden=FullConnection(outputLayer,hiddenLayer, name='c3')
                rnn.addRecurrentConnection(output_to_hidden)
                


        #rnn.addRecurrentConnection(hidden_to_hidden)
        momentum=rootObj.MachineLearning.prediction.algorithm.RecurrentNeuralNetwork.momentum
        weightdecay=rootObj.MachineLearning.prediction.algorithm.RecurrentNeuralNetwork.learningRate
        rnn.sortModules()
        trainer=BackpropTrainer(rnn,dataset=trdata,momentum=0.1,verbose=True,weightdecay=0.01)
        trainer.train();
        result=(percentError(trainer.testOnClassData(dataset=trdata),trdata['class']))
        #result1=percentError(trainer.testOnClassData(dataset=tsdata1),tsdata1['class'])

        print ('%f \n') % (100-result)
        #print ('%f \n') % (100-result1)

        ts=time.time()
        directory = output_location + sep + str(int(ts))
        makedirs(directory)
        fileObject=open(output_location + sep + str(int(ts)) + sep + 'pybrain_RNN','w')
        pickle.dump(trainer,fileObject)
        pickle.dump(rnn,fileObject)
        fileObject.close()
开发者ID:RathinakumarVisweswaran,项目名称:MeetCI,代码行数:94,代码来源:RNN.py


示例19: buildSimpleLSTMNetwork

def buildSimpleLSTMNetwork(peepholes = False):
    N = RecurrentNetwork('simpleLstmNet')
    i = LinearLayer(100, name = 'i')
    h = LSTMLayer(10, peepholes = peepholes, name = 'lstm')
    o = LinearLayer(1, name = 'o')
    b = BiasUnit('bias')
    N.addModule(b)
    N.addOutputModule(o)
    N.addInputModule(i)
    N.addModule(h)
    N.addConnection(FullConnection(i, h, name = 'f1'))
    N.addConnection(FullConnection(b, h, name = 'f2'))
    N.addRecurrentConnection(FullConnection(h, h, name = 'r1'))
    N.addConnection(FullConnection(h, o, name = 'r1'))
    N.sortModules()
    return N
开发者ID:kamilsa,项目名称:KAIProject,代码行数:16,代码来源:honn.py


示例20: buildElmanNetwork

def buildElmanNetwork(hiddenSize):
    net = RecurrentNetwork()
    inLayer = LinearLayer(sampleSize())
    hiddenLayer = SigmoidLayer(hiddenSize)
    outLayer = SigmoidLayer(outputSize())
    net.addInputModule(inLayer)
    net.addModule(hiddenLayer)
    net.addOutputModule(outLayer)
    hiddenRecursive = IdentityConnection(hiddenLayer, hiddenLayer)
    inToHidden = FullConnection(inLayer, hiddenLayer)
    hiddenToOut = FullConnection(hiddenLayer, outLayer)
    net.addRecurrentConnection(hiddenRecursive)
    net.addConnection(inToHidden)
    net.addConnection(hiddenToOut)
    net.sortModules()
    net.randomize()
    return net
开发者ID:Melamoto,项目名称:ML-Melody-Co-composition,代码行数:17,代码来源:melody_model.py



注:本文中的pybrain.structure.RecurrentNetwork类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python module.Module类代码示例发布时间:2022-05-25
下一篇:
Python structure.FeedForwardNetwork类代码示例发布时间:2022-05-25
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap