다음 글을 참조하여 번역합니다(+ 개인 공부), 예제는 tf 2.0을 기준으로 합니다.
https://www.tensorflow.org/guide/data?hl=en
tf.data: Build TensorFlow input pipelines | TensorFlow Core
The tf.data API enables you to build complex input pipelines from simple, reusable pieces. For example, the pipeline for an image model might aggregate data from files in a distributed file system, apply random perturbations to each image, and merge random
www.tensorflow.org
Using high-level APIs
tf.keras
tf.keras API는 머신러닝 모델을 생성하고 실행하는 데 있어서 단순함을 제공합니다. .fit(), .evaluate(), .predict()는 입력으로 사용하는 데이터셋 활용을 도와줍니다. 다음 예제에서 keras 사용을 보여줍니다.
train, test = tf.keras.datasets.fashion_mnist.load_data()
images, labels = train
images = images/255.0
labels = labels.astype(np.int32)
fmnist_train_ds = tf.data.Dataset.from_tensor_slices((images, labels))
fmnist_train_ds = fmnist_train_ds.shuffle(5000).batch(32)
model = tf.keras.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(10)
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
Model.fit과 Model.evaluate를 사용하기 위해 (feature, label) 데이터셋을 통과시킵니다.
model.fit(fmnist_train_ds, epochs=2)
- Train for 1875 steps
Epoch 1/2
1875/1875 [==============================] - 4s 2ms/step - loss: 0.6066 - accuracy: 0.7949
Epoch 2/2
1875/1875 [==============================] - 4s 2ms/step - loss: 0.4627 - accuracy: 0.8417
<tensorflow.python.keras.callbacks.History at 0x7feeac4f45f8>
Dataset.repeat()을 전달해서 데이터셋을 무한으로 사용하는 경우, model.fit에 steps_per_epochs 인자를 전달합니다.
model.fit(fmnist_train_ds.repeat(), epochs=2, steps_per_epoch=20)
- Train for 20 steps
Epoch 1/2
20/20 [==============================] - 0s 14ms/step - loss: 0.4443 - accuracy: 0.8531
Epoch 2/2
20/20 [==============================] - 0s 2ms/step - loss: 0.4467 - accuracy: 0.8422
<tensorflow.python.keras.callbacks.History at 0x7feeb4454198>
평가는 다음과 같이 사용합니다.
loss, accuracy = model.evaluate(fmnist_train_ds)
print("Loss :", loss)
print("Accuracy :", accuracy)
- 1875/1875 [==============================] - 3s 2ms/step - loss: 0.4412 - accuracy: 0.8485
Loss : 0.4411765540321668
Accuracy : 0.84845
데이터셋을 repeat()과 같이 길게 전달한다면, 평가 steps 인자를 전달해주어야 합니다.
loss, accuracy = model.evaluate(fmnist_train_ds.repeat(), steps=10)
print("Loss :", loss)
print("Accuracy :", accuracy)
- 10/10 [==============================] - 0s 3ms/step - loss: 0.4964 - accuracy: 0.8156
Loss : 0.4964326351881027
Accuracy : 0.815625
Model.predict는 레이블을 필요로 하지 않습니다.
predict_ds = tf.data.Dataset.from_tensor_slices(images).batch(32)
result = model.predict(predict_ds, steps = 10)
print(result.shape)
- (320, 10)
레이블을 포함한 데이터셋을 통과시킬지라도 레이블을 자동으로 무시합니다.
result = model.predict(fmnist_train_ds, steps = 10)
print(result.shape)
- (320, 10)
tf.estimator
- 공식 홈페이지를 참조바랍니다.
'# Machine Learning > TensorFlow doc 정리' 카테고리의 다른 글
(Keras doc 번역) Few-Shot learning with Reptile (1) | 2020.08.10 |
---|---|
tf.data tutorial 번역 (4) (1) | 2020.02.27 |
tf.data tutorial 번역 (3) (0) | 2020.02.26 |
tf.data tutorial 번역 (2) (0) | 2020.02.18 |
tf.data tutorial 번역 (1) (0) | 2020.02.14 |