Grokking-Deep-Learning icon indicating copy to clipboard operation
Grokking-Deep-Learning copied to clipboard

Chapter 11: code formatting is broken

Open christophboesch opened this issue 5 years ago • 8 comments

Code formatting in Chapter 11, section "Predicting Movie Reviews" is broken. I was not able to reproduce the results in the book, p. 196, and assume that there is some error in my loop structure. Could could you update the formatting of the example?

christophboesch avatar Jul 10 '19 08:07 christophboesch

Could you provide the code for better clarification ? @christophboesch

naruto678 avatar Jul 10 '19 13:07 naruto678

Could you provide the code for better clarification ? @christophboesch

Sure! And thank you for the fast response!

import numpy as np
np.random.seed(1)

def sigmoid(x):
    return 1/(1 + np.exp(-x))

alpha, iterations = (0.01, 2)
hidden_size = 100

weights_0_1 = 0.2 * np.random.random((len(vocab),hidden_size)) - 0.1
weights_1_2 = 0.2 * np.random.random((hidden_size,1)) - 0.1

correct,total = (0,0)

for iter in range(iterations):
    for i in range(len(input_dataset)-1000):
        x,y = (input_dataset[i],target_dataset[i])
        layer_1 = sigmoid(np.sum(weights_0_1[x],axis=0))
        layer_2 = sigmoid(np.dot(layer_1,weights_1_2))
        
        layer_2_delta = layer_2 - y
        layer_1_delta = layer_2_delta.dot(weights_1_2.T)
        
        weights_0_1[x] -= layer_1_delta * alpha
        
        if(np.abs(layer_2_delta) < 0.5):
            correct += 1
        total += 1
        if(i%10 == 9):
            progress = str(i/float(len(input_dataset)))
            sys.stdout.write('\rIter:'+str(iter)\
                             +' Progress:'+progress[2:4]\
                             +'.'+progress[4:6]\
                             +'% Training Accuracy:'\
                             +str(correct/float(total)) + '%')
    print()
correct,total = (0,0)
for i in range(len(input_dataset)-1000,len(input_dataset)):
    
    x = input_dataset[i]
    y = target_dataset[i]
    
    layer_1 = sigmoid(np.sum(weights_0_1[x],axis=0))
    layer_2 = sigmoid(np.dot(layer_1,weights_1_2))
    
    if(np.abs(layer_2 - y) < 0.5):
        correct += 1
    total += 1
print("Test Accuracy: " + str(correct / float(total)))

christophboesch avatar Jul 10 '19 18:07 christophboesch

Found the reason ... you are not updating the weights_1_2 for the entire training and hence you are not able to train the model.

naruto678 avatar Jul 11 '19 08:07 naruto678

@naruto678: Thank you! That was exactly the problem. @iamtrask: Maybe it is still possible to fix the formatting in the code example to facilitate self diagnostics of errors ;)

christophboesch avatar Jul 11 '19 10:07 christophboesch

What do you mean when you say correction of formatting???

naruto678 avatar Jul 11 '19 10:07 naruto678

The coding example for chapter 11, section "Predicting Movie Reviews" looks like this: Screenshot 2019-07-11 at 12 48 50 If the second part of the code would also make use of indentation and syntax highlighting, it would be easier to compare the solutions.

christophboesch avatar Jul 11 '19 10:07 christophboesch

That is true .I will try to correct the formatting but it is up to @iamtrask to accept the pull request.

naruto678 avatar Jul 11 '19 11:07 naruto678

Actually, now that you mention it, I can create the pull request myself. With your help I have a working version of the code now ;) Thanks once again.

christophboesch avatar Jul 11 '19 14:07 christophboesch