def buildDS(n, num, dur): ds = SequentialDataSet(1,1) dt = n['gfnn'].dt t = np.arange(0, dur, dt) length = len(t) for i in range(num): x = np.zeros(length) # tempo between 1 and 2 bps bps = 1 + np.random.random() p = 1./bps lastPulse = np.random.random() * p for j in range(length): if t[j] > lastPulse and t[j] >= lastPulse + p: x[j] = 0.1 lastPulse = t[j] else: if j > 0: if x[j-1] < 1e-5: x[j] = 0 else: x[j] = x[j-1] * 0.5 # try to predict the next sample target = np.roll(x, -1) ds.newSequence() for j in range(length): ds.addSample(x[j], target[j]) return ds
def generateSuperimposedSineData( sinefreqs, space, yScales=None ): sine = SuperimposedSine( sinefreqs ) if yScales is not None: sine.yScales = array(yScales) dataset = SequentialDataSet(0,1) data = sine.getFuncValues(space) dataset.newSequence() for i in range(len(data)): dataset.addSample([], data[i]) return dataset
def generateSuperimposedSineData(sinefreqs, space, yScales=None): sine = SuperimposedSine(sinefreqs) if yScales is not None: sine.yScales = array(yScales) dataset = SequentialDataSet(0, 1) data = sine.getFuncValues(space) dataset.newSequence() for i in xrange(len(data)): dataset.addSample([], data[i]) return dataset
def makeMelodyDataSet(melodies, inspirationFunc=randomInspiration, inspirationLength=8): seqDataSet = SequentialDataSet(sampleSize(), outputSize()) for m in melodies: barCount = m.bars[-1]+1 assert barCount <= 8, "Bar counts greater than 8 unsupported" inspiration = inspirationFunc(inspirationLength,m) seqDataSet.newSequence() for s in range(len(m.pitches)-1): seqDataSet.addSample( makeNoteSample(m.pitches[s], m.durations[s], inspiration[s % inspirationLength], m.bars[s]), makeNoteTarget(m.pitches[s+1], m.durations[s+1])) return seqDataSet
def list_to_dataset(inputs, outputs, dataset=None): """List to Dataset Convert a standard list to a dataset. The list must be given in the following format: Inputs: 2 dimension list (N x M) Outputs: 2 dimension list (N x K) N: Number of time steps in data series M: Number of inputs per time step K: Number of outputs per time step Arguments: inputs: The input list given under the above conditions. outputs: The output list given under the above conditions. dataset: A SequentialDataSet object to add a new sequence. New dataset generated if None. (Default: None) Returns: A SequentialDataSet object built from the retrieved input/output data. """ assert len(inputs) > 0 assert len(outputs) > 0 assert len(inputs) == len(outputs) # The dataset object has not been initialized. We must determine the # input and output size based on the unpacked data num_samples = len(inputs) in_dim = 1 if len(inputs.shape) == 1 else inputs.shape[1] out_dim = 1 if len(outputs.shape) == 1 else outputs.shape[1] # If the dataset does not exist, create it. Otherwise, use the dataset # given if not dataset: dataset = SequentialDataSet(in_dim, out_dim) # Make a new sequence for the given input/output pair dataset.newSequence() for i in range(num_samples): dataset.addSample(inputs[i], outputs[i]) return dataset
def list_to_dataset(inputs, outputs, dataset=None): """List to Dataset Convert a standard list to a dataset. The list must be given in the following format: Inputs: 2 dimension list (N x M) Outputs: 2 dimension list (N x K) N: Number of time steps in data series M: Number of inputs per time step K: Number of outputs per time step Arguments: inputs: The input list given under the above conditions. outputs: The output list given under the above conditions. dataset: A SequentialDataSet object to add a new sequence. New dataset generated if None. (Default: None) Returns: A SequentialDataSet object built from the retrieved input/output data. """ assert len(inputs) > 0 assert len(outputs) > 0 assert len(inputs) == len(outputs) # The dataset object has not been initialized. We must determine the # input and output size based on the unpacked data num_samples = len(inputs) in_dim = 1 if len(inputs.shape) == 1 else inputs.shape[1] out_dim = 1 if len(outputs.shape) == 1 else outputs.shape[1] # If the dataset does not exist, create it. Otherwise, use the dataset # given if not dataset: dataset = SequentialDataSet(in_dim, out_dim) # Make a new sequence for the given input/output pair dataset.newSequence() for i in range(num_samples): dataset.addSample(inputs[i], outputs[i]) return dataset
rnn.addOutputModule(SoftmaxLayer(dim=outputSize, name='out')) rnn.addConnection(FullConnection(rnn['in'], rnn['in_proc'], name='c1')) rnn.addConnection(FullConnection(rnn['in_proc'], rnn['hidden'], name='c2')) rnn.addRecurrentConnection( FullConnection(rnn['hidden'], rnn['hidden'], name='c3')) rnn.addConnection(FullConnection(rnn['hidden'], rnn['out_proc'], name='c4')) rnn.addConnection(FullConnection(rnn['out_proc'], rnn['out'], name='c5')) rnn.sortModules() # Construct dataset trainingData = SequentialDataSet(inputSize, outputSize) for index, row in df.iterrows(): trainingData.newSequence() inputSequence = list((row.values)[0]) outputVector = [0, 0, 0, 0] if index == 'A': outputVector[0] = 1 if index == 'B': outputVector[1] = 1 if index == 'C': outputVector[2] = 1 if index == 'D': outputVector[3] = 1
rnn.addModule(TanhLayer(dim=hiddenSize, name = 'out_proc')) rnn.addOutputModule(SoftmaxLayer(dim=outputSize, name='out')) rnn.addConnection(FullConnection(rnn['in'], rnn['in_proc'], name='c1')) rnn.addConnection(FullConnection(rnn['in_proc'], rnn['hidden'], name='c2')) rnn.addRecurrentConnection(FullConnection(rnn['hidden'], rnn['hidden'], name='c3')) rnn.addConnection(FullConnection(rnn['hidden'], rnn['out_proc'], name='c4')) rnn.addConnection(FullConnection(rnn['out_proc'], rnn['out'], name='c5')) rnn.sortModules() # Construct dataset trainingData = SequentialDataSet(inputSize, outputSize) for index, row in df.iterrows(): trainingData.newSequence() inputSequence = list((row.values)[0]) outputVector = [0, 0, 0, 0] if index == 'A': outputVector[0] = 1 if index == 'B': outputVector[1] = 1 if index == 'C': outputVector[2] = 1 if index == 'D': outputVector[3] = 1
# plt.show() # i1 = np.sin(np.arange(0, 20)) # i2 = np.sin(np.arange(0, 20)) * 2 # # t1 = np.ones([1, 20]) # t2 = np.ones([1, 20]) * 2 # # input = np.array([i1, i2, i1, i2]).reshape(20 * 4, 1) # target = np.array([t1, t2, t1, t2]).reshape(20 * 4, 1) # Create datasets print 'Preparing dataset ...' # ts = sampler.load_csv('data/series.csv') ds = SequentialDataSet(inLayerCount, outLayerCount) ds.newSequence() # ds = SupervisedDataSet(inLayerCount, outLayerCount) for row in df.itertuples(index=False): ds.addSample(row[0:columns-2], row[columns-2]) ds.endOfData() # Create bp trainer trainer = BackpropTrainer(net, ds) # Trains the datasets print 'Training ...' epoch = 1000 error = 1.0 while error > delta_error and epoch >= 0:
b = BiasUnit('bias') net.addModule(b) net.addOutputModule(o) net.addInputModule(i) net.addModule(h) net.addConnection(FullConnection(i, h, outSliceTo = 4*dim, name = 'f1')) net.addConnection(FullConnection(b, h, outSliceTo = 4*dim, name = 'f2')) net.addRecurrentConnection(FullConnection(h, h, inSliceTo = dim, outSliceTo = 4*dim, name = 'r1')) net.addRecurrentConnection(IdentityConnection(h, h, inSliceFrom = dim, outSliceFrom = 4*dim, name = 'rstate')) net.addConnection(FullConnection(h, o, inSliceTo = dim, name = 'f3')) net.sortModules() print net ds = SequentialDataSet(15, 1) ds.newSequence() input = open(sys.argv[1], 'r') for line in input.readlines(): row = np.array(line.split(',')) ds.addSample([float(x) for x in row[:15]], float(row[16])) print ds if len(sys.argv) > 2: test = SequentialDataSet(15, 1) test.newSequence() input = open(sys.argv[2], 'r') for line in input.readlines(): row = np.array(line.split(',')) test.addSample([float(x) for x in row[:15]], float(row[16])) else:
def makeMelodyDataSet(melodies): seqDataSet = SequentialDataSet(sampleSize(), outputSize()) for m in melodies: seqDataSet.newSequence() m.addSamples(seqDataSet) return seqDataSet