Skip to content

Commit ffdf0dc

Browse files
committed
update for lab6 and data
1 parent a94564e commit ffdf0dc

File tree

2 files changed

+306
-0
lines changed

2 files changed

+306
-0
lines changed

data-04-zoo.csv

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# https://archive.ics.uci.edu/ml/machine-learning-databases/zoo/zoo.data,,,,,,,,,,,,,,,,
2+
# 1. animal name: (deleted),,,,,,,,,,,,,,,,
3+
# 2. hair Boolean",,,,,,,,,,,,,,,,
4+
# 3. feathers Boolean",,,,,,,,,,,,,,,,
5+
# 4. eggs Boolean",,,,,,,,,,,,,,,,
6+
# 5. milk Boolean",,,,,,,,,,,,,,,,
7+
# 6. airborne Boolean",,,,,,,,,,,,,,,,
8+
# 7. aquatic Boolean",,,,,,,,,,,,,,,,
9+
# 8. predator Boolean",,,,,,,,,,,,,,,,
10+
# 9. toothed Boolean",,,,,,,,,,,,,,,,
11+
# 10. backbone Boolean",,,,,,,,,,,,,,,,
12+
# 11. breathes Boolean",,,,,,,,,,,,,,,,
13+
# 12. venomous Boolean",,,,,,,,,,,,,,,,
14+
# 13. fins Boolean",,,,,,,,,,,,,,,,
15+
# 14. legs Numeric (set of values: {0",2,4,5,6,8}),,,,,,,,,,,
16+
# 15. tail Boolean",,,,,,,,,,,,,,,,
17+
# 16. domestic Boolean",,,,,,,,,,,,,,,,
18+
# 17. catsize Boolean",,,,,,,,,,,,,,,,
19+
# 18. type Numeric (integer values in range [0",6]),,,,,,,,,,,,,,,
20+
1,0,0,1,0,0,1,1,1,1,0,0,4,0,0,1,0
21+
1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,0
22+
0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,0,3
23+
1,0,0,1,0,0,1,1,1,1,0,0,4,0,0,1,0
24+
1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0
25+
1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,1,0
26+
1,0,0,1,0,0,0,1,1,1,0,0,4,1,1,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,4
110+
0,0,1,0,0,0,0,0,1,1,0,0,4,1,0,1,2
111+
0,0,1,0,0,0,1,1,1,1,0,0,4,1,0,0,2
112+
0,0,1,0,0,1,1,1,1,0,0,1,0,1,0,1,3
113+
1,0,0,1,1,0,0,1,1,1,0,0,2,1,0,0,0
114+
1,0,0,1,0,0,0,1,1,1,0,0,4,1,0,0,0
115+
0,1,1,0,1,0,1,0,1,1,0,0,2,1,0,1,1
116+
1,0,0,1,0,0,0,1,1,1,0,0,2,1,0,1,0
117+
1,0,1,0,1,0,0,0,0,1,1,0,6,0,0,0,5
118+
1,0,0,1,0,0,1,1,1,1,0,0,4,1,0,1,0
119+
0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,6
120+
0,1,1,0,1,0,0,0,1,1,0,0,2,1,0,0,1

