简体   繁体   中英

How to change the dtype of an “object” to float32?

I'm actually trying to program a Keras Model. In my point of view, a keras Model needs a list of np.arrays as x (or a Numpy Array). In my case x is looking like this:

print(training.dtype)

object

print(training.shape)

(406,)

print(training[0].dtype)

float64

print(training[0].shape)

(5140, 5)

This is the size of my Train data (x). If I want to train the model I get this error:

return array(a, dtype, copy=False, order=order)

ValueError: setting an array element with a sequence.

That's why I think, I prepared the data wrong. If I want to convert them with .astype to float32, I get the same error.

Thanks for your help!

The issue is not changing the type. The issue is in the batch samples not being of the same size, so no np array could be created. You can solve this by using padding as mentioned in the comments. Have a look at keras pad_sequences What does Keras.io.preprocessing.sequence.pad_sequences do?

If the entries in train2 do not all have the same size, you will need to pad them. As this is something that needs to be done quite regularly, Keras offers a function for this: pad_sequences

Once they all are the same size, np.array(train2) will create one single numpy array that you can pass to model.fit() .

Depending on your model, the extra data you are adding this way may or may not be an issue. A common way to deal with this is Masking . Use this to generate a mask that will automatically be passed down the model so that certain values (the values you added via padding) are ignored. Note however, that not all layers support masking, so maybe this is not an option for you.

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