1

From what I understand, this error is caused by not using numpy arrays in tensorflow. I convert all of my lists to numpy arrays, but it doesn't work.

Is the dimensionality wrong? How can I determine what's causing the error?

The shapes and dtypes of the relevant arrays are:

Shapes and dtypes:
traininginput:
(44137,)  (Should be (44137, 221)?)
object
trainingoutput:
(44137,)  (Should be (44137, 1)?)
object
validationinput:
(2454,)   (Should be (2454, 221)?)
object
validationoutput:
(2454,)   (Should be (2454, 1)?)
object

Here's the code:

from __future__ import absolute_import, division, print_function
import scipy.io as sio
import tflearn
import numpy
from itertools import chain

mat_contents = sio.loadmat('spdata05_036.mat')
print(type(mat_contents['spdata']))

spdata = mat_contents['spdata']

print(spdata.dtype)
data = spdata[0][0][0][0][0][0]
labels = spdata[0][0][0][0][0][1]
set = spdata[0][0][0][0][0][2]

print(data[0,:,0,:].ndim)
sliced = data[0,:,0,:].transpose()



print(len(sliced))
traininginput = [[]]
trainingoutput = [[]]
validationinput = [[]]
validationoutput = [[]]
print(set[0].size)
for indx, slice in enumerate(sliced):
    if (set[0][indx] == 0):
        traininginput.append(slice)
        trainingoutput.append(labels[0][indx])

    if (set[0][indx] == 1):
        validationinput.append(slice)
        validationoutput.append(labels[0][indx])


traininginput = numpy.asarray(traininginput)
trainingoutput = numpy.asarray(trainingoutput)
validationinput = numpy.asarray(validationinput)
validationoutput = numpy.asarray(validationoutput)

tflearn.init_graph()

net = tflearn.input_data(shape=[None, 221])
net = tflearn.fully_connected(net, 64)
net = tflearn.dropout(net, 0.5)
net = tflearn.fully_connected(net, 10, activation = 'softmax')
net = tflearn.regression(net, optimizer='adam', loss='categorical_crossentropy')

model = tflearn.DNN(net, tensorboard_verbose=1)
model.fit(traininginput, trainingoutput, n_epoch=100, validation_set=(validationinput, validationoutput), show_metric=True, run_id="blah")

Produces the following in the console:

daniel@beepboop:~/ML$ python tf
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcudnn.so locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcufft.so locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcurand.so locally
hdf5 not supported (please install/reinstall h5py)
<type 'numpy.ndarray'>
[('signals', 'O')]
2
48961
48961
I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:900] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
I tensorflow/core/common_runtime/gpu/gpu_init.cc:102] Found device 0 with properties: 
name: GeForce GTX 860M
major: 5 minor: 0 memoryClockRate (GHz) 1.0195
pciBusID 0000:01:00.0
Total memory: 2.00GiB
Free memory: 1.72GiB
I tensorflow/core/common_runtime/gpu/gpu_init.cc:126] DMA: 0 
I tensorflow/core/common_runtime/gpu/gpu_init.cc:136] 0:   Y 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:755] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 860M, pci bus id: 0000:01:00.0)
I tensorflow/core/common_runtime/gpu/gpu_device.cc:755] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 860M, pci bus id: 0000:01:00.0)
---------------------------------
Run id: blah
Log directory: /tmp/tflearn_logs/
---------------------------------
Training samples: 44137
Validation samples: 2454
--
--
Traceback (most recent call last):
  File "tf", line 52, in <module>
    model.fit(traininginput, trainingoutput, n_epoch=100, validation_set=(validationinput, validationoutput), show_metric=True, run_id="blah")
  File "/home/daniel/.local/lib/python2.7/site-packages/tflearn/models/dnn.py", line 188, in fit
    run_id=run_id)
  File "/home/daniel/.local/lib/python2.7/site-packages/tflearn/helpers/trainer.py", line 277, in fit
    show_metric)
  File "/home/daniel/.local/lib/python2.7/site-packages/tflearn/helpers/trainer.py", line 684, in _train
    feed_batch)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 340, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 548, in _run
    np_val = np.array(subfeed_val, dtype=subfeed_t.dtype.as_numpy_dtype)
ValueError: setting an array element with a sequence.
daniel@beepboop:~/ML$ 
Daniel Paczuski Bak
  • 3,720
  • 8
  • 32
  • 78

3 Answers3

0

What's the shape and dtype of fit inputs. I'm wondering specifically about arrays like traininginput, trainingoutput

I can produce your error message with

In [2]: np.array([[1,2,3],[1,2],[]],dtype=float)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-8d52a0bc26f2> in <module>()
----> 1 np.array([[1,2,3],[1,2],[]],dtype=float)

ValueError: setting an array element with a sequence.

In [3]: np.array([[1,2,3],[1,2],[]],dtype=object)
Out[3]: array([[1, 2, 3], [1, 2], []], dtype=object)

My input is a list of lists of varying length. It can create a object dtype array, but if told to make it floats if raises this sequence error.

When debugging numpy code, the first thing to check is the shape and dtype of the relevant arrays.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
0

It seems that you are feeding an array with inconsistent dimension. Thus the error that is raised. TensorFlow tries to convert you array to numpy array but fail, because inconsistent dimension arrays cannot be turned into numpy ndarray. So it may be an issue related to your data.

See: Tensorflow (python): "ValueError: setting an array element with a sequence" in train_step.run(...)

Also for debugging purpose, can you try to remove the validation set, to see if this is working.

Community
  • 1
  • 1
0

I had the same error "ValueError: setting an array element with a sequence." for iris problem set. So to solve it i changed my datatype. I converted data to numpy.ndarray and for labels I first made a one-hot array for all the labels and then made a list of this arrays.

and it solved my error