본문 바로가기

DataScience/Keras

Keras 정리 1

반응형
SMALL

본게시물은 Keras 링크의 내용을 공부하며 한글로 번역 및 정리한 문서입니다. 

Keras: the Python deep learning API

 

Keras: the Python deep learning API

State-of-the-art research. Keras is used by CERN, NASA, NIH, and many more scientific organizations around the world (and yes, Keras is used at the LHC). Keras has the low-level flexibility to implement arbitrary research ideas while offering optional high

keras.io


 

keras 사용 환경 및 버전 ( 윈도우 환경에서 ) - 맥북환경은 또달라요 miniforge로 해서 ...

초기 케라스 사용환경
환경 버전 


1. tf.data.Dataset objects 혹은 numpy Array 로 구성된 데이터를 준비.

2. 데이터를 Normalization 혹은 vocabulary indexing을 통하여 전처리 

3. Keras API를 사용한 prediction모델 구축 

4. Checkpoint, metrics monitoring (debuging ? ) , fault tolerance [input data, output data를 프린트하며 디버깅 ]

- Keras fit() method를 사용하여 모델 트레이닝 . 

5. Test data로 모델 평가 및 새로운 데이터를 추론에 사용하는 방법을 평가 

cf) GPU 및 하이터튜닝을 활용하여 속도 및 모델 개선 , 


-tf.data.Dataset (objects )


-fit()

 


Data Loading & preprocessing 

 

- Neural network는 text file , images , csv file 같이 원시 데이터를 처리하지 않음 

vectorized & standardized를 처리함 

 

  • text file - string tensors로 읽은후 단어를 쪼개어 단어를 index하고 integer tensor로 변환해야함 
  • Images - integer tensor로 디코딩하여 floating point로 변환 후 0~1 사이로 normalized해야함
  • CSV data -floting point tensor로 변환된 numerical features과 integer tensor로 index 및 변환된 categorical features로 parsing 함.  이후 각 feature를 0~mean 및 unit variacne로 normalized해야함 . 

normalized

 


  • incode
  • decode

  • tensor
    • Tensor 는 N-Dimensional array를 조작하는 프레임워크임  numpy와 유사함. 
    • numpy와 차이점
      • GPU , TPU등의 하드웨어 가속기를 사용할수 있음 
      • 자동으로 Gradient를 할수있음 
      • 분산하여 연산이가능함
  • variables
    • Variables 은 특수한 tensor로 weight 값을 저장하곤함 .
      • .assign(value) , .assign_add(increment), .assign(decrement)로  Varialbe의 값을 변경가능함.
  • gradients 
    • numpy 와의 큰차이 : 미분표현을 검색가능하다 정도 ? .. 
    • GradientTape을 써서 볼수있음 . tape.watch() !!

Data loading 

 -  Keras 모델은 아래의 세개의 인풋을 받아서 처리함.

  • Numpy arrays  : 라이브러리임 ㅋㅋ import numpy as np ! 
  • TensorFlow Dataset objects  :  memory에 적합하지는 않다고하는데.... 아무튼 분산 파일 시스템에서 스트리밍되는 데이터셋에 더 적합한 고성능 옵션임 
  • Python generators  :  keras.utils.Sequence Class 같은 거로  데이터 배치하는 그런거라는데 ... 넘파이만 써서 잘모르겠다는점...

  • tf.keras.preprocessing.image_dataset_from_directory 는 이미지 파일을  클래스별 폴도로 정렬된 이미지 파일을 이미지 텐서의 레비블링된 데이터 셋으로 변환
  • tf.keras.preprocessing.text_dataset_from_directory 는 text파일을 분류함 .
  • tf.data.experimental.make_csv_dataset은 CSVv파일로부터 구조를 로딩함 
    • ex ) 
    • dataset = keras.preprocessing.image_dataset_from_directory('path/to/main_directory', batch_size=64, image_size = (200,200) ) #create a dataset 
    • for data, labels in dataset:
      • print(data.shape) 
      • print(data.dtype)
      • print(labels,shape)
      • print(labels.dtype)

Data preprocessing with keras

Data - > String / int / float  Numpy array / Dataset / 

python ->String / int / float tensor ------------------/  batch


  • String -> Tokenization 후 token indexing
  • Normalization
  • 데이터를 작은 값( 신경망의 입력값은 0에 가까워야한다 ? 왜 ??....?? ) 으로 다시 계산하면 Mean & unit variance가 0 혹은 [0,1 ]범위의 데이터가 나옴 

***

외부 데이터 전처리 파이프라인이 아닌 한 모형의 일부로 데이터 전처리를 수행해야됨 ( 모델의 휴대성 (성능 ) 고려 )

 -> 원래 파이프라인과 다시만드는 파이프라인 사이의 작은 불일치는 모델의 성능저하에 원인이 될수도 있음 .! 

