Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tensorflow
GitHub Repository: tensorflow/docs-l10n
Path: blob/master/site/ko/guide/migrate/metrics_optimizers.ipynb
38653 views
Kernel: Python 3
#@title Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.

TF1์—์„œ tf.metrics๋Š” ๋ชจ๋“  ๋ฉ”ํŠธ๋ฆญ ํ•จ์ˆ˜์— ๋Œ€ํ•œ API ๋„ค์ž„์ŠคํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค. ๊ฐ ๋ฉ”ํŠธ๋ฆญ์€ label๊ณผ prediction์„ ์ž…๋ ฅ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•˜๊ณ  ํ•ด๋‹น ๋ฉ”ํŠธ๋ฆญ ํ…์„œ๋ฅผ ๊ฒฐ๊ณผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. TF2์—์„œ tf.keras.metrics๋Š” ๋ชจ๋“  ๋ฉ”ํŠธ๋ฆญ ํ•จ์ˆ˜์™€ ๊ฐ์ฒด๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. Metric ๊ฐ์ฒด๋Š” tf.keras.Model๊ณผ tf.keras.layers.layer์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”ํŠธ๋ฆญ ๊ฐ’์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์„ค์น˜ํ•˜๊ธฐ

๋ช‡ ๊ฐ€์ง€ ํ•„์š”ํ•œ TensorFlow ๊ฐ€์ ธ์˜ค๊ธฐ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

import tensorflow as tf import tensorflow.compat.v1 as tf1

๊ทธ๋ฆฌ๊ณ  ๋ฐ๋ชจ์šฉ์œผ๋กœ ๋ช‡ ๊ฐ€์ง€ ๊ฐ„๋‹จํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค.

features = [[1., 1.5], [2., 2.5], [3., 3.5]] labels = [0, 0, 1] eval_features = [[4., 4.5], [5., 5.5], [6., 6.5]] eval_labels = [0, 1, 1]

TF1: Estimator๋ฅผ ์‚ฌ์šฉํ•˜๋Š” tf.compat.v1.metrics

TF1์—์„œ ๋ฉ”ํŠธ๋ฆญ์€ eval_metric_ops๋กœ EstimatorSpec์— ์ถ”๊ฐ€๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์—ฐ์‚ฐ์€ tf.metrics์— ์ •์˜๋œ ๋ชจ๋“  ๋ฉ”ํŠธ๋ฆญ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ์ œ์— ๋”ฐ๋ผ tf.metrics.accuracy๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

def _input_fn(): return tf1.data.Dataset.from_tensor_slices((features, labels)).batch(1) def _eval_input_fn(): return tf1.data.Dataset.from_tensor_slices( (eval_features, eval_labels)).batch(1) def _model_fn(features, labels, mode): logits = tf1.layers.Dense(2)(features) predictions = tf.math.argmax(input=logits, axis=1) loss = tf1.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=logits) optimizer = tf1.train.AdagradOptimizer(0.05) train_op = optimizer.minimize(loss, global_step=tf1.train.get_global_step()) accuracy = tf1.metrics.accuracy(labels=labels, predictions=predictions) return tf1.estimator.EstimatorSpec(mode, predictions=predictions, loss=loss, train_op=train_op, eval_metric_ops={'accuracy': accuracy}) estimator = tf1.estimator.Estimator(model_fn=_model_fn) estimator.train(_input_fn)
estimator.evaluate(_eval_input_fn)

๋˜ํ•œ, tf.estimator.add_metrics()๋ฅผ ํ†ตํ•ด ๋ฉ”ํŠธ๋ฆญ์„ Estimator์— ์ง์ ‘ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

def mean_squared_error(labels, predictions): labels = tf.cast(labels, predictions.dtype) return {"mean_squared_error": tf1.metrics.mean_squared_error(labels=labels, predictions=predictions)} estimator = tf1.estimator.add_metrics(estimator, mean_squared_error) estimator.evaluate(_eval_input_fn)

