I'm troubleshooting a number of different problems in an experimental network implemented in keras with a theano backend. I'm relatively new to keras and theano (I did some small experiments a few months ago and are returning now, having forgotten most of what I learned then). Lots of my problems would be more transparent if I could inspect the shapes of tensors but try as I might I never find a human readable representation of shapes.
For instance, I have some issues of the "Exception: Input 0 is incompatible with layer c_out: expected ndim=2, found ndim=3" type. While I know that this is a problem with the number of dimensions rather than the size of them, knowing the sizes of the dimensions would help me understand why there is a mismatch and what each dimension contains. Basically, I'm saying that my mental model of tensors and how computations flow through keras/theano is still pretty fuzzy and I need all the help I can get :)
If I print a TensorVariable.shape attribute I get the string "Shape.0", this seems to be an lvector of subtensors but I can't seem to access the values. There is supposed to be a function keras.backend.int_shape(x) which returns the shape as a tuple of integers and/or Nones but when I try it I get "AttributeError: module 'keras.backend' has no attribute 'int_shape'". The input to the c_out layer comes from a merge layer called merge_c, but when I call merge_c.output_shape or merge_c.get_output_shape_at(0) I get "AttributeError: 'TensorVariable' object has no attribute 'get_output_shape_at'".
Can anyone recommend a way to access a human readable representation of tensor shapes?
@Falkenjack Use keras.backend.shape(x)
to get the shape of a tensor or use model.summary()
to print the shapes of all of the layers in your model. Please ask usage questions on stackoverflow, slack, or the google group.
Cheers,
Ben
Looks like there are several methods? What is the difference?
class_weights_list = [1.0]
class_weights_numpy = np.array(class_weights_list)
class_weights_keras = K.variable(class_weights_numpy)
print('class_weights_keras.get_shape', class_weights_keras.get_shape)
print('K.shape(class_weights_keras)', K.shape(class_weights_keras))
('class_weights_keras.get_shape', <bound method Variable.get_shape of <tf.Variable 'Variable:0' shape=(1,) dtype=float32_ref>>)
('K.shape(class_weights_keras)', <tf.Tensor 'Shape:0' shape=(1,) dtype=int32>)
I found int_shape to be much more readable.
keras.backend.int_shape(network)
Note the two ways of accessing shape via shape
and int_shape
. The first one gives the rank and the second one gives the dimension of the tensor.
k=Input(shape=(29,))
import keras.backend as bk
print(bk.shape(k))
Tensor("Shape_7:0", shape=(2,), dtype=int32)
print(bk.int_shape(k))
(None, 29)
You can get your tensor shape as follow:
H, W, n_ch = tensor.shape.as_list()[1:]
Most helpful comment
@Falkenjack Use
keras.backend.shape(x)
to get the shape of a tensor or usemodel.summary()
to print the shapes of all of the layers in your model. Please ask usage questions on stackoverflow, slack, or the google group.Cheers,
Ben