It sounds like easy not i dont know how to do.
i have numpy 2d array of
X = (1783,30)
and i want to split them in batches of 64. I write the code like this.
batches = abs(len(X) / BATCH_SIZE ) + 1 // It gives 28
I am trying to do prediction of results batchwise. So i fill the batch with zeros and i overwrite them with predicted results.
predicted = []
for b in xrange(batches):
data4D = np.zeros([BATCH_SIZE,1,96,96]) #create 4D array, first value is batch_size, last number of inputs
data4DL = np.zeros([BATCH_SIZE,1,1,1]) # need to create 4D array as output, first value is batch_size, last number of outputs
data4D[0:BATCH_SIZE,:] = X[b*BATCH_SIZE:b*BATCH_SIZE+BATCH_SIZE,:] # fill value of input xtrain
#predict
#print [(k, v[0].data.shape) for k, v in net.params.items()]
net.set_input_arrays(data4D.astype(np.float32),data4DL.astype(np.float32))
pred = net.forward()
print 'batch ', b
predicted.append(pred['ip1'])
print 'Total in Batches ', data4D.shape, batches
print 'Final Output: ', predicted
But in the last batch number 28, there are only 55 elements instead of 64 (total elements 1783), and it gives
ValueError: could not broadcast input array from shape (55,1,96,96) into shape (64,1,96,96)
What is the fix for this?
PS: the network predictione requires exact batch size is 64 to predict.
There are two ways to split the array one is row-wise and the other is column-wise. By default, the array is split in row-wise (axis=0) . You can also use numpy. split() function to split an array into multiple sub-arrays horizontally (column-wise).
Use the hsplit() method to split the 2-D array into three 2-D arrays along rows. Note: Similar alternates to vstack() and dstack() are available as vsplit() and dsplit() .
array_split() method in Python is used to split an array into multiple sub-arrays of equal size. In Python, an array is a data structure that is used to store multiple items of the same type together.
Dividing a NumPy array by a constant is as easy as dividing two numbers. To divide each and every element of an array by a constant, use division arithmetic operator / . Pass array and constant as operands to the division operator as shown below. where a is input array and c is a constant.
I don't really understand your question either, especially what X looks like. If you want to create sub-groups of equal size of your array, try this:
def group_list(l, group_size):
"""
:param l: list
:param group_size: size of each group
:return: Yields successive group-sized lists from l.
"""
for i in xrange(0, len(l), group_size):
yield l[i:i+group_size]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With