当前位置:首页 >> 脚本专栏

keras获得model中某一层的某一个Tensor的输出维度教程

获得某层tensor的输出维度

代码如下所示:

from keras import backend as K

@wraps(Conv2D)
def my_conv(*args,**kwargs):
  new_kwargs={'kernel_regularizer':l2(5e-6)}
  new_kwargs['padding']='valid' #'same'
  new_kwargs['strides']=(2,2) if kwargs.get('strides')==(2,2) else (1,1)
  # new_kwargs['kernel_initializer']=keras.initializers.glorot_uniform(seed=0)
  new_kwargs.update(kwargs)
  return Conv2D(*args,**new_kwargs)
def conv(x,**kwargs):
  x=my_conv(**kwargs)(x)
  x=BatchNormalization(axis=-1)(x)
  x=LeakyReLU(alpha=0.05)(x)
  return x

def inception_resnet_a(x_input):
  x_short=x_input
  s1=conv(x_input,filters=32,kernel_size=(1,1))

  s2=conv(x_input,filters=32,kernel_size=(1,1))
  s2=conv(s2,filters=32,kernel_size=(3,3),padding='same')

  s3=conv(x_input,filters=32,kernel_size=(1,1))
  s3=conv(s3,filters=48,kernel_size=(3,3),padding='same')
  s3=conv(s3,filters=64,kernel_size=(3,3),padding='same')
  x=keras.layers.concatenate([s1,s2,s3])
  x=conv(x,filters=384,kernel_size=(1,1))
  x=layers.Add()([x_short,x])
  x=LeakyReLU(alpha=0.05)(x)
  
  print(K.int_shape(x))

使用K.int_shape(tensor_name)即可得到对应tensor的维度

以上这篇keras获得model中某一层的某一个Tensor的输出维度教程就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。