Chap07.2 - 텐서플로 추상화와 간소화, TFLearn

TFLearnChap07.1 Estimator에서 살펴본 tf.estimator와 마찬가지로 텐서플로의 추상화 라이브러리이다. 이번에는 TFLearn에 대해 알아보도록 하자.

7.3 TFLearn

7.3.1 설치

TFLearn은 텐서플로에 포함되어 있지 않기 때문에 별도의 설치가 필요하다. Terminal(또는 cmd)pip 명령을 이용해 설치할 수 있다.

pip install tflearn

7.3.2 CNN

TFLearnChap07.1 - tf.estimator와 유사하지만, TFLearn을 사용하면 조금 더 깔끔하게 모델을 만들 수 있다. TFLearn.org에서는 TFLearn을 다음과 같이 소개하고 있다.

Easy-to-use and understand high-level API for implementing deep neural networks, with tutorial and examples.

Fast prototyping through highly modular built-in neural network layers, regularizers, optimizers, metrics...

Full transparency over Tensorflow. All functions are built over tensors and can be used independently of TFLearn.

Powerful helper functions to train any TensorFlow graph, with support of multiple inputs, outputs and optimizers.

Easy and beautiful graph visualization, with details about weights, gradients, activations and more...

Effortless device placement for using multiple CPU/GPU.

TFLearn에서의 모델 생성은 regression()을 사용하여 래핑되고 마무리된다. regression()함수에서 손실함수(loss) 및 최적화(optimizer)를 설정해준다.

그렇다면, TFLearn을 이용해 MNIST 데이터를 분류하는 CNN 모델을 만들어 보도록 하자.

 

importnumpyasnp

 

importtflearn

 

importtflearn.datasets.mnistasmnist

 

fromtflearn.layers.coreimportinput_data, dropout, fully_connected

 

fromtflearn.layers.convimportconv_2d, max_pool_2d

 

fromtflearn.layers.normalizationimportlocal_response_normalization

 

fromtflearn.layers.estimatorimportregression

 

 

 

# 데이터를 로딩하고 기본적인 변환을 수행

 

train_x, train_y, test_x, test_y=mnist.load_data(one_hot=True,

 

data_dir='../data')

 

train_x=train_x.reshape([-1, 28, 28, 1])

 

test_x=test_x.reshape([-1, 28, 28, 1])

 

 

 

# Building the network

 

CNN=input_data(shape=[None, 28, 28, 1], name='input')

 

CNN=conv_2d(CNN, 32, 5, activation='relu', regularizer="L2")

 

CNN=max_pool_2d(CNN, 2)

 

CNN=local_response_normalization(CNN)

 

CNN=conv_2d(CNN, 64, 5, activation='relu', regularizer='L2')

 

CNN=max_pool_2d(CNN, 2)

 

CNN=local_response_normalization(CNN)

 

CNN=fully_connected(CNN, 1024, activation=None)

 

CNN=dropout(CNN, 0.5)

 

CNN=fully_connected(CNN, 10, activation='softmax')

 

CNN=regression(CNN, optimizer='adam', learning_rate=0.0001,

 

loss='categorical_crossentropy', name='target')

 

 

 

# Training the network

 

model=tflearn.DNN(CNN, tensorboard_verbose=0,

 

tensorboard_dir='./MNIST_tflearn_board/',

 

checkpoint_path='./MNIST_tflearn_checkpoints/checkpoint')

 

model.fit({'input': train_x}, {'target': train_y}, n_epoch=3,

 

validation_set=({'input': test_x}, {'target': test_y}),

 

snapshot_step=1000, show_metric=True, run_id='convnet_mnist')

view raw tflearn_mnist_cnn.py hosted with by GitHub

위의 코드에서 tflearn.DNN()함수는 tf.estimator.Estimator()와 비슷한 기능을 하는데, regression()으로 래핑된 모델을 인스턴스화하고 만들어진 모델을 전달하는 역할을 한다. 또한 텐서보드(TensorBoard)와 체크포인트(checkpoint) 디렉터리 등을 설정할 수 있다. 모델 적합 연산은 .fit() 메서드를 이용해 수행된다.

