int32 타입으로 변경, Estimator API 사용, tf.keras.datasets.mnist 사용

This commit is contained in:
rickiepark
2018-05-19 14:37:46 +09:00
parent 2b77ff699a
commit 490da4e4e7

View File

@@ -115,7 +115,7 @@
"X = iris.data[:, (2, 3)] # 꽃잎 길이, 꽃잎 너비\n", "X = iris.data[:, (2, 3)] # 꽃잎 길이, 꽃잎 너비\n",
"y = (iris.target == 0).astype(np.int)\n", "y = (iris.target == 0).astype(np.int)\n",
"\n", "\n",
"per_clf = Perceptron(max_iter=5, random_state=42)\n", "per_clf = Perceptron(max_iter=100, random_state=42)\n",
"per_clf.fit(X, y)\n", "per_clf.fit(X, y)\n",
"\n", "\n",
"y_pred = per_clf.predict([[2, 0.5]])" "y_pred = per_clf.predict([[2, 0.5]])"
@@ -322,13 +322,6 @@
"# MNIST를 위한 FNN" "# MNIST를 위한 FNN"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## tf.learn를 사용"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 10,
@@ -338,10 +331,94 @@
"import tensorflow as tf" "import tensorflow as tf"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"경고: `tf.examples.tutorials.mnist`은 삭제될 예정이므로 대신 `tf.keras.datasets.mnist`를 사용하겠습니다. `tf.contrib.learn` API는 `tf.estimators`와 `tf.feature_columns`로 옮겨졌고 상당히 많이 바뀌었습니다. 특히 `infer_real_valued_columns_from_input()` 함수와 `SKCompat` 클래스가 없습니다."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()\n",
"X_train = X_train.astype(np.float32).reshape(-1, 28*28) / 255.0\n",
"X_test = X_test.astype(np.float32).reshape(-1, 28*28) / 255.0\n",
"y_train = y_train.astype(np.int32)\n",
"y_test = y_test.astype(np.int32)\n",
"X_valid, X_train = X_train[:5000], X_train[5000:]\n",
"y_valid, y_train = y_train[:5000], y_train[5000:]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Estimator API를 사용 (이전엔 `tf.contrib.learn`)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"feature_cols = [tf.feature_column.numeric_column(\"X\", shape=[28 * 28])]\n",
"dnn_clf = tf.estimator.DNNClassifier(hidden_units=[300,100], n_classes=10,\n",
" feature_columns=feature_cols)\n",
"\n",
"input_fn = tf.estimator.inputs.numpy_input_fn(\n",
" x={\"X\": X_train}, y=y_train, num_epochs=40, batch_size=50, shuffle=True)\n",
"dnn_clf.train(input_fn=input_fn)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_input_fn = tf.estimator.inputs.numpy_input_fn(\n",
" x={\"X\": X_test}, y=y_test, shuffle=False)\n",
"eval_results = dnn_clf.evaluate(input_fn=test_input_fn)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"eval_results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y_pred_iter = dnn_clf.predict(input_fn=test_input_fn)\n",
"y_pred = list(y_pred_iter)\n",
"y_pred[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## tf.learn를 사용"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 11,
"metadata": {}, "metadata": {
"scrolled": true
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@@ -359,9 +436,8 @@
} }
], ],
"source": [ "source": [
"from tensorflow.examples.tutorials.mnist import input_data\n", "# from tensorflow.examples.tutorials.mnist import input_data\n",
"tf.logging.set_verbosity(tf.logging.ERROR) # deprecated 경고 메세지를 출력하지 않기 위해 \n", "# mnist = input_data.read_data_sets(\"/tmp/data/\")"
"mnist = input_data.read_data_sets(\"/tmp/data/\")"
] ]
}, },
{ {
@@ -370,16 +446,18 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"X_train = mnist.train.images\n", "# X_train = mnist.train.images\n",
"X_test = mnist.test.images\n", "# X_test = mnist.test.images\n",
"y_train = mnist.train.labels.astype(\"int\")\n", "# y_train = mnist.train.labels.astype(\"int\")\n",
"y_test = mnist.test.labels.astype(\"int\")" "# y_test = mnist.test.labels.astype(\"int\")"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 13,
"metadata": {}, "metadata": {
"collapsed": true
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@@ -1241,8 +1319,6 @@
} }
], ],
"source": [ "source": [
"import tensorflow as tf\n",
"\n",
"config = tf.contrib.learn.RunConfig(tf_random_seed=42) # 책에는 없음\n", "config = tf.contrib.learn.RunConfig(tf_random_seed=42) # 책에는 없음\n",
"\n", "\n",
"feature_cols = tf.contrib.learn.infer_real_valued_columns_from_input(X_train)\n", "feature_cols = tf.contrib.learn.infer_real_valued_columns_from_input(X_train)\n",
@@ -1341,7 +1417,7 @@
"reset_graph()\n", "reset_graph()\n",
"\n", "\n",
"X = tf.placeholder(tf.float32, shape=(None, n_inputs), name=\"X\")\n", "X = tf.placeholder(tf.float32, shape=(None, n_inputs), name=\"X\")\n",
"y = tf.placeholder(tf.int64, shape=(None), name=\"y\")" "y = tf.placeholder(tf.int32, shape=(None), name=\"y\")"
] ]
}, },
{ {
@@ -1434,6 +1510,20 @@
"batch_size = 50" "batch_size = 50"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def shuffle_batch(X, y, batch_size):\n",
" rnd_idx = np.random.permutation(len(X))\n",
" n_batches = len(X) // batch_size\n",
" for batch_idx in np.array_split(rnd_idx, n_batches):\n",
" X_batch, y_batch = X[batch_idx], y[batch_idx]\n",
" yield X_batch, y_batch"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 25, "execution_count": 25,
@@ -1490,13 +1580,11 @@
"with tf.Session() as sess:\n", "with tf.Session() as sess:\n",
" init.run()\n", " init.run()\n",
" for epoch in range(n_epochs):\n", " for epoch in range(n_epochs):\n",
" for iteration in range(mnist.train.num_examples // batch_size):\n", " for X_batch, y_batch in shuffle_batch(X_train, y_train, batch_size):\n",
" X_batch, y_batch = mnist.train.next_batch(batch_size)\n",
" sess.run(training_op, feed_dict={X: X_batch, y: y_batch})\n", " sess.run(training_op, feed_dict={X: X_batch, y: y_batch})\n",
" acc_train = accuracy.eval(feed_dict={X: X_batch, y: y_batch})\n", " acc_batch = accuracy.eval(feed_dict={X: X_batch, y: y_batch})\n",
" acc_val = accuracy.eval(feed_dict={X: mnist.validation.images,\n", " acc_valid = accuracy.eval(feed_dict={X: X_valid, y: y_valid})\n",
" y: mnist.validation.labels})\n", " print(epoch, \"배치 데이터 정확도:\", acc_batch, \"검증 세트 정확도:\", acc_valid)\n",
" print(epoch, \"훈련 정확도:\", acc_train, \"검증 세트 정확도:\", acc_val)\n",
"\n", "\n",
" save_path = saver.save(sess, \"./my_model_final.ckpt\")" " save_path = saver.save(sess, \"./my_model_final.ckpt\")"
] ]
@@ -1517,7 +1605,7 @@
"source": [ "source": [
"with tf.Session() as sess:\n", "with tf.Session() as sess:\n",
" saver.restore(sess, \"./my_model_final.ckpt\") # 또는 save_path를 사용합니다\n", " saver.restore(sess, \"./my_model_final.ckpt\") # 또는 save_path를 사용합니다\n",
" X_new_scaled = mnist.test.images[:20]\n", " X_new_scaled = X_test[:20]\n",
" Z = logits.eval(feed_dict={X: X_new_scaled})\n", " Z = logits.eval(feed_dict={X: X_new_scaled})\n",
" y_pred = np.argmax(Z, axis=1)" " y_pred = np.argmax(Z, axis=1)"
] ]
@@ -1538,7 +1626,7 @@
], ],
"source": [ "source": [
"print(\"예측 클래스:\", y_pred)\n", "print(\"예측 클래스:\", y_pred)\n",
"print(\"진짜 클래스:\", mnist.test.labels[:20])" "print(\"진짜 클래스:\", y_test[:20])"
] ]
}, },
{ {
@@ -1547,6 +1635,8 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"# 크롬 브라우저는 확인되었지만 다른 브라우저에서는 작동되지 않을 수 있습니다.\n",
"\n",
"from IPython.display import clear_output, Image, display, HTML\n", "from IPython.display import clear_output, Image, display, HTML\n",
"\n", "\n",
"def strip_consts(graph_def, max_const_size=32):\n", "def strip_consts(graph_def, max_const_size=32):\n",
@@ -1647,7 +1737,7 @@
"reset_graph()\n", "reset_graph()\n",
"\n", "\n",
"X = tf.placeholder(tf.float32, shape=(None, n_inputs), name=\"X\")\n", "X = tf.placeholder(tf.float32, shape=(None, n_inputs), name=\"X\")\n",
"y = tf.placeholder(tf.int64, shape=(None), name=\"y\") " "y = tf.placeholder(tf.int32, shape=(None), name=\"y\") "
] ]
}, },
{ {
@@ -1661,7 +1751,8 @@
" activation=tf.nn.relu)\n", " activation=tf.nn.relu)\n",
" hidden2 = tf.layers.dense(hidden1, n_hidden2, name=\"hidden2\",\n", " hidden2 = tf.layers.dense(hidden1, n_hidden2, name=\"hidden2\",\n",
" activation=tf.nn.relu)\n", " activation=tf.nn.relu)\n",
" logits = tf.layers.dense(hidden2, n_outputs, name=\"outputs\")" " logits = tf.layers.dense(hidden2, n_outputs, name=\"outputs\")\n",
" y_proba = tf.nn.softmax(logits)"
] ]
}, },
{ {
@@ -1748,12 +1839,11 @@
"with tf.Session() as sess:\n", "with tf.Session() as sess:\n",
" init.run()\n", " init.run()\n",
" for epoch in range(n_epochs):\n", " for epoch in range(n_epochs):\n",
" for iteration in range(mnist.train.num_examples // batch_size):\n", " for X_batch, y_batch in shuffle_batch(X_train, y_train, batch_size):\n",
" X_batch, y_batch = mnist.train.next_batch(batch_size)\n",
" sess.run(training_op, feed_dict={X: X_batch, y: y_batch})\n", " sess.run(training_op, feed_dict={X: X_batch, y: y_batch})\n",
" acc_train = accuracy.eval(feed_dict={X: X_batch, y: y_batch})\n", " acc_batch = accuracy.eval(feed_dict={X: X_batch, y: y_batch})\n",
" acc_test = accuracy.eval(feed_dict={X: mnist.test.images, y: mnist.test.labels})\n", " acc_valid = accuracy.eval(feed_dict={X: X_valid, y: y_valid})\n",
" print(epoch, \"훈련 정확도:\", acc_train, \"테스트 정확도:\", acc_test)\n", " print(epoch, \"배치 데이터 정확도:\", acc_batch, \"검증 세트 정확도:\", acc_valid)\n",
"\n", "\n",
" save_path = saver.save(sess, \"./my_model_final.ckpt\")" " save_path = saver.save(sess, \"./my_model_final.ckpt\")"
] ]
@@ -1859,7 +1949,7 @@
"reset_graph()\n", "reset_graph()\n",
"\n", "\n",
"X = tf.placeholder(tf.float32, shape=(None, n_inputs), name=\"X\")\n", "X = tf.placeholder(tf.float32, shape=(None, n_inputs), name=\"X\")\n",
"y = tf.placeholder(tf.int64, shape=(None), name=\"y\") " "y = tf.placeholder(tf.int32, shape=(None), name=\"y\") "
] ]
}, },
{ {
@@ -1976,7 +2066,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"잠시만요! 조기 종료를 구현하는 것이 좋겠죠? 이렇게 하려면 검증 세트가 필요합니다. 다행히 텐서플로의 `input_data()` 함수에서 반환되는 데이터셋은 이미 훈련 세트(이미 섞여있는 60,000개의 샘플), 검증 세트(5,000개의 샘플), 테스트 세트(5,000개의 샘플)로 나뉘어져 있습니다. 손쉽게 `X_valid`와 `y_valid`를 정의할 수 있습니다:" "잠시만요! 조기 종료를 구현하는 것이 좋겠죠? 이렇게 하려면 검증 세트가 필요합니다."
] ]
}, },
{ {
@@ -2060,8 +2150,7 @@
" sess.run(init)\n", " sess.run(init)\n",
"\n", "\n",
" for epoch in range(start_epoch, n_epochs):\n", " for epoch in range(start_epoch, n_epochs):\n",
" for iteration in range(mnist.train.num_examples // batch_size):\n", " for X_batch, y_batch in shuffle_batch(X_train, y_train, batch_size):\n",
" X_batch, y_batch = mnist.train.next_batch(batch_size)\n",
" sess.run(training_op, feed_dict={X: X_batch, y: y_batch})\n", " sess.run(training_op, feed_dict={X: X_batch, y: y_batch})\n",
" accuracy_val, loss_val, accuracy_summary_str, loss_summary_str = sess.run([accuracy, loss, accuracy_summary, loss_summary], feed_dict={X: X_valid, y: y_valid})\n", " accuracy_val, loss_val, accuracy_summary_str, loss_summary_str = sess.run([accuracy, loss, accuracy_summary, loss_summary], feed_dict={X: X_valid, y: y_valid})\n",
" file_writer.add_summary(accuracy_summary_str, epoch)\n", " file_writer.add_summary(accuracy_summary_str, epoch)\n",