Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python을 사용하여 모델을 컴파일하는 데 Tensorflow를 어떻게 사용할 수 있습니까?

<시간/>

Tensorflow에서 생성된 모델은 'compile' 방법을 사용하여 컴파일할 수 있습니다. 손실은 'SparseCategoricalCrossentropy' 방법을 사용하여 계산됩니다.

자세히 알아보기: TensorFlow란 무엇이며 Keras가 TensorFlow와 함께 신경망을 생성하는 방법은 무엇입니까?

Google Colaboratory를 사용하여 아래 코드를 실행하고 있습니다. Google Colab 또는 Colaboratory는 브라우저를 통해 Python 코드를 실행하는 데 도움이 되며 구성이 필요 없고 GPU(그래픽 처리 장치)에 대한 무료 액세스가 필요합니다. Colaboratory는 Jupyter Notebook을 기반으로 구축되었습니다.

print("The model is being compiled")
model.compile(optimizer='adam',loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
   metrics=['accuracy'])
print("The architecture of the model")
model.summary()

코드 크레딧:https://www.tensorflow.org/tutorials/images/classification

출력

The model is being compiled
The architecture of the model
Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #  
=================================================================
rescaling_1 (Rescaling)      (None, 180, 180, 3)       0        
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 180, 180, 16)      448      
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 90, 90, 16)        0        
_________________________________________________________________
conv2d_7 (Conv2D)            (None, 90, 90, 32)        4640    
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 45, 45, 32)        0        
_________________________________________________________________
conv2d_8 (Conv2D)            (None, 45, 45, 64)        18496    
_________________________________________________________________
max_pooling2d_6 (MaxPooling2 (None, 22, 22, 64)        0        
_________________________________________________________________
flatten_1 (Flatten)          (None, 30976)             0        
_________________________________________________________________
dense_2 (Dense)              (None, 128)               3965056  
_________________________________________________________________
dense_3 (Dense)              (None, 5)                 645      
=================================================================
Total params: 3,989,285
Trainable params: 3,989,285
Non-trainable params: 0
_________________________________________________________________

설명

  • 옵티마이저.Adam 옵티마이저 및 손실.SparseCategoricalCrossentropy 손실 함수가 사용됩니다.
  • metric 인수를 전달하면 모든 학습 에포크에 대한 학습 및 검증 정확도를 볼 수 있습니다.
  • 모델이 컴파일되면 '요약' 방법을 사용하여 아키텍처 요약이 표시됩니다.