step01_formula

  1. # -*- coding: utf-8 -*-
  2. """
  3. 단순 선형회귀방정식 : x(1) -> y
  4. - y = a*X + b (a:기울기, b:절편)
  5. - error = Y - y
  6. """
  7.  
  8. import tensorflow as tf
  9.  
  10. # 변수 정의
  11. X = tf.placeholder(tf.float32) # 입력 : shape 생략
  12. Y = tf.placeholder(tf.float32) # 출력=정답 : shape 생략
  13. a = tf.Variable(0.5) # 기울기
  14. b = tf.Variable(1.5) # 절편
  15.  
  16. # 회귀방정식 정의
  17. y = (X * a) + b # 예측치
  18.  
  19. # model 오차(error)식 정의
  20. model_err = Y - y
  21.  
  22. # 비용함수(cost function[예측치, 정답]) -> 오차 반환하는 함수
  23. cost = tf.reduce_mean(tf.square(model_err)) # MSE(mean square error)
  24. '''
  25. tf.square : 절대값, penalty
  26. tf.reduce_mean : 각 관측치의 오차의 평균
  27. '''
  28.  
  29. init = tf.global_variables_initializer() # 변수 초기화 object
  30.  
  31. with tf.Session() as sess:
  32. # 변수 초기화
  33. sess.run(init) # a, b변수 초기화
  34. print('a, b변수 =', sess.run( (a, b) )) # a, b변수 = (0.5, 1.5)
  35.  
  36. # model 예측치 : y = (6.5*0.5) + 1.5
  37. y_pred = sess.run(y, feed_dict = {X : 6.5}) # X = 6.5
  38. print('model 예측치 = ', y_pred) # 4.75
  39.  
  40. # model 오차 = model_err = Y - y
  41. feed_data = {X : 6.5, Y : 5.2}
  42. model_error = sess.run(model_err, feed_dict = feed_data)
  43. print('model 오차=', model_error) # 0.4499998
  44.  
  45. # cost : 비용함수 식
  46. cost_val = sess.run(cost, feed_dict = feed_data)
  47. print('비용함수 =', cost_val) # 0.20249982
  48.  
  49. '''
  50. 기울기 = 0.5, 절편 = 1.5
  51. y 예측치 = 4.75
  52. model err = 0.449
  53. cost val = 0.202
  54.  
  55. 경사하강법알고리즘 : 최적의 기울기와 절편을 구하는 라이브러리 tf 지원
  56. '''

step02_regression

  1. # -*- coding: utf-8 -*-
  2. """
  3. Tensorflow 단순선형회귀모델 생성
  4. """
  5.  
  6. import tensorflow as tf
  7. import numpy as np
  8.  
  9. # x[3] -> y[3] : 공급 data
  10. x_data = np.array([1,2,3]) # 입력
  11. y_data = np.array([2,4,6]) # 정답
  12.  
  13. # 변수 정의
  14. X = tf.placeholder(tf.float32) # x_data
  15. Y = tf.placeholder(tf.float32) # y_data
  16. a = tf.Variable(tf.random_normal([1])) # 기울기 - 난수(1)
  17. b = tf.Variable(tf.random_normal([1])) # 절편 - 난수(1)
  18.  
  19. # prediction
  20. y_pred = (X * a) + b # 회귀방정식
  21.  
  22. # cost function -> 오차 반환
  23. cost = tf.reduce_mean(tf.square(y_pred - Y)) # MSE
  24.  
  25. # 경사하강법 알고리즘 : 최적화 수행(최적의 기울기, 절편)
  26. opt = tf.train.GradientDescentOptimizer(0.1) # 학습률 = 0.5~0.0001
  27. train = opt.minimize(cost) # 오차 최소화
  28.  
  29. init = tf.global_variables_initializer()
  30.  
  31. # session object
  32. with tf.Session() as sess :
  33. # 변수 초기화
  34. sess.run(init) # a, b변수 초기화
  35. a_val, b_val = sess.run((a, b))
  36. print('기울기 = %.2f, 절편 초기화 = %.2f '%(a_val, b_val))
  37. # 기울기 = -0.19, 절편 초기화= 1.15
  38.  
  39. # model 50회 학습
  40. for step in range(50) : # 0~49
  41. feed_data = {X : x_data, Y : y_data}
  42.  
  43. # model train
  44. sess.run(train, feed_dict = feed_data)
  45. # cost value
  46. cost_val = sess.run(cost, feed_dict = feed_data)
  47.  
  48. print('step = ', step+1, 'cost =', cost_val, sess.run(a), sess.run(b))
  49.  
  50. # 최적에 model : a=[1.9064653] b=[0.2126264]
  51. # step = 50 cost = 0.0064856675 [1.9064653] [0.2126264]
  52.  
  53. # X = 2.5 -> [최적 model] -> Y ?
  54. model_pred = sess.run(y_pred, feed_dict = {X : 2.5} ) # test set
  55. print('Y 예측치 =', model_pred) # Y 예측치 = [4.9683733]

