반응형
Notice
Recent Posts
Recent Comments
Link
관리 메뉴

bro's coding

tensorflow.분류(중간층X) 본문

[AI]/python.tensorflow

tensorflow.분류(중간층X)

givemebro 2020. 5. 12. 15:17
반응형
#속성 4개 3중 분류

iris=load_iris()
X=tf.placeholder(tf.float32,shape=(None,4))
y=tf.placeholder(tf.float32,shape=(None,3))

w=tf.Variable(tf.random.normal([4,3],0,0.1))
b=tf.Variable(tf.random.normal([3],0,0.1))


pred_y=X@w+b

##
entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y,logits=pred_y))
##

optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.01)
train_op=optimizer.minimize(entropy)

costs=[]
sess=tf.InteractiveSession()
tf.global_variables_initializer().run()

from sklearn.model_selection import train_test_split
X_trian,X_test,y_train,y_test=train_test_split(iris.data,np.eye(3)[iris.target])

for i in range(2000):
    entropy_val,_=sess.run([entropy,train_op],feed_dict={X:X_trian,y:y_train})
    costs.append(entropy_val)

# visualazation
import matplotlib.pyplot as plt    
plt.plot(costs)


entropy_val,_=sess.run([entropy,train_op],feed_dict={X:X_test,y:y_test})
pred_yyy=pred_y.eval(feed_dict={X:X_test,y:y_test}).argmax(axis=1)
score=(pred_yyy==y_test.argmax(axis=1)).mean()
score
0.9736842105263158
#속성 4개 3중 분류
# RMSPropOptimizer
iris=load_iris()
X=tf.placeholder(tf.float32,shape=(None,4))
y=tf.placeholder(tf.float32,shape=(None,3))

w=tf.Variable(tf.random.normal([4,3],0,0.1))
b=tf.Variable(tf.random.normal([3],0,0.1))


pred_y=X@w+b

##
entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y,logits=pred_y))
##

optimizer=tf.train.RMSPropOptimizer(learning_rate=0.01)
train_op=optimizer.minimize(entropy)

costs=[]
sess=tf.InteractiveSession()
tf.global_variables_initializer().run()

from sklearn.model_selection import train_test_split
X_trian,X_test,y_train,y_test=train_test_split(iris.data,np.eye(3)[iris.target])

for i in range(2000):
    entropy_val,_=sess.run([entropy,train_op],feed_dict={X:X_trian,y:y_train})
    costs.append(entropy_val)
    
# visualazation
import matplotlib.pyplot as plt    
plt.plot(costs)


entropy_val,_=sess.run([entropy,train_op],feed_dict={X:X_test,y:y_test})
pred_yyy=pred_y.eval(feed_dict={X:X_test,y:y_test}).argmax(axis=1)
score=(pred_yyy==y_test.argmax(axis=1)).mean()
score
1.0
반응형

'[AI] > python.tensorflow' 카테고리의 다른 글

tensorflow.분류.mnist  (0) 2020.05.12
tensorflow.mnist  (0) 2020.05.12
tensorflow.분류(중간층)  (0) 2020.05.12
tensorflow.분류(중간층).relu,sigmoid 비교  (0) 2020.05.12
tensorflow.placeholder  (0) 2020.05.11
tensorflow.irisdata적용  (0) 2020.05.11
tensorflow.optimizer  (0) 2020.05.11
tensorflow.행렬곱,전치행렬  (0) 2020.05.11
Comments