-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/haven-jeon/TensorFlow-Book-R # Conflicts: # ch10_rnn/Concept03_kor_word_spacing_rnn.Rmd
- Loading branch information
Showing
5 changed files
with
284 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,278 @@ | ||
Ch 10: Concept 04 | ||
================ | ||
|
||
Korean word spacing RNN | ||
======================= | ||
|
||
``` r | ||
#https://github.com/hunkim/DeepLearningZeroToAll/blob/master/lab-12-2-char-seq-rnn.py | ||
library(tensorflow) | ||
library(hashmap) | ||
|
||
sents <- "아버지가 방에 들어가신다. " | ||
|
||
uniq_char <- unique(strsplit(sents, split='')[[1]]) | ||
|
||
chmap <- hashmap(uniq_char, 1:length(uniq_char)) | ||
train_x <- array(0, dim=c(1, 15, length(uniq_char))) | ||
|
||
chars <- strsplit(sents, split='')[[1]] | ||
|
||
for(i in 1:length(chars)){ | ||
train_x[1,i,chmap[[chars[i]]]] <- 1 | ||
} | ||
|
||
train_y <- matrix(0, ncol=15) | ||
|
||
for(i in 2:length(chars)){ | ||
|
||
if(chars[i] == ' '){ | ||
train_y[1,i - 1] <- 1 | ||
} | ||
} | ||
|
||
|
||
|
||
# hyper parameters | ||
dic_size <- as.integer(length(chmap$keys())) # RNN input size (one hot size) | ||
hidden_size <- 2L # RNN output size | ||
num_classes <- 2L # final output size (RNN or softmax, etc.) | ||
batch_size <- 1L # one sample data, one batch | ||
sequence_length <- as.integer(length(chars)) # number of lstm rollings (unit #) | ||
|
||
|
||
X <- tf$placeholder(tf$float32, list(NULL, sequence_length, dic_size)) # X data | ||
Y <- tf$placeholder(tf$int32, list(NULL, sequence_length)) # Y label | ||
|
||
cell <- tf$contrib$rnn$BasicLSTMCell( | ||
num_units=hidden_size, state_is_tuple=TRUE) | ||
initial_state <- cell$zero_state(batch_size, tf$float32) | ||
outputs_states <- tf$nn$dynamic_rnn( | ||
cell, X, initial_state=initial_state, dtype=tf$float32) | ||
|
||
#---- | ||
# FC layer | ||
X_for_fc <- tf$reshape(outputs_states[[1]], list(-1L, hidden_size)) | ||
outputs <- tf$contrib$layers$fully_connected(outputs_states[[1]], num_classes, activation_fn=NULL) | ||
|
||
# reshape out for sequence_loss | ||
outputs <- tf$reshape(outputs, list(batch_size, sequence_length, num_classes)) | ||
|
||
weights <- tf$ones(list(batch_size, sequence_length)) | ||
sequence_loss <- tf$contrib$seq2seq$sequence_loss( | ||
logits=outputs, targets=Y, weights=weights) | ||
loss <- tf$reduce_mean(sequence_loss) | ||
train <- tf$train$AdamOptimizer(learning_rate=0.1)$minimize(loss) | ||
|
||
prediction <- tf$argmax(outputs, axis=2L) | ||
|
||
with(tf$Session() %as% sess, { | ||
sess$run(tf$global_variables_initializer()) | ||
for(i in 1:50){ | ||
l <- sess$run(list(loss, train), feed_dict=dict(X= train_x, Y= train_y)) | ||
result <- sess$run(prediction, feed_dict=dict(X= train_x)) | ||
print(result) | ||
} | ||
}) | ||
``` | ||
|
||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 0 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 0 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 0 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 0 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 0 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 0 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 0 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 0 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 0 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 0 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 0 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 0 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 0 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 0 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 | ||
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] | ||
## [1,] 0 0 0 1 0 0 1 0 0 0 0 0 0 | ||
## [,14] [,15] | ||
## [1,] 1 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
-25.6 KB
(21%)
...rnn/Concept03_rnn_real_world_files/figure-markdown_github/unnamed-chunk-3-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-25.8 KB
(21%)
...rnn/Concept03_rnn_real_world_files/figure-markdown_github/unnamed-chunk-3-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.