step02_regression2

  1. # -*- coding: utf-8 -*-
  2. """
  3. women.csv 데이터 파일 -> 단순선형회귀모델
  4. <조건1> x변수 : height, y변수 : weight
  5. <조건2> learning_rate = 0.1
  6. <조건3> 학습횟수 = 100회
  7. <조건4> 학습과정 출력 : step, cost, a, b
  8. """
  9.  
  10. import pandas as pd
  11. import tensorflow as tf
  12.  
  13. women = pd.read_csv("../data/women.csv")
  14. print(women.info())
  15. '''
  16. height 15 non-null int64 - x
  17. weight 15 non-null int64 - y
  18. '''
  19.  
  20. # 공급 데이터 생성
  21. x_data = women['height'] # 1차원
  22. y_data = women['weight'] # 1차원
  23.  
  24. # x,y 정규화 : 0~1
  25. x_data = x_data / 72
  26. y_data = y_data / 164
  27.  
  28. # X,Y 변수 정의
  29. X = tf.placeholder(tf.float32) # x_data
  30. Y = tf.placeholder(tf.float32) # y_data
  31. # 기울기, 절편 변수 정의
  32. a = tf.Variable(tf.random_normal([1])) # 기울기 - 난수(1)
  33. b = tf.Variable(tf.random_normal([1])) # 절편 - 난수(1)
  34.  
  35. # 회귀방정식 정의
  36. y_pred = (X * a) + b
  37.  
  38. # cost function
  39. cost = tf.reduce_mean(tf.square(y_pred - Y))
  40.  
  41. # 경사하강법 알고리즘 객체 생성
  42. opt = tf.train.GradientDescentOptimizer(learning_rate = 0.1)
  43. train = opt.minimize(cost)
  44.  
  45. # 변수 초기화 객체 생성
  46. init = tf.global_variables_initializer()
  47.  
  48. # session object
  49. with tf.Session() as sess :
  50. # 변수 초기화
  51. sess.run(init)
  52.  
  53. # 기울기, 절편 초기값 출력
  54. a_val, b_val = sess.run( [a, b])
  55. print("a_val = %.2f, b_val = %.2f"%(a_val, b_val))
  56.  
  57. # model 학습
  58. for step in range(100) :
  59. # 공급 data
  60. feed_data = {X : x_data, Y : y_data}
  61.  
  62. # model training
  63. sess.run(train, feed_dict = feed_data)
  64. # cost value
  65. cost_val = sess.run(cost, feed_dict = feed_data)
  66.  
  67. # print(step, cost, a, b)
  68. print('step=', step+1, 'cost=', cost_val, sess.run(a), sess.run(b))
  69.  
  70. # model test
  71. model_pred = sess.run(y_pred, feed_dict = {X : x_data} )
  72. Y_val = sess.run(Y, feed_dict = {Y : y_data})
  73. print(model_pred[:5])
  74. print(Y_val[:5])
  75. '''
  76. [129.85823 130.88507 131.91191 132.93877 133.9656 ]
  77. [115. 117. 120. 123. 126.]
  78. '''
  79.  
  80. # model 평가 : MSE
  81. mse = sess.run(tf.reduce_mean(tf.square(model_pred - Y_val)))
  82. print('MSE =', mse) # MSE = 9.112154e-05

