Ejemplo n.º 1
0
double conv(size_t i, Matrix *y, Matrix *w) {
    double acc = 0.0;
    for(size_t ni=0; ni<w->nrow; ni++) {
//        printf("%zu: ", ni);
        size_t wi=0;
        int yi=i;
        for(; (wi<w->ncol) && (yi>=0); yi--, wi++) {
//            printf("y:%d:%3.3f w:%d:%3.3f, ", yi, wi,  getMatrixElement(y, ni, yi), getMatrixElement(w, ni, wi));
            acc += getMatrixElement(y, ni, yi) * getMatrixElement(w, ni, wi);
        }
//        printf("\n");
    }
    return acc;
}
Ejemplo n.º 2
0
void fillSparseMatrix(SparseMatrix *sm, Matrix *m, size_t col_offset) {
    for(size_t i = 0; i < m->nrow; i++ ) { 
        for(size_t j = 0; j < m->ncol; j++) {
            setSparseMatrixElement(sm, i, j+col_offset, getMatrixElement(m, i, j) );            
        }
    }
}
Ejemplo n.º 3
0
void writeMatrix(const char * const fileName, const Matrix * const M)
{
  int i, j, rows, cols;
  FILE * outFile;
  double value;

	outFile = fopen(fileName, "w");
  if(NULL == outFile)
  {
    changeColor("magenta");
    printf("=[ I can't find %s\n", fileName);
    return;
  }

	fprintf(outFile, "%i %i\n", M->rowSize, M->colSize);

  for(i = 0; i < M->rowSize; i++)
  {
    for(j = 0; j < M->colSize; j++)
    {
      fprintf(outFile, "%lg", getMatrixElement(M, i, j));
      if(j < M->colSize - 1)
        fprintf(outFile, " ");
    }
    fprintf(outFile, "\n");
  }

	fclose(outFile);
}
Ejemplo n.º 4
0
Matrix* calcErrorGrad(Matrix *y, Matrix *w, doubleVector *target, int jobs) {
    assert(y->ncol == target->size);

    pthread_t *threads = (pthread_t *) malloc( jobs * sizeof( pthread_t ) );
    FiltWorker *workers = (FiltWorker*) malloc( jobs * sizeof(FiltWorker) );
    
    for(size_t wi=0; wi < jobs; wi++) {
        int points_per_thread = (y->ncol + jobs - 1) / jobs;
        workers[wi].first = min( wi    * points_per_thread, y->ncol );
        workers[wi].last  = min( (wi+1) * points_per_thread, y->ncol );
        workers[wi].y = y;
        workers[wi].w = w;
        workers[wi].target = target;
    }
    for( int i = 0; i < jobs; i++ )  {
        P( pthread_create( &threads[i], NULL, error_grad_routine,  &workers[i]) );
    }
    for( int i = 0; i < jobs; i++ )  {
        P( pthread_join( threads[i], NULL) );
    }

    Matrix *dedw = createZeroMatrix(w->nrow, w->ncol);
    for( size_t wi=0; wi< jobs; wi++) {
        for(size_t i=0; i<dedw->nrow; i++) {
            for(size_t j=0; j<dedw->ncol; j++) {
                incMatrixElement(dedw, i, j, getMatrixElement(workers[wi].dedw, i, j));
            }
        }
    }
    for(size_t i=0; i<dedw->nrow; i++) {
        for(size_t j=0; j<dedw->ncol; j++) {
            double dedw_acc = getMatrixElement(dedw, i, j);
            setMatrixElement(dedw, i, j, dedw_acc/target->size);
        }
    }

    free(threads);
    free(workers);

    return dedw;
}
Ejemplo n.º 5
0
static int readFile(FILE *file, pmatrix_t *matrix, index_t rows, index_t columns) {
	index_t x, y;
	initMatrix(matrix, rows, columns);
	if (matrix == NULL)
		return ERRORCODE_CANT_MALLOC;
	for(y = 0; y < rows; y++) {
		for(x = 0; x < columns; x++)
			if (fscanf(file, UNIT_SPECIFIER, getMatrixElement(*matrix, y, x)) != 1)
				return ERRORCODE_PARSER_ERROR; 
	}
	return ERRORCODE_NOERRORS;
}
Ejemplo n.º 6
0
void *error_grad_routine(void *args) {
    FiltWorker *fw = (FiltWorker*)args;
    fw->dedw = createZeroMatrix(fw->w->nrow, fw->w->ncol);

    for(size_t i=fw->first; i<fw->last; i++) {
        double gr_val = -2*(fw->target->array[i] - conv(i, fw->y, fw->w));
        for(size_t wi=0; wi < fw->dedw->nrow; wi++) {
            int yi=i;
            size_t wj=0;
            for(; (wj < fw->dedw->ncol) && (yi>=0); wj++, yi--) {
                incMatrixElement(fw->dedw, wi, wj, gr_val * getMatrixElement(fw->y, wi, yi));
            }
        }
    }
    return(NULL);
}
Ejemplo n.º 7
0
void matrixToEulerZXY(const float matrix[3*3],
                      float &thetaX, float &thetaY, float& thetaZ)
{
    thetaX = asin( getMatrixElement(matrix, 2, 1));
    if ( thetaX < M_PI_2 ) {
        if ( thetaX > -M_PI_2 ) {
            thetaZ = atan2( -getMatrixElement(matrix, 0, 1),
                             getMatrixElement(matrix, 1, 1) );
            thetaY = atan2( -getMatrixElement(matrix, 2, 0),
                             getMatrixElement(matrix, 2, 2) );
        } else {
            // Not a unique solution
            thetaZ = -atan2( getMatrixElement(matrix, 0, 2),
                             getMatrixElement(matrix, 0, 0) );
            thetaY = 0.0;
        }
    } else {
        // Not a unique solution
        thetaZ = atan2( getMatrixElement(matrix, 0, 2),
                        getMatrixElement(matrix, 0, 0) );
        thetaY = 0.0;
    }
}
Ejemplo n.º 8
0
Matrix* processThroughReceptiveFields(Matrix *ts, const double *centers, const Constants *c) {
    Matrix *ts_out = createMatrix(c->M, ts->ncol);
//    printf("%d %d %d", c->M, ts->nrow, c->M % ts->nrow);
    assert(c->M % ts->nrow == 0);
    
    size_t neurons_per_ts = c->M / ts->nrow;
    size_t i, ri;
    for(i=0, ri=0; (i < c->M) && (ri < ts->nrow); i+=neurons_per_ts, ri++) {
        for(size_t ni=i; ni<(i+neurons_per_ts); ni++) {
            for(size_t vi=0; vi < ts->ncol; vi++) {
                double val = getMatrixElement(ts, ri, vi);                
                double val_field = c->preproc->gain * exp( - ((centers[ni] - val) * (centers[ni] - val))/c->preproc->sigma );
                setMatrixElement(ts_out, ni, vi, val_field);
            }
        }
    }
    return(ts_out);
}
Ejemplo n.º 9
0
Matrix *runLbfgsOptim(Matrix *y, doubleVector *target, int L, int jobs, double epsilon) {
    int M = y->nrow;
    Matrix *w = createZeroMatrix(M, L);
    
    lbfgsfloatval_t fx;
    lbfgsfloatval_t *x = lbfgs_malloc(w->nrow*w->ncol);
    lbfgs_parameter_t param;

    if (x == NULL) {
        printf("ERROR: Failed to allocate a memory block for variables.\n");
        exit(1);
    }

    /* Initialize the variables. */
    for (size_t i = 0; i < w->nrow; i++) {
        for (size_t j = 0; j < w->ncol;j++) {
            x[j*w->nrow + i] = getMatrixElement(w, i, j);
        }    
    }
    lbfgs_parameter_init(&param);
    param.epsilon = epsilon;
    param.m = 20;

    LbfgsInput inp;
    inp.target = target;
    inp.y = y;
    inp.jobs = jobs;
    inp.L = w->ncol;
    int ret = lbfgs(w->nrow*w->ncol, x, &fx, evaluate, progress, (void*)&inp, &param);

    printf("L-BFGS optimization terminated with status code = %d\n", ret);
    printf("  fx = %f\n", fx);
    Matrix *w_opt = formMatrixFromFloatVal(x, w->nrow, w->ncol);
    
    lbfgs_free(x);

    return(w_opt);
}
Ejemplo n.º 10
0
pMatrixVector* processTimeSeriesSet(pMatrixVector *v, const Constants *c) {
    double area_max = DBL_MIN;
    double area_min = DBL_MAX;
    for(size_t i=0; i < v->size; i++) {    
        Matrix *ts = v->array[i];
        for(size_t ts_i=0; ts_i < ts->nrow; ts_i++) {
            for(size_t vi=0; vi < ts->ncol; vi++) {
                double val = getMatrixElement(ts, ts_i, vi);
                if(val > area_max) area_max = val;
                if(val < area_min) area_min = val;
            }
        }
    }
    double centers[c->M];
    for(size_t ni=0; ni<c->M; ni++) {
        centers[ni] = area_min + (ni+0.5)*(area_max-area_min)/c->M;
    }

    pMatrixVector *out = TEMPLATE(createVector,pMatrix)();
    for(size_t i=0; i<v->size; i++) {
        TEMPLATE(insertVector,pMatrix)(out, processThroughReceptiveFields(v->array[i], centers, c));
    }
    return(out);
}
Ejemplo n.º 11
0
int main(int argc, char **argv) {
    ArgOptionsPrep a = parsePrepOptions(argc, argv);
    Constants *c = createConstants(a.const_filename);

    
    bool saveStat = false;
    if(a.stat_file) {
        saveStat = true;
    }

    pMatrixVector* ts_data = readMatrixListFromFile(a.input_file);
    pMatrixVector* ts_labels = readMatrixListFromFile(a.input_labels_file);
    assert(ts_labels->size == 1);
    assert(ts_labels->array[0]->nrow == ts_data->size);
    
    pMatrixVector *out_data = TEMPLATE(createVector,pMatrix)();
    assert(ts_data->size > 0);

    pMatrixVector *ts_data_pr = processTimeSeriesSet(ts_data, c);    
    indVector *ts_indices = TEMPLATE(createVector,ind)();
    for(size_t ti=0; ti< ts_data_pr->size; ti++) {
        TEMPLATE(insertVector,ind)(ts_indices, ti);    
    }
    srand(time(NULL));
    shuffleIndVector(ts_indices);
    
    SpikesList *net = createSpikesList(c->M);
    doubleVector *ts_labels_current = TEMPLATE(createVector,double)();
    double t = 0;
    doubleVector *timeline = TEMPLATE(createVector,double)();
    AdExLayer *l = createAdExLayer(c->M, saveStat);
    for(size_t ts_i=0; ts_i < ts_indices->size; ts_i++) {
        Matrix *ts = ts_data_pr->array[ ts_indices->array[ts_i] ];
        size_t nsamples = ts->ncol;
        toStartValuesAdExLayer(l, c);
        size_t j;
        for(j = 0; j < nsamples; t+= c->preproc->dt, j++) {
            for(size_t ni=0; ni < l->N; ni++) {
                double I = getMatrixElement(ts, ni, j);
                propagateCurrentAdExLayer(l, &ni, &I);
                simulateAdExLayerNeuron(l, &ni, c);
                if(l->fired[ni] == 1) {
                    TEMPLATE(insertVector,double)(net->list[ni], c->preproc->mult*t);
                    l->fired[ni] = 0;
                }
            }
        }
        t += 250/c->preproc->mult;
        TEMPLATE(insertVector,double)(timeline, c->preproc->mult*t);
        TEMPLATE(insertVector,double)(ts_labels_current, getMatrixElement(ts_labels->array[0], ts_indices->array[ts_i], 0) );
    }
    Matrix *spikes = vectorArrayToMatrix(net->list, net->size);
    TEMPLATE(insertVector,pMatrix)(out_data, spikes);    
    
    Matrix *timeline_m = vectorArrayToMatrix(&timeline, 1);
    transposeMatrix(timeline_m);
    TEMPLATE(insertVector,pMatrix)(out_data, timeline_m);    
    
    Matrix *classes = vectorArrayToMatrix(&ts_labels_current, 1);
    transposeMatrix(classes);
    TEMPLATE(insertVector,pMatrix)(out_data, classes);    

    saveMatrixListToFile(a.output_file, out_data);

    if(l->saveStat) {
        pMatrixVector *stat_data = TEMPLATE(createVector,pMatrix)();
        for(size_t i=0; i<ts_data_pr->size; i++) {
            TEMPLATE(insertVector,pMatrix)(stat_data, copyMatrix(ts_data_pr->array[i]));
        }        
        Matrix *Vs = vectorArrayToMatrix(l->stat_V, l->N);
        TEMPLATE(insertVector,pMatrix)(stat_data, Vs);
        Matrix *ws = vectorArrayToMatrix(l->stat_w, l->N);
        TEMPLATE(insertVector,pMatrix)(stat_data, ws);


        saveMatrixListToFile(a.stat_file, stat_data);

        TEMPLATE(deleteVector,pMatrix)(stat_data);
    }

    TEMPLATE(deleteVector,double)(timeline);
    deleteAdExLayer(l);
    deleteSpikesList(net);
    deleteConstants(c);
    TEMPLATE(deleteVector,pMatrix)(out_data);
    TEMPLATE(deleteVector,pMatrix)(ts_data_pr);
    TEMPLATE(deleteVector,pMatrix)(ts_data);
    return(0);
}
Ejemplo n.º 12
0
inline void setInMatrix(pmatrix_t matrix, index_t rows, index_t columns, unit_t value) {
	*getMatrixElement(matrix, rows, columns) = value;
	return;
}
Ejemplo n.º 13
0
inline unit_t getFromMatrix(pmatrix_t matrix, index_t rows, index_t columns) {
	return *getMatrixElement(matrix, rows, columns);
}