本文整理汇总了Python中pybrain.datasets.SequentialDataSet.newSequence方法的典型用法代码示例。如果您正苦于以下问题:Python SequentialDataSet.newSequence方法的具体用法?Python SequentialDataSet.newSequence怎么用?Python SequentialDataSet.newSequence使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pybrain.datasets.SequentialDataSet
的用法示例。
在下文中一共展示了SequentialDataSet.newSequence方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: train
# 需要导入模块: from pybrain.datasets import SequentialDataSet [as 别名]
# 或者: from pybrain.datasets.SequentialDataSet import newSequence [as 别名]
def train(self, params):
n = params['encoding_num']
net = buildNetwork(n, params['num_cells'], n,
hiddenclass=LSTMLayer,
bias=True,
outputbias=params['output_bias'],
recurrent=True)
net.reset()
ds = SequentialDataSet(n, n)
trainer = RPropMinusTrainer(net, dataset=ds)
history = self.window(self.history, params)
resets = self.window(self.resets, params)
for i in xrange(1, len(history)):
if not resets[i-1]:
ds.addSample(self.encoder.encode(history[i-1]),
self.encoder.encode(history[i]))
if resets[i]:
ds.newSequence()
if len(history) > 1:
trainer.trainEpochs(params['num_epochs'])
net.reset()
for i in xrange(len(history) - 1):
symbol = history[i]
output = self.net.activate(self.encoder.encode(symbol))
predictions = self.encoder.classify(output, num=params['num_predictions'])
if resets[i]:
net.reset()
return net
示例2: train
# 需要导入模块: from pybrain.datasets import SequentialDataSet [as 别名]
# 或者: from pybrain.datasets.SequentialDataSet import newSequence [as 别名]
def train(self, params):
self.net.reset()
ds = SequentialDataSet(self.nDimInput, self.nDimOutput)
trainer = RPropMinusTrainer(self.net, dataset=ds, verbose=False)
history = self.window(self.history, params)
resets = self.window(self.resets, params)
for i in xrange(params['prediction_nstep'], len(history)):
if not resets[i-1]:
ds.addSample(self.inputEncoder.encode(history[i-params['prediction_nstep']]),
self.outputEncoder.encode(history[i][0]))
if resets[i]:
ds.newSequence()
# print ds.getSample(0)
# print ds.getSample(1)
# print ds.getSample(1000)
# print " training data size", ds.getLength(), " len(history) ", len(history), " self.history ", len(self.history)
# print ds
if len(history) > 1:
trainer.trainEpochs(params['num_epochs'])
self.net.reset()
for i in xrange(len(history) - params['prediction_nstep']):
symbol = history[i]
output = self.net.activate(ds.getSample(i)[0])
if resets[i]:
self.net.reset()
示例3: visulizeDataSet
# 需要导入模块: from pybrain.datasets import SequentialDataSet [as 别名]
# 或者: from pybrain.datasets.SequentialDataSet import newSequence [as 别名]
def visulizeDataSet(network, data, seqno, in_labels, out_labels):
seq = data.getSequence(seqno)
tmpDs = SequentialDataSet(data.indim, data.outdim)
tmpDs.newSequence()
for i in xrange(data.getSequenceLength(seqno)):
tmpDs.addSample(seq[0][i], seq[1][i])
nplots = len(in_labels) + len(out_labels)
for i in range(len(in_labels)):
p = PL.subplot(nplots, 1, i + 1)
p.clear()
p.plot(tmpDs['input'][:, i])
p.set_ylabel(in_labels[i])
for i in range(len(out_labels)):
p = PL.subplot(nplots, 1, i + 1 + len(in_labels))
p.clear()
output = ModuleValidator.calculateModuleOutput(network, tmpDs)
p.plot(tmpDs['target'][:, i], label='train')
p.plot(output[:, i], label='sim')
p.legend()
p.set_ylabel(out_labels[i])
示例4: train
# 需要导入模块: from pybrain.datasets import SequentialDataSet [as 别名]
# 或者: from pybrain.datasets.SequentialDataSet import newSequence [as 别名]
def train(self, params):
"""
Train LSTM network on buffered dataset history
After training, run LSTM on history[:-1] to get the state correct
:param params:
:return:
"""
if params['reset_every_training']:
n = params['encoding_num']
self.net = buildNetwork(n, params['num_cells'], n,
hiddenclass=LSTMLayer,
bias=True,
outputbias=params['output_bias'],
recurrent=True)
self.net.reset()
# prepare training dataset
ds = SequentialDataSet(params['encoding_num'], params['encoding_num'])
history = self.window(self.history, params)
resets = self.window(self.resets, params)
for i in xrange(1, len(history)):
if not resets[i - 1]:
ds.addSample(self.encoder.encode(history[i - 1]),
self.encoder.encode(history[i]))
if resets[i]:
ds.newSequence()
print "Train LSTM network on buffered dataset of length ", len(history)
if params['num_epochs'] > 1:
trainer = RPropMinusTrainer(self.net,
dataset=ds,
verbose=params['verbosity'] > 0)
if len(history) > 1:
trainer.trainEpochs(params['num_epochs'])
# run network on buffered dataset after training to get the state right
self.net.reset()
for i in xrange(len(history) - 1):
symbol = history[i]
output = self.net.activate(self.encoder.encode(symbol))
self.encoder.classify(output, num=params['num_predictions'])
if resets[i]:
self.net.reset()
else:
self.trainer.setData(ds)
self.trainer.train()
# run network on buffered dataset after training to get the state right
self.net.reset()
for i in xrange(len(history) - 1):
symbol = history[i]
output = self.net.activate(self.encoder.encode(symbol))
self.encoder.classify(output, num=params['num_predictions'])
if resets[i]:
self.net.reset()
示例5: create_data
# 需要导入模块: from pybrain.datasets import SequentialDataSet [as 别名]
# 或者: from pybrain.datasets.SequentialDataSet import newSequence [as 别名]
def create_data(self, inputs, targets):
data = SequentialDataSet(inputs, targets)
for i in xrange(0, len(self.dataframe) - 1):
data.newSequence()
ins = self.dataframe.ix[i].values
target = self.dataframe.ix[i + 1].values[0]
data.appendLinked(ins, target)
self.data = data
示例6: buildAppropriateDataset
# 需要导入模块: from pybrain.datasets import SequentialDataSet [as 别名]
# 或者: from pybrain.datasets.SequentialDataSet import newSequence [as 别名]
def buildAppropriateDataset(module):
""" build a sequential dataset with 2 sequences of 3 samples, with arndom input and target values,
but the appropriate dimensions to be used on the provided module. """
if module.sequential:
d = SequentialDataSet(module.indim, module.outdim)
for dummy in range(2):
d.newSequence()
for dummy in range(3):
d.addSample(randn(module.indim), randn(module.outdim))
else:
d = SupervisedDataSet(module.indim, module.outdim)
for dummy in range(3):
d.addSample(randn(module.indim), randn(module.outdim))
return d
示例7: trainNetwork
# 需要导入模块: from pybrain.datasets import SequentialDataSet [as 别名]
# 或者: from pybrain.datasets.SequentialDataSet import newSequence [as 别名]
def trainNetwork(dirname):
numFeatures = 5000
ds = SequentialDataSet(numFeatures, 1)
tracks = glob.glob(os.path.join(dirname, 'train??.wav'))
for t in tracks:
track = os.path.splitext(t)[0]
# load training data
print "Reading %s..." % track
data = numpy.genfromtxt(track + '_seg.csv', delimiter=",")
labels = numpy.genfromtxt(track + 'REF.txt', delimiter='\t')[0::10,1]
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],))
# initialize the neural network
print "Initializing neural network..."
net = buildNetwork(numFeatures, 50, 1,
hiddenclass=LSTMLayer, outputbias=False, recurrent=True)
# 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(100):
new_error = trainer.train()
print "error: " + str(new_error)
if abs(error - new_error) < 0.1: break
error = new_error
# save the network
print "Saving neural network..."
NetworkWriter.writeToFile(net, os.path.basename(dirname) + 'net')
示例8: rnnTrain
# 需要导入模块: from pybrain.datasets import SequentialDataSet [as 别名]
# 或者: from pybrain.datasets.SequentialDataSet import newSequence [as 别名]
def rnnTrain(data):
ds = SequentialDataSet(3,3)
s = np.size(input) / 9
ds.newSequence()
for idx1 in range(s):
ds.appendLinked(data[idx1],(1,0,0))
ds.newSequence()
for idx2 in range(s):
ds.appendLinked(data[idx2+s],(0,1,0))
ds.newSequence()
for idx3 in range(s):
ds.appendLinked(data[idx3+s+s],(0,0,1))
net = buildNetwork(3, 8, 3, bias=True, recurrent=True, hiddenclass=LSTMLayer)
trainer = BackpropTrainer(net,ds)
trainer.trainEpochs(1000)
return net
示例9: simple_rnn_classifier
# 需要导入模块: from pybrain.datasets import SequentialDataSet [as 别名]
# 或者: from pybrain.datasets.SequentialDataSet import newSequence [as 别名]
class simple_rnn_classifier():
def __init__(self, ds):
self.alldata = SequentialDataSet(ds.num_features, 1)
# Now add the samples to the data set.
idx = 1
self.alldata.newSequence()
for sample in ds.all_moves:
self.alldata.addSample(sample.get_features(), [ds.get_classes().index(sample.class_)])
idx += 1
if (idx%6 == 0):
self.alldata.newSequence()
self.tstdata, self.trndata = self.alldata.splitWithProportion(0.25)
#print "Number of training patterns: ", len(self.trndata)
#print "Input and output dimensions: ", self.trndata.indim, self.trndata.outdim
#print "First sample (input, target, class):"
#print self.trndata['input'][0], self.trndata['target'][0], self.trndata['class'][0]
# 5 hidden layers.
self.rnn = buildNetwork(self.trndata.indim,
3,
self.trndata.outdim,
hiddenclass=LSTMLayer,
outclass=SigmoidLayer,
recurrent=True)
self.rnn.randomize()
self.trainer = BackpropTrainer(self.nn, dataset=self.trndata, momentum=0.1, verbose=True, weightdecay=0.01)
def start_training(self):
f = open("./results/rnn_perf.txt", "w");
for i in range(200):
print "training step: " , i
self.trainer.trainEpochs(1)
err = self.evaluate()
f.write(str(err) + ",")
f.flush()
f.close()
def evaluate(self):
print "epoch:" , self.trainer.totalepochs
correct = 0
wrong = 0
self.fnn.sortModules()
for Idx in range (len(self.tstdata)):
out = self.fnn.activate(self.tstdata['input'][Idx])
if argmax(out) == argmax(self.tstdata['target'][Idx]) :
correct += 1
else:
wrong += 1
correct_ratio = correct*1.0/(wrong + correct)
self.correct_perc.append(correct_ratio)
print "Wrong Predictions: " , wrong , "Ratio = ", wrong*100.0/(wrong+correct) , "%"
print "Correct Predictions: ", correct, "Ratio = ", correct*100.0/(wrong+correct) , "%"
if (self.max_ratio < correct_ratio):
print "Found new max, saving network"
self.write_out("best_perfrming_")
self.max_ratio = correct_ratio
return 1 - correct_ratio
def write_out(self, name=""):
NetworkWriter.writeToFile(self.fnn, "./results/" + name + "rnn.xml")
示例10: epochs
# 需要导入模块: from pybrain.datasets import SequentialDataSet [as 别名]
# 或者: from pybrain.datasets.SequentialDataSet import newSequence [as 别名]
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
示例11: RWR
# 需要导入模块: from pybrain.datasets import SequentialDataSet [as 别名]
# 或者: from pybrain.datasets.SequentialDataSet import newSequence [as 别名]
#.........这里部分代码省略.........
self.task.performAction(chosen)
reward = self.task.getReward()
rewards.append(reward)
return rewards
def learn(self, batches):
self.greedyAvg = []
self.rewardAvg = []
self.lengthAvg = []
self.initr0Avg = []
for b in range(batches):
if self.verbose:
print
print 'Batch', b + 1
self.reset()
self.learnOneBatch()
self.totalEpisodes += self.batchSize
# greedy measure (avg over some greedy runs)
rws = 0.
for dummy in range(self.greedyRuns):
tmp = self.greedyEpisode()
rws += (sum(tmp) / float(len(tmp)))
self.greedyAvg.append(rws / self.greedyRuns)
if self.verbose:
print '::', round(rws / self.greedyRuns, 5), '::'
def learnOneBatch(self):
# collect a batch of runs as experience
r0s = []
lens = []
avgReward = 0.
for dummy in range(self.batchSize):
self.rawDs.newSequence()
self.valueDs.newSequence()
self.task.reset()
self.net.reset()
acts, obss, rewards = [], [], []
while not self.task.isFinished():
obs = self.task.getObservation()
act = self.net.activate(obs)
chosen = drawIndex(act)
self.task.performAction(chosen)
reward = self.task.getReward()
obss.append(obs)
y = zeros(len(act))
y[chosen] = 1
acts.append(y)
rewards.append(reward)
avgReward += sum(rewards) / float(len(rewards))
# compute the returns from the list of rewards
current = 0
returns = []
for r in reversed(rewards):
current *= self.task.discount
current += r
returns.append(current)
returns.reverse()
for i in range(len(obss)):
self.rawDs.addSample(obss[i], acts[i], returns[i])
self.valueDs.addSample(obss[i], returns[i])
r0s.append(returns[0])
lens.append(len(returns))
r0s = array(r0s)
示例12: chunkifyDataWindowSequentialDS
# 需要导入模块: from pybrain.datasets import SequentialDataSet [as 别名]
# 或者: from pybrain.datasets.SequentialDataSet import newSequence [as 别名]
def chunkifyDataWindowSequentialDS(self, chunkLen, predGap, windowLen, includeDST=True):
nData = self.aceVals.shape[0]
# List of chunks of the form [([pDyn, Bz, DST], DST_future) .... chunkLen]
allChunks = []
currChunk = []
# Build out the allChunks
for i in range(nData):
if i + predGap >= nData or i - windowLen < 0:
continue
windowRec = np.ones(5*(windowLen+1))
for j in range(windowLen+1):
windowRec[5*j+0] = np.sqrt(self.aceVals[i-j,6])
windowRec[5*j+1] = self.aceVals[i-j,5]*self.aceVals[i-j,9]
windowRec[5*j+2] = self.aceVals[i-j,5]
windowRec[5*j+3] = self.aceVals[i-j,9]
windowRec[5*j+4] = self.dstVals[i-j,1]
rec = (windowRec, self.dstVals[i+predGap,1])
currChunk.append(rec)
if len(currChunk) == chunkLen:
allChunks.append(currChunk)
currChunk = []
# Take only certain chunks
chunks = []
for chunk in allChunks:
chunks.append(chunk)
# Uncomment for shuffled records
#random.shuffle(chunks)
# Normalize
norms = np.zeros(len(chunks[0][0][0]))
for c in chunks:
for rec in c:
windowRec = rec[0]
for k in range(len(windowRec)):
norms[k] = max(norms[k], abs(windowRec[k]))
for c in chunks:
for rec in c:
windowRec = rec[0]
for k in range(len(windowRec)):
windowRec[k] = windowRec[k] / norms[k]
# Build out raw arrays of data from the chunks
trainFrac = .8
testFrac = .2
trainInput = []
trainOutput = []
testInput = []
testOutput = []
for iChunk in range(len(chunks)):
# Add it to the training set
if iChunk / (float(len(chunks))) <= trainFrac:
for rec in chunks[iChunk]:
trainInput.append(rec[0])
trainOutput.append(rec[1])
# Add it to the testing set
else:
for rec in chunks[iChunk]:
testInput.append(rec[0])
testOutput.append(rec[1])
# Convert the lists to numpy arrays
trainInput = np.array(trainInput)
trainOutput = np.array(trainOutput).reshape(len(trainOutput),1)
testInput = np.array(testInput)
testOutput = np.array(testOutput).reshape(len(testOutput),1)
# Remove DST if it's not wanted
if not includeDST:
dstCol = 4 # This index, and the increment below, must be kept
# consistent if the record size changes
while dstCol < trainInput.shape[1]:
trainInput = np.delete(trainInput, dstCol, 1)
testInput = np.delete(testInput, dstCol, 1)
dstCol += 4
# Put the values in to dataset
nInputs = trainInput.shape[1]
trainDataset = SequentialDataSet(nInputs, 1)
for i in range(trainInput.shape[0]):
if i % chunkLen == 0 and i > 0:
trainDataset.newSequence()
trainDataset.addSample(trainInput[i,:], trainOutput[i])
#.........这里部分代码省略.........
示例13: trainNetwork
# 需要导入模块: from pybrain.datasets import SequentialDataSet [as 别名]
# 或者: from pybrain.datasets.SequentialDataSet import newSequence [as 别名]
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")
示例14: network_predict
# 需要导入模块: from pybrain.datasets import SequentialDataSet [as 别名]
# 或者: from pybrain.datasets.SequentialDataSet import newSequence [as 别名]
def network_predict(options_file_location,prediction_data_location,output_location,network_location):
prediction_data_file_handle = open(prediction_data_location,"r")
prediction_data_reader = csv.reader(prediction_data_file_handle)
stdout_file = output_location+'prediction_console_output.txt'
stderr_file = output_location+'prediction_console_errput.txt'
sys.stdout = open(stdout_file,"w")
sys.stderr = open(stderr_file,"w")
prediction_results_file_location = output_location+'prediction_results.csv'
prediction_results_file_handle = open(prediction_results_file_location,"w")
options_file_handle = open(options_file_location,'r')
options_dictionary = {}
for option in options_file_handle.readlines():
key,val = option.split('=')
print key
print val
options_dictionary[key] = val;
num_predictors = int(options_dictionary['num_predictors'])
num_outputs = int(options_dictionary['num_outputs'])
num_training_epochs = int(options_dictionary['num_training_epochs'])
num_hidden_neurons = int(options_dictionary['num_hidden_neurons'])
compound_prediction = int(options_dictionary['compound_prediction'])
# teacher_forced_transient = int(options_dictionary['teacher_forced_transient'])
teacher_forced_transient = 0
prediction_dataset = SequentialDataSet(num_predictors, num_outputs)
previous_sequence_number = 1
print 'reading in prediction data...'
for row in prediction_data_reader:
#convert list of strings to list of floats
list = [float(s) for s in row]
#split input line
predictors = list[0:num_predictors]
#+1 is to skip over the sequence column
outputs = list[num_predictors+1:num_predictors+1+num_outputs]
#convert from python list to numpy array
predictors = np.array(predictors)
outputs = np.array(outputs)
sequence_number = math.trunc(list[num_predictors])
if not sequence_number==previous_sequence_number:
# print 'sequence_number '+str(sequence_number)
# print 'previous_sequence_number '+str(previous_sequence_number)
# frame_number_debug = 0;
prediction_dataset.newSequence()
previous_sequence_number = sequence_number
#add to dataset
prediction_dataset.appendLinked(predictors, outputs)
network = NetworkReader.readFrom(network_location)
if compound_prediction==0:
results, targets, mse = evalRNN.evalRNNOnSeqDataset(network,prediction_dataset)
elif compound_prediction==1:
results, targets, mse = evalRNN.compoundEvalRNNOnSeqDataset(network,prediction_dataset, teacher_forced_transient)
results_length, results_width = results.shape
np.savetxt(prediction_results_file_location,results,delimiter=" ",fmt='%9.9f')
done_file_handle = open(output_location+'predicting_done.txt',"w")
done_file_handle.write('%s' % 'done!')
done_file_handle.close()
示例15: to_bitmask
# 需要导入模块: from pybrain.datasets import SequentialDataSet [as 别名]
# 或者: from pybrain.datasets.SequentialDataSet import newSequence [as 别名]
def to_bitmask(i): return i
files = glob.glob('reel_nopickup/*.csv') #16th notes
tunes = []
for filename in files: #tune
with open(filename) as f:
notes = map(int, map(str.strip, f.readlines()))[::2] #8th ntoes
bitmasks = map(to_bitmask, notes)
tunes.append(bitmasks)
nested_tunes = map(lambda tune: [tune[x:min(len(tune) + 1, x+4)] for x in range(0, len(tune), 4)], tunes)
for tune in nested_tunes:
for (inp, target) in zip(tune, tune[1:]):
chord_ds.newSequence()
chord_ds.appendLinked(inp[0], target[0])
for tune in tunes:
for beat in tunes:
for (inp, target) in zip(beat, beat[1:]):
melody_ds.newSequence()
melody_ds.appendLinked(inp, target)
chord_network.randomize()
melody_network.randomize()
chord_trainer = BackpropTrainer(chord_network, chord_ds, learningrate=.00001, momentum=.9)
melody_trainer = BackpropTrainer(melody_network, melody_ds, learningrate=.00001, momentum=.9)
import time
print "training chords..."