TensorFlow.js中字符串张量调用gather方法报错的问题咨询
gather的解决方案 Hey there! Great question—this is a super common gotcha when working with string tensors in TensorFlow.js. Let me break down what's happening and how to fix it.
Why the error happens
TensorFlow.js's gather() API is explicitly designed to work with numeric tensors (like float, int, or boolean types). String tensors aren't supported by this operation out of the box, which is why you're seeing that error message: Argument 'x' passed to 'gather' must be numeric tensor, but got string tensor.
How to work around it
Since string tensors can be easily converted to regular JavaScript arrays, we can manually replicate the gather behavior by:
- Extracting the raw string data from the tensor
- Extracting the index values from the indices tensor
- Mapping the indices to the corresponding string elements
- Converting the result back to a string tensor
Here's a clean implementation using async/await (to avoid nested .then() calls):
async function gatherStringTensor(stringTensor, indicesTensor) { // Get raw data from both tensors const stringArray = await stringTensor.data(); const indicesArray = await indicesTensor.data(); // Use indices to pick out the desired elements const gatheredElements = indicesArray.map(index => stringArray[index]); // Convert the result back to a TensorFlow.js string tensor return tf.tensor1d(gatheredElements); } // Example usage const x = tf.tensor1d(["a1", "a2", "a3", "a4"]); const indices = tf.tensor1d([1, 3, 3], 'int32'); gatherStringTensor(x, indices).then(result => { result.print(); // Will output Tensor ["a2", "a4", "a4"] });
If you prefer using .then() instead of async/await, here's that version:
const x = tf.tensor1d(["a1", "a2", "a3", "a4"]); const indices = tf.tensor1d([1, 3, 3], 'int32'); x.data().then(strArray => { indices.data().then(idxArray => { const gathered = idxArray.map(i => strArray[i]); tf.tensor1d(gathered).print(); // Outputs Tensor ["a2", "a4", "a4"] }); });
A quick note
String tensors in TensorFlow.js are mostly intended for auxiliary tasks (like labeling data) rather than heavy computation, which is why many core operations (including gather) don't support them. This manual workaround is the most straightforward way to achieve your desired behavior right now.
内容的提问来源于stack exchange,提问作者Maria Shuvelle

