안녕하세요.

이번 글에서는 tensorflow와 keras의 관계에 대해서 알아보도록 하겠습니다.

 

 

 

1. Keras and Multi-Backend

Keras는 기본적으로 high level API 입니다. 아래 링크에서 언급하고 있듯이 딥러닝의 아주 기본적인 연산들(ex: tensor들의 연산 or 미분)이나 CPU, GPU, TPU 같은 hardware를 동작시키는 같은 작업(ex: CUDA)들은 Keras에서 구현하지 않습니다.  

 

https://keras.io/about/

 

Keras documentation: About Keras

About Keras Keras is a deep learning API written in Python, running on top of the machine learning platform TensorFlow. It was developed with a focus on enabling fast experimentation. Being able to go from idea to result as fast as possible is key to doing

keras.io

 

하지만, Keras가 딥러닝 학습을 하려면 앞서 언급한 딥러닝의 기본적인 연산을 해야하는데, 이를 어떻게 사용할 수 있을까요?

 

Keras는 backend engine에서 제공하는 특화된 tensor library를 사용함으로써 딥러닝의 기본적인 연산을 수행합니다. 예를 들어, tensorflow, theano, CNTK 라는 딥러닝 프레임워크를 backend로 사용합니다. 다양한 backend (tensorflow, theano, CNTK) 를 사용한다고 하여 아래와 같이 표현하기도 합니다.

 

"Kerass는 multi-backend(멀티 백엔드)를 지원합니다"

  • Theano: developed by LISA Lab at Université de Montréal → http://deeplearning.net/software/theano
  • Tensorflow: developed by Google https://www.tensorflow.org
  • CNTKL: developed by Microsoft http://github.com/Microsoft/CNTK

이미지 출처: https://www.digikey.de/en/maker/projects/getting-started-with-machine-learning-using-tensorflow-and-keras/0746640deea84313998f5f95c8206e5b

 

https://keras.io/ko/backend/

 

Backend - Keras Documentation

Keras 백엔드 "백앤드"는 무엇인가요? Keras는 딥러닝 모델을 개발하기 위한 고수준의 구성요성 요소를 제공하는 모델 레벨의 라이브러리입니다. Keras는 텐서 곱셈, 합성곱 등의 저수준의 연산을 제

keras.io

 

Backend를 설정하는 방식은 아래의 순서를 따르면 됩니다.

  1. keras.json이 있는 경로로 접속
    • ex) C:\Users\(사용자이름)\.keras\keras.json
  2. keras.json 파일에서 backend 필드부분에 사용하려는 backend를 설정해줌
//backend로 Tensorflow 설정
{
    "image_dim_ordering": "channels_last",
    "epsilon": 1e-07,
    "floatx": "float32",
    "backend": "tensorflow"
}

 

//Backend로 Theano 설정
{
    "image_dim_ordering": "th",
    "epsilon": 1e-07,
    "floatx": "float32",
    "backend": "theano"
}

 

 

 

 

2. Tensorflow와 Keras의 관계 (Tensorflow 2.5 & Keras 2.4)

예전에는 기존 케라스 사용자들이 Keras API를 사용하면서 backend로 종종 tensorflow를 사용했습니다.

 

하지만, 2020.06.18에 케라스가 2.4 버전을 release 하면서 아래와 같은 말을 했습니다. (현재(2021.06.30)까지도 keras 최신 버전은 2.4입니다)

 

"We have discontinued multi-backend Keras to refocus exclusively on the TensorFlow implementation of Keras."

 

"In the future, we will develop the TensorFlow implementation of Keras in the present repo, at keras-team/keras. For the time being, it is being developed in tensorflow/tensorflow and distributed as tensorflow.keras. In this future, the keras package on PyPI will be the same as tf.keras."

 

 

(↓↓Keras 2.4 Release↓)

https://github.com/keras-team/keras/releases

 

Releases · keras-team/keras

Deep Learning for humans. Contribute to keras-team/keras development by creating an account on GitHub.

github.com

 

 

결국 multi-backend를 더 이상 지원하지 않고 오로지 tensorflow backend만을 지원할 것이라고 선언했습니다. 기존에는 Keras-team/keras라는 repo에서 다양한 backend를 지원하는 keras API를 업데이트하고 있었는데, 이제는 tf.keras에서 keras 관련 기능들을 업데이트 할거라고 명시했습니다.

 

