Skip to content

Commit

Permalink
fix conflict
Browse files Browse the repository at this point in the history
Merge branch 'master' of https://github.com/haven-jeon/TensorFlow-Book-R

# Conflicts:
#	ch10_rnn/Concept03_kor_word_spacing_rnn.Rmd
  • Loading branch information
gogamza committed May 19, 2017
2 parents 150cf44 + a5ae05f commit b6eb021
Show file tree
Hide file tree
Showing 5 changed files with 284 additions and 12 deletions.
5 changes: 1 addition & 4 deletions ch10_rnn/Concept03_kor_word_spacing_rnn.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ output: github_document
library(tensorflow)
library(hashmap)
makeCorpus <- function(str){
strv <- strsplit(str,split="")[[1]]
lenstrv <- length(strv)
Expand Down Expand Up @@ -249,7 +250,3 @@ wsp$predict(sent_mat_x, length(sent), 1)
plot(loss_v)
```





278 changes: 278 additions & 0 deletions ch10_rnn/Concept03_kor_word_spacing_rnn.md
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
13 changes: 5 additions & 8 deletions ch10_rnn/Concept03_rnn_real_world.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,11 @@ test_y <- matrix(test_y, ncol=5 , byrow=T)
predictor$train(train_x, train_y, test_x, test_y)
```

## step: 0 train err: 0.820042 test err: 1.690292
## step: 100 train err: 0.041629 test err: 0.253392
## step: 200 train err: 0.040549 test err: 0.245927
## step: 300 train err: 0.039487 test err: 0.248768
## step: 400 train err: 0.038130 test err: 0.211382
## step: 500 train err: 0.036092 test err: 0.278987
## step: 600 train err: 0.033883 test err: 0.289871
## step: 700 train err: 0.032755 test err: 0.219035
## step: 0 train err: 0.763628 test err: 1.973412
## step: 100 train err: 0.041158 test err: 0.303015
## step: 200 train err: 0.038779 test err: 0.340862
## step: 300 train err: 0.035553 test err: 0.350543
## step: 400 train err: 0.033465 test err: 0.325148
## [1] "Model saved to ./model.ckpt"

``` r
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b6eb021

Please sign in to comment.