简体   繁体   中英

Can I convert a Numpy ndarray to a Tensor?

I have an object, that has a length of 40,000. Each of those 40,000 arrays has a varying length. All the values in the arrays are integers (0-25).

My XTrain data looks something like this:

[array([13,  8,  3,  7, 12, 16, 11,  1,  9, 17,  2, 18,  3,  5, 12, 19,  9,
       10, 20, 14,  1, 15, 12, 19,  2,  2,  6, 20, 14, 19,  2,  7, 12,  2,
        2,  1, 10,  8,  5, 10, 11,  2, 12, 11,  5,  7, 18, 12, 16, 20,  2,
       10, 11,  7,  1,  7,  5,  5,  1,  4,  9, 10,  9, 13, 11, 20,  7, 10,
       15, 15, 12, 13, 16, 20, 16,  8, 14, 13,  8, 19, 11, 12,  8, 12, 16,
       16, 11, 13, 15, 19,  7,  6, 14,  8,  4, 11, 12, 14, 12, 19,  2,  3,
        2,  7, 14, 18,  5,  2,  8, 19, 19, 20,  4, 17, 20,  8, 12,  3, 17,
        1, 12, 10,  6,  4, 19, 10, 12,  9,  6, 11,  7,  7,  4, 12, 13,  8,
        7,  6, 11, 16, 10, 15, 19, 15,  8, 16, 15, 14, 17,  8,  2, 12, 24])
 array([13,  2, 20, 11, 12, 14,  8,  8, 17, 16, 20,  1,  3,  1,  7,  2, 14,
       11,  2, 20,  1,  4, 10, 11,  7, 16,  3,  1,  2,  6,  8,  6, 20,  1,
       17, 20, 11, 16,  1, 15,  1, 12, 10, 17,  3,  9, 11, 20,  1, 13, 10,
        7, 12, 17, 10, 16,  8,  6,  4,  1, 11, 15,  3, 10, 16,  4,  1,  7,
        2, 10, 14,  1,  7, 11, 11, 17,  8, 11,  1,  1,  1,  6, 15,  8, 14,
       15, 11,  1,  6, 11, 12, 17, 14, 20,  4,  6,  7, 14,  1,  6, 10, 12,
        9, 20, 11,  9,  8, 10, 16, 11, 11,  8,  6,  5, 15,  4, 16, 10,  3,
        1,  1, 11, 10,  5,  2,  8,  7, 12, 13, 16, 10,  1, 10, 13,  1,  8,
       20, 11,  7,  1,  2,  8,  9,  3, 20, 17, 20, 10,  4, 15, 20,  7, 12,
       11, 11,  1, 20,  8,  9, 19, 11,  7, 16, 17, 20,  4, 10,  1,  7, 16,
       17,  2,  2, 10,  1,  1, 16,  2, 10, 15,  1,  4,  9, 13, 20, 11, 13,
        1,  8, 14, 17,  1,  8,  3,  7, 12,  8,  7, 11, 20, 20, 11,  8,  2,
        3,  8, 16,  4, 19, 16,  1,  1, 20, 11,  1,  1,  5, 11,  2,  1,  4])
    ...
]

Upon trying to convert this data to a Tensor by using:

x_train = tf.convert_to_tensor(
    XTrain
)

I am given the following error: ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).

EDIT 1: Someone suggested using this instead:

x_train = tf.convert_to_tensor(
    XTrain, np.float32
)

I then instead get TypeError: only size-1 arrays can be converted to Python scalars & ValueError: setting an array element with a sequence.

My XTrain data is created like this:

XTrain = np.empty([len(AArecords)], dtype=object);
XTest = np.empty([len(AArecords)], dtype=object);
i = 0;
while (i < 40000):
  XTrain[i] = np.array(aa2int(AArecords[i]))
  i += 1
XTrain = np.transpose(XTrain)

AArecords looks like this:

['MGNEKSLAHTRWNCKYHIVFAPKYRRQVFYREKRRAI...'
 'MRVLKFGGTSVANAERFLRVADILESNA...'
 'MVKVYAPASSANMSVGFD...*'
 ...]

You could convert non-rectangular Python sequence to RaggedTensor as:

x_train = tf.ragged.constant(XTrain)

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