0
$\begingroup$

It is my first GRU model so pardon the stupidity. I am trying to learn by training a simple GRU network on variable length sequences. The sequences are numpy arrays of tensors. The length of numpy array varies from sample to sample. The model generator and fit code is below:

def declare_model(emb_size, gru_size, num_classes):
    inputs = keras.Input(shape=(None, emb_size))
    gru_out = keras.layers.Bidirectional(keras.layers.GRU(gru_size, return_sequences=False))(inputs)
    gru_out = keras.layers.Flatten()(gru_out)
    predictions = keras.layers.Dense(num_classes, activation='sigmoid')(gru_out)
    model = keras.Model(inputs=inputs, outputs=predictions)
    model.compile(optimizer=keras.optimizers.Adam(), loss='binary_crossentropy', metrics=['accuracy'])
    return model
m = declare_model(emb_size=200, gru_size=20, num_classes=2)
m.fit(dafr["Data"], dafr["Label"], epochs=100, batch_size=32, validation_split=0.2)

The type of an element of 'dafr["Data"]' is "numpy.ndarray" type of each element of this element is "torch.Tensor" shape of each tensor is "200 {torch. Size([200])}" and dtype of tensor is float. Type of element of 'dafr["Label"]' is 'numpy.int64'. While fitting I am getting error "ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).". Why is this error occurring and how can I resolve it?

$\endgroup$

1 Answer 1

0
$\begingroup$

The error is happening because Keras models are designed to work with numpy arrays, not with PyTorch tensors. To resolve this issue, you need to convert the PyTorch tensors to numpy arrays. You can do this by calling the numpy() method on each tensor. For example, you can modify the code this way:

import numpy as np

dafr["Data"] = np.array([x.numpy() for x in dafr["Data"]])
dafr["Label"] = np.array(dafr["Label"])

After converting the data, you should be able to fit the model without encountering the error.

$\endgroup$
1
  • $\begingroup$ Thanks for answer, but I have tried it. The error says that all numpy arrays (inside the list) should be of same size. $\endgroup$ Feb 14 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.