Пример #1
0
static void compute_kl_divergence_of_rnn_state (
        const struct rnn_state *rnn_s,
        int truncate_length,
        int block_length,
        int divide_num,
        double *kl_div,
        double *entropy_t,
        double *entropy_o,
        double *gen_rate)
{
    if (rnn_s->length > truncate_length) {
        double min, max;
        int **sequence_t, **sequence_o;
        struct block_frequency bf_t, bf_o;
        const int length = rnn_s->length - truncate_length;
        if (rnn_s->rnn_p->output_type == STANDARD_TYPE) {
            min = -1.0; max = 1.0;
        } else {
            min = 0.0; max = 1.0;
        }
        MALLOC2(sequence_t, length, rnn_s->rnn_p->out_state_size);
        MALLOC2(sequence_o, length, rnn_s->rnn_p->out_state_size);
        for (int n = 0; n < length; n++) {
            int N = n + truncate_length;
            for (int i = 0; i < rnn_s->rnn_p->out_state_size; i++) {
                sequence_t[n][i] = f2symbol(rnn_s->teach_state[N][i],
                    min, max, divide_num);
                sequence_o[n][i] = f2symbol(rnn_s->out_state[N][i], min,
                        max, divide_num);
            }
        }
        init_block_frequency(&bf_t, (const int* const*)sequence_t,
                rnn_s->rnn_p->out_state_size, length, block_length);
        init_block_frequency(&bf_o, (const int* const*)sequence_o,
                rnn_s->rnn_p->out_state_size, length, block_length);
        *kl_div = kullback_leibler_divergence(&bf_t, &bf_o);
        *entropy_t = block_entropy(&bf_t) / block_length;
        *entropy_o = block_entropy(&bf_o) / block_length;
        *gen_rate = generation_rate(&bf_t, &bf_o);
        free_block_frequency(&bf_t);
        free_block_frequency(&bf_o);
        FREE2(sequence_t);
        FREE2(sequence_o);
    } else {
        *kl_div = 0;
        *entropy_t = 0;
        *entropy_o = 0;
        *gen_rate = 0;
    }
}
Пример #2
0
static void test_generation_rate (void)
{
    int dimension, length, max_block_length;
    struct block_frequency bf_x, bf_y;
    int **x, **y;
    double gen_rate;

    dimension = 4;
    length = 1024;
    max_block_length = 5;

    MALLOC2(x, length, dimension);
    MALLOC2(y, length, dimension);

    int *tmp;
    MALLOC(tmp, length);
    gen_Morse_sequence(tmp, length);
    for (int n = 0; n < length; n++) {
        for (int i = 0; i < dimension; i++) {
            x[n][i] = tmp[(n+i) % length];
            y[n][i] = tmp[(n+i+300) % length];
        }
    }
    FREE(tmp);
    for (int n = 1; n < max_block_length; n++) {
        init_block_frequency(&bf_x, (const int* const*)x, dimension, length, n);
        init_block_frequency(&bf_y, (const int* const*)y, dimension, length, n);
        gen_rate = generation_rate(&bf_x, &bf_y);
        assert_equal_double(1, gen_rate, 1e-3);
        free_block_frequency(&bf_x);
        free_block_frequency(&bf_y);
    }
    for (int n = 0; n < length; n++) {
        for (int i = 0; i < dimension; i++) {
            x[n][i] = i;
            y[n][i] = i + 1;
        }
    }
    for (int n = 1; n < max_block_length; n++) {
        init_block_frequency(&bf_x, (const int* const*)x, dimension, length, n);
        init_block_frequency(&bf_y, (const int* const*)y, dimension, length, n);
        gen_rate = generation_rate(&bf_x, &bf_y);
        assert_equal_double(0, gen_rate, 1e-3);
        free_block_frequency(&bf_x);
        free_block_frequency(&bf_y);
    }
    FREE2(x);
    FREE2(y);
}
Пример #3
0
list_add(struct list *h, int v)
{
	while(h->init)
		h = h->next;
	h->i = v;
	h->init = 1;
	h->next = MALLOC2(24);
}
Пример #4
0
main()
{
	struct list *head = MALLOC2(24); //sizeof(struct list)); //sizeof *head);

	printf("head = %p\n", head);

	head->init = 0;

	list_add(head, 2);
	list_add(head, 5);
	list_add(head, 1);

	list_print(head);
}
Пример #5
0
static void init_test_solver_info (
        struct test_solver_info *ts_info,
        int length,
        int dim)
{
    ts_info->length = length;
    ts_info->dim = dim;

    MALLOC2(ts_info->data, length, dim);
    MALLOC(ts_info->vector, dim);
    for (int i = 0; i < dim; i++) {
        MALLOC(ts_info->vector[i], dim);
    }
    MALLOC(ts_info->spectrum, dim);
}
Пример #6
0
static void print_lyapunov_spectrum_of_mregn (
        FILE *fp,
        long epoch,
        const struct mixture_of_rnn_experts *mre,
        const struct recurrent_neural_network *gn,
        int spectrum_size,
        int mre_delay_length,
        int gn_delay_length,
        int truncate_length)
{
    int max_num;
    // decides spectrum_size which is the number to evaluate Lyapunov exponents
    max_num = mre->out_state_size * mre_delay_length;
    max_num += mre->expert_num * gn_delay_length;
    for (int i = 0; i < mre->expert_num; i++) {
        max_num += mre->expert_rnn[i].rnn_p.c_state_size;
    }
    max_num += gn->rnn_p.c_state_size;
    if (max_num < spectrum_size || spectrum_size < 0) {
        spectrum_size = max_num;
    }

    if (spectrum_size <= 0) return;

    double **spectrum = NULL;
    MALLOC2(spectrum, gn->series_num, spectrum_size);
#ifdef _OPENMP
#pragma omp parallel for
#endif
    for (int i = 0; i < gn->series_num; i++) {
        compute_lyapunov_spectrum_of_mregn_state(mre->mre_s + i, gn->rnn_s + i,
                spectrum_size, mre_delay_length, gn_delay_length,
                truncate_length, spectrum[i]);
    }
    fprintf(fp, "%ld", epoch);
    for (int i = 0; i < gn->series_num; i++) {
        for (int j = 0; j < spectrum_size; j++) {
            fprintf(fp, "\t%f", spectrum[i][j]);
        }
    }
    fprintf(fp, "\n");
    FREE2(spectrum);
}
Пример #7
0
void ptRun(const gchar*     Name,
           gint       NrParameters,
           const GimpParam* Parameter,
           gint *nreturn_vals,
           GimpParam **return_vals) {
  printf("(%s,%d) '%s'\n",__FILE__,__LINE__,__PRETTY_FUNCTION__);
  printf("Name         : '%s'\n",Name);
  printf("NrParameters : %d\n",NrParameters);
  if (!strcmp(Name,"photivoSendToGimp")) {
    printf("RunMode      : %d\n",Parameter[0].data.d_int32);
    printf("FileName1    : '%s'\n",Parameter[1].data.d_string);
    printf("FileName2    : '%s'\n",Parameter[2].data.d_string);

    QFile GimpFile(Parameter[1].data.d_string);
    bool result = GimpFile.open(QIODevice::ReadOnly | QIODevice::Text);
    assert(result);

    QTextStream In(&GimpFile);

    QString ImageFileName = In.readLine();
    QString ExifFileName  = In.readLine();
    QString ICCFileName   = In.readLine();

    // Read image
    FILE *InputFile = fopen(ImageFileName.toLocal8Bit().data(),"rb");
    if (!InputFile) {
      ptLogError(1,ImageFileName.toLocal8Bit().data());
      return; // ptError_FileOpen;
    }

    short    Colors;
    unsigned short Width;
    unsigned short Height;
    unsigned short BitsPerColor;
    char     Buffer[128];

    // Extremely naive. Probably just enough for testcases.
    char *s = fgets(Buffer,127,InputFile);
    assert ( s );
    int n = sscanf(Buffer,"P%hd",&Colors);
    assert ( 1 == n );
    assert(Colors == 6 );
    do {
      s = fgets(Buffer,127,InputFile);
      assert ( s );
    } while (Buffer[0] == '#');
    sscanf(Buffer,"%hd %hd",&Width,&Height);
    s = fgets(Buffer,127,InputFile);
    assert ( s );
    sscanf(Buffer,"%hd",&BitsPerColor);
    assert(BitsPerColor == 0xffff);

    Colors = 3;

    unsigned short (* ImageForGimp)[3] =
      (unsigned short (*)[3]) CALLOC2(Width*Height,sizeof(*ImageForGimp));
    ptMemoryError(ImageForGimp,__FILE__,__LINE__);

    unsigned short*  PpmRow = (unsigned short *) CALLOC2(Width*Height,sizeof(*PpmRow));
    ptMemoryError(PpmRow,__FILE__,__LINE__);

    for (unsigned short Row=0; Row<Height; Row++) {
      size_t RV = fread(PpmRow,Colors*2,Width,InputFile);
      if (RV != (size_t) Width) {
        printf("ReadPpm error. Expected %d bytes. Got %d\n",Width,(int)RV);
        exit(EXIT_FAILURE);
      }
      if (htons(0x55aa) != 0x55aa) {
        swab((char *)PpmRow,(char *)PpmRow,Width*Colors*2);
      }
      for (unsigned short Col=0; Col<Width; Col++) {
        for (short c=0;c<3;c++) {
          ImageForGimp[Row*Width+Col][c] = PpmRow[Col*Colors+c];
        }
      }
    }

    FREE2(PpmRow);
    FCLOSE(InputFile);

    QFile ExifFile(ExifFileName);
    result = ExifFile.open(QIODevice::ReadOnly);
    assert(result);
    qint64 FileSize = ExifFile.size();
    QDataStream ExifIn(&ExifFile);
    char* ExifBuffer = (char *) MALLOC2(FileSize);
    ptMemoryError(ExifBuffer,__FILE__,__LINE__);
    unsigned ExifBufferLength = ExifIn.readRawData(ExifBuffer,FileSize);
    ExifFile.close();

    QFile ICCFile(ICCFileName);
    result = ICCFile.open(QIODevice::ReadOnly);
    assert(result);
    qint64 FileSize2 = ICCFile.size();
    QDataStream ICCIn(&ICCFile);
    char* ICCBuffer = (char *) MALLOC2(FileSize2);
    ptMemoryError(ICCBuffer,__FILE__,__LINE__);
    unsigned ICCBufferLength = ICCIn.readRawData(ICCBuffer,FileSize2);
    ICCFile.close();

    // And now copy to gimp.
    gint32 GimpImage = gimp_image_new(Width,
                                      Height,
                                      GIMP_RGB);
    assert (GimpImage != -1);
    gint32 GimpLayer = gimp_layer_new(GimpImage,
                                      "BG",
                                      Width,
                                      Height,
                                      GIMP_RGB_IMAGE,
                                      100.0,
                                      GIMP_NORMAL_MODE);
#if GIMP_MINOR_VERSION<=6
    gimp_image_add_layer(GimpImage,GimpLayer,0);
#else
    gimp_image_insert_layer(GimpImage,GimpLayer,0,0);
#endif
    GimpDrawable* Drawable = gimp_drawable_get(GimpLayer);
    GimpPixelRgn PixelRegion;
    gimp_pixel_rgn_init(&PixelRegion,
                        Drawable,
                        0,
                        0,
                        Drawable->width,
                        Drawable->height,
                        true,
                        false);
    unsigned short TileHeight = gimp_tile_height();
    for (unsigned short Row=0; Row<Height; Row+=TileHeight) {
      unsigned short NrRows = MIN(Height-Row, (int)TileHeight);
      guint8* Buffer = g_new(guint8,TileHeight*Width*3);
      for (unsigned short i=0; i<NrRows; i++) {
        for (unsigned short j=0; j<Width; j++) {
          for (short c=0;c<3;c++) {
            Buffer[3*(i*Width+j)+c] =
              ImageForGimp[(Row+i)*Width+j][c]>>8;
          }
        }
      }
      gimp_pixel_rgn_set_rect(&PixelRegion,
                              Buffer,
                              0,
                              Row,
                              Width,
                              NrRows);
      g_free(Buffer);
    }
    gimp_drawable_flush(Drawable);
    gimp_drawable_detach(Drawable);
    FREE2(ImageForGimp);
    GimpParasite* GimpExifData = gimp_parasite_new("exif-data",
                                                   GIMP_PARASITE_PERSISTENT,
                                                   ExifBufferLength,
                                                   ExifBuffer);
    gimp_image_parasite_attach(GimpImage,GimpExifData);
    gimp_parasite_free(GimpExifData);
    FREE2(ExifBuffer);

    GimpParasite* GimpICCData = gimp_parasite_new("icc-profile",
                                                   GIMP_PARASITE_PERSISTENT,
                                                   ICCBufferLength,
                                                   ICCBuffer);
    gimp_image_parasite_attach(GimpImage,GimpICCData);
    gimp_parasite_free(GimpICCData);
    FREE2(ICCBuffer);

    static GimpParam Values[2];
    *nreturn_vals = 2;
    *return_vals = Values;
    Values[0].type = GIMP_PDB_STATUS;
    Values[0].data.d_status = GIMP_PDB_SUCCESS;
    Values[1].type = GIMP_PDB_IMAGE;
    Values[1].data.d_image = GimpImage;

    QFile::remove(ImageFileName);
    QFile::remove(ExifFileName);
    QFile::remove(ICCFileName);
    QFile::remove(Parameter[1].data.d_string);

  }
Пример #8
0
static void test_block_entropy (void)
{
    int dimension, length, max_block_length;
    double entropy;
    struct block_frequency bf;
    int **sequence;

    dimension = 3;
    length = 10000;
    max_block_length = 5;

    MALLOC2(sequence, length, dimension);
    for (int n = 0; n < length; n++) {
        for (int i = 0; i < dimension; i++) {
            sequence[n][i] = 0;
        }
    }
    for (int n = 1; n <= max_block_length; n++) {
        init_block_frequency(&bf, (const int* const*)sequence, dimension,
                length, n);
        entropy = block_entropy(&bf) / n;
        assert_equal_double(0, entropy, 1e-3);
        free_block_frequency(&bf);
    }

    for (int n = 0; n < length; n++) {
        for (int i = 0; i < dimension; i++) {
            sequence[n][i] = (n + i) % 2;
        }
    }
    for (int n = 1; n <= max_block_length; n++) {
        init_block_frequency(&bf, (const int* const*)sequence, dimension,
                length, n);
        entropy = block_entropy(&bf) / n;
        assert_equal_double(1.0/n, entropy, 1e-3);
        free_block_frequency(&bf);
    }

    for (int n = 0; n < length; n++) {
        for (int i = 0; i < dimension; i++) {
            sequence[n][i] = (n + i) % 4;
        }
    }
    for (int n = 1; n <= max_block_length; n++) {
        init_block_frequency(&bf, (const int* const*)sequence, dimension,
                length, n);
        entropy = block_entropy(&bf) / n;
        assert_equal_double(2.0/n, entropy, 1e-3);
        free_block_frequency(&bf);
    }

    int *tmp;
    MALLOC(tmp, length);
    gen_Morse_sequence(tmp, length);
    for (int n = 0; n < length; n++) {
        for (int i = 0; i < dimension; i++) {
            sequence[n][i] = tmp[n];
        }
    }
    FREE(tmp);
    for (int n = 1; n <= max_block_length ; n++) {
        init_block_frequency(&bf, (const int* const*)sequence, dimension,
                length, n);
        entropy = block_entropy(&bf) / n;
        mu_assert(entropy >= 0.5);
        free_block_frequency(&bf);
    }
    FREE2(sequence);
}
Пример #9
0
static void test_kullback_leibler_divergence (void)
{
    int dimension, length, max_block_length;
    struct block_frequency bf_x, bf_y;
    int **x, **y;
    double kl_div;

    dimension = 2;
    length = 1024;
    max_block_length = 5;

    MALLOC2(x, length, dimension);
    MALLOC2(y, length, dimension);

    int *tmp;
    MALLOC(tmp, length);
    gen_Morse_sequence(tmp, length);
    for (int n = 0; n < length; n++) {
        for (int i = 0; i < dimension; i++) {
            x[n][i] = tmp[(n+i) % length];
            y[n][i] = tmp[(n+i+300) % length];
        }
    }
    FREE(tmp);
    for (int n = 1; n < max_block_length; n++) {
        init_block_frequency(&bf_x, (const int* const*)x, dimension, length, n);
        init_block_frequency(&bf_y, (const int* const*)y, dimension, length, n);
        kl_div = kullback_leibler_divergence(&bf_x, &bf_y);
        assert_equal_double(0, kl_div, 1e-3);
        free_block_frequency(&bf_x);
        free_block_frequency(&bf_y);
    }
    init_genrand(61107L);
    for (int i = 0; i < 10; i++) {
        for (int n = 0; n < length; n++) {
            for (int j = 0; j < dimension; j++) {
                x[n][j] = xor128() % 2;
                y[n][j] = xor128() % 2;
            }
        }
        for (int n = 1; n < max_block_length; n++) {
            init_block_frequency(&bf_x, (const int* const*)x, dimension, length,
                    n);
            init_block_frequency(&bf_y, (const int* const*)y, dimension, length,
                    n);
            kl_div = kullback_leibler_divergence(&bf_x, &bf_y);
            mu_assert(kl_div >= 0);
            free_block_frequency(&bf_x);
            free_block_frequency(&bf_y);

            init_block_frequency(&bf_x, (const int* const*)x, dimension,
                    length-100, n);
            init_block_frequency(&bf_y, (const int* const*)y, dimension, length,
                    n);
            kl_div = kullback_leibler_divergence(&bf_x, &bf_y);
            mu_assert(kl_div >= 0);
            free_block_frequency(&bf_x);
            free_block_frequency(&bf_y);

            init_block_frequency(&bf_x, (const int* const*)x, dimension, length,
                    n);
            init_block_frequency(&bf_y, (const int* const*)y, dimension,
                    length-100, n);
            kl_div = kullback_leibler_divergence(&bf_x, &bf_y);
            mu_assert(kl_div >= 0);
            free_block_frequency(&bf_x);
            free_block_frequency(&bf_y);
        }
    }

    for (int n = 0; n < length; n++) {
        for (int i = 0; i < dimension; i++) {
            x[n][i] = i;
            y[n][i] = i + 1;
        }
    }
    for (int n = 1; n < max_block_length; n++) {
        init_block_frequency(&bf_x, (const int* const*)x, dimension, length, n);
        init_block_frequency(&bf_y, (const int* const*)y, dimension, length, n);
        kl_div = kullback_leibler_divergence(&bf_x, &bf_y);
        mu_assert(kl_div > 0);
        free_block_frequency(&bf_x);
        free_block_frequency(&bf_y);
    }
    FREE2(x);
    FREE2(y);
}
Пример #10
0
/*
 * returns the Lyapunov spectrum from n-dimensional time series by using the
 * Jacobian matrix
 *
 *   @parameter  data     : n-dimensional time series
 *   @parameter  t        : length of time series
 *   @parameter  m        : number of Lyapunov exponents
 *   @parameter  n        : dimension of time series
 *   @parameter  T        : interval to compute Gram-Schmidt orthogonalization
 *   @parameter  func     : function pointer to compute Jacobian matrix
 *   @parameter  obj      : object used in func
 *   @parameter  spectrum : Lyapunov spectrum (result)
 *   @parameter  matrix   : Jacobian matrix (temporal data cache)
 *   @parameter  vector   : orthogonal vector (temporal data cache)
 *
 *   @return              : Lyapunov spectrum
 */
double* lyapunov_spectrum (
        const double* const* data,
        int t,
        int m,
        int n,
        int T,
        jacobian_map func,
        void *obj,
        double* spectrum,
        double **matrix,
        double ***vector)
{
    if (m < 1 || n < 1) {
        return spectrum;
    }

    double tmp_vec[n];
    int flag_matrix_alloc, flag_vector_alloc;

    if (matrix == NULL) {
        MALLOC2(matrix, n, n);
        flag_matrix_alloc = 1;
    } else {
        flag_matrix_alloc = 0;
    }
    if (vector == NULL) {
        MALLOC(vector, m);
        MALLOC(vector[0], m * m);
        MALLOC(vector[0][0], m * m * n);
        for (int i = 0; i < m; i++) {
            vector[i] = vector[0] + i * m;
            vector[i][0] = vector[0][0] + i * m * n;
            for (int j = 0; j < m; j++) {
                vector[i][j] = vector[i][0] + j * n;
            }
        }
        flag_vector_alloc = 1;
    } else {
        flag_vector_alloc = 0;
    }

    for (int i = 0; i < m; i++) {
        spectrum[i] = 0; /* initializes i'th Lyapunov exponent */

        /* vector initialization */
        for (int j = 0; j <= i; j++) {
            for (int k = 0; k < n; k++) {
                vector[i][j][k] = (k%(i+1) == j) ? 1 : 0;
            }
        }
        gram_schmidt_orthogonalization(vector[i], i+1, n);
        for (int j = 0; j <= i; j++) {
            resize(vector[i][j], n, 1); /* resizes vector length to 1 */
        }
    }

    /* computing the Lyapunov spectrum */
    for (int i = 0; i < t; i++) {
        /* computing Jacobian matrix */
        if (func(data[i], n, i, matrix, obj) == NULL) {
            return NULL;
        }
        for (int j = 0; j < m; j++) {
            for (int k = 0; k <= j; k++) {
                product((const double* const*)matrix, vector[j][k],
                        tmp_vec, n, n);
                memcpy(vector[j][k], tmp_vec, n * sizeof(double));
            }
        }
        if (( (i+1) % T ) == 0 || i+1 == t) {
            for (int j = 0; j < m; j++) {
                gram_schmidt_orthogonalization(vector[j], j+1, n);
                for (int k = 0; k <= j; k++) {
                    // computing expansion rate
                    spectrum[j] += log(get_length(vector[j][k], n));
                    resize(vector[j][k], n, 1); /* resizes vector length to 1 */
                }
            }
        }
    }
    for (int i = 0; i < m; i++) {
        spectrum[i] /= t; /* time average */
        for (int j = 0; j < i; j++) {
            spectrum[i] -= spectrum[j];
        }
    }
    qsort(spectrum, m, sizeof(double), compar);

    if (flag_matrix_alloc) {
        FREE2(matrix);
    }
    if (flag_vector_alloc) {
        FREE(vector[0][0]);
        FREE(vector[0]);
        FREE(vector);
    }
    return spectrum;
}