2017/07/02 機械学習 名古屋 第11回勉強会
antimon2(後藤 俊介)
- Rのように中身がぐちゃぐちゃでなく、
- Rubyのように遅くなく、
- Lispのように原始的またはエレファントでなく、
- Prologのように変態的なところはなく、
- Javaのように硬すぎることはなく、
- Haskellのように抽象的すぎない
ほどよい言語である
- C のように高速だけど、
Ruby のような動的型付言語である- Lisp のようにプログラムと同等に扱えるマクロがあって、しかも
Matlab のような直感的な数式表現もできる- Python のように総合的なプログラミングができて、
R のように統計処理も得意で、
Perl のように文字列処理もできて、
Matlab のように線形代数もできて、
shell のように複数のプログラムを組み合わせることもできる- 超初心者にも習得は簡単で、
超上級者の満足にも応えられる- インタラクティブにも動作して、コンパイルもできる
(Why We Created Julia から抜粋・私訳)
julia> 1 + 2 - 3 * 4 # 四則演算(除算以外)
-9
julia> 7 / 5 # `整数 / 整数` の結果は浮動小数
1.4
julia> 7 ÷ 5 # `整数 ÷ 整数` の結果は整数
1
julia> 2 ^ 10 # 冪乗は `^`
1024
julia> 123 & 234 | 345 # 論理積 / 論理和
376
julia> 123 ⊻ 234 # 排他的論理和(==`xor(123, 234)`)
145
julia> a = [1, 2, 3, 4, 5]
5-element Array{Int64,1}:
1
2
3
4
5
julia> a[1] # Julia は 1-origin
1
julia> println(a[2:3]) # 範囲指定は両端含む
[2, 3]
julia> a = [n^2 for n=1:5]
5-element Array{Int64,1}:
1
4
9
16
25
julia> A = [x+10y for y=1:3, x=1:3]
3×3 Array{Int64,2}:
11 12 13
21 22 23
31 32 33
julia> [(a,b,c) for c=1:15,b=1:15,a=1:15 if a^2+a*b+b^2==c^2]
6-element Array{Tuple{Int64,Int64,Int64},1}:
(3, 5, 7)
(5, 3, 7)
(6, 10, 14)
(7, 8, 13)
(8, 7, 13)
(10, 6, 14)
julia> x = [1., 2., 3.];
julia> y = [3., 1., 2.];
julia> x + y # `x .+ y` と書いても同じ(elementwise operation)
[4., 3., 5.]
julia> x .* y # これは `x * y` と書くとNG
[3., 2., 6.]
julia> x ⋅ y # 内積(dot積、`dot(x, y)` と書いても同じ)
11.0
julia> x × y # 外積(cross積、`cross(x, y)` と書いても同じ)
[1., 7., -5.]
julia> A = [1 2; 3 4] # この記法は MATLAB/Octave 由来
2×2 Array{Int64,2}:
1 2
3 4
julia> A.' # `○.'` は転置行列の記法(これも MATLAB/Octave 由来)
2×2 Array{Int64,2}:
1 3
2 4
julia> A = [1 2; 3 4]; B = [3 0; 0 6];
julia> A + B # A .+ B でも同様
2×2 Array{Int64,2}:
4 2
3 10
julia> A * B # matrix multiply
2×2 Array{Int64,2}:
3 12
9 24
julia> A .* B # elementwise multiply
2×2 Array{Int64,2}:
3 0
0 24
julia> sin(0.1)
0.09983341664682815
julia> sin.([0.1, 0.2, 0.3, 0.4])
4-element Array{Float64,1}:
0.0998334
0.198669
0.29552
0.389418
julia> [0.1, 0.2, 0.3, 0.4] .^ 2
# => [0.01, 0.04, 0.09, 0.16]
julia> f(x) = x^2 + 2x - 1
f (generic function with 1 method)
julia> f(1)
2
julia> f.(1:5)
# => [2, 7, 14, 23, 34]
julia> 1//2 == 0.5
true
julia> 1//2 - 1//3
1//6
julia> 1im ^ 2 == -1
true
julia> (1.0 + 0.5im) * (2.0 - 3.0im)
3.5 - 2.0im
参照: 竹内関数(マッカーシー版)
Julia でのコード例:
function tak(x::Int, y::Int, z::Int)::Int if x <= y z else tak(tak(x - 1, y, z), tak(y - 1, z, x), tak(z - 1, x, y)) end end tak(22, 11, 0) # => 11
\(a^2 + b^2 = 100c + d\)
\(c^2 + d^2 = 100a + b\)
を満たす2桁以下の正整数の組 (a, b, c, d) をすべて求めよ。
Julia でのコード例:
[(a,b,c,d) for d=1:99,c=1:99,b=1:99,a=1:99 if a^2+b^2==100c+d&&c^2+d^2==100a+b] # => [(12, 33, 12, 33) # (38, 69, 62, 5) # (59, 65, 77, 6) # (62, 5, 38, 69) # (77, 6, 59, 65) # (88, 33, 88, 33)]
@time
マクロで時間計測time
モジュールを利用:set +s
を利用import Data.Time
を利用実行時間(s)
Julia | Python | GHCi | GHC | |
---|---|---|---|---|
ex1 | 1.78 | 91.67 | 189.05 | 1.44 |
ex2 | 2.63 | 20.66 | 174.26 | 1.40 |
# Pkg.add("MXNet") using MXNet
mlp = @mx.chain mx.Variable(:data) => mx.FullyConnected(name=:fc1, num_hidden=128) => mx.Activation(name=:relu1, act_type=:relu) => mx.FullyConnected(name=:fc2, num_hidden=64) => mx.Activation(name=:relu2, act_type=:relu) => mx.FullyConnected(name=:fc3, num_hidden=10) => mx.SoftmaxOutput(name=:softmax)
# setup model model = mx.FeedForward(mlp, context=mx.cpu()) # optimization algorithm (Momentum SGD) optimizer = mx.SGD(lr=0.1, momentum=0.9) # fit parameters mx.fit(model, optimizer, train_data, n_epoch=20, eval_data=eval_data)
# prediction probs = mx.predict(model, eval_data) # collect all labels from eval data labels = vcat(([label for label in copy(mx.get(eval_data, batch, :softmax_label))] for batch in eval_data)...) # compute the accuracy accuracy = 100 * mean(vec(mapslices(indmax, probs, 1)) .== labels .+ 1) println(mx.format("Accuracy on eval set: {1:.2f}%", accuracy)) # => Accuracy on eval set: 97.90%
using Mux using MXNet import JSON model = mx.load_checkpoint("MXNET_CNN_20170625", 0, mx.FeedForward)
function classify(a::Vector{Float32}) image = reshape(a, (28, 28, 1, 1)) result = mx.predict(model, mx.ArrayDataProvider(image)) return vec(result) end function classify(a::Vector) classify(convert(Vector{Float32}, a)) end
function classify(req::Dict) params = parseQuery(req) return try JSON.json(classify(JSON.parse(params[:x]))) catch JSON.json(zeros(Float32, 10)) end end
@app sv = ( Mux.defaults, page(req->Mux.fresp("classify.html")), branch(req->req[:method]=="POST"&&length(req[:path])==1&&Mux.matchpath!(["classify"], req), classify), Mux.notfound()) serve(sv)
<script> // (ry function classify() { var myCanvas = document.getElementById("myCanvas"); var ctx = myCanvas.getContext("2d"); var data = []; // (ry: canvas からピクセルデータを読み込む処理 jQuery.ajax("classify", { dataType: "json", data: "x=" + JSON.stringify(data), type: "POST", success: function (res) { // (ry: 整形して表示する処理 } }); } // (ry </script>
<div id="board"> <canvas id="myCanvas" width="112px" height="112px"></canvas> <p> <button id="classify" onclick="classify()">Classify</button> <button id="clear" onclick="myClear()">Clear</button> </p><p> Result: <input type="text" id="result_output" size="5" value=""> <br><textarea id="result_detail" cols="30" rows="10"></textarea> </p> </div>
julialang
で!)ご清聴ありがとうございます。