Python 2.7.12 (x64)
Library problems used near: PyBrain, SciPy


Context of the problem: it is necessary to obtain from PyBrain a result passed via a variable of type list, the contents of which change when the function is accessed, the result is a two-dimensional ndarray.

Task: it is necessary to build a list of several two-dimensional ndarray of different sizes.

The problem itself: not a two-dimensional ndarray is written into the specified variable of the list type, but a list of one-dimensional ndarray.

Partially successful solution attempts: trying to get rid of the external list means scipy.array () and scipy.vstack () gives a completely satisfactory result, but only on a separate variable - when trying to assign an element of that list the value of this variable it falls to list from one-dimensional ndarray.
Who can tell how to get around this problem?


I unfortunately still can not repeat this problem in "greenhouse" conditions, but attempts to find a problem look like this:

out=[] self.layer[i]._forwardImplementation(self.inputs[i],out) print 'out =\n',out,type(out) out=vstack(out) print 'out =\n',out,type(out) self.outputs[i][:]=array(out.T) print 'self.outputs[i] =\n',self.outputs[i],type(self.outputs[i]) 

And the conclusion of this:

 out = [array([ 0., 0., 1., 1.]), array([ 0., 1., 0., 1.])] <type 'list'> out = [[ 0. 0. 1. 1.] [ 0. 1. 0. 1.]] <type 'numpy.ndarray'> self.outputs[i] = [array([ 0., 0.]), array([ 0., 1.]), array([ 1., 0.]), array([ 1., 1.])] <ty pe 'list'> 
  • Please give examples of your input and expected data - MaxU
  • For the first time I use stackoverflow.com, everything is unclear as the problem itself. Usually, there was enough documentation, the Internet and a little shamanism, but here a dead end ... - Vasa

2 answers 2

I did not quite understand what the problem was - here is a list of two two-dimensional ndarray various shapes:

 In [116]: a = np.arange(12).reshape(4,3) In [117]: b = np.arange(8).reshape(2,4) In [118]: a Out[118]: array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) In [119]: b Out[119]: array([[0, 1, 2, 3], [4, 5, 6, 7]]) In [120]: lst = [a, b] In [121]: lst Out[121]: [array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]), array([[0, 1, 2, 3], [4, 5, 6, 7]])] 
  • I wrote that I can not repeat the problem in "greenhouse" conditions, but there is a problem, it is clearly visible in the specified part of the code (the last output is out -> the output of self.outputs [i]). In interactive mode, everything works fine, how much I have not experimented, but when you run the script, this problem gets out at each iteration of the loop. What is the difference - not yet understood. Transposing an array does not affect the result, and another array () is added in case of “what if it helps” - without them, it was the same. - Vasa
  • @Vasa, try to give a reproducible example - MaxU
  • I've been trying for 3 days already, but I can’t find a possible difference - it’s useful to ask knowledgeable people, but I’m looking at the situation one by one, like me: “is it really that way?” - I can not understand why, I do not know where to dig. I can share this under-code, but there it’s necessary to change part of PyBrain, which I can hardly reproduce the first time, and the trouble with running is not a little ... - Vasa

Thanks to everyone who looked in, the problem was unexpectedly found in the source code:

 self.outputs[i][:]=array(out.T) 

But the error ---- ^ - assigning a value not to the list element, but to its contents, since Initially it was a list of empty lists.
So the following code would be suitable for reproducing the problem:

 >>> from scipy import array >>> lst=[[], [], []] >>> a=array([[1, 2, 3], [4, 5, 6]]) >>> a array([[1, 2, 3], [4, 5, 6]]) >>> lst [[], [], []] >>> lst[1][:]=a >>> lst [[], [array([1, 2, 3]),array([4, 5, 6])], []]