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

Python을 사용하여 모델을 평가하기 위해 Tensorflow를 Estimator와 함께 어떻게 사용할 수 있습니까?

<시간/>

Tensorflow는 estimator와 함께 사용되어 'classifier' 모듈에 있는 'evaluate' 메소드의 도움으로 모델을 평가할 수 있습니다.

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

모든 레이어에 정확히 하나의 입력 텐서와 하나의 출력 텐서가 있는 일반 레이어 스택으로 작업하는 데 사용되는 순차 모델을 구축하는 데 도움이 되는 Keras Sequential API를 사용할 것입니다.

하나 이상의 레이어를 포함하는 신경망을 컨볼루션 레이어라고 합니다. Convolutional Neural Network를 사용하여 학습 모델을 구축할 수 있습니다.

TensorFlow Text에는 TensorFlow 2.0과 함께 사용할 수 있는 텍스트 관련 클래스 및 작업 모음이 포함되어 있습니다. TensorFlow Text는 시퀀스 모델링을 전처리하는 데 사용할 수 있습니다.

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

Estimator는 완전한 모델에 대한 TensorFlow의 상위 수준 표현입니다. 손쉬운 확장 및 비동기식 교육을 위해 설계되었습니다.

모델은 홍채 데이터 세트를 사용하여 학습됩니다.

예시

eval_result = classifier.evaluate(input_fn=lambda: input_fn(test, test_y, training=False))
print('\nTest dataset accuracy is: {accuracy:0.3f}\n'.format(**eval_result))

코드 크레딧 -https://www.tensorflow.org/tutorials/estimator/premade#first_things_first

출력

INFO:tensorflow:Calling model_fn.
WARNING:tensorflow:Layer dnn is casting an input tensor from dtype float64 to the layer's dtype of float32, which is new behavior in TensorFlow 2. The layer has dtype float32 because its dtype defaults to floatx.
If you intended to run this layer in float32, you can safely ignore this warning. If in doubt, this warning is likely only an issue if you are porting a TensorFlow 1.X model to TensorFlow 2.
To change all layers to have dtype float64 by default, call `tf.keras.backend.set_floatx('float64')`. To change just this layer, pass dtype='float64' to the layer constructor. If you are the author of this layer, you can disable autocasting by passing autocast=False to the base Layer constructor.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Starting evaluation at 2020-09-10T01:40:47Z
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Restoring parameters from /tmp/tmpbhg2uvbr/model.ckpt-5000
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
INFO:tensorflow:Inference Time : 0.21153s
INFO:tensorflow:Finished evaluation at 2020-09-10-01:40:47
INFO:tensorflow:Saving dict for global step 5000: accuracy = 0.96666664, average_loss = 0.42594802, global_step = 5000, loss = 0.42594802
INFO:tensorflow:Saving 'checkpoint_path' summary for global step 5000: /tmp/tmpbhg2uvbr/model.ckpt-5000
Test dataset accuracy is: 0.967

설명

  • 모엘이 훈련되면 성능에 대한 일부 정보를 얻을 수 있습니다.

  • '평가' 기능에 매개변수가 전달되지 않습니다.

  • eval에 대한 input_fn은 단일 데이터 에포크만 생성합니다.

  • eval_result 사전에는 평균 손실(샘플당 평균 손실), 손실(미니 배치당 평균 손실) 및 추정기의 global_step 값(교육 반복 횟수)이 포함됩니다.