step03_learningRate

  1. '''
  2. learning rate
  3. batch size
  4. ppt 참고
  5. '''
  6.  
  7. import matplotlib.pyplot as plt
  8. import numpy as np
  9. import tensorflow as tf
  10. from sklearn.datasets import load_iris
  11.  
  12. tf.set_random_seed(123) # A,b random seed - A,b 초기값 고정
  13.  
  14. iris = load_iris() # 0-1에 근사한 변수 선택
  15. x_data = np.array([x[3] for x in iris.data]) # 꽃잎 넓이
  16. y_data = np.array([x[2] for x in iris.data]) # 꽃잎 길이
  17. '''
  18. Sepal.Length Sepal.Width Petal.Length Petal.Width Species
  19. 0 5.1 3.5 1.4 0.2 setosa
  20. '''
  21. learning_rate = 0.1 # 0.1 > 0.8 > 0.01
  22. batch_size = 50
  23.  
  24. X = tf.placeholder(dtype=tf.float32, shape=[None, 1])
  25. Y = tf.placeholder(dtype=tf.float32, shape=[None, 1])
  26. a = tf.Variable(tf.random_normal(shape=[1,1], mean=0, stddev=1))
  27. b = tf.Variable(tf.random_normal(shape=[1,1], mean=0, stddev=1))
  28.  
  29. # 단순 선형회귀모델
  30. model_output = tf.add(tf.matmul(X, a), b)
  31.  
  32. '''cost function'''
  33. cost_l1 = tf.reduce_mean(tf.abs(Y - model_output)) # MAE
  34. cost_l2 = tf.reduce_mean(tf.square(Y - model_output)) # MSE
  35.  
  36. opt_l1 = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
  37. train_l1 = opt_l1.minimize(cost_l1)
  38.  
  39. opt_l2 = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
  40. train_l2 = opt_l1.minimize(cost_l2)
  41.  
  42. sess = tf.Session()
  43. sess.run(tf.global_variables_initializer())
  44.  
  45. cost_l1_vec = []
  46. cost_l2_vec = []
  47.  
  48. print('init a =', sess.run(a), ' init b=', sess.run(b))
  49.  
  50. for i in range(50) :
  51. idx = np.random.choice(len(x_data), size=batch_size)
  52. batch_x_data = np.transpose([x_data[idx]])
  53. batch_y_data = np.transpose([y_data[idx]])
  54.  
  55. ''' model train '''
  56. feed_data = {X : batch_x_data, Y : batch_y_data}
  57. sess.run(train_l1, feed_dict = feed_data)
  58. ''' cost value save '''
  59. cost_l1_vec.append(sess.run(cost_l1, feed_dict = feed_data))
  60. cost_l2_vec.append(sess.run(cost_l2, feed_dict = feed_data))
  61.  
  62. ''' cost, a, b print '''
  63. if (i+1) % 10 == 0:
  64. print('step =', (i+1), 'a =', sess.run(a), ' b=', sess.run(b))
  65.  
  66. ''' cost values '''
  67. print('cost values')
  68. print(cost_l1_vec[-5:])
  69. print(cost_l2_vec[-5:])
  70. '''
  71. [1.0456585, 0.6184105, 0.99478513, 0.80187345, 0.9482855]
  72. [1.6759095, 0.6657938, 1.5966302, 0.93213594, 1.4661572]
  73. '''
  74.  
  75. '''L1,L2 cost, learning rate, iteration '''
  76. plt.plot(cost_l1_vec, '-', label='cost L1')
  77. plt.plot(cost_l2_vec, '--', label='cost L2')
  78. plt.title('cost L1 vs L2 per Generation')
  79. plt.xlabel('Generation')
  80. plt.ylabel('Cost values')
  81. plt.legend(loc='best')
  82. plt.show()

step04_batchSize

  1. '''
  2. learning rate
  3. batch size
  4. ppt 참고
  5. '''
  6.  
  7. import matplotlib.pyplot as plt
  8. import numpy as np
  9. import tensorflow as tf
  10. from sklearn.datasets import load_iris
  11.  
  12. tf.set_random_seed(123) # A,b random seed - A,b 초기값 고정
  13.  
  14. iris = load_iris() # 0-1에 근사한 변수 선택
  15. x_data = np.array([x[3] for x in iris.data]) # 꽃잎 넓이
  16. y_data = np.array([x[2] for x in iris.data]) # 꽃잎 길이
  17. '''
  18. Sepal.Length Sepal.Width Petal.Length Petal.Width Species
  19. 0 5.1 3.5 1.4 0.2 setosa
  20. '''
  21. learning_rate = 0.1
  22. batch_size = 50 # 50 > 25 > 150
  23. #iter_size = 50
  24.  
  25. X = tf.placeholder(dtype=tf.float32, shape=[None, 1])
  26. Y = tf.placeholder(dtype=tf.float32, shape=[None, 1])
  27. A = tf.Variable(tf.random_normal(shape=[1,1], mean=0, stddev=1))
  28. b = tf.Variable(tf.random_normal(shape=[1,1], mean=0, stddev=1))
  29.  
  30. # 단순 선형회귀모델
  31. model_output = tf.add(tf.matmul(X, A), b)
  32.  
  33. '''cost function'''
  34. cost_l1 = tf.reduce_mean(tf.abs(Y - model_output))
  35. cost_l2 = tf.reduce_mean(tf.square(Y - model_output))
  36.  
  37. opt_l1 = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
  38. train_l1 = opt_l1.minimize(cost_l1)
  39.  
  40. opt_l2 = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
  41. train_l2 = opt_l1.minimize(cost_l2)
  42.  
  43. sess = tf.Session()
  44. sess.run(tf.global_variables_initializer())
  45.  
  46. cost_l1_vec = []
  47. cost_l2_vec = []
  48.  
  49. print('init A =', sess.run(A), ' init b=', sess.run(b))
  50.  
  51. for i in range(50) :
  52. idx = np.random.choice(len(x_data), size=batch_size)
  53. batch_x_data = np.transpose([x_data[idx]])
  54. batch_y_data = np.transpose([y_data[idx]])
  55.  
  56. ''' model train '''
  57. feed_data = {X : batch_x_data, Y : batch_y_data}
  58. sess.run(train_l1, feed_dict = feed_data)
  59. ''' cost value save '''
  60. cost_l1_vec.append(sess.run(cost_l1, feed_dict = feed_data))
  61. cost_l2_vec.append(sess.run(cost_l2, feed_dict = feed_data))
  62.  
  63. ''' cost, A, b print '''
  64. if (i+1) % 10 == 0:
  65. print('step =', (i+1), 'A =', sess.run(A), ' b=', sess.run(b))
  66.  
  67. ''' cost values '''
  68. print('cost values')
  69. print(cost_l1_vec[-5:])
  70. print(cost_l2_vec[-5:])
  71. '''
  72. [1.0456585, 0.6184105, 0.99478513, 0.80187345, 0.9482855]
  73. [1.6759095, 0.6657938, 1.5966302, 0.93213594, 1.4661572]
  74. '''
  75.  
  76. '''L1,L2 cost, learning rate, iteration '''
  77. plt.plot(cost_l1_vec, '-', label='cost L1')
  78. plt.plot(cost_l2_vec, '--', label='cost L2')
  79. plt.title('cost L1 vs L2 per Generation')
  80. plt.xlabel('Generation')
  81. plt.ylabel('Cost values')
  82. plt.legend(loc='best')
  83. plt.show()

