Search code examples
pythonarraysnumpyneural-networkartificial-intelligence

ValueError: setting an array element with a sequence. in a Neural Net


i was following a tutorial about neural networks but trying to do it my way, but in the line:

_Y[i0,i1]=ForwardPass(Neural_net, np.array([[x0,x1]]))

the console say:

ValueError: setting an array element with a sequence.

The variables are defined in this block:

    res = 50

    _x0=np.linspace(-1.5,1.5,res)
    _x1=np.linspace(-1.5,1.5,res)

    _Y = np.zeros((res,res))

    for i0,x0 in enumerate(_x0):
      for i1,x1 in enumerate(_x1):
        _Y[i0,i1]=ForwardPass(Neural_net, np.array([[x0,x1]]))

here is the code of the ForwardPass Function:

def ForwardPass(neu_net, ent):

  outs=[ent]

  for l, layer in enumerate(neu_net):

    Sp=outs[-1]@neu_net[l].w + neu_net[l].b
    a=neu_net[l].act_f[0](Sp)

    outs.append(a)
  return outs

Solution

  • If you try to add a print statement inside the iteration to check for the values of _Y[i0,i1]:

    res = 50
    
    _x0=np.linspace(-1.5,1.5,res)
    _x1=np.linspace(-1.5,1.5,res)
    
    _Y = np.zeros((res,res))
    
    for i0,x0 in enumerate(_x0):
        for i1,x1 in enumerate(_x1):
            print(_Y[i0,i1])
    >>0.0
      0.0
      0.0
      ..
    

    _Y[i0,i1] is a floating value while the result coming from ForwardPass(Neural_net, np.array([[x0,x1]])) is a list.

    Hence as the error ValueError: setting an array element with a sequence. says its because you're trying to assign a list(array) to a float value(array element)