0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python / numpy > QMC(Halton_sequence)に基づく要素の取り出しの実装

0
Last updated at Posted at 2016-10-22
動作環境
Ubuntu 14.04 LTS desktop amd64
GeForce GTX 750 Ti
ASRock Z170M Pro4S [Intel Z170chipset]
TensorFlow v0.11
cuDNN v5.1 for Linux
CUDA v7.5
Python 2.7.6
IPython 5.1.0 -- An enhanced Interactive Python.

TensorFlowにおいてmnist.next_batch()の代わりに使う予定のQMCコード。
小さなコードで動作確認をしてみた。

imglistからHalton Sequenceを用いて要素を取り出す。
関連 http://qiita.com/7of9/items/7a8920531fd715d0b75a
関連 http://qiita.com/7of9/items/9db1251e714952794023

v0.1

sample.py
import numpy as np

imglist = np.array([10,20,30,40,50,60,70,80,90])

def Halton_sequence(i0):
    xbase = 2
    ybase = 3

    invxbase = 1.0 / xbase
    facx = 1.0 / xbase

    invybase = 1.0 / ybase
    facy = 1.0 / ybase

    inp = i0
    x0 = 0.0
    while inp > 0:
        x0 = x0 + (inp % xbase) * invxbase
        inp = inp / xbase
        invxbase = invxbase * facx

    inp = i0
    y0 = 0.0
    while inp > 0:
        y0 = y0 + (inp % ybase) * invybase
        inp = inp / ybase
        invybase = invybase * facy

    return x0, y0

length = len(imglist)

idx = np.array([])
for i0 in range(10):
	# discard 2nd
	res, _ = Halton_sequence(i0)
	# print res * length
	idx = np.append(idx, res * length)

print idx
print imglist[idx.astype(int)]
結果
$ python sample.py 
[ 0.      4.5     2.25    6.75    1.125   5.625   3.375   7.875   0.5625
  5.0625]
[10 50 30 70 20 60 40 80 10 60]

v0.2 QMC_getIndex()追加

sample.py
'''
v0.2 2016 Oct. 22
	- add QMC_getIndex()
v0.1 2016 Oct. 22
	- use Halton_sequence() together with unorganized code
'''

import numpy as np

imglist = np.array([10,20,30,40,50,60,70,80,90])

def Halton_sequence(i0):
    xbase = 2
    ybase = 3

    invxbase = 1.0 / xbase
    facx = 1.0 / xbase

    invybase = 1.0 / ybase
    facy = 1.0 / ybase

    inp = i0
    x0 = 0.0
    while inp > 0:
        x0 = x0 + (inp % xbase) * invxbase
        inp = inp / xbase
        invxbase = invxbase * facx

    inp = i0
    y0 = 0.0
    while inp > 0:
        y0 = y0 + (inp % ybase) * invybase
        inp = inp / ybase
        invybase = invybase * facy

    return x0, y0

def QMC_getIndex(listSize, start, getsize):
	idx = np.array([])
	for i0 in range(start, start + getsize):
		res, _ = Halton_sequence(i0) # discard 2nd dim
		idx = np.append(idx, res * listSize)
	return idx

length = len(imglist)
idx = QMC_getIndex(length, 0, 10)
print "idx:",idx
print "img:",imglist[idx.astype(int)]

idx = QMC_getIndex(length, 10, 10)
print "idx:",idx
print "img:",imglist[idx.astype(int)]
結果
$ python sample.py 
idx: [ 0.      4.5     2.25    6.75    1.125   5.625   3.375   7.875   0.5625
  5.0625]
img: [10 50 30 70 20 60 40 80 10 60]
idx: [ 2.8125   7.3125   1.6875   6.1875   3.9375   8.4375   0.28125  4.78125
  2.53125  7.03125]
img: [30 80 20 70 40 90 10 50 30 80]
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?