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

#1

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?

0 Likes

(Stephen Johnson) #2

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

 let batchsize: Int32 = 10
0 Likes

#3

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

0 Likes

(Stefano Giomo) #4

let shape = TensorShape(…) // init properly

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

0 Likes

#5

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)
                               ^~~~~~~~~~~~
0 Likes

(Stefano Giomo) #6

shape.dimensions

0 Likes

(Stephen Johnson) #7

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

(Stephen Johnson) #8

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

0 Likes

#9

Thanks @stephenjohnson it works now!

1 Like