step05_iterationSize

  1. '''
  2. iteration size
  3. ppt 참고
  4. '''
  5.  
  6. import matplotlib.pyplot as plt
  7. import numpy as np
  8. import tensorflow as tf
  9. from sklearn.datasets import load_iris
  10. from sklearn.model_selection import train_test_split
  11.  
  12. tf.set_random_seed(123) # A,b random seed - A,b 초기값 고정
  13. '''
  14. Sepal.Length Sepal.Width Petal.Length Petal.Width Species
  15. 0 5.1 3.5 1.4 0.2 setosa
  16. '''
  17. iris = load_iris()
  18. x_data = np.array([x[3] for x in iris.data]) # 꽃잎 넓이
  19. y_data = np.array([x[1] for x in iris.data]) # 꽃받침 넓이
  20.  
  21. # train/test split
  22. train_x, test_x, train_y, test_y = train_test_split(
  23. x_data, y_data, test_size=0.3, random_state=123)
  24.  
  25. learning_rate = 0.1
  26. batch_size = 50
  27. iter_size = 500 # 50 > 500
  28.  
  29. X = tf.placeholder(dtype=tf.float32, shape=[None, 1])
  30. Y = tf.placeholder(dtype=tf.float32, shape=[None, 1])
  31. A = tf.Variable(tf.random_normal(shape=[1,1], mean=0, stddev=1))
  32. b = tf.Variable(tf.random_normal(shape=[1,1], mean=0, stddev=1))
  33.  
  34. # 단순 선형회귀모델
  35. model_output = tf.add(tf.matmul(X, A), b)
  36.  
  37. '''cost function'''
  38. cost = tf.reduce_mean(tf.square(Y - model_output))
  39.  
  40. opt = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
  41. train = opt.minimize(cost)
  42.  
  43. sess = tf.Session()
  44. sess.run(tf.global_variables_initializer())
  45.  
  46. cost_train_vec = []
  47. cost_test_vec = []
  48.  
  49. print('init A =', sess.run(A), ' init b=', sess.run(b))
  50.  
  51. for i in range(iter_size) :
  52. idx = np.random.choice(len(x_data), size=batch_size)
  53. batch_x_data = np.transpose([x_data[idx]])
  54. batch_y_data = np.transpose([y_data[idx]])
  55.  
  56. ''' model train '''
  57. feed_data = {X : batch_x_data, Y : batch_y_data}
  58. sess.run(train, feed_dict = feed_data)
  59. ''' cost value save '''
  60.  
  61. cost_train_vec.append(sess.run(cost, feed_dict = feed_data))
  62.  
  63. ''' cost, A, b print '''
  64. if (i+1) % 10 == 0:
  65. print('step =', (i+1), 'A =', sess.run(A), ' b=', sess.run(b))
  66.  
  67. # Accuracy report
  68. batch_x_data = np.transpose([test_x])
  69. batch_y_data = np.transpose([test_y])
  70. feed_data = {X : batch_x_data, Y : batch_y_data}
  71. cost_test_vec.append(sess.run(cost, feed_dict = feed_data))
  72.  
  73. ''' cost values '''
  74. print('cost values')
  75. print(cost_train_vec[-5:])
  76. print(cost_test_vec[-5:])
  77. '''
  78. [0.016437106, 0.014954234, 0.020819508, 0.015294206, 0.014486735]
  79. [0.03201838, 0.031695694, 0.031021086, 0.030468997, 0.030134797]
  80. '''
  81.  
  82. '''L1,L2 cost, learning rate, iteration '''
  83. plt.plot(cost_train_vec, '-', label='cost train')
  84. plt.plot(cost_test_vec, '--', label='cost test')
  85. plt.title('cost train vs test per Generation')
  86. plt.xlabel('Generation')
  87. plt.ylabel('Cost values')
  88. plt.legend(loc='best')
  89. plt.show()

