python tensorflow model
step01_formula
# -*- coding: utf-8 -*- """ 단순 선형회귀방정식 : x(1) -> y - y = a*X + b (a:기울기, b:절편) - error = Y - y """ import tensorflow as tf # 변수 정의 X = tf.placeholder(tf.float32) # 입력 : shape 생략 Y = tf.placeholder(tf.float32) # 출력=정답 : shape 생략 a = tf.Variable(0.5) # 기울기 b = tf.Variable(1.5) # 절편 # 회귀방정식 정의 y = (X * a) + b # 예측치 # model 오차(error)식 정의 model_err = Y - y # 비용함수(cost function[예측치, 정답]) -> 오차 반환하는 함수 cost = tf.reduce_mean(tf.square(model_err)) # MSE(mean square error) ''' tf.square : 절대값, penalty tf.reduce_mean : 각 관측치의 오차의 평균 ''' init = tf.global_variables_initializer() # 변수 초기화 object with tf.Session() as sess: # 변수 초기화 sess.run(init) # a, b변수 초기화 print('a, b변수 =', sess.run( (a, b) )) # a, b변수 = (0.5, 1.5) # model 예측치 : y = (6.5*0.5) + 1.5 y_pred = sess.run(y, feed_dict = {X : 6.5}) # X = 6.5 print('model 예측치 = ', y_pred) # 4.75 # model 오차 = model_err = Y - y feed_data = {X : 6.5, Y : 5.2} model_error = sess.run(model_err, feed_dict = feed_data) print('model 오차=', model_error) # 0.4499998 # cost : 비용함수 식 cost_val = sess.run(cost, feed_dict = feed_data) print('비용함수 =', cost_val) # 0.20249982 ''' 기울기 = 0.5, 절편 = 1.5 y 예측치 = 4.75 model err = 0.449 cost val = 0.202 경사하강법알고리즘 : 최적의 기울기와 절편을 구하는 라이브러리 tf 지원 '''
step02_regression
# -*- coding: utf-8 -*- """ Tensorflow 단순선형회귀모델 생성 """ import tensorflow as tf import numpy as np # x[3] -> y[3] : 공급 data x_data = np.array([1,2,3]) # 입력 y_data = np.array([2,4,6]) # 정답 # 변수 정의 X = tf.placeholder(tf.float32) # x_data Y = tf.placeholder(tf.float32) # y_data a = tf.Variable(tf.random_normal([1])) # 기울기 - 난수(1) b = tf.Variable(tf.random_normal([1])) # 절편 - 난수(1) # prediction y_pred = (X * a) + b # 회귀방정식 # cost function -> 오차 반환 cost = tf.reduce_mean(tf.square(y_pred - Y)) # MSE # 경사하강법 알고리즘 : 최적화 수행(최적의 기울기, 절편) opt = tf.train.GradientDescentOptimizer(0.1) # 학습률 = 0.5~0.0001 train = opt.minimize(cost) # 오차 최소화 init = tf.global_variables_initializer() # session object with tf.Session() as sess : # 변수 초기화 sess.run(init) # a, b변수 초기화 a_val, b_val = sess.run((a, b)) print('기울기 = %.2f, 절편 초기화 = %.2f '%(a_val, b_val)) # 기울기 = -0.19, 절편 초기화= 1.15 # model 50회 학습 for step in range(50) : # 0~49 feed_data = {X : x_data, Y : y_data} # model train sess.run(train, feed_dict = feed_data) # cost value cost_val = sess.run(cost, feed_dict = feed_data) print('step = ', step+1, 'cost =', cost_val, sess.run(a), sess.run(b)) # 최적에 model : a=[1.9064653] b=[0.2126264] # step = 50 cost = 0.0064856675 [1.9064653] [0.2126264] # X = 2.5 -> [최적 model] -> Y ? model_pred = sess.run(y_pred, feed_dict = {X : 2.5} ) # test set print('Y 예측치 =', model_pred) # Y 예측치 = [4.9683733]
step02_regression2
# -*- coding: utf-8 -*- """ women.csv 데이터 파일 -> 단순선형회귀모델 <조건1> x변수 : height, y변수 : weight <조건2> learning_rate = 0.1 <조건3> 학습횟수 = 100회 <조건4> 학습과정 출력 : step, cost, a, b """ import pandas as pd import tensorflow as tf women = pd.read_csv("../data/women.csv") print(women.info()) ''' height 15 non-null int64 - x weight 15 non-null int64 - y ''' # 공급 데이터 생성 x_data = women['height'] # 1차원 y_data = women['weight'] # 1차원 # x,y 정규화 : 0~1 x_data = x_data / 72 y_data = y_data / 164 # X,Y 변수 정의 X = tf.placeholder(tf.float32) # x_data Y = tf.placeholder(tf.float32) # y_data # 기울기, 절편 변수 정의 a = tf.Variable(tf.random_normal([1])) # 기울기 - 난수(1) b = tf.Variable(tf.random_normal([1])) # 절편 - 난수(1) # 회귀방정식 정의 y_pred = (X * a) + b # cost function cost = tf.reduce_mean(tf.square(y_pred - Y)) # 경사하강법 알고리즘 객체 생성 opt = tf.train.GradientDescentOptimizer(learning_rate = 0.1) train = opt.minimize(cost) # 변수 초기화 객체 생성 init = tf.global_variables_initializer() # session object with tf.Session() as sess : # 변수 초기화 sess.run(init) # 기울기, 절편 초기값 출력 a_val, b_val = sess.run( [a, b]) print("a_val = %.2f, b_val = %.2f"%(a_val, b_val)) # model 학습 for step in range(100) : # 공급 data feed_data = {X : x_data, Y : y_data} # model training sess.run(train, feed_dict = feed_data) # cost value cost_val = sess.run(cost, feed_dict = feed_data) # print(step, cost, a, b) print('step=', step+1, 'cost=', cost_val, sess.run(a), sess.run(b)) # model test model_pred = sess.run(y_pred, feed_dict = {X : x_data} ) Y_val = sess.run(Y, feed_dict = {Y : y_data}) print(model_pred[:5]) print(Y_val[:5]) ''' [129.85823 130.88507 131.91191 132.93877 133.9656 ] [115. 117. 120. 123. 126.] ''' # model 평가 : MSE mse = sess.run(tf.reduce_mean(tf.square(model_pred - Y_val))) print('MSE =', mse) # MSE = 9.112154e-05
step03_learningRate
''' learning rate batch size ppt 참고 ''' import matplotlib.pyplot as plt import numpy as np import tensorflow as tf from sklearn.datasets import load_iris tf.set_random_seed(123) # A,b random seed - A,b 초기값 고정 iris = load_iris() # 0-1에 근사한 변수 선택 x_data = np.array([x[3] for x in iris.data]) # 꽃잎 넓이 y_data = np.array([x[2] for x in iris.data]) # 꽃잎 길이 ''' Sepal.Length Sepal.Width Petal.Length Petal.Width Species 0 5.1 3.5 1.4 0.2 setosa ''' learning_rate = 0.1 # 0.1 > 0.8 > 0.01 batch_size = 50 X = tf.placeholder(dtype=tf.float32, shape=[None, 1]) Y = tf.placeholder(dtype=tf.float32, shape=[None, 1]) a = tf.Variable(tf.random_normal(shape=[1,1], mean=0, stddev=1)) b = tf.Variable(tf.random_normal(shape=[1,1], mean=0, stddev=1)) # 단순 선형회귀모델 model_output = tf.add(tf.matmul(X, a), b) '''cost function''' cost_l1 = tf.reduce_mean(tf.abs(Y - model_output)) # MAE cost_l2 = tf.reduce_mean(tf.square(Y - model_output)) # MSE opt_l1 = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) train_l1 = opt_l1.minimize(cost_l1) opt_l2 = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) train_l2 = opt_l1.minimize(cost_l2) sess = tf.Session() sess.run(tf.global_variables_initializer()) cost_l1_vec = [] cost_l2_vec = [] print('init a =', sess.run(a), ' init b=', sess.run(b)) for i in range(50) : idx = np.random.choice(len(x_data), size=batch_size) batch_x_data = np.transpose([x_data[idx]]) batch_y_data = np.transpose([y_data[idx]]) ''' model train ''' feed_data = {X : batch_x_data, Y : batch_y_data} sess.run(train_l1, feed_dict = feed_data) ''' cost value save ''' cost_l1_vec.append(sess.run(cost_l1, feed_dict = feed_data)) cost_l2_vec.append(sess.run(cost_l2, feed_dict = feed_data)) ''' cost, a, b print ''' if (i+1) % 10 == 0: print('step =', (i+1), 'a =', sess.run(a), ' b=', sess.run(b)) ''' cost values ''' print('cost values') print(cost_l1_vec[-5:]) print(cost_l2_vec[-5:]) ''' [1.0456585, 0.6184105, 0.99478513, 0.80187345, 0.9482855] [1.6759095, 0.6657938, 1.5966302, 0.93213594, 1.4661572] ''' '''L1,L2 cost, learning rate, iteration ''' plt.plot(cost_l1_vec, '-', label='cost L1') plt.plot(cost_l2_vec, '--', label='cost L2') plt.title('cost L1 vs L2 per Generation') plt.xlabel('Generation') plt.ylabel('Cost values') plt.legend(loc='best') plt.show()
step04_batchSize
''' learning rate batch size ppt 참고 ''' import matplotlib.pyplot as plt import numpy as np import tensorflow as tf from sklearn.datasets import load_iris tf.set_random_seed(123) # A,b random seed - A,b 초기값 고정 iris = load_iris() # 0-1에 근사한 변수 선택 x_data = np.array([x[3] for x in iris.data]) # 꽃잎 넓이 y_data = np.array([x[2] for x in iris.data]) # 꽃잎 길이 ''' Sepal.Length Sepal.Width Petal.Length Petal.Width Species 0 5.1 3.5 1.4 0.2 setosa ''' learning_rate = 0.1 batch_size = 50 # 50 > 25 > 150 #iter_size = 50 X = tf.placeholder(dtype=tf.float32, shape=[None, 1]) Y = tf.placeholder(dtype=tf.float32, shape=[None, 1]) A = tf.Variable(tf.random_normal(shape=[1,1], mean=0, stddev=1)) b = tf.Variable(tf.random_normal(shape=[1,1], mean=0, stddev=1)) # 단순 선형회귀모델 model_output = tf.add(tf.matmul(X, A), b) '''cost function''' cost_l1 = tf.reduce_mean(tf.abs(Y - model_output)) cost_l2 = tf.reduce_mean(tf.square(Y - model_output)) opt_l1 = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) train_l1 = opt_l1.minimize(cost_l1) opt_l2 = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) train_l2 = opt_l1.minimize(cost_l2) sess = tf.Session() sess.run(tf.global_variables_initializer()) cost_l1_vec = [] cost_l2_vec = [] print('init A =', sess.run(A), ' init b=', sess.run(b)) for i in range(50) : idx = np.random.choice(len(x_data), size=batch_size) batch_x_data = np.transpose([x_data[idx]]) batch_y_data = np.transpose([y_data[idx]]) ''' model train ''' feed_data = {X : batch_x_data, Y : batch_y_data} sess.run(train_l1, feed_dict = feed_data) ''' cost value save ''' cost_l1_vec.append(sess.run(cost_l1, feed_dict = feed_data)) cost_l2_vec.append(sess.run(cost_l2, feed_dict = feed_data)) ''' cost, A, b print ''' if (i+1) % 10 == 0: print('step =', (i+1), 'A =', sess.run(A), ' b=', sess.run(b)) ''' cost values ''' print('cost values') print(cost_l1_vec[-5:]) print(cost_l2_vec[-5:]) ''' [1.0456585, 0.6184105, 0.99478513, 0.80187345, 0.9482855] [1.6759095, 0.6657938, 1.5966302, 0.93213594, 1.4661572] ''' '''L1,L2 cost, learning rate, iteration ''' plt.plot(cost_l1_vec, '-', label='cost L1') plt.plot(cost_l2_vec, '--', label='cost L2') plt.title('cost L1 vs L2 per Generation') plt.xlabel('Generation') plt.ylabel('Cost values') plt.legend(loc='best') plt.show()
step05_iterationSize
''' iteration size ppt 참고 ''' import matplotlib.pyplot as plt import numpy as np import tensorflow as tf from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split tf.set_random_seed(123) # A,b random seed - A,b 초기값 고정 ''' Sepal.Length Sepal.Width Petal.Length Petal.Width Species 0 5.1 3.5 1.4 0.2 setosa ''' iris = load_iris() x_data = np.array([x[3] for x in iris.data]) # 꽃잎 넓이 y_data = np.array([x[1] for x in iris.data]) # 꽃받침 넓이 # train/test split train_x, test_x, train_y, test_y = train_test_split( x_data, y_data, test_size=0.3, random_state=123) learning_rate = 0.1 batch_size = 50 iter_size = 500 # 50 > 500 X = tf.placeholder(dtype=tf.float32, shape=[None, 1]) Y = tf.placeholder(dtype=tf.float32, shape=[None, 1]) A = tf.Variable(tf.random_normal(shape=[1,1], mean=0, stddev=1)) b = tf.Variable(tf.random_normal(shape=[1,1], mean=0, stddev=1)) # 단순 선형회귀모델 model_output = tf.add(tf.matmul(X, A), b) '''cost function''' cost = tf.reduce_mean(tf.square(Y - model_output)) opt = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) train = opt.minimize(cost) sess = tf.Session() sess.run(tf.global_variables_initializer()) cost_train_vec = [] cost_test_vec = [] print('init A =', sess.run(A), ' init b=', sess.run(b)) for i in range(iter_size) : idx = np.random.choice(len(x_data), size=batch_size) batch_x_data = np.transpose([x_data[idx]]) batch_y_data = np.transpose([y_data[idx]]) ''' model train ''' feed_data = {X : batch_x_data, Y : batch_y_data} sess.run(train, feed_dict = feed_data) ''' cost value save ''' cost_train_vec.append(sess.run(cost, feed_dict = feed_data)) ''' cost, A, b print ''' if (i+1) % 10 == 0: print('step =', (i+1), 'A =', sess.run(A), ' b=', sess.run(b)) # Accuracy report batch_x_data = np.transpose([test_x]) batch_y_data = np.transpose([test_y]) feed_data = {X : batch_x_data, Y : batch_y_data} cost_test_vec.append(sess.run(cost, feed_dict = feed_data)) ''' cost values ''' print('cost values') print(cost_train_vec[-5:]) print(cost_test_vec[-5:]) ''' [0.016437106, 0.014954234, 0.020819508, 0.015294206, 0.014486735] [0.03201838, 0.031695694, 0.031021086, 0.030468997, 0.030134797] ''' '''L1,L2 cost, learning rate, iteration ''' plt.plot(cost_train_vec, '-', label='cost train') plt.plot(cost_test_vec, '--', label='cost test') plt.title('cost train vs test per Generation') plt.xlabel('Generation') plt.ylabel('Cost values') plt.legend(loc='best') plt.show()
step06_sigmoid_classification
# -*- coding: utf-8 -*- """ Logistic Regression classification - sigmoid(X*a + b) """ import tensorflow as tf from sklearn import metrics # model 평가 # x변수 = [공부 시간, 동영상강의] x_data = [[1,2], [2,3], [3,1], [4,3], [5,3], [6,2]] # y변수 = 정답(pass=1 or fail=0) y_data = [[0], [0], [0], [1], [1], [1]] # X,Y,w,b X = tf.placeholder(tf.float32, [None, 2])# [?,2] Y = tf.placeholder(tf.float32, [None, 1])# [?,1] w = tf.Variable(tf.random_normal([2,1])) b = tf.Variable(tf.random_normal([1])) # 1. model = y 예측치 model = tf.sigmoid(tf.matmul(X, w) + b) # 2. cost function : entropy cost = -tf.reduce_mean(Y * tf.log(model) + (1 - Y) * tf.log(1 - model)) # 3. 경사하강법 opt = tf.train.GradientDescentOptimizer(0.01) train = opt.minimize(cost) init = tf.global_variables_initializer() # 0~1 확률 -> cutoff=0.5(1 or 0) predict = tf.cast(model > 0.5, dtype=tf.float32) # bool(T/F) -> 숫자(1/0) with tf.Session() as sess : sess.run(init) # w,b 초기화 feed_data = {X : x_data, Y : y_data} # 100번 학습 for step in range(100) : _, cost_val = sess.run([train, cost], feed_dict = feed_data) # 10배수 결과 출력 if ((step+1) % 10 == 0) : print('step=', step+1, 'cost=', cost_val, sess.run(w), sess.run(b)) # Accuracy report model_re, predict_re = sess.run([model, predict], feed_dict = feed_data) print('확률값 =', model_re) # 0~1 확률값 print('예측값 =', predict_re) # 1 or 0 acc = metrics.accuracy_score(y_data, predict_re) print('accuracy =', acc) # accuracy = 0.8333333333333334
step06_sigmoid_classification2_iris
# -*- coding: utf-8 -*- """ Logistic Regression classification - sigmoid(X*a + b) - y변수 이진분류 - iris dataset 적용 - [50, 50], 50 """ import tensorflow as tf from sklearn.datasets import load_iris from sklearn import metrics tf.set_random_seed(123) # w,b seed값 # 1. iris data loading iris = load_iris() # 2. 변수 선택 # x : 1~4개, y:5번칼럼(100) x_data = iris.data[:100,:] y_data = iris.target[:100] print(x_data.shape) # (100, 4) print(y_data.shape) # (100,) - label 2개 print(y_data[:5]) # [0 0 0 0 0] print(y_data[-5:]) # [1 1 1 1 1] # x변수 정규화(0~1) def data_nor(data) : dmax = data.max() dmin = data.min() return (data - dmin) / (dmax - dmin) # 함수 호출 x_data = data_nor(x_data) print(x_data[:5,:]) # X,Y,w,b 변수 정의 X = tf.placeholder(tf.float32, [None,4]) # [?,4] Y = tf.placeholder(tf.float32) # 가변형 변수 w = tf.Variable(tf.random_normal([4,1])) # [input, output] b = tf.Variable(tf.random_normal([1])) # [output=node] # 3. model = LG model = tf.sigmoid(tf.matmul(X, w) + b) # 4. cost function : entropy cost = -tf.reduce_mean(Y * tf.log(model) + (1 - Y) * tf.log(1 - model)) # 5. 경사하강법 opt = tf.train.GradientDescentOptimizer(0.01) train = opt.minimize(cost) # 오차 최소화 # 6. model 예측치 식 predict = tf.cast(model >= 0.5, dtype= tf.float32) # bool -> digit init = tf.global_variables_initializer() with tf.Session() as sess : sess.run(init) # w, b 초기화 feed_data = {X : x_data, Y : y_data} # 학습 500~1000번 for step in range(1000) : _, cost_val = sess.run([train, cost], feed_dict = feed_data) if((step+1) % 100 == 0) : print('step=', step+1, 'cost=', cost_val) print('weight =', sess.run(w)) print('b =', sess.run(b)) # w,b -> 최적화 model_val, predict_val = sess.run([model, predict], feed_dict = feed_data) acc = metrics.accuracy_score(y_data, predict_val) print('accuracy = ', acc) print('='*30) print('real value=', y_data[:60]) print('predict =', predict_val[:60])
step07_entropy
''' entropy : 불확실성 척도 - 분류모델에서 cost function으로 사용 - y의 예측치와 정답의 확률적 차이에 대한 불확실성 척도 ''' import numpy as np # x1[정답] : 앞면, x2[예측치] : 뒷면 x1 = 0.5; x2 = 0.5 e1 = -x1 * np.log2(x1) -x2 * np.log2(x2) print('e1=', e1) # entropy = -sum(x * log(x)) e1 = -(x1 * np.log2(x1) + x2 * np.log2(x2)) print('e1=', e1) # e1= 1.0 #cost = -tf.reduce_mean(Y * tf.log(model) + (1 - Y) * tf.log(1 - model)) cost = -np.mean(x1 * np.log2(x1) + (x2) * np.log2(x2)) print('cost=', cost) # x1 : 앞면, x2 : 뒷면 x1 = 0.9; x2 = 0.1 e2 = -(x1 * np.log2(x1) + x2 * np.log2(x2)) print('e2=', e2) # e2= 0.4689955935892812 cost = -np.mean(x1 * np.log2(x1) + (x2) * np.log2(x2)) print('cost=', cost)
step08_sotfmax_classification
# -*- coding: utf-8 -*- """ 분류분석 : 다항분류 """ import tensorflow as tf import numpy as np # x변수 : [털,날개] x_data = np.array( [[0, 0], [1, 0], [1, 1], [0, 0], [0, 1], [1, 1]]) # y변수 : [기타, 포유류, 조류] # one hot encoding y_data = np.array([ [1, 0, 0], # 기타 [0, 1, 0], # 포유류 [0, 0, 1], # 조류 [1, 0, 0], [1, 0, 0], [0, 0, 1] ]) # X,Y,w,b 변수 정의 X = tf.placeholder(tf.float32, [None, 2]) # 2차원 Y = tf.placeholder(tf.float32, [None, 3]) # 2차원 w = tf.Variable(tf.random_normal([2, 3])) # [input, output] b = tf.Variable(tf.random_normal([3])) # [output=hidden node] # model 생성 model = tf.matmul(X, w) + b # cost function : softmat + entropy cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits( logits=model, labels=Y)) # optimization #opt = tf.train.GradientDescentOptimizer(0.1) opt = tf.train.AdamOptimizer(0.1) train = opt.minimize(cost) #train = tf.train.AdamOptimizer(0.1).minimize(cost) # [0,0,1] -> [0.01, 0.01, 0.98] = 1 predict = tf.arg_max(model, 1) # [0.98, 0.01, 0.01]-> 0 최댓값의 index 반환 label = tf.arg_max(Y, 1) # [1, 0, 0] -> 0 with tf.Session() as sess : sess.run(tf.global_variables_initializer()) # w,b 초기화 feed_data = {X : x_data, Y : y_data} for step in range(1000) : _, cost_val = sess.run([train, cost], feed_dict = feed_data) if ((step+1) % 100 == 0): print('step=', (step+1), 'cost =', cost_val) # 최적화 model test predict_re, label_re = sess.run([predict, label], feed_dict = feed_data) # T/F -> 1/0 -> mean acc = tf.reduce_mean(tf.cast(tf.equal(predict_re, label_re), tf.float32)) print('accuracy =', sess.run(acc, feed_dict = feed_data)) # accuracy = 1.0 print('predict=', predict_re) print('label=', label_re) ''' predict= [0 1 2 0 0 2] label= [0 1 2 0 0 2] '''
step08_sotfmax_classification2_iris
# -*- coding: utf-8 -*- """ 분류분석 : 다항분류 - iris dataset 적용 """ import tensorflow as tf import numpy as np from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn import metrics iris = load_iris() x_data = iris.data # 4개 y_data = iris.target # 1개 print(y_data) # 0, 1, 2 -> [1, 0, 0] # x변수 정규화(0~1) def data_nor(data) : dmax = data.max() dmin = data.min() return (data - dmin) / (dmax - dmin) # 함수 호출 x_data = data_nor(x_data) # one hot encoding y_label = [] # 빈list for y in y_data : if y == 0 : y_label.append([1,0,0]) if y == 1 : y_label.append([0,1,0]) if y == 2 : y_label.append([0,0,1]) y_data = np.array(y_label) # X,Y,w,b 변수 정의 X = tf.placeholder(tf.float32, [None, 4]) # 2차원 Y = tf.placeholder(tf.float32, [None, 3]) # 2차원 w = tf.Variable(tf.random_normal([4, 3])) # [input, output] b = tf.Variable(tf.random_normal([3])) # [output=hidden node] # 8:2 data split train_x, test_x, train_y, test_y = train_test_split( x_data, y_data, test_size=0.2, random_state=123) # train_x, train_y -> model 생성 # test_x, test_y -> model 평가 # model 생성 model = tf.matmul(X, w) + b # cost function : softmat + entropy cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits( logits=model, labels=Y)) # optimization #opt = tf.train.GradientDescentOptimizer(0.1) opt = tf.train.AdamOptimizer(0.1) train = opt.minimize(cost) #train = tf.train.AdamOptimizer(0.1).minimize(cost) # [0,0,1] -> [0.01, 0.01, 0.98] = 1 predict = tf.arg_max(model, 1) # [0.98, 0.01, 0.01]-> 0 최댓값의 index 반환 label = tf.arg_max(Y, 1) # [1, 0, 0] -> 0 with tf.Session() as sess : sess.run(tf.global_variables_initializer()) # w,b 초기화 feed_data = {X : train_x, Y : train_y} # 학습용 for step in range(1000) : _, cost_val = sess.run([train, cost], feed_dict = feed_data) if ((step+1) % 100 == 0): print('step=', (step+1), 'cost =', cost_val) # 최적화 model test feed_data = {X : test_x, Y : test_y} # 평가용 predict_re, label_re = sess.run([predict, label], feed_dict = feed_data) # T/F -> 1/0 -> mean acc = tf.reduce_mean(tf.cast(tf.equal(predict_re, label_re), tf.float32)) print('accuracy =', sess.run(acc, feed_dict = feed_data)) # accuracy = 0.97333336 print('predict=', predict_re) print('label=', label_re) ''' predict= [0 1 2 0 0 2] label= [0 1 2 0 0 2] '''
python tensorflow model的更多相关文章
- Python Tensorflow CNN 识别验证码
Python+Tensorflow的CNN技术快速识别验证码 文章来源于: https://www.jianshu.com/p/26ff7b9075a1 验证码处理的流程是:验证码分析和处理—— te ...
- 基于Ubuntu+Python+Tensorflow+Jupyter notebook搭建深度学习环境
基于Ubuntu+Python+Tensorflow+Jupyter notebook搭建深度学习环境 前言一.环境准备环境介绍软件下载VMware下安装UbuntuUbuntu下Anaconda的安 ...
- 学习笔记TF053:循环神经网络,TensorFlow Model Zoo,强化学习,深度森林,深度学习艺术
循环神经网络.https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/re ...
- 从零开始Windows环境下安装python+tensorflow
从零开始Windows环境下安装python+tensorflow 2017年07月12日 02:30:47 qq_16257817 阅读数:29173 标签: windowspython机器学习te ...
- tensorflow model save and restore
TensorFlow 模型保存/载入 我们在上线使用一个算法模型的时候,首先必须将已经训练好的模型保存下来.tensorflow保存模型的方式与sklearn不太一样,sklearn很直接,一个skl ...
- Python Tensorflow下的Word2Vec代码解释
前言: 作为一个深度学习的重度狂热者,在学习了各项理论后一直想通过项目练手来学习深度学习的框架以及结构用在实战中的知识.心愿是好的,但机会却不好找.最近刚好有个项目,借此机会练手的过程中,我发现其实各 ...
- Anaconda安装python tensorflow 环境
1.安装Anaconda3 https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 2.安装python 3.6 (base) C:\Users\ ...
- [AI开发]Python+Tensorflow打造自己的计算机视觉API服务
"与其停留在概念理论层面,不如动手去实现一个简单demo ." ——鲁迅 没有源码都是耍流氓github 前言 目前提供AI开发相关API接口的公司有很多,国外如微软. ...
- 不要怂,就是GAN (生成式对抗网络) (三):判别器和生成器 TensorFlow Model
在 /home/your_name/TensorFlow/DCGAN/ 下新建文件 utils.py,输入如下代码: import scipy.misc import numpy as np # 保存 ...
随机推荐
- nowcoder300J Mex
题目链接 题意 给出一个长度为\(n(n \le 10^5)\)序列,求其每个子序列之和所组成的集合的\(mex\) 思路 这么水的题都没想出来,感觉自己脑子瓦特了. 假设前\(i\)位可以组成区间\ ...
- enumerate() 函数
enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中. 具体参考博客http://www.runoob. ...
- CMakeList.txt(2):CMakeLists.txt编写规则
#project namePROJECT(test_math) 指定生成的工程名为test_math #head file path INCLUDE_DIRECTORIES(includ ...
- Tomcat系列(2)——Tomcat文件目录7个
核心部分 bin (运行脚本) conf (配置文件) lib (核心库文件) logs (日志目录) temp (临时目录) webapps (自动装载的应用程序的目录) work (JVM临时文件 ...
- js制作可拖拽可点击的悬浮球
兼容mouse事件和touch事件,支持IE9及其以上 效果展示:https://jsfiddle.net/shifeng/7xebf3u0/ // index.html <!DOCTYPE h ...
- ES6.3.2 副本失败处理
ES6.3.2 副本失败处理 副本的失败处理对理解ES的数据副本模型很有帮助.在ES6.3.2 index操作源码流程的总结中提到:ES的写操作会先写主分片,然后主分片再将操作同步到副本分片.本文给出 ...
- CRMEB 商城系统常见错误修复办法
清空了用户表,没有清空拼团记录表导致,解决办法 --v2.5.2 清空拼团表-- TRUNCATE table eb_store_pink 修改了组合数据规则导致的,更新数据即可 --v2.5.2 e ...
- MySQL sum聚合函数
select sum(if(money > 0, money, 0)) as money from total_money 意思是如果money > 0, 将money的值累加到tot ...
- Java 实现TCP/IP协议的收发数据(服务端)
功能如下: 注: 只有服务端,没有客户端,测试时采用第三方软件作为客户端的. 收发数据目前能正常收发数据,只是中文的会变成乱码显示. 采用Thread类实现一个收发数据的线程. 服务端代码: impo ...
- less封装样式有规律的类选择器-遁地龙卷风
1.解决的问题 .class-rule(p,2,width 20px animation-dely 0.1s);可以生成下列css样式 .p2 { animation-dely: 0.2s; widt ...