コード例 #1
0
ファイル: test.py プロジェクト: Piyush3dB/lstm
def example_0():
    # learns to repeat simple sequence from random inputs
    np.random.seed(3)

    # Number of iterations or epochs
    nEpochs = 100;

    # Internal cell widths
    cellWidth = 100
    
    # Number of random input numbers for each output
    xSize = 50

    ## Initialise parameters
    # Containg weights and derivatives of loss function wrt weights)
    weights = LstmWeights(cellWidth, xSize)
    
    ## Prepare target outputs
    outData  = [0.5, 0.2, 0.1, 0.5]
    
    # number of unfolded cells
    nCells   = len(outData)

    # Initialise LSTM 
    trainLSTM = LstmNetwork(weights, nCells, cellWidth, xSize)

    # Input data
    inData = np.random.random([nCells, xSize]) # [4, 50]
    
    # Train and sample at the same time
    for epoch in range(nEpochs):

        #
        # Train model
        #

        # Input data and propagate forwards through time
        trainLSTM.fwdProp(inData, weights)

        # Evaluate loss function and back propagate through time
        loss, grads = trainLSTM.bptt(outData, ToyLossLayer, weights)

        # Clear inputs to start afresh for next epoch
        trainLSTM.gotoStartCell()

        # Collect the gradients


        # Apply weight update
        weights.update(grads, lr=0.1)
        #weights.weightUpdate(lr=0.1)


        #
        # Test model and print logging information
        #

        # Sample from new model configured with the trained weights
        testLSTM = LstmNetwork(weights, nCells, cellWidth, xSize)
        testLSTM.fwdProp(inData, weights)
        state = testLSTM.sample()

        #pdb.set_trace()

        # Print logging information
        for ind in range(nCells):
            print "  Input %d rand.  Target = %1.3f. Output = %1.3f. Delta = %1.3f" % (xSize, float(outData[ind]), float(state[ind]), outData[ind]-state[ind])
        print "Epoch: %3d. loss: %5.10f\n" % (epoch, loss)