TensorFlow.jsで遊ぶ / nds59

TensorFlow.jsで遊ぶ / nds59

第59回 長岡IT開発者勉強会 (NDS#59) で発表した資料。
テーマ「学習・AI・入学」

82d6167c4d14393c2e20b37a74b363c5?s=128

kasacchiful

March 02, 2019
Tweet

Transcript

  1. 5FOTPS'MPXKTͰ༡Ϳ ୈճ௕Ԭ*5։ൃऀษڧձ /%4   ּݪ޺ !LBTBDDIJGVM

  2. ࣗݾ঺հ w ּݪ޺ !LBTBDDIJGVM  w ৽ׁࢢࡏॅ w 3VCZ͕͓ؾʹೖΓ w

    +B445৽ׁ࣮ߦҕһ w +"846(৽ׁࢧ෦
  3. +"846(/JJHBUB  ౔ ։࠵ʂʹ͍͕ͨञͷਞʂ IUUQTKBXTVHOJJHBUBDPOOQBTTDPN

  4. ࠓճͷ಺༰ IUUQTKTUFOTPSqPXPSH

  5. 5FOTPS'MPXKTͱ͸ʁ +BWB4DSJQUͰ σΟʔϓϥʔχϯά

  6. 5FOTPS'MPXKTͱ͸ʁ w 5FOTPS'MPXͷ+BWB4DSJQU൛ w 8FCϒϥ΢β΍/PEFKTͰಈ࡞Մೳ w 5FOTPS'MPX΍,FSBTͷֶशࡁϞσϧΛίϯόʔτՄೳ w 5FOTPS'MPXKT୯ମͰ΋ֶशՄೳ w

    8FC(-͕༗ޮͳΒɺ(16΋࢖͑Δ
  7. 5FOTPS'MPXKTͰֶश // Define a model for linear regression. const model

    = tf.sequential(); model.add(tf.layers.dense({units: 1, inputShape: [1]})); // Prepare the model for training: Specify the loss and the optimizer. model.compile({loss: 'meanSquaredError', optimizer: 'sgd'}); // Generate some synthetic data for training. const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]); const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]); // Train the model using the data. model.fit(xs, ys).then(() => { // Use the model to do inference on a data point the model hasn't seen before: // Open the browser devtools to see the output model.predict(tf.tensor2d([5], [1, 1])).print(); }); Ҿ༻5SZ5FOTPS'MPXKT IUUQTDPEFQFOJPQFO FEJUBCMFUSVFFEJUPST
  8. ࠓճ΍Δ͜ͱ  ৽ׁͷ໊࢈඼ͷը૾෼ྨͰ༡Ϳ  1PTF/FUʹΑΔ࢟੎ਪఆͰ༡Ϳ

  9. ࠓճ΍Δ͜ͱ  ৽ׁͷ໊࢈඼ͷը૾෼ྨͰ༡Ϳ  1PTF/FUʹΑΔ࢟੎ਪఆͰ༡Ϳ

  10. ৽ׁͷ໊࢈඼ͷը૾෼ྨ w ೥݄ʹʮୈճ௕Ԭ*5։ൃऀษڧձ /%4 ʯͰ͓࿩ͨ͠͠಺༰Ͱ͢ɻ w ࢿྉIUUQTTQFBLFSEFDLDPNLBTBDDIJGVMOET w σϞιʔεIUUQTHJUIVCDPNLBTBDDIJGVMOETTBNQMFCMPCNBTUFS OET@WHH@TBNQMFJQZOC

  11. ֶश͢Δը૾ w ৽ׁΛ୅ද͢Δ#ڃάϧϝ w ࡫ஂࢠ w ֟ͷछʢُా੡՛ʗӽޙ੡՛ʗ࿘Ֆ԰੡՛ͳͲʣ w ΠλϦΞϯʢΈ͔͖ͮͱϑϨϯυ͸෼͚͍ͯ·ͤΜʣ w

    όεηϯλʔͷΧϨʔʢ෱ਆ௮͚ଟΊͷԫ৭͍ΧϨʔͰ ͢ʣ
  12. શମਤ ਖ਼ղϥϕϧ෇ ֶशը૾σʔλ ֶश ਪ࿦ ະ஌ͷ ը૾σʔλ 7(( Ϟσϧ ൑ఆ

    ,FSBT ֶशࡁ Ϟσϧ 5FOTPS'MPXKT ܗࣜͷ ֶशࡁϞσϧ ί ϯ ό ʔ λ ม׵
  13. ,FSBTϞσϧͷίϯόʔτ // install $ pip install tensorflowjs // convert $

    tensorflowjs_converter --input_format keras model.h5 ./model // modelͷத਎ $ ls model/ group1-shard10of17 group1-shard13of17 group1-shard16of17 group1- shard2of17 group1-shard5of17 group1-shard8of17 group1-shard11of17 group1-shard14of17 group1-shard17of17 group1- shard3of17 group1-shard6of17 group1-shard9of17 group1-shard12of17 group1-shard15of17 group1-shard1of17 group1- shard4of17 group1-shard7of17 model.json IܗࣜͷϞσϧϑΝΠϧΛίϯόʔτ͠ɺUBSHFU@EJS ͜͜Ͱ͸NPEFM ʹอଘ
  14. ,FSBTϞσϧͷίϯόʔτ import keras import tensorflowjs as tfjs model = keras.models.load_model(”./model.h5”)

    tfjs.converters.save_keras_model(model, ’./model’) 1ZUIPOεΫϦϓτ্Ͱ΋ɺ,FSBTϞσϧϑΝΠϧอଘ࣌ʹม׵Մೳ
  15. +4ͰͷϞσϧͷϩʔυ const model = await tf.loadModel(”./model/model.json”); NPEFMKTPOΛಡΈࠐΈ࣌ʹࢦఆ͢Ε͹ྑ͍

  16. σϞ

  17. ࠓճ΍Δ͜ͱ  ৽ׁͷ໊࢈඼ͷը૾෼ྨͰ༡Ϳ  1PTF/FUʹΑΔ࢟੎ਪఆͰ༡Ϳ

  18. 1PTF/FUͱ͸ʁ w 5FOTPS'MPXKTΛ࢖ͬͯϒϥ΢β্ͰϦΞϧλΠϜ࢟੎ਪ ఆͰ͖ΔϞσϧ w 5FOTPS'MPXKTͷσϞͷͭʹ͋Δ w .PCJMF/FUϕʔεͳͷͰɺਪ࿦ಈ࡞͕֓Ͷ͍ܰ

  19. 1PTF/FUͰݕग़Ͱ͖Δ෦Ґ w ඓ w ࠨ؟ɺӈ؟ w ࠨࣖɺӈࣖ w ࠨݞɺӈݞ w

    ࠨͻ͡ɺӈͻ͡ w ࠨखटɺӈखट w ࠨ৲ɺӈ৲ w ࠨͻ͟ɺӈͻ͟ w ࠨ଍टɺӈ଍ट
  20. σϞ  إʹ໨Ӆͯ͠͠༡Ϳ  ΞΩϥͰ༡Ϳ  ϘʔϧͰ༡Ϳ

  21. ࠷ޙʹ w 5FOTPS'MPXKT͸ɺ8FCϒϥ΢β্ͳͲͰਪ࿦͢Δࡍͷબ ୒ࢶͷͭ w /PEFKTͰ΋ಈ͘ͷͰɺ3BTQCFSSZ1J΍PCOJ[Ͱ΋͍͚Δ ͸ͣ w ࢟੎ਪఆͰ༡Ϳͷ͸ָ͍͠