$30 off During Our Annual Pro Sale. View Details »

Go로 진행하는 AI 서비스, TensorFlow for Go _ 한성민 [Go to Songdo 2023, Golang Korea]o

Go로 진행하는 AI 서비스, TensorFlow for Go _ 한성민 [Go to Songdo 2023, Golang Korea]o

Sungmin Han

July 19, 2023
Tweet

More Decks by Sungmin Han

Other Decks in Technology

Transcript

  1. Go로 진행하는 AI 서비스,
    TensorFlow for Go
    한성민
    Organizer @Golang Korea
    Next Session
    16:00 ~ 16:30

    View Slide

  2. - MLOps Lead at Riiid
    - GDE for ML
    - GDG Golang Korea Organizer
    - Python Mentor at F-Lab
    - Former) Research Engineer at Naver Clova
    - Former) Software Engineer at 심심이
    Speaker
    Golang Korea
    ML GDE

    View Slide

  3. powe
    Index
    ● Background
    ● TensorFlow
    ● TensorFlow Go
    ● Conclusion

    View Slide

  4. Background

    View Slide

  5. About Machine Learning…
    Background

    View Slide

  6. About Machine Learning…
    Background
    ID TEXT OUTPUT
    1 Golang Korea에서 진행하는 컨퍼런스가 매우 멋있습니다. 매우 긍정
    2 오늘은 Golang Korea에서 좋은 세션을 들었습니다. 긍정
    3 Golang Korea 커뮤니티가 활발하여 좋습니다. 긍정
    4 Songdo의 경치가 아름다워서 힐링되는 기분입니다. 중립
    5 Songdo의 날씨는 항상 좋아 산책하기 딱 좋습니다. 중립
    6 Songdo에 가면 사람들이 친절하여 늘 편안합니다. 긍정
    7 Golang Korea에서 진행되는 컨퍼런스는 별로였습니다. 부정
    8 Golang Korea에서 개발에 관련된 좋은 정보를 얻었습니다. 긍정
    9 Songdo는 분위기가 특이하여 재미있게 놀았습니다. 긍정
    10 Golang Korea의 컨퍼런스가 너무 지루하여 힘들었습니다. 매우 부정
    11 Go To Songdo 2023 행사에 참여하여 즐거운 시간을 보냈습니다. 긍정
    12 Songdo의 관광 명소들을 둘러보며 새로운 경험을 했습니다. 긍정

    View Slide

  7. Go에서 하면 멋질거야!
    Background
    ● 빠른 런타임 속도를 가지고 있고.

    View Slide

  8. Go에서 하면 멋질거야!
    Background
    ● 빠른 런타임 속도를 가지고 있고.
    ● 고루틴으로 경량 스레드 이용도 가능하고.

    View Slide

  9. Go에서 하면 멋질거야!
    Background
    ● 빠른 런타임 속도를 가지고 있고.
    ● 고루틴으로 경량 스레드 이용도 가능하고.
    ● cgo를 통해 C와의 호환성이 필요한 작업도 연동할 수 있고.

    View Slide

  10. Go에서 하면 멋질거야!
    Background
    ● 빠른 런타임 속도를 가지고 있고.
    ● 고루틴으로 경량 스레드 이용도 가능하고.
    ● cgo를 통해 C와의 호환성이 필요한 작업도 연동할 수 있고.

    View Slide

  11. TensorFlow

    View Slide

  12. TensorFlow eco-systems
    TensorFlow
    Data
    Data
    Data
    Data
    TF Traning
    Traning Code
    Training
    Model
    Serving
    TF Serving

    View Slide

  13. TensorFlow 생태계
    TensorFlow
    https://blog.tensorflow.org/2019/09/tensorflow-20-is-now-available.html

    View Slide

  14. TensorFlow APIs
    TensorFlow

    View Slide

  15. TensorFlow for Go
    TensorFlow
    https://www.tensorflow.org/install/lang_go?hl=ko

    View Slide

  16. TensorFlow for
    Go

    View Slide

  17. Go TensorFlow 패키지 설치
    TensorFlow

    View Slide

  18. Go TensorFlow 패키지 설치
    TensorFlow
    go get github.com/tensorflow/tensorflow/tensorflow/go
    1

    View Slide

  19. TensorFlow Session
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    package main
    import (
    tf "github.com/tensorflow/tensorflow/tensorflow/go"
    "github.com/tensorflow/tensorflow/tensorflow/go/op"
    "fmt"
    )
    func main() {
    s := op.NewScope()
    c := op.Const(s, "Hello from TensorFlow version " + tf.Version())
    graph, err := s.Finalize()
    # err handling
    sess, err := tf.NewSession(graph, nil)
    # err handling
    output, err := sess.Run(nil, []tf.Output{c}, nil)
    # err handling
    }

    View Slide

  20. Go TensorFlow 패키지 설치
    TensorFlow

    View Slide

  21. Go TensorFlow 패키지 설치
    TensorFlow
    Tensorflow + Go, the gopher way

    View Slide

  22. TensorFlow Session in tfgo
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    package main
    import (
    "fmt"
    tg "github.com/galeone/tfgo"
    tf "github.com/galeone/tensorflow/tensorflow/go"
    )
    func main() {
    root := tg.NewRoot()
    A := tg.NewTensor(root, tg.Const(root, [2][2]int32{{1, 2}, {-1, -2}}))
    x := tg.NewTensor(root, tg.Const(root, [2][1]int64{{10}, {100}}))
    b := tg.NewTensor(root, tg.Const(root, [2][1]int32{{-10}, {10}}))
    Y := A.MatMul(x.Output).Add(b.Output)
    Z := A.Clone()
    results := tg.Exec(root, []tf.Output{Y.Output, Z.Output}, nil, &tf.SessionOptions{})
    fmt.Println("Y: ", results[0].Value(), "Z: ", results[1].Value())
    fmt.Println("Y == A", Y == A) // ==> true
    fmt.Println("Z == A", Z == A) // ==> false
    }

    View Slide

  23. Output
    1
    2
    3
    Y: [[200] [-200]] Z: [[200] [-200]]
    Y == A true
    Z == A false

    View Slide

  24. TensorFlow on Python
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    import tensorflow as tf
    model = tf.keras.Sequential(
    [
    tf.keras.layers.Conv2D(
    8,
    (3, 3),
    strides=(2, 2),
    padding="valid",
    input_shape=(28, 28, 1),
    activation=tf.nn.relu,
    name="inputs",
    ), # 14x14x8
    tf.keras.layers.Conv2D(
    16, (3, 3), strides=(2, 2), padding="valid", activation=tf.nn.relu
    ), # 7x716
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(10, name="logits"), # linear
    ]
    )
    tf.saved_model.save(model, "output/keras")

    View Slide

  25. TensorFlow on Go
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    package main
    import (
    "fmt"
    tg "github.com/galeone/tfgo"
    tf "github.com/galeone/tensorflow/tensorflow/go"
    )
    func main() {
    model := tg.LoadModel("test_models/output/keras", []string{"serve"}, nil)
    modelInput, _ := tf.NewTensor([1][28][28][1]float32{}) // dummy
    results := model.Exec([]tf.Output{
    model.Op("StatefulPartitionedCall", 0),
    }, map[tf.Output]*tf.Tensor{
    model.Op("serving_default_inputs_input", 0): modelInput,
    })
    predictions := results[0]
    fmt.Println(predictions.Value())
    }

    View Slide

  26. TensorFlow on Go

    View Slide

  27. TensorFlow Session
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    package main
    import (
    tf "github.com/galeone/tensorflow/tensorflow/go"
    "github.com/galeone/tensorflow/tensorflow/go/op"
    "fmt"
    )
    func main() {
    s := op.NewScope()
    c := op.Const(s, "Hello from TensorFlow version " + tf.Version())
    graph, err := s.Finalize()
    # err handling
    sess, err := tf.NewSession(graph, nil)
    # err handling
    output, err := sess.Run(nil, []tf.Output{c}, nil)
    # err handling
    }

    View Slide

  28. Output

    View Slide

  29. Conclusion

    View Slide

  30. 그래서 TensorFlow Go 되나요?
    Conclusion
    네 됩니다…

    View Slide

  31. TensorFlow Go를 쓰려면
    Conclusion
    Operation System의 Tensorflow Library 레벨의
    호환성과 모듈을 잘 맞춰준다면 가능해보입니다.

    View Slide

  32. 솔직한 소감은?
    Conclusion
    음..

    View Slide

  33. 솔직한 소감은?
    Conclusion

    View Slide

  34. 솔직한 소감은?
    Conclusion
    Machine Learning에서 C++ Binding을 대체할 수
    는 있어 보이나, 생태계가 아직 성장할 여지가 많은
    듯 합니다.

    View Slide

  35. 솔직한 소감은?
    Conclusion
    모델 학습은 Python으로 쓰는게 제일 좋고,
    서빙쪽은 C++을 대체하는 서빙 인터페이스 좋아보
    입니다.

    View Slide

  36. wdep[=
    \=[-0 b
    Q & A

    View Slide