本文整理汇总了Python中pybrain.structure.RecurrentNetwork类的典型用法代码示例。如果您正苦于以下问题:Python RecurrentNetwork类的具体用法?Python RecurrentNetwork怎么用?Python RecurrentNetwork使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RecurrentNetwork类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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()
示例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)
示例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.")
示例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
示例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])
示例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
示例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
示例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)
示例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
示例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
示例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
示例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()
示例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
示例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
示例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