2018/02/03 機械学習 名古屋 第14回勉強会
antimon2(後藤 俊介)
julialang
もしくは julia言語
で!
- Rのように中身がぐちゃぐちゃでなく、
- Rubyのように遅くなく、
- Lispのように原始的またはエレファントでなく、
- Prologのように変態的なところはなく、
- Javaのように硬すぎることはなく、
- Haskellのように抽象的すぎない
ほどよい言語である
- C のように高速だけど、
Ruby のような動的型付言語である- Lisp のようにプログラムと同等に扱えるマクロがあって、しかも
Matlab のような直感的な数式表現もできる- Python のように総合的なプログラミングができて、
R のように統計処理も得意で、
Perl のように文字列処理もできて、
Matlab のように線形代数もできて、
shell のように複数のプログラムを組み合わせることもできる- 超初心者にも習得は簡単で、
超上級者の満足にも応えられる- インタラクティブにも動作して、コンパイルもできる
(Why We Created Julia から抜粋・私訳)
# 24584bits(=3073bytes)の Primitive Type を定義 primitive type CIFAR10Record 24584 end
function Base.read(stream::IO, ::Type{CIFAR10Record}) bytes = read(stream, UInt8, 3073) reinterpret(CIFAR10Record, bytes)[1] end
# 例:
record0 = open("test_batch.bin", "r") do f
return read(f, CIFAR10Record)
end
function Base.show(io::IO, record::CIFAR10Record) bytes = reinterpret(UInt8, [record]) print(io, "CIFAR10Record(") # show 1st byte(=label) show(io, bytes[1]) print(io, ", ") # show hashcode of the rest of bytes(=image) show(io, hash(bytes[2:end])) print(io, ')') end
# 例:
string(record0)
# => "CIFAR10Record(0x03, 0xd0b45b812aae12b1)"
function readtrain(channel::Channel{Tuple{CIFAR10Record}}, fid::Int, datadir::String=datadir) if isopen(channel) filepath = joinpath(datadir, "data_batch_$(fid).bin") open(filepath, "r") do f while isopen(channel) for idx in shuffle(0:9999) seek(f, idx * 3073) record = read(f, CIFAR10Record) put!(channel, (record,)) end end end end end
function train_batch_produce(channel::Channel{Tuple{CIFAR10Record}}, datadir=datadir) train_channels = [Channel{Tuple{CIFAR10Record}}(32) for _=1:5] for fid in 1:5 @schedule readtrain(train_channels[fid], fid, datadir) end while true try put!(channel, take!(rand(train_channels))) catch ex for ch in train_channels close(ch) end return end end end
struct CF10Batch channel::Channel{Tuple{CIFAR10Record}} batchsize::Int end function (f::CF10Batch)() buf = reshape(reinterpret( UInt8, collect(Iterators.take(f.channel, f.batchsize)) ), (:, f.batchsize)) return (buf[2:3073, :], buf[1, :]) end
train_channel = Channel{Tuple{CIFAR10Record}}(32) @schedule train_batch_produce(train_channel) trainbatch = CF10Batch(train_channel, 128) # data, label = trainbatch() # ↑実行する度に新しいデータが返ってくる
ご清聴ありがとうございます。