Tassou
Tassou

Reputation: 449

How to understand Caffe reshape parameters and reimplement in Keras?

I am not sure how to interpret the reshape parameters. here http://caffe.berkeleyvision.org/tutorial/layers/reshape.html it says that 0 means copy and -1 means infer. Is it the same when -1 is not the last parameter ? Can anyone help me understand it?

 layer {
  name: "Layer1"
  type: "Reshape"
  bottom: "Layer1"
  top: "Layer2"
  reshape_param {
  shape {
      dim: 0
      dim: 2
      dim: -1
      dim: 0
}
}

Also, if I want to implement the same layer in Keras, do I also use the Keras reshape layer like :

Layer2 = K.reshape(Layer1,(-1,input_dim)) 

Upvotes: 1

Views: 305

Answers (1)

Daniel Möller
Daniel Möller

Reputation: 86610

This means that considering you have an input of shape (a, b, c, d, e), your output will have shape:

(a, 2, b * c * e / 2, d)

The values a and d are copied from the previous layer. The value 2 is forced, and the value -1 calculates whatever it needs to keep the same number of elements as the input.

In Keras, since you're not changing the first dimension (the batch size), you only need a regular Reshape layer that will ignore the batch size:

Reshape((2,-1,youMustKnowThis)) 

In a Sequential model, just add this layer:

sequentialModel.add(Reshape((2,-1,youMustKnowThis))

In a functional API Model, pass the output of the previous layer:

newShaped = Reshape((2,-1,youMustKnowThis))(outputOfPreviousLayer)

Upvotes: 2

Related Questions