TF2: tf.keras.Model์„ ์‚ฌ์šฉํ•˜๋Š” Keras ๋ฉ”ํŠธ๋ฆญ API

TF2์—์„œ tf.keras.metrics๋Š” ๋ชจ๋“  ๋ฉ”ํŠธ๋ฆญ ํด๋ž˜์Šค์™€ ํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” OOP ์Šคํƒ€์ผ๋กœ ์„ค๊ณ„๋˜์–ด ์žˆ์œผ๋ฉฐ ๋‹ค๋ฅธ tf.keras API์™€ ๊ธด๋ฐ€ํ•˜๊ฒŒ ํ†ตํ•ฉํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“  ๋ฉ”ํŠธ๋ฆญ์€ tf.keras.metrics ๋„ค์ž„์ŠคํŽ˜์ด์Šค์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ผ๋ฐ˜์ ์œผ๋กœ tf.compat.v1.metrics์™€ tf.keras.metrics ์‚ฌ์ด์—์„œ ์ง์ ‘ ๋งคํ•‘ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ์˜ˆ์ œ์—์„œ ๋ฉ”ํŠธ๋ฆญ์ด model.compile() ๋ฉ”์„œ๋“œ์— ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๋Š” ๋ ˆ์ด๋ธ” ๋ฐ ์˜ˆ์ธก ํ…์„œ๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ  ๋ฉ”ํŠธ๋ฆญ ์ธ์Šคํ„ด์Šค๋งŒ ์ƒ์„ฑํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. Keras ๋ชจ๋ธ์€ ๋ชจ๋ธ ์ถœ๋ ฅ๊ณผ ๋ ˆ์ด๋ธ”์„ ๋ฉ”ํŠธ๋ฆญ ๊ฐ์ฒด๋กœ ๋ผ์šฐํŒ…ํ•ฉ๋‹ˆ๋‹ค.

dataset = tf.data.Dataset.from_tensor_slices((features, labels)).batch(1) eval_dataset = tf.data.Dataset.from_tensor_slices( (eval_features, eval_labels)).batch(1) inputs = tf.keras.Input((2,)) logits = tf.keras.layers.Dense(2)(inputs) predictions = tf.math.argmax(input=logits, axis=1) model = tf.keras.models.Model(inputs, predictions) optimizer = tf.keras.optimizers.Adagrad(learning_rate=0.05) model.compile(optimizer, loss='mse', metrics=[tf.keras.metrics.Accuracy()])
model.evaluate(eval_dataset, return_dict=True)

Eager ์‹คํ–‰์„ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค์ •ํ•˜๋ฉด tf.keras.metrics.Metric ์ธ์Šคํ„ด์Šค๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•˜์—ฌ numpy ๋ฐ์ดํ„ฐ ๋˜๋Š” Eager ํ…์„œ๋ฅผ ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. tf.keras.metrics.Metric ๊ฐ์ฒด๋Š” ์ƒํƒœ ์ €์žฅ ์ปจํ…Œ์ด๋„ˆ์ž…๋‹ˆ๋‹ค. ๋ฉ”ํŠธ๋ฆญ ๊ฐ’์€ metric.update_state(y_true, y_pred)๋ฅผ ํ†ตํ•ด ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ฒฐ๊ณผ๋Š” metrics.result()๋กœ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

accuracy = tf.keras.metrics.Accuracy() accuracy.update_state(y_true=[0, 0, 1, 1], y_pred=[0, 0, 0, 1]) accuracy.result().numpy()
accuracy.update_state(y_true=[0, 0, 1, 1], y_pred=[0, 0, 0, 0]) accuracy.update_state(y_true=[0, 0, 1, 1], y_pred=[1, 1, 0, 0]) accuracy.result().numpy()

tf.keras.metrics.Metric์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ tf.keras.metrics.Metric์˜ API ์„ค๋ช…์„œ์™€ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ๊ฐ€์ด๋“œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

