获取单输入尺寸,该层只被使用了一次。
代码语言:javascript复制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)
如果该层被使用了两次
代码语言:javascript复制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就好:
代码语言:javascript复制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)
以上这篇keras 获取某层的输入/输出 tensor 尺寸操作就是小编分享给大家的全部内容了,希望能给大家一个参考。