I’m trying to create mini batches in Udacity lesson. I will make character-wise RNN. But i missed something in this codes.
def get_batches(arr, batch_size, seq_length):
'''Create a generator that returns batches of size
batch_size x seq_length from arr.
Arguments
---------
arr: Array you want to make batches from
batch_size: Batch size, the number of sequences per batch
seq_length: Number of encoded chars in a sequence
'''
batch_size_total = batch_size * seq_length
# total number of batches we can make
n_batches = len(arr)//batch_size_total
# Keep only enough characters to make full batches
arr = arr[:n_batches * batch_size_total]
# Reshape into batch_size rows
arr = arr.reshape((batch_size, -1))
# iterate through the array, one sequence at a time
for n in range(0, arr.shape[1], seq_length):
# The features
x = arr[:, n:n+seq_length]
# The targets, shifted by one
y = np.zeros_like(x)
try:
y[:, :-1], y[:, -1] = x[:, 1:], arr[:, n+seq_length]
except IndexError:
y[:, :-1], y[:, -1] = x[:, 1:], arr[:, 0]
yield x, y

i missed all iteration throught the array line.
1- what is the try, expcept and yield ?
2- why we iterate loop with range(0, arr.shape[1], seq_length) ? what is the meaning?
3- x = arr[:, n:n+seq_length] what does this line do? first index : means we look all rows(batches) but for columns why we write n:n + seq_length what is it?
4- y[:, :-1], y[:, -1] = x[:, 1:], arr[:, n+seq_length] i saw this line shift the x array. but how does it do ?
5- y[:, :-1], y[:, -1] = x[:, 1:], arr[:, 0] and my last question what is difference from upper code. they said this line last y index turns start of first index of array? how does it work?
I know my question sometimes looks weird question. Can anyone explain this questions, i will be pleased because im beginner in this area. ps. sorry for my bad english. @ptrblck