-
[기초 1] tensorflow 2 keras 모델 정의딥러닝/tensorflow 2020. 4. 22. 23:53
keras 모델 정의는 2가지로 나뉘어져 있다.
첫 번째는 Sequential model
두 번째는 tensorflow.keras.Model 을 상속받은 custom model
그럼 Dense Layer 로만 구성된 모델로 예제를 구성하겠다.
1. Sequential model
import tensorflow as tf from tensorflow.keras import layers # make sequential model instance def makeModel(): sequential_model = tf.keras.Sequential() # hidden layer sequential_model.add(layers.Dense(64, activation='relu')) sequential_model.add(layers.Dense(64, activation='relu')) # output layer sequential_model.add(layers.Dense(10, activation='softmax')) return sequential_model if __name__ == "__main__": my_model = makeModel()
2. custom model
import tensorflow as tf from tensorflow.keras import layers # custom model class myModel(tf.keras.Model): # make layers in init function def __init__(self): super(myModel, self).__init__() self.hidden_layer_1 = layers.Dense(64, activation='relu') self.hidden_layer_2 = layers.Dense(64, activation='relu') self.output_layer = layers.Dense(10, activation='softmax') # make graph (flow data) def call(self, input): hidden_data_1 = self.hidden_layer_1(input) hidden_data_2 = self.hidden_layer_2(input) output_data = self.output_layer(hidden_data) return output_data if __name__ == "__main__": my_model = myModel()
둘의 차이는 한눈에도 들어오지만 굳이 꼽자면 크게 두가지이다.
첫 번째는
Sequential은 layer를 순서대로 넣어주면 자동적으로 graph가 그려지는 것에 반해,
custom model은 직접 call function 안에서 graph를 그려줘야 한다.
여기서 graph를 그린다는 의미는 데이터의 흐름도라고 생각하면 편하다.
아래 링크에서 그래프를 시각화한 것을 잘 표현하였으므로 참조하면 이해하기 편하다.
https://tensorflowkorea.gitbooks.io/tensorflow-kr/content/g3doc/how_tos/graph_viz/
그래프 시각화 · 텐서플로우 문서 한글 번역본
No results matching ""
tensorflowkorea.gitbooks.io
두 번째는
Sequential은 특별한 function, class 정의가 필요없이 바로 instance 생성이 가능하지만,
custom model은 class로 상속받아 새로 정의한 후 instance를 생성해야 한다.
자세한 용어 설명 참조
딥러닝 용어
Sequential
https://www.tensorflow.org/guide/keras/overview?hl=ko#sequential_모델
케라스: 빠르게 훑어보기 | TensorFlow Core
Note: 이 문서는 텐서플로 커뮤니티에서 번역했습니다. 커뮤니티 번역 활동의 특성상 정확한 번역과 최신 내용을 반영하기 위해 노력함에도 불구하고 공식 영문 문서의 내용과 일치하지 않을 수 있습니다. 이 번역에 개선할 부분이 있다면 tensorflow/docs 깃헙 저장소로 풀 리퀘스트를 보내주시기 바랍니다. 문서 번역이나 리뷰에 참여하려면 docs-ko@tensorflow.org로 메일을 보내주시기 바랍니다. tf.keras 임포트 tf.keras는 케
www.tensorflow.org
tensorflow.keras.Model
https://www.tensorflow.org/api_docs/python/tf/keras/Model
tf.keras.Model | TensorFlow Core v2.1.0
See Stable See Nightly Model groups layers into an object with training and inference features. View aliases Main aliases tf.keras.models.Model Compat aliases for migration See Migration guide for more details. tf.compat.v1.keras.Model, tf.compat.v1.keras.
www.tensorflow.org
컴퓨터 용어
상속
위키독스
온라인 책을 제작 공유하는 플랫폼 서비스
wikidocs.net
super 사용법
https://bluese05.tistory.com/5
python super() 함수와 2.X 와 3.X 사용법
Python Super() 함수 Python에서 다중 상속시 발생할 수 있는 문제점이 있다. 이 현상은 Python 뿐만 아니라 다중 상속이 가능한 어떤 언어에서나 발생할 수 있는 문제이다. 아래와 같은 상황을 보자. D 클래스가..
bluese05.tistory.com
'딥러닝 > tensorflow' 카테고리의 다른 글
[기초 3] data 가공 (0) 2020.06.22 [기초 2] tensorflow 2 keras 모델 인스턴스 화 (0) 2020.05.02 [기초 0] tensorflow 2 keras 기초 (0) 2020.04.22 [tensorflow 2] HardNet 모델 (0) 2020.04.22 [tensorflow 2] loss, optimizer 유동적으로 가져오기 (0) 2020.04.22