1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | # coding: utf-8 # In[2]: import os import tensorflow as tf os.chdir("C:/Users/kebee/Desktop/toobig/6주차 TensorFlow-Tutorials/6주차 TensorFlow-Tutorials") # In[3]: import pandas as pd doc = pd.read_csv("white.csv") # In[4]: #데이터 보기 doc.head(6) # In[7]: type(doc.y) # In[9]: #str로 레이블 타입 바꾸기 doc.y= doc.y.astype("str") # In[17]: #레이블 벨런스 및 수 확인 doc.y.value_counts() # In[16]: #레이블 행렬 구조 확인 doc.shape # In[79]: import pandas as pd import numpy as np from sklearn.model_selection import train_test_split #변수와 레이블 분리 X_data=doc.iloc[:,0:11] y_data = doc.y # In[80]: #레이블 원핫 인코딩 적용 from sklearn.preprocessing import OneHotEncoder ohe = OneHotEncoder() y_data1 = y_data.values y_data1 = ohe.fit_transform(y_data1.reshape(-1, 1)).toarray() y_data1 # In[81]: #training데이터와 test데이터 나누기 X_train, X_test, Y_train, Y_test = train_test_split(X_data, y_data1, test_size=0.2) # In[ ]: import math import numpy as np # 1. sigmoid 함수이용, GradientDescentOptimizer X = tf.placeholder(tf.float32,[None, 11]) Y = tf.placeholder(tf.float32,[None, 7]) #초기값 W1 = tf.Variable(tf.random_normal([11,7],stddev=0.01)) b1 = tf.Variable(tf.zeros([7])) hypothesis = tf.sigmoid(tf.matmul(X, W1) + b1) #cross entropy cost function 사용 cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=hypothesis, labels=Y)) #GradientDescent optimizer사용 optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost) # 예측값 H(X) > 0.5 이면 true, 아니면 false predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32) # 0이나 1의 값을 트레이닝 횟수만큼 평균치 계산 accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32)) #훈련 init = tf.global_variables_initializer() sess= tf.Session() sess.run(init) #10000번 돌리고 1000번당 출력 for step in range(10001): cost_val, _ = sess.run([cost, optimizer], feed_dict={X:X_train, Y:Y_train}) if step % 1000 == 0: print(step, '\t', cost_val) # In[155]: #정확도 27% predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32) is_correct = tf.equal(tf.argmax(predicted,1), tf.argmax(Y,1)) accuracy = tf.reduce_mean(tf.cast(is_correct,tf.float32)) print('정확도 : ', sess.run(accuracy, feed_dict={X: X_test, Y: Y_test})) # In[152]: # 2. Layer 확대와 Node 수변경, sigmoid 함수이용, Adamoptimizer X = tf.placeholder(tf.float32,[None, 11]) Y = tf.placeholder(tf.float32,[None, 7]) #layer수 3개 W1 = tf.Variable(tf.random_normal([11,10],stddev=0.01)) b1 = tf.Variable(tf.zeros([10])) h1 = tf.sigmoid(tf.matmul(X, W1) + b1) W2 = tf.Variable(tf.random_normal([10,10],stddev=0.01)) b2 = tf.Variable(tf.zeros([10])) h2 = tf.sigmoid(tf.matmul(h1, W2) + b2) W3 = tf.Variable(tf.random_normal([10,7],stddev=0.01)) b3 = tf.Variable(tf.zeros([7])) hypothesis = tf.matmul(h2, W3) + b3 #cross entropy cost function 사용 cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=hypothesis, labels=Y)) # Adam사용 optimizer = tf.train.AdamOptimizer(learning_rate=0.1).minimize(cost) # 예측값 H(X) > 0.5 is true, else false predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32) # 0이나 1의 값을 트레이닝 횟수만큼 평균치 계산 accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32)) #훈련 init = tf.global_variables_initializer() sess= tf.Session() sess.run(init) #10000번 돌리고 1000번당 출력 #실행 및 출력 for step in range(10001): cost_val, _ = sess.run([cost, optimizer], feed_dict={X:X_train, Y:Y_train}) if step % 1000 == 0: print(step, '\t', cost_val) # In[153]: predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32) is_correct = tf.equal(tf.argmax(predicted,1), tf.argmax(Y,1)) accuracy = tf.reduce_mean(tf.cast(is_correct,tf.float32)) print('정확도 : ', sess.run(accuracy, feed_dict={X: X_test, Y: Y_test})) # In[165]: #3. Weight 초기화하는Xavier 알고리즘, Layer 확대와Node 수변경, Adamoptimizer, relu 함수이용 tf.reset_default_graph() #layer수 3개 X = tf.placeholder(tf.float32,[None, 11]) Y = tf.placeholder(tf.float32,[None, 7]) W1 = tf.get_variable("W1",shape = [11,10],initializer = tf.contrib.layers.xavier_initializer()) b1 = tf.Variable(tf.zeros([10])) h1 = tf.nn.relu(tf.matmul(X, W1) + b1) W2 = tf.get_variable("W2",shape = [10,10],initializer = tf.contrib.layers.xavier_initializer()) b2 = tf.Variable(tf.zeros([10])) h2 = tf.nn.relu(tf.matmul(h1, W2) + b2) W3 = tf.get_variable("W3",shape = [10,7] ,initializer = tf.contrib.layers.xavier_initializer()) b3 = tf.Variable(tf.zeros([7])) hypothesis = tf.matmul(h2, W3) + b3 #cross entropy cost function 사용 cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=hypothesis, labels=Y)) #adam optimizer 사용 optimizer = tf.train.AdamOptimizer(learning_rate=0.1).minimize(cost) # 예측값 H(X) > 0.5 is true, else false predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32) # 0이나 1의 값을 트레이닝 횟수만큼 평균치 계산 accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32)) #훈련 init = tf.global_variables_initializer() sess= tf.Session() sess.run(init) #실행 및 출력 for step in range(10001): cost_val, _ = sess.run([cost, optimizer], feed_dict={X:X_train, Y:Y_train}) if step % 1000 == 0: print(step, '\t', cost_val) # In[166]: predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32) is_correct = tf.equal(tf.argmax(predicted,1), tf.argmax(Y,1)) accuracy = tf.reduce_mean(tf.cast(is_correct,tf.float32)) print('정확도 : ', sess.run(accuracy, feed_dict={X: X_test, Y: Y_test})) # In[182]: # 4. Dropout, sigmoid 함수이용, GradientDescentOptimizer, Layer 확대와 Node 수변경 X = tf.placeholder(tf.float32,[None, 11]) Y = tf.placeholder(tf.float32,[None, 7]) keep_prob = tf.placeholder(tf.float32) #layer수 3개 W1 = tf.Variable(tf.random_normal([11,30],stddev=0.01)) b1 = tf.Variable(tf.zeros([30])) h1 = tf.sigmoid(tf.matmul(X, W1) + b1) h1 = tf.nn.dropout(h1, keep_prob) W2 = tf.Variable(tf.random_normal([30,256],stddev=0.01)) b2 = tf.Variable(tf.zeros([256])) h2 = tf.sigmoid(tf.matmul(h1, W2) + b2) h2 = tf.nn.dropout(h2, keep_prob) W3 = tf.Variable(tf.random_normal([256,7],stddev=0.01)) b3 = tf.Variable(tf.zeros([7])) hypothesis = tf.matmul(h2, W3) + b3 #cross entropy cost function 사용 cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=hypothesis, labels=Y)) #GradientDescent optimizer사용 optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost) #훈련 init = tf.global_variables_initializer() sess= tf.Session() sess.run(init) #실행 및 출력 #드롭아웃 0.8 확률로 적용 for step in range(10001): cost_val, _ = sess.run([cost, optimizer], feed_dict={X:X_train, Y:Y_train,keep_prob:0.8}) if step % 1000 == 0: print(step, '\t', cost_val) # In[183]: predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32) is_correct = tf.equal(tf.argmax(predicted,1), tf.argmax(Y,1)) accuracy = tf.reduce_mean(tf.cast(is_correct,tf.float32)) #테스트할 때는 드롭아웃을 적용하지 않는다 print('정확도 : ', sess.run(accuracy, feed_dict={X: X_test, Y: Y_test,keep_prob:1})) # In[179]: #5. Dropout, relu 함수이용, Layer 확대와Node 수변경, Adamoptimizer, Weight 초기화하는Xavier 알고리즘이용 tf.reset_default_graph() #layer 4개 X = tf.placeholder(tf.float32,[None, 11]) Y = tf.placeholder(tf.float32,[None, 7]) keep_prob = tf.placeholder(tf.float32) #Weight 초기화하는Xavier 알고리즘 W1 = tf.get_variable("W1",shape = [11,20],initializer = tf.contrib.layers.xavier_initializer()) #bias담기 b1 = tf.Variable(tf.zeros([20])) #relu함수 h1 = tf.nn.relu(tf.matmul(X, W1) + b1) #dropout적용 h1 = tf.nn.dropout(h1, keep_prob) #Weight 초기화하는Xavier 알고리즘 W2 = tf.get_variable("W2",shape = [20,80],initializer = tf.contrib.layers.xavier_initializer()) b2 = tf.Variable(tf.zeros([80])) h2 = tf.nn.relu(tf.matmul(h1, W2) + b2) h2 = tf.nn.dropout(h2, keep_prob) W3 = tf.get_variable("W3",shape = [80,30] ,initializer = tf.contrib.layers.xavier_initializer()) b3 = tf.Variable(tf.zeros([30])) h3 = tf.nn.relu(tf.matmul(h2, W3) + b3) h3 = tf.nn.dropout(h3, keep_prob) W4 = tf.get_variable("W4",shape = [30,7] ,initializer = tf.contrib.layers.xavier_initializer()) b4 = tf.Variable(tf.zeros([7])) h4 = tf.nn.dropout(h3, keep_prob) hypothesis = tf.matmul(h4, W4) + b4 #cross entropy cost function 사용 cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=hypothesis, labels=Y)) #adam optimizer optimizer = tf.train.AdamOptimizer(learning_rate=0.1).minimize(cost) #실행 및 출력 init = tf.global_variables_initializer() sess= tf.Session() sess.run(init) for step in range(10001): cost_val, _ = sess.run([cost, optimizer], feed_dict={X:X_train, Y:Y_train,keep_prob:0.8}) if step % 1000 == 0: print(step, '\t', cost_val) # In[180]: #테스트 predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32) is_correct = tf.equal(tf.argmax(predicted,1), tf.argmax(Y,1)) accuracy = tf.reduce_mean(tf.cast(is_correct,tf.float32)) print('정확도 : ', sess.run(accuracy, feed_dict={X: X_test, Y: Y_test,keep_prob:1})) | cs |
Designed by sketchbooks.co.kr / sketchbook5 board skin
Sketchbook5, 스케치북5
Sketchbook5, 스케치북5
Sketchbook5, 스케치북5
Sketchbook5, 스케치북5