인공지능/딥러닝

신경망을 이용한 붓꽃 데이터 분류하기

해피밀세트 2020. 6. 12. 02:32
반응형

 

 

 

단층 신경망

 

# 라이브러리 불러오기

import pandas as pd

import tensorflow as tf

import numpy as np

from pandas import get_dummies

from sklearn.model_selection import train_test_split

tf.__version__

 

# 아이리스 데이터 불러오기

iris = pd.read_csv("C:/data/iris.csv")

iris.head()

# 독립변수, 종속변수 분리

x_data = iris.iloc[:,:-1]

y_lables = iris.iloc[:,-1]

y_lables.unique()

# 수동으로 원핫 인코딩

lables = {"Iris-setosa":[1,0,0],

          "Iris-versicolor":[0,1,0],

          "Iris-virginica":[0,0,1]}

y_onehot = list(map(lambda v:lables[v], y_lables))

 

# 훈련 데이터셋, 테스트 데이터셋 분리

x_train,x_test,y_train,y_test = train_test_split(x_data,y_onehot,train_size=0.8)

x_train.shape

# x,y,w,b 변수 정의

x = tf.placeholder(tf.float32,[None,4])

y = tf.placeholder(tf.float32,[None,3])

w = tf.Variable(tf.random_normal([4,3],seed=0),name="weight")

b = tf.Variable(tf.random_normal([3],seed=0),name="bias")

 

# hypothesis 계산식 만들기

hypothesis = tf.nn.softmax(tf.matmul(x,w) + b)

 

# 손실함수로 크로스엔트로피 사용

cross_entropy = -tf.reduce_sum(y*tf.log(hypothesis))

 

# 경사하강법을 사용하여 cost 줄이기

train = tf.train.GradientDescentOptimizer(0.005).minimize(cross_entropy)

 

# 예측결과와 정확도 정의

predict = tf.equal(tf.argmax(hypothesis,1),tf.argmax(y,1))

accuracy = tf.reduce_mean(tf.cast(predict,tf.float32))

 

# 세션 열기
sess = tf.Session()

# 변수 초기화

sess.run(tf.global_variables_initializer())


# 1000
스탭 학습

for step in range(1001) :

    cross_v,w_v,b_v,_ = sess.run([cross_entropy,w,b,train],

                                feed_dict={x:x_train,y:y_train})

    if step % 200 == 0 :

        print(step,cross_v,w_v,b_v)

# 학습모델 정확도 확인

acc = sess.run(accuracy,feed_dict={x:x_test, y:y_test})

print("정답률 :",acc)

 

# 새로운 데이터 예측

a = sess.run(hypothesis, feed_dict={x:[[5.1,3.5,1.4,0.2]]})

print(a, sess.run(tf.argmax(a,1)))

 

 


 

다층 신경망

 

# 라이브러리 불러오기

import pandas as pd

import tensorflow as tf

import numpy as np

from pandas import get_dummies

from sklearn.model_selection import train_test_split

tf.__version__

 

# 아이리스 데이터 불러오기

iris = pd.read_csv("C:/data/iris.csv")

iris.head()

# 독립변수 컬럼명 뽑아내기
cols = iris.columns
features = cols[0:4]
features

# 종속변수 컬럼명 뽑아내기
labels = cols[4]

labels

# 인덱스값 뽑아내기

data_norm = pd.DataFrame(iris)

indices = data_norm.index.tolist()

indices = np.array(indices)

indices

# 인덱스번호 흐트려놓기

np.random.shuffle(indices)

indices

# 랜덤한 인덱스의 이름 출력

x = data_norm.reindex(indices)[features]

y = data_norm.reindex(indices)[labels]

y

# 원핫인코딩

y = get_dummies(y)

y

# 훈련 데이터셋, 테스트 데이터셋 분리

x_train,x_test,y_train,y_test = train_test_split(x,y,train_size=0.8)

 

# 입력층 / x,y 변수 지정

x = tf.placeholder(tf.float32,[None,4])

y = tf.placeholder(tf.float32,[None,3])

 

### 첫번째 은닉층

# weight / 은닉층이므로 열을 마음대로 지정해도 된다.

weight_1 = tf.Variable(tf.random_normal([4,10],seed=0),name="weight_1")

bias_1 = tf.Variable(tf.random_normal([10],seed=0),name="bias_1")

logits_1 = tf.matmul(x,weight_1)+bias_1

# 활성화 함수로 relu사용

relu_1 = tf.nn.relu(logits_1)

 

### 두번째 은닉층

weight_2 = tf.Variable(tf.random_normal([10,3],seed=0),name="weight_2")

bias_2 = tf.Variable(tf.random_normal([3],seed=0),name="bias_2")

logits_2 = tf.matmul(relu_1,weight_2)+bias_2

 

# 텐서에서 제공하는 크로스엔트로피 메소드를 사용하면 소프트맥스를 먼저 쓰면 안됨
# 크로스엔트로피 직접 짜서 쓰면 소프트맥스를 먼저 써도 됨.

hypothesis = tf.nn.softmax(logits_2)

 

### 출력층

#
소프트맥스 함수와 크로스앤트로피를 이용
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits_2, labels=y))

 

# 경사하강법을 사용하여 cost 줄이기

train = tf.train.GradientDescentOptimizer(learning_rate = 0.001).minimize(cost)

 

# 예측결과와 정확도 정의

predict = tf.equal(tf.argmax(hypothesis,1),tf.argmax(y,1))

accuracy = tf.reduce_mean(tf.cast(predict,tf.float32))

 

# 세션 열기
sess = tf.Session()

# 변수 초기화

sess.run(tf.global_variables_initializer())


# 2000
스탭 학습

for step in range(2001):

    sess.run(train,feed_dict={x:x_train,y:y_train})

    if step%400 == 0 :

        loss, acc = sess.run([cost,accuracy],feed_dict={x:x_test, y:y_test})

        print("step :",step,"loss :",loss,"Acc :",acc)

# 학습모델 정확도 확인

a = sess.run(hypothesis,feed_dict={x:x_test})

print(a,sess.run(tf.argmax(a,1)))

# 데이터 형식이 안맞아서 오류가 난다.

np.mean(sess.run(tf.argmax(a,1))) == np.argmax(y_test,1)

# y_test array 바꿔주면 오류가 나지 않는다.

np.mean(sess.run(tf.argmax(a,1))) == np.argmax(np.array(y_test).astype(np.float32),1)

 

반응형