Convert Tensor<Float> to Tensor<Bool>?

758 views
Skip to first unread message

Xander Dunn

unread,
Jan 29, 2021, 1:58:06 PM1/29/21
to Swift for TensorFlow

I have a Tensor<Float> that is all 0s and 1s. It’s a mask that was created previously. How can I convert this to a Tensor<Bool> to use with gathering(where:alongAxis:)? These ideas came to mind:

let myFloatMask: Tensor<Float> = Tensor<Float>(arrayLiteral: [1.0, 0.0, 0.0, 1.0])

// boolean test like https://stackoverflow.com/questions/53562417/how-to-convert-a-pytorch-tensor-of-ints-to-a-tensor-of-booleans
let booleanMask = myFloatMask == 1.0

// .where like https://www.tensorflow.org/api_docs/python/tf/where
let booleanMask = myFloatMask.where( { $0 > 0} )

// cast like https://www.tensorflow.org/api_docs/python/tf/cast
let booleanMask = Tensor<Bool>(myFloatMask)
let booleanMask = Tensor<Bool>(copying: myFloatMask, to: myDevice)

I don’t believe any of these are actually supported. Is there a recommended approach to such a conversion from Float to Bool?

Thanks,
Xander

​

Parker Schuh

unread,
Jan 29, 2021, 2:04:27 PM1/29/21
to Xander Dunn, Swift for TensorFlow
myFloatMask .> 0

--
To unsubscribe from this group and stop receiving emails from it, send an email to swift+un...@tensorflow.org.

Xander Dunn

unread,
Jan 30, 2021, 3:36:39 PM1/30/21
to Parker Schuh, Swift for TensorFlow
Thanks Parker! I've used the element-wise operators such as .<, .>, etc, but I thought they were defined only on rhs: Tensor<Scalar>, lhs: Tensor<Scalar>. I didn't realize they were defined for rhs: Tensor<Scalar>, lhs: Scalar.

Reply all
Reply to author
Forward
0 new messages