TF1.x ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ Keras ์˜ตํ‹ฐ๋งˆ์ด์ €๋กœ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ํ•˜๊ธฐ

Adam ์˜ตํ‹ฐ๋งˆ์ด์ €์™€ ๊ฒฝ์‚ฌ ํ•˜๊ฐ• ์˜ตํ‹ฐ๋งˆ์ด์ €์™€ ๊ฐ™์€ tf.compat.v1.train์˜ ์˜ตํ‹ฐ๋งˆ์ด์ €๋Š” tf.keras.optimizers์—์„œ ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ํ•ฉ๋‹ˆ๋‹ค.

์•„๋ž˜ ํ‘œ์—๋Š” ์ด๋Ÿฌํ•œ ๋ ˆ๊ฑฐ์‹œ ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ Keras์— ๋งž๊ฒŒ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์š”์•ฝ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ํ•™์Šต๋ฅ  ์—…๋ฐ์ดํŠธํ•˜๊ธฐ์™€ ๊ฐ™์€ ์ถ”๊ฐ€ ๋‹จ๊ณ„๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๋Š” ํ•œ TF1.x ๋ฒ„์ „์„ TF2 ๋ฒ„์ „์œผ๋กœ ์ง์ ‘ ๊ต์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ๋ณ€ํ™˜ํ•˜๋ฉด ์ด์ „ ์ฒดํฌํฌ์ธํŠธ๊ฐ€ ํ˜ธํ™˜๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

TF1.x TF2 ์ถ”๊ฐ€ ๋‹จ๊ณ„
`tf.v1.train.GradientDescentOptimizer` `tf.keras.optimizers.SGD` ์—†์Œ
`tf.v1.train.MomentumOptimizer` `tf.keras.optimizers.SGD` `momentum` ์ธ์ˆ˜ ํฌํ•จ
`tf.v1.train.AdamOptimizer` `tf.keras.optimizers.Adam` `beta1`๊ณผ `beta2` ์ธ์ˆ˜๋ฅผ `beta_1`๊ณผ `beta_2`๋กœ ์ด๋ฆ„ ๋ณ€๊ฒฝ
`tf.v1.train.RMSPropOptimizer` `tf.keras.optimizers.RMSprop` `decay` ์ธ์ˆ˜๋ฅผ `rho`๋กœ ์ด๋ฆ„ ๋ณ€๊ฒฝ
`tf.v1.train.AdadeltaOptimizer` `tf.keras.optimizers.Adadelta` ์—†์Œ
`tf.v1.train.AdagradOptimizer` `tf.keras.optimizers.Adagrad` ์—†์Œ
`tf.v1.train.FtrlOptimizer` `tf.keras.optimizers.Ftrl` `accum_name`์™€ `linear_name` ์ธ์ˆ˜ ์ œ๊ฑฐ
`tf.contrib.AdamaxOptimizer` `tf.keras.optimizers.Adamax` `beta1`๊ณผ `beta2` ์ธ์ˆ˜๋ฅผ `beta_1`๊ณผ `beta_2`๋กœ ์ด๋ฆ„ ๋ณ€๊ฒฝ
`tf.contrib.Nadam` `tf.keras.optimizers.Nadam` `beta1`๊ณผ `beta2` ์ธ์ˆ˜๋ฅผ `beta_1`๊ณผ `beta_2`๋กœ ์ด๋ฆ„ ๋ณ€๊ฒฝ

์ฐธ๊ณ : TF2์—์„œ ๋ชจ๋“  ์—ก์‹ค๋ก (์ˆ˜์น˜ ์•ˆ์ • ์ƒ์ˆ˜)์€ ์ด์ œ 1e-8 ๋Œ€์‹  1e-7๋กœ ๊ธฐ๋ณธ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. ์ด ์ฐจ์ด๋Š” ๋Œ€๋ถ€๋ถ„์˜ ์‚ฌ์šฉ ์‚ฌ๋ก€์—์„œ ๋ฌด์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.