尝试在类中构建功能性api模型,但出现错误“ AttributeError:'Model'对象没有属性'shape'“

时间:2019-10-03 14:04:57

标签: python tensorflow keras

每个图像的输入大小为(174, 300, 1),总共有1200个输入图像。我正在尝试在一个类中构建模型,如下所示:

from keras import Model

from keras.engine.input_layer import Input

from keras.layers import Convolution2D,MaxPooling2D,Lambda

from keras.layers import Dense

from keras.layers.core import Activation,Flatten,Dropout

from keras.optimizers import Adam

from keras.layers.normalization import BatchNormalization

class MyModel(Model):

    def __init__(self):
        super(MyModel, self).__init__()
        self.conv1=Convolution2D(filters=8,kernel_size=8,padding='same')
        self.batch_norm1=BatchNormalization()
        self.activation1=Activation('relu')
        self.conv2=Convolution2D(filters=16,kernel_size=8,activation='relu',padding='same')
        self.batch_norm2=BatchNormalization()
        self.activation2=Activation('relu')
        self.MaxPooling2D=MaxPooling2D(pool_size =(2, 2))
        self.Dropout=Dropout(0.5)
        self.Flatten=Flatten()
        self.dense1=Dense(16,activation='relu')
        self.dense2=Dense(1,kernel_regularizer=regularizers.l2(0.4))


    def call(self,x):
        input_size=Input(shape=(1200,174,320,1))
        x = Lambda(lambda x:x[0,:,:])(input_size)
        x=self.conv1(x)
        x=self.batch_norm1(x)
        x=self.activation1(x)
        x=self.conv2(x)
        x=self.batch_norm2(x)
        x=self.activation2(x)
        x=self.MaxPooling2D(x)
        x=self.Dropout(x)
        x=self.Flatten(x)
        x=self.dense1(x)
        x=self.dense2(x)
        x=Model(input_size,x) 
        return x


model=MyModel()

adam=Adam(learning_rate=1e-4)

model.compile(optimizer=adam,loss="mse")

model.fit((x_train, y_train), epochs=50, batch_size=8,validation_split=0.1)

但是我得到了这个错误:

AttributeError: 'Model' object has no attribute 'shape'

0 个答案:

没有答案