简体   繁体   中英

Can I feed a deep learning model with a numpy array of two-dimensional arrays?

I'm trying to apply deep learning on a certain type of data (I clarify that I'm completely new in the deep learning field).

My problem is that my data have this shape:

array([[list([0.2711662547481215, 0.8077617617949696]),
    list([0.2740944703391002, 0.8077307987902311]),
    list([0.27975517109824677, 0.8105948767285374]), ...,
    list([0.2682358275139472, 0.7961672223420195]),
    list([0.26828227202105487, 0.7963242490089074]),
    list([0.26825241483791423, 0.7962280425298988])],
   [list([0.19316381088239035, 0.5278528814946285]),
    list([0.18176279020905559, 0.5279490879736373]),
    list([0.17593953367503223, 0.5337004661038035]), ...,
    list([0.1874776762264944, 0.3347222452601722]),
    list([0.19028093397692153, 0.3317276803733254]),
    list([0.19318371567115078, 0.3260205351070712])],
   [list([0.29431331243330516, 0.5278639397106065]),
    list([0.2971652263340356, 0.5279137016825076]),
    list([0.3028425144171491, 0.5250098141666806]), ...,
    list([0.3087608716085834, 0.5393921298676885]),
    list([0.3086790408103461, 0.5392881826374951]),
    list([0.3087752472893548, 0.5393158281774402])],
   ...,
   [list([0.1701350761081715, 0.45287817716367823]),
    list([0.17019589629605056, 0.4500627553756753]),
    list([0.17029763188304833, 0.450014099225372]), ...,
    list([0.1700244939483913, 0.4067189720282427]),
    list([0.16734729986011357, 0.4067134429202537]),
    list([0.17002670559158692, 0.40671233709865584])],
   [list([0.23650759422982293, 0.9316270506079255]),
    list([0.23931638108823905, 0.9231288116288199]),
    list([0.23652307573219214, 0.9231332349152112]), ...,
    list([0.25673417707521246, 0.908707792171889]),
    list([0.23367116183146175, 0.8682977535234241]),
    list([0.239428069069617, 0.8567585051503641])],
   [list([0.0, 0.0]), list([0.0, 0.0]), list([0.0, 0.0]), ...,
    list([0.3085728819369571, 0.8452137276693151]),
    list([0.3085463422186099, 0.851007127020198]),
    list([0.3085363898242297, 0.8481662713354454])]], dtype=object)

This because each element represent a point, and each point has two coordinates x and y.

This is how I have built my model:

model = models.Sequential()

model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1024, activation='relu'))
model.add(layers.Dense(1024, activation='relu'))
model.add(layers.Dense(1024, activation='relu'))
model.add(layers.Dense(1, activation='softmax'))

model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy', metrics=['acc'])

But once I start the training phase

model.fit(x_train, y_train, epochs=10, batch_size=128)

I get this error:

Epoch 1/10
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-27-3b52916d7a95> in <module>
----> 1 model.fit(x_train, y_train, epochs=10, batch_size=128)

~/.local/lib/python3.6/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
   1237                                         steps_per_epoch=steps_per_epoch,
   1238                                         validation_steps=validation_steps,
-> 1239                                         validation_freq=validation_freq)
   1240 
   1241     def evaluate(self,

~/.local/lib/python3.6/site-packages/keras/engine/training_arrays.py in fit_loop(model, fit_function, fit_inputs, out_labels, batch_size, epochs, verbose, callbacks, val_function, val_inputs, shuffle, initial_epoch, steps_per_epoch, validation_steps, validation_freq)
    194                     ins_batch[i] = ins_batch[i].toarray()
    195 
--> 196                 outs = fit_function(ins_batch)
    197                 outs = to_list(outs)
    198                 for l, o in zip(out_labels, outs):

~/.local/lib/python3.6/site-packages/tensorflow/python/keras/backend.py in __call__(self, inputs)
   3275         tensor_type = dtypes_module.as_dtype(tensor.dtype)
   3276         array_vals.append(np.asarray(value,
-> 3277                                      dtype=tensor_type.as_numpy_dtype))
   3278 
   3279     if self.feed_dict:

~/.local/lib/python3.6/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
     83 
     84     """
---> 85     return array(a, dtype, copy=False, order=order)
     86 
     87 

ValueError: setting an array element with a sequence.

I guess the error is due to my data's format but I don't know how to solve it.

The error can be seen in the error message, you need to fit an array (2d), you are giving it an array of lists, try x_train = np.array(x_train) and y_train = np.array(y_train) before calling fit. This should resolve this problem, but i am certain you'll get more errors

Also try flattening your array, currently your array has lists of lists, if the each list is of different size then it can not be converted to an array, in that case, flatten first, and then convert.

Also i am assuming that each example x is an array of size 2, which represents a point (x,y coordinate pairs) and not an array of coordinates

Try Reshaping your input with x_train = np.array(x_train)

Also, specify the Input shape in the first Dense Layer of your NN

Example:

Define the model 1 CNN

model = Sequential()

model.add(Conv1D(filters=25, kernel_size=3, activation='tanh',input_shape=(2751,28,1)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM