时间:2020-10-03 10:16:54 | 栏目:Python代码 | 点击:次
获取单输入尺寸,该层只被使用了一次。
import keras from keras.layers import Input, LSTM, Dense, Conv2D from keras.models import Model a = Input(shape=(32, 32, 3)) b = Input(shape=(64, 64, 3)) conv = Conv2D(16, (3, 3), padding='same') conved_a = conv(a) # 到目前为止只有一个输入,以下可行: assert conv.input_shape == (None, 32, 32, 3)
如果该层被使用了两次
import keras from keras.layers import Input, LSTM, Dense, Conv2D from keras.models import Model a = Input(shape=(32, 32, 3)) b = Input(shape=(64, 64, 3)) conv = Conv2D(16, (3, 3), padding='same') conved_a = conv(a) # 到目前为止只有一个输入,以下可行: assert conv.input_shape == (None, 32, 32, 3) conved_b = conv(b) # 现在 `.input_shape` 属性不可行,但是这样可以: assert conv.get_input_shape_at(0) == (None, 32, 32, 3) assert conv.get_input_shape_at(1) == (None, 64, 64, 3)
如果是输出,只需要改成output就好:
import keras from keras.layers import Input, LSTM, Dense, Conv2D from keras.models import Model a = Input(shape=(32, 32, 3)) b = Input(shape=(64, 64, 3)) conv = Conv2D(16, (3, 3), padding='same') conved_a = conv(a) # 到目前为止只有一个输入,以下可行: assert conv.input_shape == (None, 32, 32, 3) conved_b = conv(b) # 就改了output,当然尺寸我也改了 assert conv.get_output_shape_at(0) == (None, 32, 32, 16) assert conv.get_output_shape_at(1) == (None, 64, 64, 16)
补充知识:keras中获取shape的正确方法
在keras的网络中,如果用layer_name.shape的方式获取shape信息将会返还tensorflow.python.framework.tensor_shape.TensorShape其中包含的是tensorflow.python.framework.tensor_shape.Dimension
正确的方式是使用
import keras.backend as K
K.int_shape(laye_name)