모델 적합(.fit()), 즉 학습이 완료되면, 다음과 같은 메소드를 이용해 모델을 평가, 예측, 저장 및 불러오기 등을 수행할 수 있다.

메서드

설명

evaluate(X, Y, batch_size)

주어진 샘플에서 모델을 평가

fit(X, Y, n_epoch)

입력 feature X와 타겟 Y를 모델에 적용하여 학습

get_weights(weight_tensor)

변수의 가중치를 반환

load(model_file)

학습된 모델 가중치를 불러오기

predict(x)

주어진 x 데이터를 모델을 이용해 예측

save(model_file)

학습된 모델 가중치를 저장

set_weights(tensor, weights)

주어진 값을 텐서 변수에 할당

# Evaluate the network
evaluation = model.evaluate(X=test_xY=test_ybatch_size=128)
print(evaluation)

# Predict
pred = model.predict(test_x)
accuarcy = (np.argmax(pred1)==np.argmax(test_y1)).mean()
print(accuarcy)

[0.9861]
0.9861

 

7.3.3. RNN

이번에는 TFLearn을 이용해 RNN을 구현해 보도록하자. 구현할 RNN 모델은 영화 리뷰에 대한 감성분석으로, 리뷰에 대해 좋거나/나쁘거나 두 개의 클래스를 분류하는 모델이다. 데이터는 학습 및 테스트 데이터가 각각 25,000개로 이루어진 IMDb 리뷰 데이터를 사용한다.

import tflearn
from tflearn.data_utils import to_categoricalpad_sequences
from tflearn.datasets import imdb

# IMDb 데이터셋 로드
(train_xtrain_y), (test_xtest_y), _ = imdb.load_data(path='../data/imdb.pkl',
                                                        n_words=10000,
                                                        valid_portion=0.1)

위에서 불러온 IMDb 데이터는 각각 다른 시퀀스 길이를 가지고 있으므로 최대 시퀀스 길이를 100으로 하여 tflearn.data_utils.pad_sequences()를 사용해 제로 패딩으로 시퀀스의 길이를 맞춰준다.

# Sequence padding and Converting labels to binary vectors
train_x = pad_sequences(train_xmaxlen=100value=0.)
test_x = pad_sequences(test_xmaxlen=100value=0.)
train_y = to_categorical(train_ynb_classes=2)
test_y = to_categorical(test_ynb_classes=2)

그런다음, tflearn.embedding()으로 벡터 공간으로의 임베딩을 수행한다. 아래의 코드에서 확인할 수 있듯이 각 단어는 128 크기인 벡터에 매핑된다. 이렇게 임베딩된 결과를 LSTM layerfully_connected layer를 추가해 모델을 구성해준다.

# Building a LSTM network
# Embedding
RNN = tflearn.input_data([None100])
RNN = tflearn.embedding(RNNinput_dim=10000output_dim=128)

# LSTM Cell
RNN = tflearn.lstm(RNN128dropout=0.8)
RNN = tflearn.fully_connected(RNN2activation='softmax')
RNN = tflearn.regression(RNNoptimizer='adam',
                        learning_rate=0.001loss='categorical_crossentropy')

# Training the network
model = tflearn.DNN(RNNtensorboard_verbose=0,
                   tensorboard_dir='./IMDb-tflearn_board/')
model.fit(train_xtrain_y,
         validation_set=(test_xtest_y),
         show_metric=Truebatch_size=32)

