Exemplo n.º 1
0
 /**
  * Returns the next number in the pseudo-random sequence generated by
  * the Mersenne Twister 19937 algorithm.
  * @returns The next number in the pseudo-random sequence
  */
 double MersenneTwister::nextValue()
 {
   /// A variate generator to combine a random number generator with a distribution
   uniform_generator uniform_rand(m_generator, m_uniform_dist);
   // There is no reason why this call shouldn't be considered const
   return uniform_rand();
 }
Exemplo n.º 2
0
void test_sort(){

    
    cout << "\n----TESTING SORT LIBRARY----\n" << endl;
    
    //class template instance, must be called in conjunction with a variate or rand generator
    br::uniform_int_distribution<> uniform_rand(1,20000);

    vector<int> vec;
    
    while( (int)(vec.end() - vec.begin()) < 25 )
        vec.push_back( uniform_rand(rng) );
    
    cout << "\nInsertion Sort\n" << endl;
    InsertionSort<int>( vec.begin(), vec.end());
    copy(vec.begin(), vec.end(), ostream_iterator<int> (cout, "\n"));
    
    KnuthShuffle<int>(vec.begin(),vec.end()); //reset

    cout << "\n\nQuick Sort\n" << endl;

    
    KnuthShuffle<int>(vec.begin(),vec.end()); //reset
    
    cout << "\n\nMerge Sort\n" << endl;
    
    
    cout << "\n" << endl;
    

}
Exemplo n.º 3
0
double tcplib(struct histmap *hmp, struct entry *tbl, unsigned short seed[3])
{
  float prob;
  int maxbin = hmp->nbins-1;
  int base = 0;
  int bound = maxbin;
  int mid = (int)((float)maxbin / 2.0 + 0.5);
  
  prob = ((float) (pc_nrand(seed) % PRECIS)) / (float) PRECIS;

  do {
    while (prob <= tbl[mid-1].prob && mid != 1) {
      bound = mid;
      mid -= (int)(((float)(mid - base)) / 2.0 + 0.5);
    } 
    while (prob > tbl[mid].prob) {
      base = mid;
      mid += (int)(((float)(bound - mid)) / 2.0 + 0.5);
    }
  } while (!(mid == 1 || (prob >= tbl[mid-1].prob && prob <= tbl[mid].prob)));
      
  if (mid == 1 && prob < tbl[0].prob) {
    return ((double) tbl[0].value);
  } else {
    return ((double) uniform_rand(tbl[mid-1].value, tbl[mid].value, seed));
  }
}
Exemplo n.º 4
0
float_t ConvNet::train_once() {
	float_t err = 0;
	int iter = 0;

	while (iter < M) {
		//auto train_x_index = iter % train_size_;
		iter++;
		auto train_x_index = uniform_rand(0, train_size_ - 1);
		layers[0]->input_ = train_x_[train_x_index];
		layers.back()->exp_y = (int) train_y_[train_x_index];
		/*
		 Start forward feeding.
		 */

		for (auto layer : layers) {
			layer->forward();
			if (layer->next != nullptr) {
				layer->next->input_ = layer->output_;
			}

		}
		err += layers.back()->err;

		/*
		 back propgation
		 */
		for (auto i = layers.rbegin(); i != layers.rend(); i++) {
			(*i)->back_prop();
		}
	}

	return err / M;
}
Exemplo n.º 5
0
/**
 * Box-Muller
 */
