Indexing errors in implementing Lesson 6

I’m trying to implement the approach taught by @jeremy in Lesson 6 where we hand create the model to predict the 4th charactedm given 3 characters. I’m doing this on my own data, where I’m predicting the 3rd item, given 2 items and I’m running into a weird error. Here’s my code:

x1_dat = []
x2_dat = []
y_dat = []
cs = 3
for session in Sessions_group:
session = session.split(’,’)
for i in xrange(0,len(session)-cs+1):
x1_dat.append(session[i])
x2_dat.append(session[i+1])
y_dat.append(session[i+2])

x1_dat = np.stack(x1_dat)
x2_dat = np.stack(x2_dat)
y_dat = np.stack(y_dat)

n_fac = 100
def embedding_input(name, n_in, n_out):
inp = Input(shape=(1,), dtype=‘int64’, name=name)
emb = Embedding(n_in, n_out, input_length=1)(inp)
return inp, Flatten()(emb)

vocab_size = len(uniqueArtifacts)
x1_in, x1 = embedding_input(‘x1’, vocab_size, n_fac)
x2_in, x2 = embedding_input(‘x2’, vocab_size, n_fac)
n_hidden = 512
dense_in = Dense(n_hidden, activation=‘relu’)
x1_hidden = dense_in(x1)
dense_hidden = Dense(n_hidden, activation=‘tanh’)
x2_dense = dense_in(x2)
hidden_2 = dense_hidden(x1_hidden)
x2_hidden = merge([x2_dense, hidden_2])
dense_out = Dense(vocab_size, activation=‘softmax’)
x3_out = dense_out(x2_hidden)

model = Model([x1_in, x2_in], x3_out)
model.compile(loss=‘sparse_categorical_crossentropy’, optimizer=Adam())
model.fit([x1_dat, x2_dat], y_dat, batch_size=10, nb_epoch=1, verbose = 1)

Here’s the error I get:

IndexError Traceback (most recent call last)
in ()
----> 1 model.fit([x1_dat, x2_dat], y_dat, batch_size=10, nb_epoch=1, verbose = 1)

/home/prateek/anaconda2/lib/python2.7/site-packages/keras/engine/training.pyc in fit(self, x, y, batch_size, nb_epoch, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch)
1194 val_f=val_f, val_ins=val_ins, shuffle=shuffle,
1195 callback_metrics=callback_metrics,
-> 1196 initial_epoch=initial_epoch)
1197
1198 def evaluate(self, x, y, batch_size=32, verbose=1, sample_weight=None):

/home/prateek/anaconda2/lib/python2.7/site-packages/keras/engine/training.pyc in fitloop(self, f, ins, out_labels, batch_size, nb_epoch, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch)
889 batch_logs[‘size’] = len(batch_ids)
890 callbacks.on_batch_begin(batch_index, batch_logs)
–> 891 outs = f(ins_batch)
892 if not isinstance(outs, list):
893 outs = [outs]

/home/prateek/anaconda2/lib/python2.7/site-packages/keras/backend/theano_backend.pyc in call(self, inputs)
957 def call(self, inputs):
958 assert isinstance(inputs, (list, tuple))
–> 959 return self.function(*inputs)
960
961

/home/prateek/anaconda2/lib/python2.7/site-packages/theano/compile/function_module.pyc in call(self, *args, **kwargs)
896 node=self.fn.nodes[self.fn.position_of_error],
897 thunk=thunk,
–> 898 storage_map=getattr(self.fn, ‘storage_map’, None))
899 else:
900 # old-style linkers raise their own exceptions

/home/prateek/anaconda2/lib/python2.7/site-packages/theano/gof/link.pyc in raise_with_op(node, thunk, exc_info, storage_map)
323 # extra long error message in that case.
324 pass
–> 325 reraise(exc_type, exc_value, exc_trace)
326
327

/home/prateek/anaconda2/lib/python2.7/site-packages/theano/compile/function_module.pyc in call(self, *args, **kwargs)
882 try:
883 outputs =
–> 884 self.fn() if output_subset is None else
885 self.fn(output_subset=output_subset)
886 except Exception:

/home/prateek/anaconda2/lib/python2.7/site-packages/theano/gof/op.pyc in rval(p, i, o, n)
870 # default arguments are stored in the closure of rval
871 def rval(p=p, i=node_input_storage, o=node_output_storage, n=node):
–> 872 r = p(n, [x[0] for x in i], o)
873 for o in node.outputs:
874 compute_map[o][0] = True

/home/prateek/anaconda2/lib/python2.7/site-packages/theano/tensor/subtensor.pyc in perform(self, node, inputs, out_)
2240
2241 if self.set_instead_of_inc:
-> 2242 out[0][inputs[2:]] = inputs[1]
2243 elif config.cxx:
2244 inplace_increment(out[0], tuple(inputs[2:]), inputs[1])

IndexError: index 1864798 is out of bounds for axis 1 with size 2392
Apply node that caused the error: AdvancedIncSubtensor{inplace=False, set_instead_of_inc=True}(Alloc.0, TensorConstant{1}, ARange{dtype=‘int64’}.0, Elemwise{Cast{int32}}.0)
Toposort index: 69
Inputs types: [TensorType(float32, matrix), TensorType(int8, scalar), TensorType(int64, vector), TensorType(int32, vector)]
Inputs shapes: [(10, 2392), (), (10,), (10,)]
Inputs strides: [(9568, 4), (), (8,), (4,)]
Inputs values: [‘not shown’, array(1, dtype=int8), ‘not shown’, ‘not shown’]
Outputs clients: [[GpuFromHost(AdvancedIncSubtensor{inplace=False, set_instead_of_inc=True}.0)]]

Backtrace when the node is created(use Theano flag traceback.limit=N to make it longer):
File “/home/prateek/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py”, line 2717, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File “/home/prateek/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py”, line 2827, in run_ast_nodes
if self.run_code(code, result):
File “/home/prateek/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py”, line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File “”, line 1, in
model.compile(loss=‘sparse_categorical_crossentropy’, optimizer=Adam())
File “/home/prateek/anaconda2/lib/python2.7/site-packages/keras/engine/training.py”, line 667, in compile
sample_weight, mask)
File “/home/prateek/anaconda2/lib/python2.7/site-packages/keras/engine/training.py”, line 318, in weighted
score_array = fn(y_true, y_pred)
File “/home/prateek/anaconda2/lib/python2.7/site-packages/keras/objectives.py”, line 41, in sparse_categorical_crossentropy
return K.sparse_categorical_crossentropy(y_pred, y_true)
File “/home/prateek/anaconda2/lib/python2.7/site-packages/keras/backend/theano_backend.py”, line 1249, in sparse_categorical_crossentropy
target = T.extra_ops.to_one_hot(target, nb_class=output.shape[-1])

HINT: Use the Theano flag ‘exception_verbosity=high’ for a debugprint and storage map footprint of this apply node.