step06_sigmoid_classification

  1. # -*- coding: utf-8 -*-
  2. """
  3. Logistic Regression classification
  4. - sigmoid(X*a + b)
  5. """
  6.  
  7. import tensorflow as tf
  8. from sklearn import metrics # model 평가
  9.  
  10. # x변수 = [공부 시간, 동영상강의]
  11. x_data = [[1,2], [2,3], [3,1], [4,3], [5,3], [6,2]]
  12. # y변수 = 정답(pass=1 or fail=0)
  13. y_data = [[0], [0], [0], [1], [1], [1]]
  14.  
  15. # X,Y,w,b
  16. X = tf.placeholder(tf.float32, [None, 2])# [?,2]
  17. Y = tf.placeholder(tf.float32, [None, 1])# [?,1]
  18. w = tf.Variable(tf.random_normal([2,1]))
  19. b = tf.Variable(tf.random_normal([1]))
  20.  
  21. # 1. model = y 예측치
  22. model = tf.sigmoid(tf.matmul(X, w) + b)
  23.  
  24. # 2. cost function : entropy
  25. cost = -tf.reduce_mean(Y * tf.log(model) + (1 - Y) * tf.log(1 - model))
  26.  
  27. # 3. 경사하강법
  28. opt = tf.train.GradientDescentOptimizer(0.01)
  29. train = opt.minimize(cost)
  30.  
  31. init = tf.global_variables_initializer()
  32.  
  33. # 0~1 확률 -> cutoff=0.5(1 or 0)
  34. predict = tf.cast(model > 0.5, dtype=tf.float32) # bool(T/F) -> 숫자(1/0)
  35.  
  36. with tf.Session() as sess :
  37. sess.run(init) # w,b 초기화
  38.  
  39. feed_data = {X : x_data, Y : y_data}
  40.  
  41. # 100번 학습
  42. for step in range(100) :
  43. _, cost_val = sess.run([train, cost], feed_dict = feed_data)
  44.  
  45. # 10배수 결과 출력
  46. if ((step+1) % 10 == 0) :
  47. print('step=', step+1, 'cost=', cost_val, sess.run(w), sess.run(b))
  48.  
  49. # Accuracy report
  50. model_re, predict_re = sess.run([model, predict], feed_dict = feed_data)
  51. print('확률값 =', model_re) # 0~1 확률값
  52. print('예측값 =', predict_re) # 1 or 0
  53.  
  54. acc = metrics.accuracy_score(y_data, predict_re)
  55. print('accuracy =', acc) # accuracy = 0.8333333333333334

step06_sigmoid_classification2_iris

  1. # -*- coding: utf-8 -*-
  2. """
  3. Logistic Regression classification
  4. - sigmoid(X*a + b)
  5. - y변수 이진분류
  6. - iris dataset 적용
  7. - [50, 50], 50
  8. """
  9.  
  10. import tensorflow as tf
  11. from sklearn.datasets import load_iris
  12. from sklearn import metrics
  13.  
  14. tf.set_random_seed(123) # w,b seed값
  15.  
  16. # 1. iris data loading
  17. iris = load_iris()
  18.  
  19. # 2. 변수 선택
  20. # x : 1~4개, y:5번칼럼(100)
  21. x_data = iris.data[:100,:]
  22. y_data = iris.target[:100]
  23. print(x_data.shape) # (100, 4)
  24. print(y_data.shape) # (100,) - label 2개
  25. print(y_data[:5]) # [0 0 0 0 0]
  26. print(y_data[-5:]) # [1 1 1 1 1]
  27.  
  28. # x변수 정규화(0~1)
  29. def data_nor(data) :
  30. dmax = data.max()
  31. dmin = data.min()
  32. return (data - dmin) / (dmax - dmin)
  33.  
  34. # 함수 호출
  35. x_data = data_nor(x_data)
  36.  
  37. print(x_data[:5,:])
  38.  
  39. # X,Y,w,b 변수 정의
  40. X = tf.placeholder(tf.float32, [None,4]) # [?,4]
  41. Y = tf.placeholder(tf.float32) # 가변형 변수
  42. w = tf.Variable(tf.random_normal([4,1])) # [input, output]
  43. b = tf.Variable(tf.random_normal([1])) # [output=node]
  44.  
  45. # 3. model = LG
  46. model = tf.sigmoid(tf.matmul(X, w) + b)
  47.  
  48. # 4. cost function : entropy
  49. cost = -tf.reduce_mean(Y * tf.log(model) + (1 - Y) * tf.log(1 - model))
  50.  
  51. # 5. 경사하강법
  52. opt = tf.train.GradientDescentOptimizer(0.01)
  53. train = opt.minimize(cost) # 오차 최소화
  54.  
  55. # 6. model 예측치 식
  56. predict = tf.cast(model >= 0.5, dtype= tf.float32) # bool -> digit
  57.  
  58. init = tf.global_variables_initializer()
  59.  
  60. with tf.Session() as sess :
  61. sess.run(init) # w, b 초기화
  62.  
  63. feed_data = {X : x_data, Y : y_data}
  64.  
  65. # 학습 500~1000번
  66. for step in range(1000) :
  67. _, cost_val = sess.run([train, cost], feed_dict = feed_data)
  68.  
  69. if((step+1) % 100 == 0) :
  70. print('step=', step+1, 'cost=', cost_val)
  71. print('weight =', sess.run(w))
  72. print('b =', sess.run(b))
  73.  
  74. # w,b -> 최적화
  75. model_val, predict_val = sess.run([model, predict], feed_dict = feed_data)
  76.  
  77. acc = metrics.accuracy_score(y_data, predict_val)
  78. print('accuracy = ', acc)
  79. print('='*30)
  80. print('real value=', y_data[:60])
  81. print('predict =', predict_val[:60])

