예제 #1
0
 def __init__(self,
              input,
              target,
              ndelays=None,
              bias=0.0,
              group_index=None,
              lags=None):
     ConvolutionalInCrowdModel.__init__(self, input, target, ndelays, bias,
                                        group_index, lags)
예제 #2
0
def test1():
    #construct sample input matrix
    num_channels = 60
    num_timepoints = 5000
    stim = np.random.randn(num_channels, num_timepoints)

    #normalize stimulus so it's between -1 and 1
    stim /= np.abs(stim).max()

    #transpose matrix, incrowd expects matrix to be # of time points X # of channels
    stim = stim.transpose()

    #construct sample filter
    time_lags = np.arange(0, 8, 1, dtype='int')
    real_filter = np.random.randn(num_channels, len(time_lags))
    #make filter sparse
    real_filter[np.abs(real_filter) < 0.90] = 0.0
    #normalize filter
    real_filter /= np.abs(real_filter).max()

    num_nonzero = (np.abs(real_filter) == 0.0).sum()
    print '# of nonzero elements in filter: %d out of %d' % (
        num_nonzero, len(time_lags) * num_channels)

    #construct sample output using convolution
    output = fast_conv(stim, real_filter, time_lags)

    #add random noise to output
    output += np.random.randn(len(output)) * 1e-6

    #create a convolutional incrowd model
    cic_model = ConvolutionalInCrowdModel(stim,
                                          output,
                                          lags=time_lags,
                                          bias=0.0)

    #create incrowd optimizer, using Lasso+Elastic net for the interior solver,
    #lambda1 is the constant for the Lasso regularization
    #lambda2 is the constant for the Elastic Net regularization
    #threshold is the fraction of parameters that are introduced into the active set at each iteration
    ico = InCrowd(cic_model,
                  solver_params={
                      'lambda1': 1.0,
                      'lambda2': 1.0
                  },
                  max_additions_fraction=0.25)

    #run the optimization
    num_iters = 15
    for k in range(num_iters):
        if ico.converged:
            break
        ico.iterate()
        print 'Iteration %d, err=%0.9f' % (k + 1, (cic_model.residual(ico.x)**
                                                   2).sum())

    #get the predicted filter, make cic_model reshape the parameters into what we would expect to see
    predicted_filter = cic_model.get_filter(ico.x)
    #predicted_output = fast_conv(stim, predicted_filter, time_lags)
    predicted_output = cic_model.forward(ico.x)
    filter_diff = real_filter - predicted_filter

    plt.figure()
    ax1 = plt.subplot2grid((2, 3), (0, 0))
    plt.imshow(real_filter, aspect='auto', interpolation='nearest')
    plt.colorbar()
    plt.title('Actual Filter')

    ax2 = plt.subplot2grid((2, 3), (0, 1))
    plt.imshow(predicted_filter, aspect='auto', interpolation='nearest')
    plt.colorbar()
    plt.title('Predicted Filter')

    ax3 = plt.subplot2grid((2, 3), (0, 2))
    plt.imshow(filter_diff, aspect='auto', interpolation='nearest')
    plt.colorbar()
    plt.title('Differences')

    ax4 = plt.subplot2grid((2, 3), (1, 0), colspan=3)
    plt.plot(output, 'k-', linewidth=2.0)
    plt.plot(predicted_output, 'r-', linewidth=2.0)
예제 #3
0
def test1():
    #construct sample input matrix
    num_channels = 60
    num_timepoints = 5000
    stim = np.random.randn(num_channels, num_timepoints)

    #normalize stimulus so it's between -1 and 1
    stim /= np.abs(stim).max()

    #transpose matrix, incrowd expects matrix to be # of time points X # of channels
    stim = stim.transpose()

    #construct sample filter
    time_lags = np.arange(0, 8, 1, dtype='int')
    real_filter = np.random.randn(num_channels, len(time_lags))
    #make filter sparse
    real_filter[np.abs(real_filter) < 0.90] = 0.0
    #normalize filter
    real_filter /= np.abs(real_filter).max()

    num_nonzero = (np.abs(real_filter) == 0.0).sum()
    print '# of nonzero elements in filter: %d out of %d' % (num_nonzero, len(time_lags)*num_channels)

    #construct sample output using convolution
    output = fast_conv(stim, real_filter, time_lags)

    #add random noise to output
    output += np.random.randn(len(output))*1e-6

    #create a convolutional incrowd model
    cic_model = ConvolutionalInCrowdModel(stim, output, lags=time_lags, bias=0.0)

    #create incrowd optimizer, using Lasso+Elastic net for the interior solver,
    #lambda1 is the constant for the Lasso regularization
    #lambda2 is the constant for the Elastic Net regularization
    #threshold is the fraction of parameters that are introduced into the active set at each iteration
    ico = InCrowd(cic_model, solver_params={'lambda1':1.0, 'lambda2':1.0}, max_additions_fraction=0.25)

    #run the optimization
    num_iters = 15
    for k in range(num_iters):
        if ico.converged:
            break
        ico.iterate()
        print 'Iteration %d, err=%0.9f' % (k+1, (cic_model.residual(ico.x)**2).sum())

    #get the predicted filter, make cic_model reshape the parameters into what we would expect to see
    predicted_filter = cic_model.get_filter(ico.x)
    #predicted_output = fast_conv(stim, predicted_filter, time_lags)
    predicted_output = cic_model.forward(ico.x)
    filter_diff = real_filter - predicted_filter

    plt.figure()
    ax1 = plt.subplot2grid((2, 3), (0, 0))
    plt.imshow(real_filter, aspect='auto', interpolation='nearest')
    plt.colorbar()
    plt.title('Actual Filter')

    ax2 = plt.subplot2grid((2, 3), (0, 1))
    plt.imshow(predicted_filter, aspect='auto', interpolation='nearest')
    plt.colorbar()
    plt.title('Predicted Filter')

    ax3 = plt.subplot2grid((2, 3), (0, 2))
    plt.imshow(filter_diff, aspect='auto', interpolation='nearest')
    plt.colorbar()
    plt.title('Differences')

    ax4 = plt.subplot2grid((2, 3), (1, 0), colspan=3)
    plt.plot(output, 'k-', linewidth=2.0)
    plt.plot(predicted_output, 'r-', linewidth=2.0)
예제 #4
0
 def __init__(self, input, target, ndelays=None, bias=0.0, group_index=None, lags=None):
     ConvolutionalInCrowdModel.__init__(self, input, target, ndelays, bias, group_index, lags)