Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Juliaで機械学習

Avatar for Daniel Daniel
September 26, 2020

 Juliaで機械学習

情報科学若手の会2020にて発表させていただきました。
Juliaで機械学習をすると良いことについてまとめています。

PDFにリンクを埋め込んでいたのですが、Speaker Deckではリンクがクリックできないようなので、気になる人はダウンロードして見てください。

Avatar for Daniel

Daniel

September 26, 2020
Tweet

More Decks by Daniel

Other Decks in Programming

Transcript

  1. 書きやすい⼀例 ⾏列計算は簡単に使える julia> A = [1 2 3 4] 2×2

    Array{Int64,2}: 1 2 3 4 julia> A * A 2×2 Array{Int64,2}: 7 10 15 22 julia> A .* A 2×2 Array{Int64,2}: 1 4 9 16 5 5
  2. プロット julia> using Plots julia> x = 0:0.1:10 0.0:0.1:10.0 julia>

    y = sin.(x) # . は全てに適⽤ 101-element Array{Float64,1}: 0.0 0.09983341664682815 ⋮ -0.3664791292519284 -0.5440211108893698 julia> plot(x,y) 6 6
  3. 速い C Julia LuaJIT Rust Go Fortran Java JavaScript Matlab

    Mathematica Python R Octave iteration_pi_sum matrix_multiply matrix_statistics parse_integers print_to_file recursion_fibonacci recursion_quicksort userfunc_mandelbrot benchmark benchmark 100 101 102 103 104 Julia Micro-Benchmarks 7 7
  4. 9 9

  5. 簡単に記述できる モデルの作り⽅ julia> using Flux julia> model = Chain( Dense(784,

    128, relu), Dense(128, 10), logsoftmax, ) Chain(Dense(784, 128, relu), Dense(128, 10), logsoftmax) 10 10
  6. モデルの使い⽅ julia> model(rand(784,5)) # ランダムなデータを5 つ⼊⼒ 10×5 Array{Float32,2}: -2.0062 -3.1991

    -3.16117 -3.63618 -2.62927 -3.19613 -2.61039 -2.42026 -2.23364 -2.3462 -2.49663 -1.99805 -2.24729 -2.23044 -2.43747 -3.3243 -2.53268 -2.64656 -2.75284 -2.51889 ⋮ -2.40612 -2.15469 -2.11569 -2.35603 -2.74064 -2.17706 -2.36897 -2.75496 -2.08347 -2.19851 -3.70686 -3.23972 -3.1855 -3.24699 -2.74348 学習については Pytorch と似たような記述⽅法になってい ます。 11 11
  7. ⾃動微分 julia> f(x) = 2x + 1 # 関数定義 f

    (generic function with 1 method) julia> f(2) 5 julia> f'(2) # f(x) を微分して2 を代⼊して計算, f'(x) = 2 2 julia> @code_llvm f'(2) # LLVM の表現を⾒る define i64 @"julia_#43_13375"(i64) { top: ret i64 2 } 多変数関数のためにはより汎⽤な gradient 関数が⽤意さ れています。 12 12
  8. ⾃分の活性化関数を定義 julia> Chain( Dense(784, 128), x -> max.(0, x), #

    ReLU を定義しています Dense(128, 10), logsoftmax ) julia> model(rand(784,2)) 10×2 Array{Float32,2}: -2.36733 -3.14914 -1.59384 -2.54492 ⋮ 13 13
  9. ⾃分のレイヤーを定義 struct MyDense # レイヤーの構造体定義 W # 重み B #

    バイアス end # ⼊出⼒の個数から初期化できるようにする MyDense(input::Integer, output::Integer) = MyDense(randn(output, input), randn(output)) # レイヤーの動作の定義 (m::MyDense)(x) = m.W * x .+ m.B Flux.@functor MyDense # パラメータを学習可能にする これだけの定義で Flux のモデルに組み込めます。 14 14
  10. 便利なライブラリ 汎⽤ライブラリ Plots.jl: グラフの描写 PyCall.jl: Python の読み込み DataFrames.jl: Pandas のようにデータを格納できる

    CUDA.jl: GPU を使える 機械学習⽤ライブラリ Zygote: ⾃動微分が実装されている Metalhead: VGG 等のモデル DifferentialEquations.jl: 微分⽅程式を解ける model zoo: Flux で実装されたモデルいろいろ 16 16