complex double gaussian_rand(void)
{
	double u1, u2, s;

 	do {
	
		u1 = 2. * uniform_rand() - 1.;
		u2 = 2. * uniform_rand() - 1.;
   		s = u1 * u1 + u2 * u2;

   	} while (s > 1.);

	double re = sqrt(-2. * log(s) / s) * u1;
	double im = sqrt(-2. * log(s) / s) * u2;

	return re + 1.i * im;
}
Exemplo n.º 6
0
Arquivo: main.cpp Projeto: CCJY/coliru
int main(void)
{
    srand(time(NULL));
    params_t params={1.0,2.0}; // on choisit la moyenne à 1.0 et l'equart type à 2.0
    {
        /* test de l'implémentation */
        double y= uniform_rand(); /* y */
        double x = quantile(y,params); /* F(x)=y */
        printf("y= %.15f; F(%.15f)= %.15f\n", y, x, cumulative_normal_distribution(x, params)); /* on vérifie bien que y= F(x) */
    }
    /* générer les X */
    for(size_t i=0;i<MAX;++i)
    {
        random_variables[i] = normal_rand(params); // on génère un tableau de X et on les affiche au fur et à mesure. Ces nombres aléatoires suivent la loi normale.
        printf("%0.5f\n",random_variables[i]);
    }
    return 0;
}
Exemplo n.º 7
0
static void random_point(int D, float p[D])
{
	for (int i = 0; i < D; i++)
		p[i] = uniform_rand();
}
Exemplo n.º 8
0
Arquivo: SVM.cpp Projeto: MrAiki/SVM
// 再急勾配法(サーセンwww)による学習
int SVM::learning(void)
{

  int iteration;              // 学習繰り返しカウント

  float* diff_alpha;          // 双対問題の勾配値
  float* pre_diff_alpha;      // 双対問題の前回の勾配値(慣性項に用いる)
  float* pre_alpha;           // 前回の双対係数ベクトル(収束判定に用いる)
  register float diff_sum;    // 勾配計算用の小計
  register float kernel_val;  // カーネル関数とC2を含めた項

  //float plus_sum, minus_sum;  // 正例と負例の係数和

  // 配列(ベクトル)のアロケート
  diff_alpha     = new float[n_sample];
  pre_diff_alpha = new float[n_sample];
  pre_alpha      = new float[n_sample];

  status = SVM_NOT_LEARN;       // 学習を未完了に
  iteration  = 0;       // 繰り返し回数を初期化

  // 双対係数の初期化.乱択
  for (int i = 0; i < n_sample; i++ ) {
    // 欠損データの係数は0にして使用しない
    if ( label[i] == 0 ) {
      alpha[i] = 0;
      continue;
    }
    alpha[i] = uniform_rand(1.0) + 1.0;
  }

  // 学習ループ
  while ( iteration < maxIteration ) {

    printf("ite: %d diff_norm : %f alpha_dist : %f \r\n", iteration, two_norm(diff_alpha, n_sample), vec_dist(alpha, pre_alpha, n_sample));
    // 前回の更新値の記録
    memcpy(pre_alpha, alpha, sizeof(float) * n_sample);
    if ( iteration >= 1 ) {
      memcpy(pre_diff_alpha, diff_alpha, sizeof(float) * n_sample);
    } else {
      // 初回は0埋めで初期化
      memset(diff_alpha, 0, sizeof(float) * n_sample);
      memset(pre_diff_alpha, 0, sizeof(float) * n_sample);
    }

    // 勾配値の計算
    for (int i=0; i < n_sample; i++) {
      diff_sum = 0;
      for (int j=0; j < n_sample;j++) {
        // C2を踏まえたカーネル関数値
        kernel_val = kernel_function(&(MATRIX_AT(sample,dim_signal,i,0)), &(MATRIX_AT(sample,dim_signal,j,0)), dim_signal);
        // kernel_val = MATRIX_AT(grammat,n_sample,i,j); // via Gram matrix
        if (i == j) { 
          kernel_val += (1/C2);
        }
        diff_sum += alpha[j] * label[j] * kernel_val; 
      }
      diff_sum *= label[i];
      diff_alpha[i] = 1 - diff_sum;
    }

    // 双対変数の更新
    for (int i=0; i < n_sample; i++) {
      if ( label[i] == 0 ) {
        continue;
      }
      //printf("alpha[%d] : %f -> ", i, alpha[i]);
      alpha[i] = pre_alpha[i] 
                  + eta * diff_alpha[i]
                  + learn_alpha * pre_diff_alpha[i];
      //printf("%f \dim_signal", alpha[i]);

      // 非数/無限チェック
      if ( isnan(alpha[i]) || isinf(alpha[i]) ) {
        fprintf(stderr, "Detected NaN or Inf Dual-Coffience : pre_alhpa[%d]=%f -> alpha[%d]=%f", i, pre_alpha[i], i, alpha[i]);
        return SVM_DETECT_BAD_VAL;
      }

    }

    // 係数の制約条件1:正例と負例の双対係数和を等しくする.
    //                 手法:標本平均に寄せる
    float norm_sum = 0;
    for (int i = 0; i < n_sample; i++ ) {
      norm_sum += (label[i] * alpha[i]);
    }
    norm_sum /= n_sample;

    for (int i = 0; i < n_sample; i++ ) {
      if ( label[i] == 0 ) {
        continue;
      }
      alpha[i] -= (norm_sum / label[i]);
    }

    // 係数の制約条件2:双対係数は非負
    for (int i = 0; i < n_sample; i++ ) {
      if ( alpha[i] < 0 ) {
        alpha[i] = 0;
      } else if ( alpha[i] > C1 ) {
        // C1を踏まえると,係数の上限はC1となる.
        alpha[i] = C1;
      }  
    }

    // 収束判定 : 凸計画問題なので,収束時は大域最適が
    //            保証されている.
    if ( (vec_dist(alpha, pre_alpha, n_sample) < epsilon)
        || (two_norm(diff_alpha, n_sample) < epsilon) ) {
      // 学習の正常完了
      status = SVM_LEARN_SUCCESS;
      break;
    }

    // 学習繰り返し回数のインクリメント
    iteration++;
  }

  if (iteration >= maxIteration) {
    fprintf(stderr, "Learning is not convergenced. (iteration count > maxIteration) \r\n");
    status = SVM_NOT_CONVERGENCED;
  } else if ( status != SVM_LEARN_SUCCESS ) {
    status = SVM_NOT_LEARN;
  }
  
  // 領域開放
  delete [] diff_alpha;
  delete [] pre_diff_alpha;
  delete [] pre_alpha;
  
  return status;

}
status_t Harness::testSeek(
        const char *componentName, const char *componentRole) {
    bool isEncoder =
        !strncmp(componentRole, "audio_encoder.", 14)
        || !strncmp(componentRole, "video_encoder.", 14);

    if (isEncoder) {
        // Not testing seek behaviour for encoders.

        printf("  * Not testing seek functionality for encoders.\n");
        return OK;
    }

    const char *mime = GetMimeFromComponentRole(componentRole);

    if (!mime) {
        LOGI("Cannot perform seek test with this componentRole (%s)",
             componentRole);

        return OK;
    }

    sp<MediaSource> source = CreateSourceForMime(mime);

    sp<MediaSource> seekSource = CreateSourceForMime(mime);
    if (source == NULL || seekSource == NULL) {
        return UNKNOWN_ERROR;
    }

    CHECK_EQ(seekSource->start(), OK);

    sp<MediaSource> codec = OMXCodec::Create(
            mOMX, source->getFormat(), false /* createEncoder */,
            source, componentName);

    CHECK(codec != NULL);

    CHECK_EQ(codec->start(), OK);

    int64_t durationUs;
    CHECK(source->getFormat()->findInt64(kKeyDuration, &durationUs));

    LOGI("stream duration is %lld us (%.2f secs)",
         durationUs, durationUs / 1E6);

    static const int32_t kNumIterations = 5000;

    // We are always going to seek beyond EOS in the first iteration (i == 0)
    // followed by a linear read for the second iteration (i == 1).
    // After that it's all random.
    for (int32_t i = 0; i < kNumIterations; ++i) {
        int64_t requestedSeekTimeUs;
        int64_t actualSeekTimeUs;
        MediaSource::ReadOptions options;

        double r = uniform_rand();

        if ((i == 1) || (i > 0 && r < 0.5)) {
            // 50% chance of just continuing to decode from last position.

            requestedSeekTimeUs = -1;

            LOGI("requesting linear read");
        } else {
            if (i == 0 || r < 0.55) {
                // 5% chance of seeking beyond end of stream.

                requestedSeekTimeUs = durationUs;

                LOGI("requesting seek beyond EOF");
            } else {
                requestedSeekTimeUs =
                    (int64_t)(uniform_rand() * durationUs);

                LOGI("requesting seek to %lld us (%.2f secs)",
                     requestedSeekTimeUs, requestedSeekTimeUs / 1E6);
            }

            MediaBuffer *buffer = NULL;
            options.setSeekTo(
                    requestedSeekTimeUs, MediaSource::ReadOptions::SEEK_NEXT_SYNC);

            if (seekSource->read(&buffer, &options) != OK) {
                CHECK_EQ(buffer, NULL);
                actualSeekTimeUs = -1;
            } else {
                CHECK(buffer != NULL);
                CHECK(buffer->meta_data()->findInt64(kKeyTime, &actualSeekTimeUs));
                CHECK(actualSeekTimeUs >= 0);

                buffer->release();
                buffer = NULL;
            }

            LOGI("nearest keyframe is at %lld us (%.2f secs)",
                 actualSeekTimeUs, actualSeekTimeUs / 1E6);
        }

        status_t err;
        MediaBuffer *buffer;
        for (;;) {
            err = codec->read(&buffer, &options);
            options.clearSeekTo();
            if (err == INFO_FORMAT_CHANGED) {
                CHECK_EQ(buffer, NULL);
                continue;
            }
            if (err == OK) {
                CHECK(buffer != NULL);
                if (buffer->range_length() == 0) {
                    buffer->release();
                    buffer = NULL;
                    continue;
                }
            } else {
                CHECK_EQ(buffer, NULL);
            }

            break;
        }

        if (requestedSeekTimeUs < 0) {
            // Linear read.
            if (err != OK) {
                CHECK_EQ(buffer, NULL);
            } else {
                CHECK(buffer != NULL);
                buffer->release();
                buffer = NULL;
            }
        } else if (actualSeekTimeUs < 0) {
            EXPECT(err != OK,
                   "We attempted to seek beyond EOS and expected "
                   "ERROR_END_OF_STREAM to be returned, but instead "
                   "we got a valid buffer.");
            EXPECT(err == ERROR_END_OF_STREAM,
                   "We attempted to seek beyond EOS and expected "
                   "ERROR_END_OF_STREAM to be returned, but instead "
                   "we found some other error.");
            CHECK_EQ(err, ERROR_END_OF_STREAM);
            CHECK_EQ(buffer, NULL);
        } else {
            EXPECT(err == OK,
                   "Expected a valid buffer to be returned from "
                   "OMXCodec::read.");
            CHECK(buffer != NULL);

            int64_t bufferTimeUs;
            CHECK(buffer->meta_data()->findInt64(kKeyTime, &bufferTimeUs));
            if (!CloseEnough(bufferTimeUs, actualSeekTimeUs)) {
                printf("\n  * Attempted seeking to %lld us (%.2f secs)",
                       requestedSeekTimeUs, requestedSeekTimeUs / 1E6);
                printf("\n  * Nearest keyframe is at %lld us (%.2f secs)",
                       actualSeekTimeUs, actualSeekTimeUs / 1E6);
                printf("\n  * Returned buffer was at %lld us (%.2f secs)\n\n",
                       bufferTimeUs, bufferTimeUs / 1E6);

                buffer->release();
                buffer = NULL;

                CHECK_EQ(codec->stop(), OK);

                return UNKNOWN_ERROR;
            }

            buffer->release();
            buffer = NULL;
        }
    }

    CHECK_EQ(codec->stop(), OK);

    return OK;
}
Exemplo n.º 10
0
#include "num/multind.h"
#include "num/flpmath.h"
#include "num/rand.h"
#include "num/init.h"

