def test_conv(N=1000, M=10): #test causal convolution delays = np.arange(0, 20, 1, dtype='int') D = len(delays) A = np.random.randn(N, M) w = np.random.randn(M, D) yslow = slow_conv(A, w, delays) #print yslow yfast = fast_conv(A, w, delays) #print yfast ydiff = yslow - yfast #print list(ydiff) total_diff = np.abs(ydiff).sum() print 'total diff for causal:%f' % total_diff assert total_diff < 1e-8 #test acausal convolution delays = np.arange(-10, 11, 1, dtype='int') D = len(delays) A = np.random.randn(N, M) w = np.random.randn(M, D) yslow = slow_conv(A, w, delays) #print yslow yfast = fast_conv(A, w, delays) #print yfast ydiff = yslow - yfast #print list(ydiff) total_diff = np.abs(ydiff).sum() print 'total diff for acausal:%f' % total_diff assert total_diff < 1e-8 #test non-contiguous convolution delays = np.arange(-20, 22, 2, dtype='int') D = len(delays) A = np.random.randn(N, M) w = np.random.randn(M, D) yslow = slow_conv(A, w, delays) #print yslow yfast = fast_conv(A, w, delays) #print yfast ydiff = yslow - yfast #print list(ydiff) total_diff = np.abs(ydiff).sum() print 'total diff for non-contiguous:%f' % total_diff assert total_diff < 1e-8
def error_vector(self, x): bias = x[-1] filt = self.get_filter(x[:-1]) if self.group_index is None: yhat = fast_conv(self.input, filt, self.time_lags, bias=bias) else: yhat = fast_conv_grouped(self.input, filt, self.time_lags, group_index=self.group_index, bias=bias) return self.target_val - yhat
def test_convolutional_model(self): nchan = 3 nt = 1000 nlags = 4 X = np.random.randn(nt, nchan) filt = np.random.randn(nchan, nlags) bias = -1.5 lags = range(nlags) y = fast_conv(X, filt, lags, bias) m = ConvolutionalLinearModel(X, y, lags=lags) yhat = m.forward(filt) + bias assert np.abs(yhat - y).sum() < 1e-6 params = np.zeros([nchan * nlags + 1]) params[:-1] = filt.ravel() params[-1] = bias assert m.error_vector(params).sum() < 1e-12 #test the gradient params = np.random.randn(nchan * nlags + 1) g = m.grad(params) gfd = finite_diff_grad(m.error, params) gdiff = (g - gfd) / np.sqrt((gfd**2).sum()) assert np.abs(gdiff).sum() < 1e-3 #do some gradient descent m.params = np.zeros([nchan * nlags + 1]) tgd = ThresholdGradientDescent(m, step_size=1e-3, threshold=0.0, slope_threshold=-1e-9) niter = 5000 while not tgd.converged and tgd.iter < niter: tgd.iterate() print 'iter %d, err=%f' % (tgd.iter, tgd.errors[-1]) found_bias = tgd.best_params[-1] found_filt = m.get_filter(tgd.best_params[:-1]) filt_diff = (filt - found_filt) print 'filt=', filt print 'found_filt=', found_filt print 'filt_diff=', filt_diff assert np.abs(filt_diff).sum() < 1e-3 print 'bias=', bias print 'found_bias=', found_bias print 'bias_diff=', bias - found_bias assert np.abs(bias - found_bias) < 1e-3
def test_convolutional_model(self): nchan = 3 nt = 1000 nlags = 4 X = np.random.randn(nt, nchan) filt = np.random.randn(nchan, nlags) bias = -1.5 lags = range(nlags) y = fast_conv(X, filt, lags, bias) m = ConvolutionalLinearModel(X, y, lags=lags) yhat = m.forward(filt) + bias assert np.abs(yhat - y).sum() < 1e-6 params = np.zeros([nchan*nlags + 1]) params[:-1] = filt.ravel() params[-1] = bias assert m.error_vector(params).sum() < 1e-12 #test the gradient params = np.random.randn(nchan*nlags + 1) g = m.grad(params) gfd = finite_diff_grad(m.error, params) gdiff = (g - gfd) / np.sqrt( (gfd**2).sum() ) assert np.abs(gdiff).sum() < 1e-3 #do some gradient descent m.params = np.zeros([nchan*nlags + 1]) tgd = ThresholdGradientDescent(m, step_size=1e-3, threshold=0.0, slope_threshold=-1e-9) niter = 5000 while not tgd.converged and tgd.iter < niter: tgd.iterate() print 'iter %d, err=%f' % (tgd.iter, tgd.errors[-1]) found_bias = tgd.best_params[-1] found_filt = m.get_filter(tgd.best_params[:-1]) filt_diff = (filt-found_filt) print 'filt=',filt print 'found_filt=',found_filt print 'filt_diff=',filt_diff assert np.abs(filt_diff).sum() < 1e-3 print 'bias=',bias print 'found_bias=',found_bias print 'bias_diff=',bias-found_bias assert np.abs(bias-found_bias) < 1e-3
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)
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)