Tensorflow (python): “ValueError: setting an array element with a sequence” in train_step.run(…)

后端 未结 2 957
情歌与酒
情歌与酒 2020-12-10 12:09

I\'m trying to implement a simple logistic regression model trained with my own set of images, but I am getting this error when I try to train the model:

Trac         


        
相关标签:
2条回答
  • 2020-12-10 12:20

    The feed_dict argument to Operation.run() (also Session.run() and Tensor.eval()) accepts a dictionary mapping Tensor objects (usually tf.placeholder() tensors) to a numpy array (or objects that can be trivially converted to a numpy array).

    In your case, you are passing batch_xs, which is a list of numpy arrays, and TensorFlow does not know how to convert this to a numpy array. Let's say that batch_xs is defined as follows:

    batch_xs = [np.random.rand(100, 100),
                np.random.rand(100, 100),
                ...,                       # 29 rows omitted.
                np.random.rand(100, 100)]  # len(batch_xs) == 32.
    

    We can convert batch_xs into a 32 x 100 x 100 array using the following:

    # Convert each 100 x 100 element to 1 x 100 x 100, then vstack to concatenate.
    batch_xs = np.vstack([np.expand_dims(x, 0) for x in batch_xs])
    print batch_xs.shape
    # ==> (32, 100, 100) 
    

    Note that, if batch_ys is a list of floats, this will be transparently converted into a 1-D numpy array by TensorFlow, so you should not need to convert this argument.

    EDIT: mdaoust makes a valid point in the comments: If you pass a list of arrays into np.array (and therefore as the value in a feed_dict), it will automatically be vstacked, so there should be no need to convert your input as I suggested. Instead, it sounds like you have a mismatch between the shapes of your list elements. Try adding the following:

    assert all(x.shape == (100, 100) for x in batch_xs)
    

    ...before the call to train_step.run(), and this should reveal whether you have a mismatch.

    0 讨论(0)
  • 2020-12-10 12:32

    This particular error is coming out of numpy. Calling np.array on a sequence with a inconsistant dimensions can throw it.

    >>> np.array([1,2,3,[4,5,6]])
    
    ValueError: setting an array element with a sequence.
    

    It looks like it's failing at the point where tf ensures that all the elements of the feed_dict are numpy.arrays.

    Check your feed_dict.

    0 讨论(0)
提交回复
热议问题