#include "misc/misc.h"
#include "misc/mmio.h"
#include "misc/pd.h"
#include "misc/opts.h"


static void random_point(int D, float p[static D])
{
	for (int i = 0; i < D; i++)
		p[i] = uniform_rand();
}

static float dist(int D, const float a[static D], const float b[static D])
{
	float r = 0.;

	for (int i = 0; i < D; i++)
		r += powf(a[i] - b[i], 2.);

	return sqrtf(r);
}



static float maxn(int D, const float a[static D], const float b[static D])
Exemplo n.º 11
0
Arquivo: SRNN.cpp Projeto: MrAiki/SRNN
/* 逆誤差伝搬法による学習 局所解?なんのこったよ(すっとぼけ)*/
float SRNN::learning(void)
{
  int iteration = 0; // 学習繰り返し回数
  int seq = 0;       // 現在学習中の系列番号[0,...,len_seqence-1]
  int end_flag = 0;  // 学習終了フラグ.このフラグが成立したら今回のsequenceを最後まで回して終了する.
  // 係数行列のサイズ
  int row_in_mid = num_mid_neuron;
  int col_in_mid = dim_signal + num_mid_neuron + 1;
  int row_mid_out = dim_signal;
  int col_mid_out = num_mid_neuron + 1;

  // 行列のアロケート
  // 係数行列の更新量
  float* dWin_mid  = new float[row_in_mid * col_in_mid];
  float* dWmid_out = new float[row_mid_out * col_mid_out];
  // 前回の更新量:慣性項に用いる.
  float* prevdWin_mid  = new float[row_in_mid * col_in_mid];
  float* prevdWmid_out = new float[row_mid_out * col_mid_out];
  float* norm_sample   = new float[len_seqence * dim_signal]; // 正規化したサンプル信号; 実際の学習は正規化した信号を用います.

  // 係数行列の初期化
  for (int i=0; i < row_in_mid; i++)
    for (int j=0; j < col_in_mid; j++)
      MATRIX_AT(Win_mid,col_in_mid,i,j) = uniform_rand(width_initW);

  for (int i=0; i < row_mid_out; i++)
    for (int j=0; j < col_mid_out; j++)
      MATRIX_AT(Wmid_out,col_mid_out,i,j) = uniform_rand(width_initW);

  // 信号の正規化:経験上,非常に大切な処理
  for (int seq=0; seq < len_seqence; seq++) {
    for (int n=0; n < dim_signal; n++) {
      MATRIX_AT(norm_sample,dim_signal,seq,n) = 
            normalize_signal(MATRIX_AT(this->sample,dim_signal,seq,n),
                             MATRIX_AT(this->sample_maxmin,2,n,0),
                             MATRIX_AT(this->sample_maxmin,2,n,1));
      // printf("%f ", MATRIX_AT(norm_sample,dim_signal,seq,n));
    }
    // printf("\r\n");
  }

  // 出力層の信号
  float* out_signal = new float[dim_signal];

  // 入力層->中間層の信号和
  float* in_mid_net = new float[num_mid_neuron];
  // 中間層->出力層の信号和.
  float* mid_out_net = new float[dim_signal];

  // 誤差信号
  float* sigma = new float[dim_signal];

  // 前回の二乗誤差値:収束判定に用いる.
  float prevError;

  /* 学習ループ */
  while (1) {

    // 終了条件を満たすか確認
    if (!end_flag) {
      end_flag = !(iteration < this->maxIteration 
                   && (iteration <= this->len_seqence 
                       || this->squareError > this->goalError)
                  );
    }

    // printf("ite:%d err:%f \r\n", iteration, squareError);

    // 系列の末尾に到達していたら,最初からリセットする.
    if (seq == len_seqence && !end_flag) {
      seq = 0;
    }

    // 前回の更新量/二乗誤差を保存
    if (iteration >= 1) {
      memcpy(prevdWin_mid, dWin_mid, sizeof(float) * row_in_mid * col_in_mid);
      memcpy(prevdWmid_out, dWmid_out, sizeof(float) * row_mid_out * col_mid_out);
      prevError = squareError;
    } else {
      // 初回は0埋め
      memset(prevdWin_mid, float(0), sizeof(float) * row_in_mid * col_in_mid);
      memset(prevdWmid_out, float(0), sizeof(float) * row_mid_out * col_mid_out);
    }
    
    /* 学習ステップその1:ニューラルネットの出力信号を求める */

    // 入力値を取得
    memcpy(expand_in_signal, &(norm_sample[seq * dim_signal]), sizeof(float) * dim_signal);
    // SRNN特有:入力層に中間層のコピーが追加され,中間層に入力される.
    if (iteration == 0) {
      // 初回は0埋めする
      memset(&(expand_in_signal[dim_signal]), float(0), sizeof(float) * num_mid_neuron);
    } else {
      // コンテキスト層 = 前回のコンテキスト層の出力
      // 前回の中間層信号との線形和をシグモイド関数に通す.
      for (int d = 0; d < num_mid_neuron; d++) {
        expand_in_signal[dim_signal + d] = sigmoid_func(alpha_context * expand_in_signal[dim_signal + d] + expand_mid_signal[d]);
      }
    }
    // バイアス項は常に1に固定.
    expand_in_signal[dim_signal + num_mid_neuron] = 1;

    // 入力->中間層の出力信号和計算
    multiply_mat_vec(Win_mid,
                     expand_in_signal,
                     in_mid_net,
                     num_mid_neuron,
                     dim_signal + num_mid_neuron + 1);
    // 中間層の出力信号計算
    sigmoid_vec(in_mid_net,
                expand_mid_signal,
                num_mid_neuron);
    expand_mid_signal[num_mid_neuron] = 1;
    // 中間->出力層の出力信号和計算
    multiply_mat_vec(Wmid_out,
                     expand_mid_signal,
                     mid_out_net,
                     dim_signal,
                     num_mid_neuron + 1);
    // 出力層の出力信号計算
    sigmoid_vec(mid_out_net,
                out_signal,
                dim_signal);

    
    for (int i = 0; i < dim_signal; i++) {
      predict_signal[i] = expand_signal(out_signal[i],
                                        MATRIX_AT(sample_maxmin,2,i,0),
                                        MATRIX_AT(sample_maxmin,2,i,1));
    }
    printf("predict : %f %f %f \r\n", predict_signal[0], predict_signal[1], predict_signal[2]);
    
    // print_mat(Wmid_out, row_mid_out, col_mid_out);
    
    // この時点での二乗誤差計算
    squareError = 0;
    // 次の系列との誤差を見ている!! ここが注目ポイント
    // ==> つまり,次系列を予測させようとしている.
    for (int n = 0;n < dim_signal;n++) {
      if (seq < len_seqence - 1) {
        squareError += powf((out_signal[n] - MATRIX_AT(norm_sample,dim_signal,(seq + 1),n)),2);
      } else {
        squareError += powf((out_signal[n] - MATRIX_AT(norm_sample,dim_signal,0,n)),2);
      }
    } 
    squareError /= dim_signal;

    /* 学習の終了 */
    // 終了フラグが立ち,かつ系列の最後に達していたら学習終了
    if (end_flag && (seq == (len_seqence-1))) {
      // 予測結果をセット.
      for (int i = 0; i < dim_signal; i++) {
        predict_signal[i] = expand_signal(out_signal[i],
                                          MATRIX_AT(sample_maxmin,2,i,0),
                                          MATRIX_AT(sample_maxmin,2,i,1));
        //printf("%f ", predict_signal[i]);
      }
      break;
    }

    // 収束したと判定したら終了フラグを立てる.
    if (fabsf(squareError - prevError) < epsilon) {
      end_flag = 1;
    }

    /* 学習ステップその2:逆誤差伝搬 */
    // 誤差信号の計算
    for (int n = 0; n < dim_signal; n++) {
      if (seq < len_seqence - 1) {
        sigma[n] = (out_signal[n] - MATRIX_AT(norm_sample,dim_signal,seq+1,n)) * out_signal[n] * (1 - out_signal[n]);
      } else {
        /* 末尾と先頭の誤差を取る (大抵,大きくなる) */
        sigma[n] = (out_signal[n] - MATRIX_AT(norm_sample, dim_signal,0,n)) * out_signal[n] * (1 - out_signal[n]);
      }
    }
    // printf("Sigma : %f %f %f \r\n", sigma[0], sigma[1], sigma[2]);

    // 出力->中間層の係数の変更量計算
    for (int n = 0; n < dim_signal; n++) {
      for (int j = 0; j < num_mid_neuron + 1; j++) {
        MATRIX_AT(dWmid_out,num_mid_neuron,n,j) = sigma[n] * expand_mid_signal[j];
      }
    }

    // 中間->入力層の係数の変更量計算
    register float sum_sigma;
    for (int i = 0; i < num_mid_neuron; i++) {
      // 誤差信号を逆向きに伝播させる.
      sum_sigma = 0;
      for (int k = 0; k < dim_signal; k++) {
        sum_sigma += sigma[k] * MATRIX_AT(Wmid_out,num_mid_neuron + 1,k,i);
      }
      // 中間->入力層の係数の変更量計算
      for (int j = 0; j < col_in_mid; j++) {
        MATRIX_AT(dWin_mid,num_mid_neuron,j,i)
                          = sum_sigma * expand_mid_signal[i] *
                            (1 - expand_mid_signal[i]) *
                            expand_in_signal[j];
      }
    }

    // 係数更新
    for (int i = 0; i < row_in_mid; i++) {
      for (int j = 0; j < col_in_mid; j++) {
        //printf("[%f -> ", MATRIX_AT(Win_mid,col_in_mid,i,j));
        MATRIX_AT(Win_mid,col_in_mid,i,j) = 
              MATRIX_AT(Win_mid,col_in_mid,i,j) - 
              this->learnRate * MATRIX_AT(dWin_mid,col_in_mid,i,j) -
              this->alpha * MATRIX_AT(prevdWin_mid,col_in_mid,i,j);
        // printf("%f] ", MATRIX_AT(Win_mid,col_in_mid,i,j));
        // printf("dW : %f , prevdW : %f ", MATRIX_AT(dWin_mid,col_in_mid,i,j), MATRIX_AT(prevdWin_mid,col_in_mid,i,j));
      }
      //printf("\r\n");
    }
    for (int i = 0; i < row_mid_out; i++) {
      for (int j = 0; j < col_mid_out; j++) {
        MATRIX_AT(Wmid_out,col_mid_out,i,j)= 
              MATRIX_AT(Wmid_out,col_mid_out,i,j) - 
              this->learnRate * MATRIX_AT(dWmid_out,col_mid_out,i,j) - 
              this->alpha * MATRIX_AT(prevdWmid_out,col_mid_out,i,j);
      }
    }

    // ループ回数/系列のインクリメント
    iteration += 1;
    seq += 1;

  }
  
  delete [] dWin_mid; delete [] dWmid_out;
  delete [] prevdWin_mid; delete [] prevdWmid_out;
  delete [] norm_sample; delete [] out_signal;
  delete [] in_mid_net; delete [] mid_out_net;

  return squareError;
}
Exemplo n.º 12
0
double unirnd(void)
/*! Returns a random number from the interval (0,1). */
{
  return uniform_rand(&IDUM);
}
Exemplo n.º 13
0
Arquivo: main.cpp Projeto: CCJY/coliru
#include <stdio.h> /* printf */
#include <math.h> /* pow, sqrt, exp, erf */
#include <time.h> /* time */
#include <stdlib.h> /* srand, rand */
#define PI 3.14159265358979323846 /* au cas où la constante ne soit pas déjà incluse dans la lib standard */