즉 ideal한 모델은 가능한 원시데이터에 가까운 입력을 생각해야해! 

 image의경우 [0 255] 픽셀값 ,

 text 의경우 utf-8 문자의 문자열 


Keras preprocessing layer 사용해서 전처리 수행가능 .

1. TextVectorization layer : Vectorizing raw string of text .

   - index mapping , tokens 에서 integer indices 가능 . 

2. Normalization layer : Feature normalization 

    - holding the mean & variance of features 

3. Image rescaling , cropping , or image data augmentation 

 

& preprocessing layer는 calling layer.adapt(data)를 호출해서 사용함 .


model Build about Keras Function 

  • Layer : 단순하게 입출력 변환으로 보면됨 ( Node to Node ) Y = W*X +B 같은  
  • Model : layer의 비순환 그래프,  layer의 집합이라고 할까....?

*Keras layers

Keras - layers, models, optimizer , loss function , metrics, 등을 다루는 프레임워크임 . 

 

* Layer weight 생성 - self.add_weight() 사용 . 

 

* weight 의 학습 가능 불가능 영역

- layer에의해 생성된 weight는 학습가능하거나 불가능할수있음.

 

* layer that own layers.

layer는 재귀하여 더크게 블록을 계산할수 있음 . 

 

*Tracking losses created by layers

- add_loss() method를 통해 forward pass중 손실을 발생할수 있음 , ? 

regularization loss에 유용하게 쓰임 , 하위계층의 손실은 재귀적으로 추적됨. 

 

- loss들은 foward pass의 시작 시점에 loss를 해결함 (loss가 누적되지 않음 )

- layer.losses는 항상 마지막 foward pass동안 발생한것만 포함하지만 학습 loop에서 gradients계산전에 함산하여 사용해서 해결됨 ( 왜 ...? ) 



Training models with fit() - 데이터 모델 학습  method 

- Python generators & batches of data , Numpy arrays사용가능  

 

fit()함수를 call하기전에 optimizer와 loss function을 지정해야함 

- next  ( optimizer & loss function 은 따로 정리해놔야지...) 

compile() : 

fitting 할때 1. 데이터 , 2. batch_size, 3. epoch  수( 데이터에 대한 반복횟수)를 지정해야해

fit()의 리턴값으로 "history"객체를 반환함. 

history.history dic은 에포크당 metric값 , 손실, 등의 값을 포함한 데이터를 반환함 

 

call back: & checkpoint 

call back은 각 배치의 시작과 끝, 각 epoch의 시작과 끝에 배치하는게 좋다.

 


evaluate() 

: 예측 분류 값 평가 하는 함수 


tf.distribute.MirroredStrategy () object = GPU사용 

GPU 가속기 사용시

GPU 가속기를 사용하여 전처리를 사용하는경우 

 - 모델의 일부를 전처리를 수행하는게 좋음 .  textVectorization layer의 text기반 전처리는 부적합해

 


kerasTuner 

:  모델에 최적화된 하이퍼파라미터를 획득하기 위한 것 . 



 

학습지표 추적 

 

보드 범위에서 built in metrics를 제공함 . ( tf.keras.metrics.AUC  / tf.keras.metrics.PrecisionAtRecall ) 

- metric = tf.keras.metrics.AUC() 로 인스턴스 객체 생성 

- metric.update_state(targets, predictions) method로 배치

- 쿼리결과는 metric.result()

- 에포크의 끝, 평가 시작점에 리셋 - metric.reset_state() 

 

// self.add_loss() method와 유사함 . self.add_metric() method에 접근할수있음 

// 전달한 수량의 평균을 추적, 및 layer.reset_metrics()를 layer 혹은 모델에 호출하면서 값 재설정 가능함 . 

 

+ keras.metrics.Metric을 사용해서 하위class 정의가능. 

호출방법

update_state() 통계값 업데이트 

result() 행렬 값 반환 

reset_state()  행렬 초기화  


Compiled Function

 

static 

graphs로 컴파일을 분석하면 더나은 성능을 얻을수 있음 , 

tf.function  

 


Training mode / inference mode 

BatchNormalization layer와 Dropout layer 는 학습과 추론과정에서 다르게 행동함 . 

 - 이러한경우 method를 call시 training argument를 노출하는게 좋음.

 - 


reference 

Introduction to Keras for Engineers


 

반응형
LIST

'DataScience > Keras' 카테고리의 다른 글

Keras - 정리 3. (Sequential model )  (0) 2022.01.26
Keras 정리 2  (0) 2021.11.18
Mac OS - M1 - Tensorflow2 & Keras2 시작  (0) 2021.11.15