다시 말해, keras-team/keras 저장소는 오로지 tensorflow만을 위한 tensorflow 백엔드 전용 Keras 되고 tf.keras가 keras-team/keras를 사용할 것으로 예상되었습니다. 

 

(↓↓↓tf.keras↓↓↓)

https://www.tensorflow.org/api_docs/python/tf/keras?hl=ko 

 

Module: tf.keras  |  TensorFlow Core v2.5.0

Implementation of the Keras API, the high-level API of TensorFlow.

www.tensorflow.org

 

 

 

 

 

3. Keras separate from Tensorflow 2.6 (2021.06.30)

아래 박혜선님의 글을 인용하면 올해(2021) 초부터 keras-team/keras 저장소의 코드가 다시 채워지고 있었다고 합니다. 당시에는 tf.keras 코드를 기반으로 keras-team/keras에 코드를 그대로 복사 붙여넣기 하여 싱크를 맞추는 작업(복붙)이 대부분이였다고 하네요. 그래서 tensorflow 2.5를 설치하면 keras-nightly 패키지(←복사붙여넣기된 keras-team/keras)도 같이 설치가되어 별도의 패키지로써 제공했다고 합니다. 

 

(↓↓ 박혜선님 글 ↓)

https://tensorflow.blog/2021/05/18/keras-nightly-%ed%8c%a8%ed%82%a4%ec%a7%80/

 

keras-nightly 패키지

약 1년 전 케라스는 내부 구현을 모두 텐서플로로 리다이렉션하는 2.4 버전을 릴리스하면서 멀티 백엔드 케라스 버전의 종료를 알렸습니다. 향후에 keras-team/keras 저장소는 텐서플로 백엔드 전용

tensorflow.blog

 

 

그리고, 2021.06.30일에 Tensorflow 2.6 버전이 release 되면서, tensorflow 진영에서 아래와 같은 선언을 하게 됩니다.

  • Keras has been split into a separate PIP package (keras), and its code has been moved to the GitHub repository keras-team/keras. → Tensorflow 2.6 버전부터 keras는 tensorflow 패키지에서 분리가 됩니다. 앞으로 tensorflow 2.6 이후 부터 업데이트되는 Keras 관련 코드들은 다시 keras-team/keras로 옮겨진다고 하니 앞으로 "import keras"를 다시 사용해야 할 것 같습니다.
  • The API endpoints for tf.keras stay unchanged, but are now backed by the keras PIP package. All Keras-related PRs and issues should now be directed to the GitHub repository keras-team/keras. 

https://github.com/tensorflow/tensorflow/releases

 

Releases · tensorflow/tensorflow

An Open Source Machine Learning Framework for Everyone - tensorflow/tensorflow

github.com

 

 

결국, Keras가 다시 별도의 패키지로 분리된다는 말인데, 왜 이렇게 분리했는지는 아직 잘 모르겠네요. 아래와 같은 의문을 품고 글을 마무리 하도록 하겠습니다!

  1. 다시 multi-backend를 지원할까? 아니면 다른 backend를 고려하고 있는건가?
  2. tensorflow 2.6부터 import keras와 import tensorflow.keras가 동시에 사용이 될까? import keras 만 사용이 될까?
  3. tensorflow 2.6부터는 import keras로만 사용해야 업데이트되는 keras 기능을 사용할 수 있는걸까?

 

 

 

[Reference Sites]

https://tensorflow.blog/%EC%BC%80%EB%9D%BC%EC%8A%A4-%EB%94%A5%EB%9F%AC%EB%8B%9D/3-2-%EC%BC%80%EB%9D%BC%EC%8A%A4-%EC%86%8C%EA%B0%9C/

 

3.2 케라스 소개

3. 신경망 시작하기 | 목차 | 3.3 딥러닝 컴퓨터 셋팅   이 책에서는 코드 예제를 위해 케라스( 사용합니다. 케라스는 거의 모든 종류의 딥러닝 모델을 간편하게 만들고 훈련시킬 수 있는 파이썬

tensorflow.blog

 

'Tensorflow > 1.Basic' 카테고리의 다른 글

1.Tensor에 대한 기본 코드  (0) 2021.06.30

+ Recent posts