lab-06-2-softmax_zoo_classifier.ipynb

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Lab 06 Softmax Zoo_classifier\n",
8+
"\n",
9+
"* Softmax를 사용하여분류를 진행합니다.\n",
10+
"\n",
11+
"### 기본 Library 선언 및 Tensorflow 버전 확인"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 1,
17+
"metadata": {
18+
"scrolled": true
19+
},
20+
"outputs": [
21+
{
22+
"name": "stderr",
23+
"output_type": "stream",
24+
"text": [
25+
"/Users/sinseongjin/tf110/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6\n",
26+
" return f(*args, **kwds)\n"
27+
]
28+
}
29+
],
30+
"source": [
31+
"import tensorflow as tf\n",
32+
"import numpy as np\n",
33+
"tf.enable_eager_execution()\n",
34+
"tf.set_random_seed(777) # for reproducibility\n",
35+
"tfe = tf.contrib.eager"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": 6,
41+
"metadata": {},
42+
"outputs": [
43+
{
44+
"name": "stdout",
45+
"output_type": "stream",
46+
"text": [
47+
"(101, 16) (101, 1)\n"
48+
]
49+
}
50+
],
51+
"source": [
52+
"xy = np.loadtxt('data-04-zoo.csv', delimiter=',', dtype=np.float32)\n",
53+
"x_data = xy[:, 0:-1]\n",
54+
"y_data = xy[:, [-1]]\n",
55+
"\n",
56+
"print(x_data.shape, y_data.shape)\n",
57+
"\n",
58+
"nb_classes = 7 # 0 ~ 6\n",
59+
"\n",
60+
"# Make Y data as onehot shape\n",
61+
"Y_one_hot = tf.one_hot(list(y_data), nb_classes)\n",
62+
"Y_one_hot = tf.reshape(Y_one_hot, [-1, nb_classes])"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": 69,
68+
"metadata": {},
69+
"outputs": [],
70+
"source": [
71+
"class softmax_classifer(tf.keras.Model):\n",
72+
" def __init__(self, nb_classes):\n",
73+
" super(softmax_classifer, self).__init__()\n",
74+
" self.W = tfe.Variable(tf.random_normal([16, nb_classes]), name='weight')\n",
75+
" self.b = tfe.Variable(tf.random_normal([nb_classes]), name='bias')\n",
76+
" \n",
77+
" def softmax_regression(self, X):\n",
78+
" # tf.nn.softmax computes softmax activations\n",
79+
" # softmax = exp(logits) / reduce_sum(exp(logits), dim)\n",
80+
" logits = tf.matmul(X, self.W) + self.b\n",
81+
" return tf.nn.softmax(logits)\n",
82+
" \n",
83+
" def loss_fn(self, X, Y):\n",
84+
" logits = self.softmax_regression(X)\n",
85+
" cost_i = tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits,\n",
86+
" labels=tf.stop_gradient([Y]))\n",
87+
" return cost_i\n",
88+
" \n",
89+
" def grad_fn(self, X, Y):\n",
90+
" with tf.GradientTape() as tape:\n",
91+
" loss = self.loss_fn(X, Y)\n",
92+
" grads = tape.gradient(loss, self.variables)\n",
93+
" \n",
94+
" return grads\n",
95+
" \n",
96+
" def prediction(self, X, Y):\n",
97+
" pred = tf.argmax(self.softmax_regression(X), 1)\n",
98+
" correct_prediction = tf.equal(pred, tf.argmax(Y, 1))\n",
99+
" accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
100+
" \n",
101+
" return accuracy\n",
102+
" \n",
103+
" def fit(self, X, Y, epochs=2000, verbose=50):\n",
104+
" optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)\n",
105+
" \n",
106+
" for i in range(epochs):\n",
107+
" grads = self.grad_fn(X, Y)\n",
108+
" optimizer.apply_gradients(zip(grads, self.variables))\n",
109+
" if (i==0) | ((i+1)%verbose==0):\n",
110+
" acc = self.prediction(X, Y)\n",
111+
" print(acc)\n",
112+
" print('Loss at epoch %d: %f' %(i+1, self.loss_fn(X, Y).numpy()))"
113+
]
114+
},
115+
{
116+
"cell_type": "code",
117+
"execution_count": 70,
118+
"metadata": {},
119+
"outputs": [],
120+
"source": [
121+
"optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)\n",
122+
"model = softmax_classifer(nb_classes)"
123+
]
124+
},
125+
{
126+
"cell_type": "code",
127+
"execution_count": 71,
128+
"metadata": {
129+
"scrolled": true
130+
},
131+
"outputs": [
132+
{
133+
"name": "stdout",
134+
"output_type": "stream",
135+
"text": [
136+
"tf.Tensor(0.21782178, shape=(), dtype=float32)\n"
137+
]
138+
},
139+
{
140+
"ename": "TypeError",
141+
"evalue": "only size-1 arrays can be converted to Python scalars",
142+
"output_type": "error",
143+
"traceback": [
144+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
145+
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
146+
"\u001b[0;32m<ipython-input-71-20c3d2719a16>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx_data\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mY_one_hot\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
147+
"\u001b[0;32m<ipython-input-69-871571f540d4>\u001b[0m in \u001b[0;36mfit\u001b[0;34m(self, X, Y, epochs, verbose)\u001b[0m\n\u001b[1;32m 40\u001b[0m \u001b[0macc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprediction\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mX\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mY\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 41\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0macc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 42\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Loss at epoch %d: %f'\u001b[0m \u001b[0;34m%\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mloss_fn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mX\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mY\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
148+
"\u001b[0;32m~/tf110/lib/python3.6/site-packages/tensorflow/python/framework/ops.py\u001b[0m in \u001b[0;36m__float__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 716\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 717\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__float__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 718\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfloat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnumpy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 719\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 720\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__array__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
149+
"\u001b[0;31mTypeError\u001b[0m: only size-1 arrays can be converted to Python scalars"
150+
]
151+
}
152+
],
153+
"source": [
154+
"model.fit(x_data, Y_one_hot)"
155+
]
156+
},
157+
{
158+
"cell_type": "code",
159+
"execution_count": null,
160+
"metadata": {},
161+
"outputs": [],
162+
"source": []
163+
}
164+
],
165+
"metadata": {
166+
"kernelspec": {
167+
"display_name": "Python 3",
168+
"language": "python",
169+
"name": "python3"
170+
},
171+
"language_info": {
172+
"codemirror_mode": {
173+
"name": "ipython",
174+
"version": 3
175+
},
176+
"file_extension": ".py",
177+
"mimetype": "text/x-python",
178+
"name": "python",
179+
"nbconvert_exporter": "python",
180+
"pygments_lexer": "ipython3",
181+
"version": "3.6.4"
182+
}
183+
},
184+
"nbformat": 4,
185+
"nbformat_minor": 2
186+
}

0 commit comments

Comments
 (0)