Skip to content

Commit

Permalink
Fix python 3 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
rushter committed Oct 31, 2015
1 parent 5590dc7 commit 66edb6a
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions examples/addition_rnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from keras.layers.core import Activation, TimeDistributedDense, RepeatVector
from keras.layers import recurrent
import numpy as np
from six.moves import range

"""
An implementation of sequence to sequence learning for performing addition
Expand Down Expand Up @@ -89,7 +90,7 @@ class colors:
seen = set()
print('Generating data...')
while len(questions) < TRAINING_SIZE:
f = lambda: int(''.join(np.random.choice(list('0123456789')) for i in xrange(np.random.randint(1, DIGITS + 1))))
f = lambda: int(''.join(np.random.choice(list('0123456789')) for i in range(np.random.randint(1, DIGITS + 1))))
a, b = f(), f()
# Skip any addition questions we've already seen
# Also skip any such that X+Y == Y+X (hence the sorting)
Expand Down Expand Up @@ -139,7 +140,7 @@ class colors:
# For the decoder's input, we repeat the encoded input for each time step
model.add(RepeatVector(DIGITS + 1))
# The decoder RNN could be multiple layers stacked or a single layer
for _ in xrange(LAYERS):
for _ in range(LAYERS):
model.add(RNN(HIDDEN_SIZE, return_sequences=True))

# For each of step of the output sequence, decide which character should be chosen
Expand All @@ -156,7 +157,7 @@ class colors:
model.fit(X_train, y_train, batch_size=BATCH_SIZE, nb_epoch=1, validation_data=(X_val, y_val), show_accuracy=True)
###
# Select 10 samples from the validation set at random so we can visualize errors
for i in xrange(10):
for i in range(10):
ind = np.random.randint(0, len(X_val))
rowX, rowy = X_val[np.array([ind])], y_val[np.array([ind])]
preds = model.predict_classes(rowX, verbose=0)
Expand Down

0 comments on commit 66edb6a

Please sign in to comment.