step07_entropy

  1. '''
  2. entropy : 불확실성 척도
  3. - 분류모델에서 cost function으로 사용
  4. - y의 예측치와 정답의 확률적 차이에 대한 불확실성 척도
  5. '''
  6.  
  7. import numpy as np
  8.  
  9. # x1[정답] : 앞면, x2[예측치] : 뒷면
  10. x1 = 0.5; x2 = 0.5
  11. e1 = -x1 * np.log2(x1) -x2 * np.log2(x2)
  12. print('e1=', e1)
  13.  
  14. # entropy = -sum(x * log(x))
  15. e1 = -(x1 * np.log2(x1) + x2 * np.log2(x2))
  16. print('e1=', e1) # e1= 1.0
  17.  
  18. #cost = -tf.reduce_mean(Y * tf.log(model) + (1 - Y) * tf.log(1 - model))
  19. cost = -np.mean(x1 * np.log2(x1) + (x2) * np.log2(x2))
  20. print('cost=', cost)
  21.  
  22. # x1 : 앞면, x2 : 뒷면
  23. x1 = 0.9; x2 = 0.1
  24. e2 = -(x1 * np.log2(x1) + x2 * np.log2(x2))
  25. print('e2=', e2) # e2= 0.4689955935892812
  26.  
  27. cost = -np.mean(x1 * np.log2(x1) + (x2) * np.log2(x2))
  28. print('cost=', cost)

step08_sotfmax_classification

  1. # -*- coding: utf-8 -*-
  2. """
  3. 분류분석 : 다항분류
  4. """
  5.  
  6. import tensorflow as tf
  7. import numpy as np
  8.  
  9. # x변수 : [털,날개]
  10. x_data = np.array(
  11. [[0, 0], [1, 0], [1, 1], [0, 0], [0, 1], [1, 1]])
  12. # y변수 : [기타, 포유류, 조류]
  13.  
  14. # one hot encoding
  15. y_data = np.array([
  16. [1, 0, 0], # 기타
  17. [0, 1, 0], # 포유류
  18. [0, 0, 1], # 조류
  19. [1, 0, 0],
  20. [1, 0, 0],
  21. [0, 0, 1]
  22. ])
  23.  
  24. # X,Y,w,b 변수 정의
  25. X = tf.placeholder(tf.float32, [None, 2]) # 2차원
  26. Y = tf.placeholder(tf.float32, [None, 3]) # 2차원
  27.  
  28. w = tf.Variable(tf.random_normal([2, 3])) # [input, output]
  29. b = tf.Variable(tf.random_normal([3])) # [output=hidden node]
  30.  
  31. # model 생성
  32. model = tf.matmul(X, w) + b
  33.  
  34. # cost function : softmat + entropy
  35. cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
  36. logits=model, labels=Y))
  37.  
  38. # optimization
  39. #opt = tf.train.GradientDescentOptimizer(0.1)
  40. opt = tf.train.AdamOptimizer(0.1)
  41. train = opt.minimize(cost)
  42.  
  43. #train = tf.train.AdamOptimizer(0.1).minimize(cost)
  44.  
  45. # [0,0,1] -> [0.01, 0.01, 0.98] = 1
  46. predict = tf.arg_max(model, 1) # [0.98, 0.01, 0.01]-> 0 최댓값의 index 반환
  47. label = tf.arg_max(Y, 1) # [1, 0, 0] -> 0
  48.  
  49. with tf.Session() as sess :
  50. sess.run(tf.global_variables_initializer()) # w,b 초기화
  51.  
  52. feed_data = {X : x_data, Y : y_data}
  53.  
  54. for step in range(1000) :
  55. _, cost_val = sess.run([train, cost], feed_dict = feed_data)
  56.  
  57. if ((step+1) % 100 == 0):
  58. print('step=', (step+1), 'cost =', cost_val)
  59.  
  60. # 최적화 model test
  61. predict_re, label_re = sess.run([predict, label], feed_dict = feed_data)
  62.  
  63. # T/F -> 1/0 -> mean
  64. acc = tf.reduce_mean(tf.cast(tf.equal(predict_re, label_re), tf.float32))
  65. print('accuracy =', sess.run(acc, feed_dict = feed_data))
  66. # accuracy = 1.0
  67.  
  68. print('predict=', predict_re)
  69. print('label=', label_re)
  70. '''
  71. predict= [0 1 2 0 0 2]
  72. label= [0 1 2 0 0 2]
  73. '''

