Wednesday, June 10, 2015

Numpy ValueError: Output array is read-only

I recently received this cryptic error when working with a numpy implementation of a neural network and was having trouble finding a ready-made solution for this problem.

This error can be seen in a similar fashion if you run commands such as:

import numpy as np
a = np.arange(6)
a.setflags(write=False)

a[2] = 42
ValueError:assignment destination is read-only

This is intended behavior.

I am currently working on a neural network which utilizes gradient descent and updates through the use of a client-server relationship. When processing a new mini-batch, the neural network started to throw the titular ValueError whenever updating the weights on the 2nd minibatch. 

I have seen many posts on Stack Overflow and other sites referring to this error being a result of the array being non-contiguous in memory itself. This seems to be a problem in underlying Python code as it is built on C and it could also couple with the underlying implementation of Numpy. 

If you receive an error such as this, then the easiest way to circumvent your non-contiguous memory is to simply just make it contiguous again:

my_discontiguous_array = np.array(my_discontiguous_array).copy()

This is performing an effective deep copy and re-loading the data into contiguous memory for your use. Hopefully the garbage collector cleans up the old, un-referenceable version of the array.

No comments:

Post a Comment