Keras에서 Flatten 레이어를 사용하는 방법
이 글에서는 Keras의 Flatten 레이어를 사용하는 방법을 살펴보고, 직접 따라 하기 쉬운 간단한 코드 중심의 짧은 튜토리얼을 제공합니다. 이 글은 AI가 번역한 콘텐츠입니다. 오역이 의심되는 부분이 있으면 댓글로 알려 주세요.
Created on September 15|Last edited on September 15
Comment
신경망 아키텍처를 만들다 보면 텐서를 하나의 차원으로 평탄화해야 할 때가 자주 있습니다. 예를 들어, 합성곱 신경망(CNN)이나 비전 모델로 이미지를 배치 단위로 처리하는 경우가 그렇습니다. 트랜스포머지금 보고 있는 것은 4차원 텐서입니다, 즉 .
이제 출력값을 손실 함수로 처리하거나, 다른 모델(예: DALL·E의 디코더)에 입력하거나, 다층 퍼셉트론(MLP)에 넣으려면 단일 1차원 텐서를 만들어야 할 수 있습니다. 어떻게 하면 될까요?
목차
코드를 보여줘
from tensorflow.keras import layersfrom tensorflow.keras.models import Sequentialmodel = Sequential([# ... PreProcessing Layerslayers.Conv2D(...),layers.MaxPooling2D(),# ... Bunch of Convolutional Layerslayers.Flatten(), # < ---- ⭐️⭐️⭐️⭐️# ... MLP Layers])
일부 컨테이너 추상화(예: Sequential)에서는 Flatten 레이어를 명시적으로 추가하지 않고, 대신 Functional API를 사용해 직접 적용할 수 있습니다. 예를 들어:
from tensorflow import kerasfrom tensorflow.keras import layersdef build_model(input_shape, input_label):# .. Process the Inputs using a learned Embedding Layerembedding = layers.Embedding(input_shape)(input_labels)# ... Flatten the Tensorembedding = layers.Flatten()(embedding)# ... Further process using Convolutional blocks# ... Generate output logitsoutput = layers.Dense(1)(feature)# Create a Model Instancereturn keras.models.Model([input_image, input_labels], output)
이것으로 끝입니다! 필요한 것은 이뿐이에요. Flatten 레이어는 모든 머신러닝 엔지니어가 도구 상자에 꼭 넣어 두어야 할 중요한 레이어입니다.
Keras API에 대해 더 알아보려면 다음 영상을 확인해 보세요:
요약
이 짧은 튜토리얼에서는 Keras에서 Flatten 레이어를 사용하는 방법과 그 유용성에 대해 살펴보았습니다. W&B의 모든 기능을 확인하려면 다음을 참조하세요. 5분짜리 간단 가이드수학적 내용과 기초부터 구현하는 코드까지 다루는 리포트를 더 보고 싶다면, 아래 댓글이나 저희의 …에서 알려 주세요. 포럼 ✨!
추천 읽을거리
Preventing The CUDA Out Of Memory Error In PyTorch
A short tutorial on how you can avoid the "RuntimeError: CUDA out of memory" error while using the PyTorch framework.
How to Initialize Weights in PyTorch
A short tutorial on how you can initialize weights in PyTorch with code and interactive visualizations.
How to Compare Keras Optimizers in Tensorflow for Deep Learning
A short tutorial outlining how to compare Keras optimizers for your deep learning pipelines in Tensorflow, with a Colab to help you follow along.
How To Use GPU with PyTorch
A short tutorial on using GPUs for your deep learning models with PyTorch, from checking availability to visualizing usable.
PyTorch Dropout for regularization - tutorial
Learn how to regularize your PyTorch model with Dropout, complete with a code tutorial and interactive visualizations
How to save and load models in PyTorch
This article is a machine learning tutorial on how to save and load your models in PyTorch using Weights & Biases for version control.
Add a comment