step08_sotfmax_classification2_iris

  1. # -*- coding: utf-8 -*-
  2. """
  3. 분류분석 : 다항분류
  4. - iris dataset 적용
  5. """
  6.  
  7. import tensorflow as tf
  8. import numpy as np
  9. from sklearn.datasets import load_iris
  10. from sklearn.model_selection import train_test_split
  11. from sklearn import metrics
  12.  
  13. iris = load_iris()
  14.  
  15. x_data = iris.data # 4개
  16. y_data = iris.target # 1개
  17.  
  18. print(y_data) # 0, 1, 2 -> [1, 0, 0]
  19.  
  20. # x변수 정규화(0~1)
  21. def data_nor(data) :
  22. dmax = data.max()
  23. dmin = data.min()
  24. return (data - dmin) / (dmax - dmin)
  25.  
  26. # 함수 호출
  27. x_data = data_nor(x_data)
  28.  
  29. # one hot encoding
  30. y_label = [] # 빈list
  31. for y in y_data :
  32. if y == 0 : y_label.append([1,0,0])
  33. if y == 1 : y_label.append([0,1,0])
  34. if y == 2 : y_label.append([0,0,1])
  35.  
  36. y_data = np.array(y_label)
  37.  
  38. # X,Y,w,b 변수 정의
  39. X = tf.placeholder(tf.float32, [None, 4]) # 2차원
  40. Y = tf.placeholder(tf.float32, [None, 3]) # 2차원
  41.  
  42. w = tf.Variable(tf.random_normal([4, 3])) # [input, output]
  43. b = tf.Variable(tf.random_normal([3])) # [output=hidden node]
  44.  
  45. # 8:2 data split
  46. train_x, test_x, train_y, test_y = train_test_split(
  47. x_data, y_data, test_size=0.2, random_state=123)
  48.  
  49. # train_x, train_y -> model 생성
  50. # test_x, test_y -> model 평가
  51.  
  52. # model 생성
  53. model = tf.matmul(X, w) + b
  54.  
  55. # cost function : softmat + entropy
  56. cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
  57. logits=model, labels=Y))
  58.  
  59. # optimization
  60. #opt = tf.train.GradientDescentOptimizer(0.1)
  61. opt = tf.train.AdamOptimizer(0.1)
  62. train = opt.minimize(cost)
  63.  
  64. #train = tf.train.AdamOptimizer(0.1).minimize(cost)
  65.  
  66. # [0,0,1] -> [0.01, 0.01, 0.98] = 1
  67. predict = tf.arg_max(model, 1) # [0.98, 0.01, 0.01]-> 0 최댓값의 index 반환
  68. label = tf.arg_max(Y, 1) # [1, 0, 0] -> 0
  69.  
  70. with tf.Session() as sess :
  71. sess.run(tf.global_variables_initializer()) # w,b 초기화
  72.  
  73. feed_data = {X : train_x, Y : train_y} # 학습용
  74.  
  75. for step in range(1000) :
  76. _, cost_val = sess.run([train, cost], feed_dict = feed_data)
  77.  
  78. if ((step+1) % 100 == 0):
  79. print('step=', (step+1), 'cost =', cost_val)
  80.  
  81. # 최적화 model test
  82. feed_data = {X : test_x, Y : test_y} # 평가용
  83.  
  84. predict_re, label_re = sess.run([predict, label], feed_dict = feed_data)
  85.  
  86. # T/F -> 1/0 -> mean
  87. acc = tf.reduce_mean(tf.cast(tf.equal(predict_re, label_re), tf.float32))
  88. print('accuracy =', sess.run(acc, feed_dict = feed_data))
  89. # accuracy = 0.97333336
  90.  
  91. print('predict=', predict_re)
  92. print('label=', label_re)
  93. '''
  94. predict= [0 1 2 0 0 2]
  95. label= [0 1 2 0 0 2]
  96. '''