typedef struct
{
    double mean; /* mu */
    double deviation; /* sigma */
} params_t;

double uniform_rand() /* nombres aléatoire entre 0 et 1 (inclus) */
{
    return rand()/(double)RAND_MAX;
}

double inverse_erf(double x) /* l'inverse de l'error function(erf) */
{
    double result=0.0;
    int steps=0;
    for(;steps<100; ++steps)
    {
        result-=(erf(result)-x)*sqrt(PI)/2*exp(result*result); /* méthode de Newton appliquée au calcul de l'inverse de "erf" */
    }
    return result;
}

double normal_distribution(double x, params_t params); /* f: distribution gaussienne / loi normale*/
double cumulative_normal_distribution(double x, params_t params); /* F: fonction de répartition de la loi normale */
double quantile(double x, params_t params); /* F^-1: l'inverse de la fonction de répatition de la loi normale */
double normal_rand(params_t params) /* Inverse transform sampling */
{
    /* La répartition suit la loi normale d'après: "Caractérisation de la loi par la fonction de répartition" et le "Théorème de la réciproque" */
    return quantile(uniform_rand(),params);
}
Exemplo n.º 14
0
int main(int argc, char** argv) {
	
	int i, eviction_algorithm, seed;
	
	//Parse input
	if(argc == 2){
		eviction_algorithm = atoi(argv[1]);
		seed = time(NULL);
	}else if(argc == 3) {
		eviction_algorithm = atoi(argv[1]);
		seed = atoi(argv[2]);
	}else{
		printf("1st argument: eviction algorithm (0-Random; 1-Clock; 2-Clock2ndChance)\n2nd argument: seed for random algorithm\n");
		return -1;
	}
	
	init_memory(eviction_algorithm, seed);
	
	printf("STORE VALUES IN ORDER value = vAddr+1 (press enter to continue):\n");
	memoryMaxer(PAGE_TABLE_SIZE+2);//beyond the limit (the last 2 page creations are ignored)
	print_memory_state();
	getchar();
	
	printf("GET_VALUE FROM ALL ADDRESSES (press enter to continue):\n");
	for(i = 0; i < PAGE_TABLE_SIZE; i++){
		if(get_value(i, NULL) != i+1){
			printf("wrong answer for page %d\n", i);
			return -1;//not supposed to happen
		}
	}
	print_memory_state();
	getchar();
	
	printf("FREE RANDOM pages AND TRY TO GET_VALUE. AND CREATE SOME PAGES (press enter to continue):\n");
	for (i = 0; i < 20; i++){
		int addr = uniform_rand(0, PAGE_TABLE_SIZE);
		printf("free %d\n", addr);
		free_page(addr);
		int valid;
		get_value(addr, &valid);
		if(valid == 0) printf("Couldn't read from addr %d\n", addr);
	}
	for (i = 0; i < 10; i++) {
		vAddr addr = create_page();
		uint32_t v = uniform_rand(0, PAGE_TABLE_SIZE);
		store_value(addr, &v);
		printf("created page %d with value %d\n", addr, v);
	}
	
	
	print_memory_state();
	getchar();
	
	printf("ALTERNATE STORE AND GET (press enter to continue)\n");
	for (i = 0; i < 20; i++){
		int addr = uniform_rand(0, PAGE_TABLE_SIZE);
		uint32_t v = uniform_rand(0, 100);
		store_value(addr, &v);
		printf("store %d in vAddr %d\n", v, addr);
		int valid;
		addr = uniform_rand(0, PAGE_TABLE_SIZE);
		v = get_value(addr, &valid);
		if(valid) printf("got %d from addr %d\n", v, addr);
		else printf("Couldn't read from addr %d\n", addr);
	}
	
	
	print_memory_state();
	getchar();
	
	destroy_memory();
	
	return 0;
}
Exemplo n.º 15
0
bool ConvNet::test_once_random() {
	int test_x_index = uniform_rand(0, test_size_ - 1);
	return test_once(test_x_index);
}
Exemplo n.º 16
0
void rseed(long idum)
/*! Set the seed of the random number generator. */
{
  IDUM = -labs(idum);
  uniform_rand(&IDUM);
}
Exemplo n.º 17
0
void generateSyntheticData(int row, int hosts, double ** m, std::string prefix, std::string topofile, double demand_jump_factor, double demand_locality_factor, int merge_len)
{
    char filedir[200];
    sprintf(filedir,"%s%s", prefix.c_str(),"patterns");
    printf("dir=%s!\n", filedir);
	FILE * fPattern = fopen(filedir,"r");
	assert(fPattern != NULL && "Unable to open petterns file");
	int nWeeks;
	int frlt=fscanf(fPattern, "%i", &nWeeks);
	assert(frlt >= 0);
	double ** totflow = new double *[nWeeks];
	for (int curWeek = 0; curWeek < nWeeks;curWeek++)
	{
		totflow[curWeek] = new double[2016];
		for (int i = 0; i < 2016; i++)
			frlt=fscanf(fPattern, "%lf", &totflow[curWeek][i]);
	}

	const int nfft = 2016;
	kiss_fft_cfg cfg = kiss_fft_alloc(nfft, false, 0, 0);
	kiss_fft_cfg cfg2 = kiss_fft_alloc(nfft,true, 0, 0);
	kiss_fft_cpx cx_in[nfft], cx_out[nfft];


	for (int curWeek = 0; curWeek < nWeeks; curWeek++)
	{
		for (int i = 0; i < nfft; i++)
		{
			cx_in[i].r = totflow[curWeek][i];
			cx_in[i].i = 0;
		}
		kiss_fft(cfg, cx_in, cx_out);
		for (int i = 0; i < nfft; i++)
		{
			cx_out[i].r /= nfft;
			cx_out[i].i /= nfft;
		}
		int position[nfft];
		for (int i = 0; i < nfft; i++)
			position[i] = i;

		for (int i = 0; i < nfft - 1; i++)
			for (int j = i + 1; j < nfft; j++)
			{
				double n1 = cxnorm(cx_out[position[i]]);
				double n2 = cxnorm(cx_out[position[j]]);
				if (n1 < n2)
				{
					int tmp;
					tmp = position[i];
					position[i] = position[j];
					position[j] = tmp;
				}
			}
		for (int i = 6; i < nfft; i++)
		{
			double a = uniform_rand(0, 3);
			cx_out[position[i]].r *= a;
			cx_out[position[i]].i *= a;
		}
		kiss_fft(cfg2, cx_out, cx_in);
		for (int i = 0; i < nfft; i++)
			totflow[curWeek][i] = cx_in[i].r;
	}


    /*
	FILE * fFFTout = fopen("fftout", "w");
	for (int curWeek = 0; curWeek < nWeeks; curWeek++)
	{
		for (int i = 0; i < 2016; i++)
			fprintf(fFFTout, "%.2lf  ", totflow[curWeek][i]);
		fprintf(fFFTout, "\n");
	}
	fclose(fFFTout);
    */

	int nMean;
    char filedir2[200];
    sprintf(filedir2,"%s%s", prefix.c_str(),"pareto");
    printf("dir=%s!\n", filedir2);
	FILE * fpareto = fopen(filedir2, "r");
	frlt=fscanf(fpareto, "%i", &nMean);

	double * saveMean = new double[nMean];
	for (int i = 0; i < nMean; i++)
		frlt=fscanf(fpareto, "%lf", &saveMean[i]);

	//m[col][row];
	double ** Tin = new double*[hosts];
	double ** Tout = new double*[hosts];



	for (int i = 0; i < hosts; i++)
	{
		Tin[i] = new double[row];
		Tout[i] = new double[row];
		generateW(Tin[i], saveMean[getRandNum(nMean)], row,demand_jump_factor);
		generateW(Tout[i], saveMean[getRandNum(nMean)], row,demand_jump_factor);
	}
		/*
	for (int i = 0; i < hosts; i++)
	{
		printf("No%i ----------%lf        ", i, Tin[i][0]);
		for (int j = 0; j < 100;j++)
			printf("%.2lf -- ", Tin[i][j]);
		double avg = 0;
		for (int j = 0; j < row; j++)
			avg += Tin[i][j] / row;
		printf("avg == %.2lf\n\n", avg);
	}
			*/
	double tot_in, tot_out;
	for (int i = 0; i < row; i++)
	{
        tot_in = 0;
        tot_out = 0;
		for (int j = 0; j < hosts;j++)
		{
			tot_in += Tin[j][i];
			tot_out += Tout[j][i];
		}
        if (tot_in==0)
            tot_in=1;
		for (int j = 0; j < hosts; j++)
		{
			Tin[j][i] /= tot_in;
			Tout[j][i] /= tot_out;
		}
	}
	/*
	for (int i = 0; i < 5; i++)
	{
		printf("No%i ---------- \n\n\n", i);
		for (int j = 0; j < 100;j++)
			printf("%.6lf -- ", Tin[i][j]);
	}*/
	double maxDist = 0;
	double ** graph=NULL;
	if (topofile.length() > 1)
	{
		FILE* topof=fopen(topofile.c_str(), "r");
		assert(topof != NULL);
		int topo_n, topo_m;
        int tmp_rlt;
		tmp_rlt=fscanf(topof, "%i%i", &topo_n, &topo_m);
        if (tmp_rlt<0)
          printf("error! in fscanf\n");
		graph = new double *[topo_n];
		for (int i = 0; i < topo_n; i++)
			graph[i] = new double[topo_n];
		for (int i = 0; i < topo_n; i++)
			for (int j = 0; j < topo_n; j++)
				if (i != j)
					graph[i][j] = 214748368;
				else
					graph[i][j] = 0;
		for (int i = 0; i < topo_m; i++)
		{
			int va, vb;
			double edge_len;
			double tmp_rlt=fscanf(topof, "%i%i%lf", &va, &vb, &edge_len);
            if (tmp_rlt<0)
              printf("error! in fscanf\n");
			graph[va][vb] = edge_len;
			graph[vb][va] = edge_len;
		}
		for (int k = 0; k < topo_n; k++)
			for (int i = 0; i < topo_n; i++)
				if (k != i)
					for (int j = 0; j < topo_n; j++)
						if ((k != j) && (i != j))
							if (graph[i][j]>graph[i][k] + graph[k][j])
								graph[i][j] = graph[i][k] + graph[k][j];
		for (int i = 0; i < topo_n; i++)
			for (int j = 0; j < topo_n; j++)
				if (maxDist<graph[i][j])
					maxDist = graph[i][j];
		fclose(topof);
	}

    int next=0;
	int pickedTotPattern=0;
	for (int i = 0; i < row; i++)
	{
        double s_merge=0;
        for (int j=0;j<merge_len;j++)
        {
            s_merge+= totflow[pickedTotPattern][next];
            next++;
            if (next>=2016)
            {
                next=0;
                pickedTotPattern=0;
            }
        }
        s_merge/=merge_len;
		for (int j = 0; j < hosts; j++)
			for (int k = 0; k < hosts; k++){
				if (maxDist == 0)
					m[j*hosts + k][i] = s_merge * Tin[j][i] * Tout[k][i];
				else
					m[j*hosts + k][i] = s_merge * Tin[j][i] * Tout[k][i]* exp(-demand_locality_factor*  graph[j][k]/maxDist/2);
                if (isnan(m[j*hosts+k][i]))
                    printf("%lf %lf %lf", s_merge, Tin[j][i], Tout[k][i]);
                if (m[j*hosts+k][i]==0)
                    m[j*hosts+k][i]=1000;
            }
	}
	fclose(fPattern);
	fclose(fpareto);
	for (int i = 0; i < nWeeks; i++)
		delete[] totflow[i];
	delete[] totflow;
	delete[] saveMean;
	delete[] Tin;
	delete[] Tout;
}
Exemplo n.º 18
0
// Generates a normal random variable with 
// zero mean and stddev s
double gaussian_rand(double s)
{
  double U1 = uniform_rand(1);
  double U2 = uniform_rand(1);
  return s*sqrt(-2*log(U1))*cos(2*PI*U2);
}