아래는 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)))

 

 

아래는 "모두를 위한 딥러닝" 강좌의 예제.

http://hunkim.github.io/ml/

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)

+ Recent posts