python tensorflow model的更多相关文章

  1. Python Tensorflow CNN 识别验证码

    Python+Tensorflow的CNN技术快速识别验证码 文章来源于: https://www.jianshu.com/p/26ff7b9075a1 验证码处理的流程是:验证码分析和处理—— te ...

  2. 基于Ubuntu+Python+Tensorflow+Jupyter notebook搭建深度学习环境

    基于Ubuntu+Python+Tensorflow+Jupyter notebook搭建深度学习环境 前言一.环境准备环境介绍软件下载VMware下安装UbuntuUbuntu下Anaconda的安 ...

  3. 学习笔记TF053:循环神经网络,TensorFlow Model Zoo,强化学习,深度森林,深度学习艺术

    循环神经网络.https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/re ...

  4. 从零开始Windows环境下安装python+tensorflow

    从零开始Windows环境下安装python+tensorflow 2017年07月12日 02:30:47 qq_16257817 阅读数:29173 标签: windowspython机器学习te ...

  5. tensorflow model save and restore

    TensorFlow 模型保存/载入 我们在上线使用一个算法模型的时候,首先必须将已经训练好的模型保存下来.tensorflow保存模型的方式与sklearn不太一样,sklearn很直接,一个skl ...

  6. Python Tensorflow下的Word2Vec代码解释

    前言: 作为一个深度学习的重度狂热者,在学习了各项理论后一直想通过项目练手来学习深度学习的框架以及结构用在实战中的知识.心愿是好的,但机会却不好找.最近刚好有个项目,借此机会练手的过程中,我发现其实各 ...

  7. Anaconda安装python tensorflow 环境

    1.安装Anaconda3 https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 2.安装python 3.6 (base) C:\Users\ ...

  8. [AI开发]Python+Tensorflow打造自己的计算机视觉API服务

    "与其停留在概念理论层面,不如动手去实现一个简单demo ."       ——鲁迅 没有源码都是耍流氓github 前言 目前提供AI开发相关API接口的公司有很多,国外如微软. ...

  9. 不要怂,就是GAN (生成式对抗网络) (三):判别器和生成器 TensorFlow Model

    在 /home/your_name/TensorFlow/DCGAN/ 下新建文件 utils.py,输入如下代码: import scipy.misc import numpy as np # 保存 ...

随机推荐

  1. apue——无缓冲读写操作

    stdrw.c文件 #include "apue.h" #define BUFFSIZE 4096 #include <stdio.h> int main(int ar ...

  2. Python 几个常见函数

    本文主要总结常见的函数知识点. 1.zip函数 用来并行迭代,可以把两个序列并在一起,然后返回一个元组的列表 names = ['Ann','Jame','Anla'] ages = [11,12,1 ...

  3. docke 基本安装使用

    特性 容器 虚拟机 启动 秒级 分钟级 硬盘使用 一般为 MB 一般为 GB 性能 接近原生 弱 系统支持量 单机支持上千个容器 一般几十个 容器三大基本概念 镜像 image 容器 containe ...

  4. 利用GitHub Pages和Bootstrap创建个人网站

    作为一名想要想找前端实习的即将毕业的学生,我最近意识到拥有个人网页会使自己的简历更容易被注意到.本文主要是我创建过程及个人心得,有些操作我也是第一次,所以难免在解释中会有错误.另外说明一下,我的电脑是 ...

  5. font-spider问题【已解决】

    最近写一个项目,使用了引入的字体,然而字体太大,于是找解决方法,想要把字体压缩一下,然后找到了font-spider;font-spider使用方法这里就不多说了,网上一大把,主要是在node里面安装 ...

  6. input表单强制大小写

    如题,在HTML页面中常常有遇到强制表单大小写的场景. 在css中设置,HTML页面元素引用就可以了 强制大写: .toUp{ text-transform:uppercase; } 强制小写: .t ...

  7. [C#] .NET4.0中使用4.5中的 async/await 功能实现异步

    在.NET Framework 4.5中添加了新的异步操作库,但是在.NET Framework 4.0中却无法使用.这时不免面临着抉择,到底是升级整个解决方案还是不使用呢? 如果你的软件还没发布出去 ...

  8. mysql的The user specified as a definer (”@’%') does not exist 的解决办法

    两种可能: 1.用户权限不够 赋给用户所有权限试试 mysql> grant all privileges on *.* to root@"%" identified by ...

  9. full join no满连接的使用

    查询各个部门工资范围,按照1000~2000,2000~3000....这样的格式显示人数 select * from (select job,count(*) as "1000~2000& ...

  10. Spring Cloud 2-RabbitMQ 集成(八)

    Spring Cloud  RabbitMQ  pom.xml application.yml 提供者 消费者 队列配置 单元测试 通过消息队列MQ做为通信中心,这里采用RabbitMQ.安装方参考: ...