Training Step: 7039 | total loss: [1m[32m0.05996[0m[0m | time: 22.310s
| Adam | epoch: 010 | loss: 0.05996 - acc: 0.9827 -- iter: 22496/22500
Training Step: 7040 | total loss: [1m[32m0.05474[0m[0m | time: 23.425s
| Adam | epoch: 010 | loss: 0.05474 - acc: 0.9845 | val_loss: 0.85953 - val_acc: 0.8064 -- iter: 22500/22500
--

# evaluate the network
evaluation = model.evaluate(test_xtest_ybatch_size=128)
print(evaluation)

[0.8063999997138978]


아래의 코드는 위의 코드를 합친 전체 코드이다.

 

importtflearn

 

fromtflearn.data_utilsimportto_categorical, pad_sequences

 

fromtflearn.datasetsimportimdb

 

 

 

# IMDb 데이터셋 로드

 

(train_x, train_y), (test_x, test_y), _=imdb.load_data(path='../data/imdb.pkl',

 

n_words=10000,

 

valid_portion=0.1)

 

 

 

 

 

# Sequence padding and Converting labels to binary vectors

 

train_x=pad_sequences(train_x, maxlen=100, value=0.)

 

test_x=pad_sequences(test_x, maxlen=100, value=0.)

 

train_y=to_categorical(train_y, nb_classes=2)

 

test_y=to_categorical(test_y, nb_classes=2)

 

 

 

# Building a LSTM network

 

# Embedding

 

RNN=tflearn.input_data([None, 100])

 

RNN=tflearn.embedding(RNN, input_dim=10000, output_dim=128)

 

 

 

# LSTM Cell

 

RNN=tflearn.lstm(RNN, 128, dropout=0.8)

 

RNN=tflearn.fully_connected(RNN, 2, activation='softmax')

 

RNN=tflearn.regression(RNN, optimizer='adam',

 

learning_rate=0.001, loss='categorical_crossentropy')

 

 

 

# Training the network

 

model=tflearn.DNN(RNN, tensorboard_verbose=0,

 

tensorboard_dir='./IMDb-tflearn_board/')

 

model.fit(train_x, train_y,

 

validation_set=(test_x, test_y),

 

show_metric=True, batch_size=32)

 

 

 

# evaluate the network

 

evaluation=model.evaluate(test_x, test_y, batch_size=128)

 

print(evaluation)

view raw tflearn_imdb_lstm.py hosted with by GitHub

 

 

7.3.4 정리

이번 포스팅에서는 TFLearn을 살펴보고, 이를 이용해 CNNRNN을 구현해 보았다. 이 외에도 TFLearn에 대한 사용법 및 예제는 http://tflearn.org/ 에서 확인할 수 있다.



출처https://excelsior-cjh.tistory.com/158?category=940399 [EXCELSIOR]

 

 

 

 

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

 

 

 

 

Chap07.2 - 텐서플로 추상화와 간소화, TFLearn

TFLearnChap07.1 Estimator에서 살펴본 tf.estimator와 마찬가지로 텐서플로의 추상화 라이브러리이다. 이번에는 TFLearn에 대해 알아보도록 하자.

7.3 TFLearn

7.3.1 설치

TFLearn은 텐서플로에 포함되어 있지 않기 때문에 별도의 설치가 필요하다. Terminal(또는 cmd)pip 명령을 이용해 설치할 수 있다.

pip install tflearn

7.3.2 CNN

TFLearnChap07.1 - tf.estimator와 유사하지만, TFLearn을 사용하면 조금 더 깔끔하게 모델을 만들 수 있다. TFLearn.org에서는 TFLearn을 다음과 같이 소개하고 있다.

Easy-to-use and understand high-level API for implementing deep neural networks, with tutorial and examples.

Fast prototyping through highly modular built-in neural network layers, regularizers, optimizers, metrics...

Full transparency over Tensorflow. All functions are built over tensors and can be used independently of TFLearn.

Powerful helper functions to train any TensorFlow graph, with support of multiple inputs, outputs and optimizers.

Easy and beautiful graph visualization, with details about weights, gradients, activations and more...

Effortless device placement for using multiple CPU/GPU.

TFLearn에서의 모델 생성은 regression()을 사용하여 래핑되고 마무리된다. regression()함수에서 손실함수(loss) 및 최적화(optimizer)를 설정해준다.

그렇다면, TFLearn을 이용해 MNIST 데이터를 분류하는 CNN 모델을 만들어 보도록 하자.

 

importnumpyasnp

 

importtflearn

 

importtflearn.datasets.mnistasmnist

 

fromtflearn.layers.coreimportinput_data, dropout, fully_connected

 

fromtflearn.layers.convimportconv_2d, max_pool_2d

 

fromtflearn.layers.normalizationimportlocal_response_normalization

 

fromtflearn.layers.estimatorimportregression

 

 

 

# 데이터를 로딩하고 기본적인 변환을 수행

 

train_x, train_y, test_x, test_y=mnist.load_data(one_hot=True,

 

data_dir='../data')

 

train_x=train_x.reshape([-1, 28, 28, 1])

 

test_x=test_x.reshape([-1, 28, 28, 1])

 

 

 

# Building the network

 

CNN=input_data(shape=[None, 28, 28, 1], name='input')

 

CNN=conv_2d(CNN, 32, 5, activation='relu', regularizer="L2")

 

CNN=max_pool_2d(CNN, 2)

 

CNN=local_response_normalization(CNN)

 

CNN=conv_2d(CNN, 64, 5, activation='relu', regularizer='L2')

 

CNN=max_pool_2d(CNN, 2)

 

CNN=local_response_normalization(CNN)

 

CNN=fully_connected(CNN, 1024, activation=None)

 

CNN=dropout(CNN, 0.5)

 

CNN=fully_connected(CNN, 10, activation='softmax')

 

CNN=regression(CNN, optimizer='adam', learning_rate=0.0001,

 

loss='categorical_crossentropy', name='target')

 

 

 

# Training the network

 

model=tflearn.DNN(CNN, tensorboard_verbose=0,

 

tensorboard_dir='./MNIST_tflearn_board/',

 

checkpoint_path='./MNIST_tflearn_checkpoints/checkpoint')

 

model.fit({'input': train_x}, {'target': train_y}, n_epoch=3,

 

validation_set=({'input': test_x}, {'target': test_y}),

 

snapshot_step=1000, show_metric=True, run_id='convnet_mnist')

view raw tflearn_mnist_cnn.py hosted with by GitHub

위의 코드에서 tflearn.DNN()함수는 tf.estimator.Estimator()와 비슷한 기능을 하는데, regression()으로 래핑된 모델을 인스턴스화하고 만들어진 모델을 전달하는 역할을 한다. 또한 텐서보드(TensorBoard)와 체크포인트(checkpoint) 디렉터리 등을 설정할 수 있다. 모델 적합 연산은 .fit() 메서드를 이용해 수행된다.

모델 적합(.fit()), 즉 학습이 완료되면, 다음과 같은 메소드를 이용해 모델을 평가, 예측, 저장 및 불러오기 등을 수행할 수 있다.

메서드

설명

evaluate(X, Y, batch_size)

주어진 샘플에서 모델을 평가

fit(X, Y, n_epoch)

입력 feature X와 타겟 Y를 모델에 적용하여 학습

get_weights(weight_tensor)

변수의 가중치를 반환

load(model_file)

학습된 모델 가중치를 불러오기

predict(x)

주어진 x 데이터를 모델을 이용해 예측

save(model_file)

학습된 모델 가중치를 저장

set_weights(tensor, weights)

주어진 값을 텐서 변수에 할당

# Evaluate the network
evaluation = model.evaluate(X=test_xY=test_ybatch_size=128)
print(evaluation)

# Predict
pred = model.predict(test_x)
accuarcy = (np.argmax(pred1)==np.argmax(test_y1)).mean()
print(accuarcy)

[0.9861]
0.9861

 

7.3.3. RNN

이번에는 TFLearn을 이용해 RNN을 구현해 보도록하자. 구현할 RNN 모델은 영화 리뷰에 대한 감성분석으로, 리뷰에 대해 좋거나/나쁘거나 두 개의 클래스를 분류하는 모델이다. 데이터는 학습 및 테스트 데이터가 각각 25,000개로 이루어진 IMDb 리뷰 데이터를 사용한다.

import tflearn
from tflearn.data_utils import to_categoricalpad_sequences
from tflearn.datasets import imdb

# IMDb 데이터셋 로드
(train_xtrain_y), (test_xtest_y), _ = imdb.load_data(path='../data/imdb.pkl',
                                                        n_words=10000,
                                                        valid_portion=0.1)

위에서 불러온 IMDb 데이터는 각각 다른 시퀀스 길이를 가지고 있으므로 최대 시퀀스 길이를 100으로 하여 tflearn.data_utils.pad_sequences()를 사용해 제로 패딩으로 시퀀스의 길이를 맞춰준다.

# Sequence padding and Converting labels to binary vectors
train_x = pad_sequences(train_xmaxlen=100value=0.)
test_x = pad_sequences(test_xmaxlen=100value=0.)
train_y = to_categorical(train_ynb_classes=2)
test_y = to_categorical(test_ynb_classes=2)

그런다음, tflearn.embedding()으로 벡터 공간으로의 임베딩을 수행한다. 아래의 코드에서 확인할 수 있듯이 각 단어는 128 크기인 벡터에 매핑된다. 이렇게 임베딩된 결과를 LSTM layerfully_connected layer를 추가해 모델을 구성해준다.

# Building a LSTM network
# Embedding
RNN = tflearn.input_data([None100])
RNN = tflearn.embedding(RNNinput_dim=10000output_dim=128)

# LSTM Cell
RNN = tflearn.lstm(RNN128dropout=0.8)
RNN = tflearn.fully_connected(RNN2activation='softmax')
RNN = tflearn.regression(RNNoptimizer='adam',
                        learning_rate=0.001loss='categorical_crossentropy')

# Training the network
model = tflearn.DNN(RNNtensorboard_verbose=0,
                   tensorboard_dir='./IMDb-tflearn_board/')
model.fit(train_xtrain_y,
         validation_set=(test_xtest_y),
         show_metric=Truebatch_size=32)

Training Step: 7039 | total loss: [1m[32m0.05996[0m[0m | time: 22.310s
| Adam | epoch: 010 | loss: 0.05996 - acc: 0.9827 -- iter: 22496/22500
Training Step: 7040 | total loss: [1m[32m0.05474[0m[0m | time: 23.425s
| Adam | epoch: 010 | loss: 0.05474 - acc: 0.9845 | val_loss: 0.85953 - val_acc: 0.8064 -- iter: 22500/22500
--

# evaluate the network
evaluation = model.evaluate(test_xtest_ybatch_size=128)
print(evaluation)

[0.8063999997138978]


아래의 코드는 위의 코드를 합친 전체 코드이다.

 

importtflearn

 

fromtflearn.data_utilsimportto_categorical, pad_sequences

 

fromtflearn.datasetsimportimdb

 

 

 

# IMDb 데이터셋 로드

 

(train_x, train_y), (test_x, test_y), _=imdb.load_data(path='../data/imdb.pkl',

 

n_words=10000,

 

valid_portion=0.1)

 

 

 

 

 

# Sequence padding and Converting labels to binary vectors

 

train_x=pad_sequences(train_x, maxlen=100, value=0.)

 

test_x=pad_sequences(test_x, maxlen=100, value=0.)

 

train_y=to_categorical(train_y, nb_classes=2)

 

test_y=to_categorical(test_y, nb_classes=2)

 

 

 

# Building a LSTM network

 

# Embedding

 

RNN=tflearn.input_data([None, 100])

 

RNN=tflearn.embedding(RNN, input_dim=10000, output_dim=128)

 

 

 

# LSTM Cell

 

RNN=tflearn.lstm(RNN, 128, dropout=0.8)

 

RNN=tflearn.fully_connected(RNN, 2, activation='softmax')

 

RNN=tflearn.regression(RNN, optimizer='adam',

 

learning_rate=0.001, loss='categorical_crossentropy')

 

 

 

# Training the network

 

model=tflearn.DNN(RNN, tensorboard_verbose=0,

 

tensorboard_dir='./IMDb-tflearn_board/')

 

model.fit(train_x, train_y,

 

validation_set=(test_x, test_y),

 

show_metric=True, batch_size=32)

 

 

 

# evaluate the network

 

evaluation=model.evaluate(test_x, test_y, batch_size=128)

 

print(evaluation)

view raw tflearn_imdb_lstm.py hosted with by GitHub

 

 

7.3.4 정리

이번 포스팅에서는 TFLearn을 살펴보고, 이를 이용해 CNNRNN을 구현해 보았다. 이 외에도 TFLearn에 대한 사용법 및 예제는 http://tflearn.org/ 에서 확인할 수 있다.



출처https://excelsior-cjh.tistory.com/158?category=940399 [EXCELSIOR]

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
공지 오라클 기본 샘플 데이터베이스 졸리운_곰 2014.01.02 86126
공지 [SQL컨셉] 서적 "SQL컨셉"의 샘플 데이타 베이스 SAMPLE DATABASE of ORACLE 가을의 곰을... 2013.02.10 78629
공지 [G_SQL] Sample Database 가을의 곰을... 2012.05.20 95347
102 [MYSQL] 테이블 스키마 설계 고려사항 졸리운_곰 2022.12.03 1326
101 [MySQL] "아는 만큼 빨라진다" 마이SQL 성능 튜닝 팁 10가지 file 졸리운_곰 2022.11.29 1056
100 [Mysql] mysql에서 json 다루기 file 졸리운_곰 2022.08.02 1219
99 [MySQL] MySQL 에서 JSON Data사용하기 졸리운_곰 2022.08.02 1381
98 [MySQL] 관리자 root , admin 계정 추가 : MySQL 관리자 계정 추가 졸리운_곰 2021.09.26 9250
97 [MySQL] mysql 에서 컬럼과 로우 바꾸기, 행과 열 바꾸기 How to Transpose Rows to Columns Dynamically in MySQL file 졸리운_곰 2021.09.13 1030
96 [MySQL] MySQL ROLLUP , summary, 부분합 구하기 file 졸리운_곰 2021.09.01 1638
95 [mysql] 인덱스 정리 및 팁 file 졸리운_곰 2020.12.04 1384
94 [MySQL] 복제 지연 원인 및 해결 (reason for mysql replication lag/delay) file 졸리운_곰 2020.07.18 1407
93 MySQL Replication(복제) file 졸리운_곰 2020.07.18 1802
92 MySQL replication을 해보자 졸리운_곰 2020.07.18 1690
91 MySQL Replication(복제) - 단방향 이중화 file 졸리운_곰 2020.07.18 855
90 [MySQL] 행, 열 바꾸어 출력하기 CASE ~ AS file 졸리운_곰 2020.06.13 1035
89 [MySQL] 피벗 - 로우 데이터를 컬럼으로 옮기기 file 졸리운_곰 2020.06.13 1362
88 MySQL (or MariaDB) 에서 row 데이터를 column 으로 변경하기 졸리운_곰 2020.06.13 1341
87 [mysql] 운용 application 정보 (proxy, middle ware) 등: awesome-mysql file 졸리운_곰 2020.05.16 26547
86 [MySQL] 10장 여러 개의 테이블 이용하기 file 졸리운_곰 2020.05.16 1137
85 [MySQL] DB에 중복된 값의 개수를 확인하고 싶다. 졸리운_곰 2020.05.06 1184
84 sqlRelay(Mysql DB Pooling) 졸리운_곰 2020.02.17 1451
83 [참고자료] 공개커넥션풀 프로그램 졸리운_곰 2020.02.17 1982
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED