아래는 Keras에서 다운받은 데이터를 가지고 테스트 해봤음.
import tensorflow.compat.v1 as tf import numpy as np tf.disable_v2_behavior() csv_data = np.loadtxt('D:\Downloads\winequality-red.csv', delimiter=',', dtype=np.float32) x_data = csv_data[:1000:, 0:-1] y_data = csv_data[:1000:, [-1]] #print(x_data.shape, x_data,len(x_data)) #print(y_data.shape, y_data) print("x_data = ", x_data.shape[0], x_data.shape[1]) print("y_data = ", y_data.shape[0], y_data.shape[1]) NUM_IN = x_data.shape[1] NUM_OUT = y_data.shape[1] NUM_CLASS= 10 X = tf.placeholder(tf.float32, shape=[None, NUM_IN]) Y = tf.placeholder(tf.int32, shape=[None, NUM_OUT]) Y_onehot = tf.one_hot(Y, NUM_CLASS) Y_onehot = tf.reshape(Y_onehot, [-1, NUM_CLASS]) W = tf.Variable(tf.random_normal(shape=[NUM_IN,NUM_CLASS]), name='weight') B = tf.Variable(tf.random_normal(shape=[NUM_CLASS]), name='bias') logit = tf.matmul(X, W)+B H=tf.nn.softmax(logit) #cost = tf.reduce_mean(-tf.reduce_sum(Y_onehot*tf.log(H), axis=1)) cost_i = tf.nn.softmax_cross_entropy_with_logits(logits=logit, labels=Y_onehot) cost = tf.reduce_mean(cost_i) optimizer= tf.train.GradientDescentOptimizer(learning_rate=0.001) train=optimizer.minimize(cost) prediction = tf.argmax(H, 1) correct_prediction = tf.equal(prediction, tf.argmax(Y_onehot,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) sess = tf.Session() sess.run(tf.global_variables_initializer()) for step in range(10000): sess.run(train, feed_dict={X:x_data, Y:y_data}) loss,ac = sess.run([cost, accuracy], feed_dict={X:x_data, Y:y_data}) print("loss={:.3f}\t accuracy={:.2%}".format(loss, ac)) pred = sess.run(prediction, feed_dict={X:x_data}) for p, y in zip(pred, y_data.flatten()) : print( "[{}] P={} Y={}".format( p==int(y), p, int(y)))
아래는 "모두를 위한 딥러닝" 강좌의 예제.
import tensorflow.compat.v1 as tf import numpy as np tf.disable_v2_behavior() csv_data = np.loadtxt('D:\Downloads\data-03-diabetes.csv', delimiter=',', dtype=np.float32) x_data = csv_data[:, 0:-1] y_data = csv_data[:, [-1]] #print(x_data.shape, x_data,len(x_data)) #print(y_data.shape, y_data) print("x_data = ", x_data.shape[0], x_data.shape[1]) print("y_data = ", y_data.shape[0], y_data.shape[1]) NUM_IN = x_data.shape[1] NUM_OUT = y_data.shape[1] X = tf.placeholder(tf.float32, shape=[None, NUM_IN]) Y = tf.placeholder(tf.float32, shape=[None, NUM_OUT]) W = tf.Variable(tf.random_normal(shape=[NUM_IN,NUM_OUT]), name='weight') B = tf.Variable(tf.random_normal(shape=[NUM_OUT]), name='bias') H=tf.sigmoid(tf.matmul(X, W)+B) cost = -tf.reduce_mean(Y*tf.log(H) + (1-Y)*tf.log(1-H)) optimizer= tf.train.GradientDescentOptimizer(learning_rate=0.001) train=optimizer.minimize(cost) predicted = tf.cast(H > 0.5, dtype=tf.float32) accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32)) sess = tf.Session() sess.run(tf.global_variables_initializer()) for step in range(20000): out_cost, _ = sess.run([cost, train], feed_dict={X:x_data, Y:y_data}) if step % 200 == 0 : print( "step=", step, " cost=", out_cost) p, a = sess.run([predicted, accuracy], feed_dict={X:x_data, Y:y_data}) print( "predict=", p, " accuracy=", a)
'Tensorflow Deep-Learning' 카테고리의 다른 글
[BOOST] 파이썬과 C++의 동거(데이터 공유) (0) | 2020.06.10 |
---|---|
[BOOST] 빌드 방법 (0) | 2020.06.09 |
[텐서플로] MNIST 관련 4가지 예제 한꺼번에 (tensorflow 1.x) (0) | 2020.06.06 |
[텐서플로] MNIST 관련 코드 실행 오류 발생시 조치 (WIN10) (0) | 2020.06.06 |
기존 코드를 TensorFlow 2.0으로 바꾸기 (0) | 2020.02.25 |