[Colab] error: cannot convert value of type 'Int' to expected element type '_TensorElementLiteral<Int32>'

I’m trying to get a reshape layer to work, I’m first creating a shape like this

let batch_size = 10
let shape:Tensor<Int32> = [batch_size, 7, 7, 256]

But then I get this error

error: <Cell 57>:2:28: error: cannot convert value of type 'Int' to expected element type '_TensorElementLiteral<Int32>'
let shape:Tensor<Int32> = [batch_size, 7, 7, 256]
                           ^~~~~~~~~~
                           _TensorElementLiteral<Int32>( )

What is the hack here to get this to work?

Try explicitly setting batchsize to Int32. On 64-bit platform Int would by Int64.

 let batchsize: Int32 = 10

Thanks @stephenjohnson but it seems that setting explicitly to Int32 gives same error as above :man_facepalming:

let shape = TensorShape(…) // init properly

https://www.tensorflow.org/swift/api_docs/Structs/TensorShape

The thing is I need later to pass this object to Reshape, e.g.

let batch_size = 10
let target_shape = TensorShape([batch_size, 7, 7, 256])
let r1 = Reshape<Float>(shape: target_shape)

But now it’s complaining

error: <Cell 79>:3:32: error: cannot convert value of type 'TensorShape' to expected argument type 'Tensor<Int32>'
let r1 = Reshape<Float>(shape: target_shape)
                               ^~~~~~~~~~~~

shape.dimensions

Use the init without the shape argument name. The init you are using needs to be passed a tensor not a TensorShape. So do it the following way. I tried it in Colab and it works.

let batch_size = 10
let target_shape = TensorShape([batch_size, 7, 7, 256])
let r1 = Reshape<Float>(target_shape)
2 Likes

As an aside, IMHO that’s a very misleading argument name for that init.

Thanks @stephenjohnson it works now!

1 Like