解决keras.backend.reshape函数结果全是None(None,None,None,None)问题,将三维新增一维None方法

导入函数

#导入相关函数
from keras.engine.input_layer import Input
from keras.backend import reshape
from tensorflow.keras import backend as K

实现过程

def main():
    
    img_inputs = keras.Input(shape=(32, 32, 16),dtype='float32')
    x=DepthwiseConv2D(padding='same',kernel_size=3,use_bias=True)(img_inputs)
    
    #用函数keras.backend中的方法shape获取尺寸
    #b, h, w, c=(None,32,32,16),tensor类型
    b, h, w, c=K.shape(x)
    
    
    #用一般的shape获取尺寸
    #samples, rows, cols, dim = samples,32,32,16, int类型
    samples, rows, cols, dim=x.shape
    
    
    x=reshape(x,(b,h,cols,dim))
    print(x.shape) #结果为(None, None, 32, 16)
    x=reshape(x,(b,rows,cols,dim))
    print(x.shape)#结果为(None, 32, 32, 16)
    x=reshape(x,(b,rows*cols,dim))
    print(x.shape)#结果为(None, 1024, 16) 
    x=reshape(x,(b,rows,cols,dim))
    x=tf.transpose(x, perm=[0,3,1,2])
    print(x.shape)#结果为(None, 16, 32, 32)
    x=reshape(x,(b,8,2,rows,cols))
    print(x.shape)#结果为(None, 8, 2, 32, 32)

你可能感兴趣的:(python,keras,深度学习,人工智能)