8/31 과제 제출 - AI 서비스 완성! AI 웹개발 취업캠프 [NIPA/정보통신산업진흥원]

Alicia·2023년 9월 1일
0

AI_Web_nipa

목록 보기
28/31

Keras에 대해 조사

케라스 원칙

케라스는 ▲사용자 친화성모듈형손쉬운 확장파이썬과의 연계를 기본 원칙 삼아 개발됐다.

"기계가 아닌 사람을 위해 설계됐으며 인지 부하를 낮추기 위한 모범 사례에 따른다."

주요 특징

간단한 API: Keras는 직관적이고 간결한 API를 제공하여 딥러닝 모델을 빠르게 정의하고 학습할 수 있습니다. 이를 통해 개발자는 모델 구조를 명확하게 이해하고 디자인할 수 있습니다.

모듈화: Keras 모델은 레이어의 스택으로 구성됩니다. 이러한 레이어는 모듈화되어 있어 각 레이어를 쉽게 추가하거나 제거할 수 있습니다.

다양한 백엔드 지원: Keras는 TensorFlow, Theano, CNTK와 같은 여러 딥러닝 백엔드 엔진을 지원합니다. 이는 사용자가 적절한 백엔드를 선택하여 딥러닝 모델을 학습시킬 수 있도록 합니다.

사용자 정의 가능: Keras는 모델, 레이어, 손실 함수, 메트릭, 초기화 등을 사용자 정의하거나 확장할 수 있는 풍부한 기능을 제공합니다.

커뮤니티와 생태계: Keras는 활발한 사용자 커뮤니티와 다양한 예제, 튜토리얼, 모델 아키텍처를 제공합니다. 이러한 생태계는 사용자가 딥러닝 모델을 개발하고 공유하는 데 도움이 됩니다.

종류

TensorFlow Keras: TensorFlow 프레임워크의 공식 고수준 API로서, TensorFlow 2.x 버전부터는 TensorFlow Keras가 TensorFlow의 일부로 통합되었습니다. 따라서 TensorFlow를 백엔드 엔진으로 사용하는 경우 TensorFlow Keras를 사용하는 것이 자연스럽습니다. TensorFlow Keras는 TensorFlow와 완전히 통합되어 있으며 TensorFlow의 기능과 생태계를 활용할 수 있습니다.

standalone Keras: 이전에는 TensorFlow, Theano, CNTK와 같은 다양한 백엔드 엔진과 함께 독립형 Keras 버전이 사용되었습니다. 그러나 TensorFlow Keras가 표준화되면서 새로운 프로젝트에서는 TensorFlow Keras를 사용하는 것이 권장됩니다. 독립형 Keras는 더 이상 업데이트되지 않으며 새로운 기능이나 개선 사항이 TensorFlow Keras에 집중됩니다.

케라스 모델

모델은 핵심 케라스 데이터 구조다. 케라스의 두 가지 주 모델 유형은 시퀀셜(Sequential) 모델, 그리고 함수 API에 사용되는 모델(Model) 클래스다.

케라스 시퀀셜 모델
시퀀셜 모델은 계층의 선형적인 스택이며 계층은 아주 단순하게 기술이 가능하다. 다음은 케라스 문서에 있는 model.add()를 사용해 시퀀셜 모델에 두 개의 밀집 계층을 정의하는 예제다.

import keras
from keras.models import Sequential
from keras.layers import Dense

#Create Sequential model with Dense layers, using the add method
model = Sequential()

#Dense implements the operation:
#        output = activation(dot(input, kernel) + bias)
#Units are the dimensionality of the output space for the layer,
#     which equals the number of hidden units
#Activation and loss functions may be specified by strings or classes
model.add(Dense(units=64, activation='relu', input_dim=100))
model.add(Dense(units=10, activation='softmax'))

#The compile method configures the model’s learning process
model.compile(loss='categorical_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])

#The fit method does the training in batches
# x_train and y_train are Numpy arrays --just like in the Scikit-Learn API.
model.fit(x_train, y_train, epochs=5, batch_size=32)

#The evaluate method calculates the losses and metrics
#     for the trained model
loss_and_metrics = model.evaluate(x_test, y_test, batch_size=128)

#The predict method applies the trained model to inputs
#     to generate outputs
classes = model.predict(x_test, batch_size=128)

원문보기:
https://www.itworld.co.kr/news/116583#csidx40aaa44f718d2bb87a4d7484cdb8122 

케라스 함수 API
케라스 시퀀셜 모델은 간소하지만 모델 토폴로지는 제한된다. 케라스 함수 API는 다중 입력/다중 출력 모델, 방향성 비순환 그래프(DAG), 그리고 공유된 계층이 있는 모델과 같은 복잡한 모델을 만드는 데 유용하다.


함수 API는 시퀀셜 모델과 같은 계층을 사용하지만 조합 측면에서 더 높은 유연성을 제공한다. 함수 API에서 먼저 계층을 정의한 다음 모델을 생성하고 컴파일하고 피팅(학습)한다.

from keras.layers import Input, Dense
from keras.models import Model

# This returns a tensor
inputs = Input(shape=(784,))

# a layer instance is callable on a tensor, and returns a tensor
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)

# This creates a model that includes
# the Input layer and three Dense layers
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.fit(data, labels)  # starts training

원문보기:
https://www.itworld.co.kr/news/116583#csidx67aec59c1df48868e32aa8d8d48799d

0개의 댓글