diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..f3cdce0d --- /dev/null +++ b/.gitignore @@ -0,0 +1,268 @@ +################# +## Eclipse +################# + +*.pydevproject +.project +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + + +################# +## Visual Studio +################# + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results + +[Dd]ebug/ +[Rr]elease/ +x64/ +build/ +[Bb]in/ +[Oo]bj/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.log +*.scc + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +*.ncrunch* +.*crunch*.local.xml + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.Publish.xml +*.pubxml +*.publishproj + +# NuGet Packages Directory +## TODO: If you have NuGet Package Restore enabled, uncomment the next line +#packages/ + +# Windows Azure Build Output +csx +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +sql/ +*.Cache +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.[Pp]ublish.xml +*.pfx +*.publishsettings + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +App_Data/*.mdf +App_Data/*.ldf + +############# +## Windows detritus +############# + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Mac crap +.DS_Store + + +############# +## Python +############# + +*.py[cod] + +# Packages +*.egg +*.egg-info +dist/ +build/ +eggs/ +parts/ +var/ +sdist/ +develop-eggs/ +.installed.cfg + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox + +#Translations +*.mo + +#Mr Developer +.mr.developer.cfg + +############# +## PyCharm +############# + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries +.idea/*.* + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties \ No newline at end of file diff --git a/06 - RNN, ChatBot/01 - Counting.py b/06 - RNN, ChatBot/01 - Counting.py index ff41168a..a3a83a74 100644 --- a/06 - RNN, ChatBot/01 - Counting.py +++ b/06 - RNN, ChatBot/01 - Counting.py @@ -69,25 +69,33 @@ def one_hot_seq(seq_data): # [batch_size, n_steps, n_input] # -> Tensor[n_steps, batch_size, n_input] X_t = tf.transpose(X, [1, 0, 2]) -# -> Tensor[n_steps*batch_size, n_input] -X_t = tf.reshape(X_t, [-1, n_input]) -# -> [n_steps, Tensor[batch_size, n_input]] -X_t = tf.split(0, n_steps, X_t) + +# Static RNN을 사용할 경우 아래 코드까지 사용 +# # -> Tensor[n_steps*batch_size, n_input] +# X_t = tf.reshape(X_t, [-1, n_input]) +# # -> [n_steps, Tensor[batch_size, n_input]] +# # X_t = tf.split(0, n_steps, X_t) # tf 0.12 +# X_t = tf.split(X_t, n_steps, axis=0) # RNN 셀을 생성합니다. # 다음 함수들을 사용하면 다른 구조의 셀로 간단하게 변경할 수 있습니다 # BasicRNNCell,BasicLSTMCell,GRUCell -cell = tf.nn.rnn_cell.BasicRNNCell(n_hidden) +cell = tf.contrib.rnn.BasicRNNCell(n_hidden) -# tf.nn.rnn 함수를 이용해 순환 신경망을 만듭니다. +# tf.nn.dynamic_rnn 함수를 이용해 순환 신경망을 만듭니다. # 역시 겁나 매직!! -outputs, states = tf.nn.rnn(cell, X_t, dtype=tf.float32) +outputs, states = tf.nn.dynamic_rnn(cell, X_t, dtype=tf.float32, time_major=True) + +# tf.contrib.rnn.static_rnn 함수를 이용할 경우 아래 코드 사용 +# outputs, states = tf.contrib.rnn.static_rnn(cell, X_t, dtype=tf.float32) + + # 손실 함수 작성을 위해 출력값을 Y 와 같은 형태의 차원으로 재구성합니다 logits = tf.matmul(outputs[-1], W) + b cost = tf.reduce_mean( - tf.nn.softmax_cross_entropy_with_logits(logits, Y)) + tf.nn.softmax_cross_entropy_with_logits(labels=Y, logits=logits)) train_op = tf.train.RMSPropOptimizer(learning_rate=0.01).minimize(cost) @@ -104,13 +112,13 @@ def one_hot_seq(seq_data): _, loss = sess.run([train_op, cost], feed_dict={X: x_batch, Y: y_batch}) # 학습하는 동안 예측값의 변화를 출력해봅니다. - print sess.run(tf.argmax(logits, 1), feed_dict={X: x_batch, Y: y_batch}) - print sess.run(tf.argmax(Y, 1), feed_dict={X: x_batch, Y: y_batch}) + print (sess.run(tf.argmax(logits, 1), feed_dict={X: x_batch, Y: y_batch})) + print (sess.run(tf.argmax(Y, 1), feed_dict={X: x_batch, Y: y_batch})) - print 'Epoch:', '%04d' % (epoch + 1), \ - 'cost =', '{:.6f}'.format(loss) + print ('Epoch:', '%04d' % (epoch + 1), \ + 'cost =', '{:.6f}'.format(loss)) -print '최적화 완료!' +print ('최적화 완료!') ######### @@ -126,9 +134,9 @@ def one_hot_seq(seq_data): real, predict, accuracy_val = sess.run([tf.argmax(Y, 1), prediction, accuracy], feed_dict={X: x_batch, Y: y_batch}) -print "\n=== 예측 결과 ===" -print '순차열:', seq_data -print '실제값:', [num_arr[i] for i in real] -print '예측값:', [num_arr[i] for i in predict] -print '정확도:', accuracy_val +print ("\n=== 예측 결과 ===") +print ('순차열:', seq_data) +print ('실제값:', [num_arr[i] for i in real]) +print ('예측값:', [num_arr[i] for i in predict]) +print ('정확도:', accuracy_val) diff --git a/06 - RNN, ChatBot/02 - Dynamic RNN.py b/06 - RNN, ChatBot/02 - Dynamic RNN.py index e19d0021..344c9fb1 100644 --- a/06 - RNN, ChatBot/02 - Dynamic RNN.py +++ b/06 - RNN, ChatBot/02 - Dynamic RNN.py @@ -61,10 +61,13 @@ def one_hot_seq(seq_data): # RNN 셀을 생성합니다. # 다중 레이어와 과적합 방지를 위한 Dropout 기법을 사용합니다. -cell = tf.nn.rnn_cell.BasicRNNCell(n_hidden) -cell = tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=0.5) +def cell(): + rnn_cell = tf.contrib.rnn.BasicRNNCell(n_hidden) + rnn_cell = tf.contrib.rnn.DropoutWrapper(rnn_cell, output_keep_prob=0.5) + return rnn_cell + # 다중 레이어 구성을 다음과 같이 아주 간단하게 만들 수 있습니다. -cell = tf.nn.rnn_cell.MultiRNNCell([cell] * n_layers) +cell = tf.contrib.rnn.MultiRNNCell([cell() for _ in range(n_layers)]) # tf.nn.dynamic_rnn 함수를 이용해 순환 신경망을 만듭니다. outputs, states = tf.nn.dynamic_rnn(cell, X_t, dtype=tf.float32, time_major=True) @@ -78,7 +81,7 @@ def one_hot_seq(seq_data): cost = tf.reduce_mean( - tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels)) + tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=logits)) train_op = tf.train.RMSPropOptimizer(learning_rate=0.01).minimize(cost) @@ -96,11 +99,11 @@ def one_hot_seq(seq_data): _, loss4 = sess.run([train_op, cost], feed_dict={X: x_batch, Y: y_batch}) _, loss3 = sess.run([train_op, cost], feed_dict={X: x_batch2, Y: y_batch2}) - print 'Epoch:', '%04d' % (epoch + 1), 'cost =', \ + print ('Epoch:', '%04d' % (epoch + 1), 'cost =', \ 'bucket[4] =', '{:.6f}'.format(loss4), \ - 'bucket[3] =', '{:.6f}'.format(loss3) + 'bucket[3] =', '{:.6f}'.format(loss3)) -print '최적화 완료!' +print ('최적화 완료!') ######### @@ -116,11 +119,11 @@ def prediction(seq_data): real, predict, accuracy_val = sess.run([labels, prediction, accuracy], feed_dict={X: x_batch_t, Y: y_batch_t}) - print "\n=== 예측 결과 ===" - print '순차열:', seq_data - print '실제값:', [num_arr[i] for i in real] - print '예측값:', [num_arr[i] for i in predict] - print '정확도:', accuracy_val + print ("\n=== 예측 결과 ===") + print ('순차열:', seq_data) + print ('실제값:', [num_arr[i] for i in real]) + print ('예측값:', [num_arr[i] for i in predict]) + print ('정확도:', accuracy_val) # 학습 데이터에 있던 시퀀스로 테스트 diff --git a/06 - RNN, ChatBot/03 - Seq2Seq.py b/06 - RNN, ChatBot/03 - Seq2Seq.py index ba0f3d12..23f390ce 100644 --- a/06 - RNN, ChatBot/03 - Seq2Seq.py +++ b/06 - RNN, ChatBot/03 - Seq2Seq.py @@ -69,20 +69,25 @@ def one_hot_seq(seq_data): enc_input = tf.transpose(enc_input, [1, 0, 2]) dec_input = tf.transpose(dec_input, [1, 0, 2]) +def cell(): + rnn_cell = tf.contrib.rnn.BasicRNNCell(n_hidden) + rnn_cell = tf.contrib.rnn.DropoutWrapper(rnn_cell, output_keep_prob=0.5) + return rnn_cell + # 인코더 셀을 구성한다. with tf.variable_scope('encode'): - enc_cell = tf.nn.rnn_cell.BasicRNNCell(n_hidden) - enc_cell = tf.nn.rnn_cell.DropoutWrapper(enc_cell, output_keep_prob=0.5) - enc_cell = tf.nn.rnn_cell.MultiRNNCell([enc_cell] * n_layers) + # enc_cell = tf.contrib.rnn.BasicRNNCell(n_hidden) + # enc_cell = tf.contrib.rnn.DropoutWrapper(enc_cell, output_keep_prob=0.5) + enc_cell = tf.contrib.rnn.MultiRNNCell([cell() for _ in range(n_layers)]) outputs, enc_states = tf.nn.dynamic_rnn(enc_cell, enc_input, dtype=tf.float32) # 디코더 셀을 구성한다. with tf.variable_scope('decode'): - dec_cell = tf.nn.rnn_cell.BasicRNNCell(n_hidden) - dec_cell = tf.nn.rnn_cell.DropoutWrapper(dec_cell, output_keep_prob=0.5) - dec_cell = tf.nn.rnn_cell.MultiRNNCell([dec_cell] * n_layers) + # dec_cell = tf.contrib.rnn.BasicRNNCell(n_hidden) + # dec_cell = tf.contrib.rnn.DropoutWrapper(dec_cell, output_keep_prob=0.5) + dec_cell = tf.contrib.rnn.MultiRNNCell([cell() for _ in range(n_layers)]) # Seq2Seq 모델은 인코더 셀의 최종 상태값을 # 디코더 셀의 초기 상태값으로 넣어주는 것이 핵심. @@ -103,7 +108,7 @@ def one_hot_seq(seq_data): logits = tf.reshape(logits, [-1, time_steps, n_classes]) -cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits, targets)) +cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=targets, logits=logits)) train_op = tf.train.AdamOptimizer(learning_rate=0.01).minimize(cost) @@ -122,11 +127,11 @@ def one_hot_seq(seq_data): _, loss3 = sess.run([train_op, cost], feed_dict={enc_input: x_batch2, dec_input: y_batch2, targets: target_batch2}) - print 'Epoch:', '%04d' % (epoch + 1), 'cost =', \ + print ('Epoch:', '%04d' % (epoch + 1), 'cost =', \ 'bucket[4] =', '{:.6f}'.format(loss4), \ - 'bucket[3] =', '{:.6f}'.format(loss3) + 'bucket[3] =', '{:.6f}'.format(loss3)) -print '최적화 완료!' +print ('최적화 완료!') ######### @@ -143,11 +148,11 @@ def prediction_test(seq_data): dec_input: y_batch_t, targets: target_batch_t}) - print "\n=== 예측 결과 ===" - print '순차열:', seq_data - print '실제값:', [[num_arr[j] for j in dec] for dec in real] - print '예측값:', [[num_arr[i] for i in dec] for dec in predict] - print '정확도:', accuracy_val + print ("\n=== 예측 결과 ===") + print ('순차열:', seq_data) + print ('실제값:', [[num_arr[j] for j in dec] for dec in real]) + print ('예측값:', [[num_arr[i] for i in dec] for dec in predict]) + print ('정확도:', accuracy_val) # 학습 데이터에 있던 시퀀스로 테스트 @@ -193,24 +198,24 @@ def decode_loop(seq_data): return decode_seq -print "\n=== 한글자씩 점진적으로 시퀀스를 예측 ===" +print ("\n=== 한글자씩 점진적으로 시퀀스를 예측 ===") seq_data = ['123', ''] -print "123 ->", decode_loop(seq_data) +print ("123 ->", decode_loop(seq_data)) seq_data = ['67', ''] -print "67 ->", decode_loop(seq_data) +print ("67 ->", decode_loop(seq_data)) seq_data = ['3456', ''] -print "3456 ->", decode_loop(seq_data) +print ("3456 ->", decode_loop(seq_data)) -print "\n=== 전체 시퀀스를 한 번에 예측 ===" +print ("\n=== 전체 시퀀스를 한 번에 예측 ===") seq_data = ['123', 'PPP'] -print "123 ->", decode(seq_data) +print ("123 ->", decode(seq_data)) seq_data = ['67', 'PP'] -print "67 ->", decode(seq_data) +print ("67 ->", decode(seq_data)) seq_data = ['3456', 'PPPP'] -print "3456 ->", decode(seq_data) +print ("3456 ->", decode(seq_data)) diff --git a/06 - RNN, ChatBot/04 - ChatBot/chat.py b/06 - RNN, ChatBot/04 - ChatBot/chat.py index 708ecf95..bf10a80a 100644 --- a/06 - RNN, ChatBot/04 - ChatBot/chat.py +++ b/06 - RNN, ChatBot/04 - ChatBot/chat.py @@ -28,7 +28,7 @@ def run(self): line = sys.stdin.readline() while line: - print self.get_replay(line.strip()) + print (self.get_replay(line.strip())) sys.stdout.write("\n> ") sys.stdout.flush() @@ -72,7 +72,7 @@ def get_replay(self, msg): def main(_): - print "깨어나는 중 입니다. 잠시만 기다려주세요...\n" + print ("깨어나는 중 입니다. 잠시만 기다려주세요...\n") chatbot = ChatBot(FLAGS.voc_path, FLAGS.train_dir) chatbot.run() diff --git a/06 - RNN, ChatBot/04 - ChatBot/dialog.py b/06 - RNN, ChatBot/04 - ChatBot/dialog.py index df95a99b..9517bd5c 100644 --- a/06 - RNN, ChatBot/04 - ChatBot/dialog.py +++ b/06 - RNN, ChatBot/04 - ChatBot/dialog.py @@ -4,6 +4,7 @@ import tensorflow as tf import numpy as np import re +import codecs from config import FLAGS @@ -152,7 +153,7 @@ def ids_to_tokens(self, ids): def load_examples(self, data_path): self.examples = [] - with open(data_path, 'r') as content_file: + with open(data_path, 'r', encoding='utf-8') as content_file: for line in content_file: tokens = self.tokenizer(line.strip()) ids = self.tokens_to_ids(tokens) @@ -161,7 +162,8 @@ def load_examples(self, data_path): def tokenizer(self, sentence): # 공백으로 나누고 특수문자는 따로 뽑아낸다. words = [] - _TOKEN_RE_ = re.compile(b"([.,!?\"':;)(])") + # _TOKEN_RE_ = re.compile(b"([.,!?\"':;)(])") + _TOKEN_RE_ = re.compile("([.,!?\"':;)(])") for fragment in sentence.strip().split(): words.extend(_TOKEN_RE_.split(fragment)) @@ -181,7 +183,7 @@ def build_vocab(self, data_path, vocab_path): def load_vocab(self, vocab_path): self.vocab_list = self._PRE_DEFINED_ + [] - with open(vocab_path, 'r') as vocab_file: + with open(vocab_path, 'r', encoding='utf-8') as vocab_file: for line in vocab_file: self.vocab_list.append(line.strip()) @@ -194,22 +196,22 @@ def main(_): dialog = Dialog() if FLAGS.data_path and FLAGS.voc_test: - print "다음 데이터로 어휘 사전을 테스트합니다.", FLAGS.data_path + print ("다음 데이터로 어휘 사전을 테스트합니다.", FLAGS.data_path) dialog.load_vocab(FLAGS.voc_path) dialog.load_examples(FLAGS.data_path) enc, dec, target = dialog.next_batch(10) - print target + print (target) enc, dec, target = dialog.next_batch(10) - print target + print (target) elif FLAGS.data_path and FLAGS.voc_build: - print "다음 데이터에서 어휘 사전을 생성합니다.", FLAGS.data_path + print ("다음 데이터에서 어휘 사전을 생성합니다.", FLAGS.data_path) dialog.build_vocab(FLAGS.data_path, FLAGS.voc_path) elif FLAGS.voc_test: dialog.load_vocab(FLAGS.voc_path) - print dialog.vocab_dict + print (dialog.vocab_dict) if __name__ == "__main__": diff --git a/06 - RNN, ChatBot/04 - ChatBot/model.py b/06 - RNN, ChatBot/04 - ChatBot/model.py index 813da0eb..4cf2a662 100644 --- a/06 - RNN, ChatBot/04 - ChatBot/model.py +++ b/06 - RNN, ChatBot/04 - ChatBot/model.py @@ -38,24 +38,24 @@ def build_model(self): enc_cell, dec_cell = self.build_cells() with tf.variable_scope('encode'): - outputs, enc_states = tf.nn.dynamic_rnn(enc_cell, self.enc_input, dtype=tf.float32) + outputs, enc_states = tf.nn.dynamic_rnn(enc_cell, self.enc_input, dtype=tf.float32, time_major=False) with tf.variable_scope('decode'): outputs, dec_states = tf.nn.dynamic_rnn(dec_cell, self.dec_input, dtype=tf.float32, - initial_state=enc_states) + initial_state=enc_states, time_major=False) self.logits, self.cost, self.train_op = self.build_ops(outputs, self.targets) self.outputs = tf.argmax(self.logits, 2) - def build_cells(self, output_keep_prob=0.5): - enc_cell = tf.nn.rnn_cell.BasicRNNCell(self.n_hidden) - enc_cell = tf.nn.rnn_cell.DropoutWrapper(enc_cell, output_keep_prob=output_keep_prob) - enc_cell = tf.nn.rnn_cell.MultiRNNCell([enc_cell] * self.n_layers) + def cell(self, n_hidden, output_keep_prob): + rnn_cell = tf.contrib.rnn.BasicRNNCell(n_hidden) + rnn_cell = tf.contrib.rnn.DropoutWrapper(rnn_cell, output_keep_prob=output_keep_prob) + return rnn_cell - dec_cell = tf.nn.rnn_cell.BasicRNNCell(self.n_hidden) - dec_cell = tf.nn.rnn_cell.DropoutWrapper(dec_cell, output_keep_prob=output_keep_prob) - dec_cell = tf.nn.rnn_cell.MultiRNNCell([dec_cell] * self.n_layers) + def build_cells(self, output_keep_prob=0.5): + enc_cell = tf.contrib.rnn.MultiRNNCell([self.cell(self.n_hidden, output_keep_prob) for _ in range(self.n_layers)]) + dec_cell = tf.contrib.rnn.MultiRNNCell([self.cell(self.n_hidden, output_keep_prob) for _ in range(self.n_layers)]) return enc_cell, dec_cell @@ -66,7 +66,7 @@ def build_ops(self, outputs, targets): logits = tf.matmul(outputs, self.weights) + self.bias logits = tf.reshape(logits, [-1, time_steps, self.vocab_size]) - cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits, targets)) + cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=targets, logits=logits)) train_op = tf.train.AdamOptimizer(learning_rate=self.learning_late).minimize(cost, global_step=self.global_step) tf.summary.scalar('cost', cost) diff --git a/06 - RNN, ChatBot/04 - ChatBot/model/conversation.ckpt-5000.data-00000-of-00001 b/06 - RNN, ChatBot/04 - ChatBot/model/conversation.ckpt-5000.data-00000-of-00001 index e88a0e6e..17e0c4a0 100644 Binary files a/06 - RNN, ChatBot/04 - ChatBot/model/conversation.ckpt-5000.data-00000-of-00001 and b/06 - RNN, ChatBot/04 - ChatBot/model/conversation.ckpt-5000.data-00000-of-00001 differ diff --git a/06 - RNN, ChatBot/04 - ChatBot/model/conversation.ckpt-5000.index b/06 - RNN, ChatBot/04 - ChatBot/model/conversation.ckpt-5000.index index bbe07a5b..362e7543 100644 Binary files a/06 - RNN, ChatBot/04 - ChatBot/model/conversation.ckpt-5000.index and b/06 - RNN, ChatBot/04 - ChatBot/model/conversation.ckpt-5000.index differ diff --git a/06 - RNN, ChatBot/04 - ChatBot/model/conversation.ckpt-5000.meta b/06 - RNN, ChatBot/04 - ChatBot/model/conversation.ckpt-5000.meta index 2b3262c3..5a5ae5c0 100644 Binary files a/06 - RNN, ChatBot/04 - ChatBot/model/conversation.ckpt-5000.meta and b/06 - RNN, ChatBot/04 - ChatBot/model/conversation.ckpt-5000.meta differ diff --git a/06 - RNN, ChatBot/04 - ChatBot/train.py b/06 - RNN, ChatBot/04 - ChatBot/train.py index b5408ab7..91680443 100644 --- a/06 - RNN, ChatBot/04 - ChatBot/train.py +++ b/06 - RNN, ChatBot/04 - ChatBot/train.py @@ -17,11 +17,14 @@ def train(dialog, batch_size=100, epoch=100): # TODO: 세션을 로드하고 로그를 위한 summary 저장등의 로직을 Seq2Seq 모델로 넣을 필요가 있음 ckpt = tf.train.get_checkpoint_state(FLAGS.train_dir) if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path): - print "다음 파일에서 모델을 읽는 중 입니다..", ckpt.model_checkpoint_path + print ("다음 파일에서 모델을 읽는 중 입니다..", ckpt.model_checkpoint_path) model.saver.restore(sess, ckpt.model_checkpoint_path) else: - print "새로운 모델을 생성하는 중 입니다." + print ("새로운 모델을 생성하는 중 입니다.") sess.run(tf.global_variables_initializer()) + # print ("새로운 모델을 생성하는 중 입니다.") + # sess.run(tf.global_variables_initializer()) + writer = tf.summary.FileWriter(FLAGS.log_dir, sess.graph) @@ -35,23 +38,23 @@ def train(dialog, batch_size=100, epoch=100): if (step + 1) % 100 == 0: model.write_logs(sess, writer, enc_input, dec_input, targets) - print 'Step:', '%06d' % model.global_step.eval(),\ - 'cost =', '{:.6f}'.format(loss) + print ('Step:', '%06d' % model.global_step.eval(),\ + 'cost =', '{:.6f}'.format(loss)) checkpoint_path = os.path.join(FLAGS.train_dir, FLAGS.ckpt_name) model.saver.save(sess, checkpoint_path, global_step=model.global_step) - print '최적화 완료!' + print ('최적화 완료!') def test(dialog, batch_size=100): - print "\n=== 예측 테스트 ===" + print ("\n=== 예측 테스트 ===") model = Seq2Seq(dialog.vocab_size) with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(FLAGS.train_dir) - print "다음 파일에서 모델을 읽는 중 입니다..", ckpt.model_checkpoint_path + print ("다음 파일에서 모델을 읽는 중 입니다..", ckpt.model_checkpoint_path) model.saver.restore(sess, ckpt.model_checkpoint_path) enc_input, dec_input, targets = dialog.next_batch(batch_size) @@ -66,11 +69,11 @@ def test(dialog, batch_size=100): expect = dialog.decode([dialog.examples[pick * 2 + 1]], True) outputs = dialog.cut_eos(outputs[pick]) - print "\n정확도:", accuracy - print "랜덤 결과\n", - print " 입력값:", input - print " 실제값:", expect - print " 예측값:", ' '.join(outputs) + print ("\n정확도:", accuracy) + print ("랜덤 결과\n",) + print (" 입력값:", input) + print (" 실제값:", expect) + print (" 예측값:", ' '.join(outputs)) def main(_):