from keras.datasets import boston_housing from keras.models import Sequential from keras.layers import Dense,BatchNormalization,Dropout,Reshape,Flatten from keras.layers.convolutional import Conv2D,MaxPooling2D,Conv1D,MaxPooling1D import numpy as np import os (x_train, y_train), (x_test, y_test) = boston_housing.load_data() def build0(): model=Sequential(name=‘boston‘) model.add(BatchNormalization(input_shape=(13,))) model.add(Dropout(0.5)) model.add(Dense(1)) return model def build1(): model=Sequential(name=‘boston‘) model.add(BatchNormalization(input_shape=(13,))) model.add(Dense(13)) model.add(Dropout(0.5)) model.add(Dense(1)) return model def build2(): model=Sequential(name=‘boston‘) model.add(BatchNormalization(input_shape=(13,))) model.add(Dense(13,activation=‘tanh‘)) model.add(Dropout(0.5)) model.add(Dense(1)) return model def build3(): model=Sequential(name=‘boston‘) model.add(BatchNormalization(input_shape=(13,))) model.add(Dense(13,activation=‘sigmoid‘)) model.add(Dropout(0.5)) model.add(Dense(1)) return model def build4(): model=Sequential(name=‘boston‘) model.add(BatchNormalization(input_shape=(13,))) model.add(Reshape((13,1))) model.add(Conv1D(filters=13,strides=1,padding=‘same‘,kernel_size=2,activation=‘sigmoid‘)) model.add(Conv1D(filters=26, strides=1, padding=‘same‘, kernel_size=2, activation=‘sigmoid‘)) model.add(MaxPooling1D(pool_size=2,strides=1,padding=‘same‘)) model.add(Conv1D(filters=52, strides=1, padding=‘same‘, kernel_size=2, activation=‘sigmoid‘)) model.add(Conv1D(filters=104, strides=1, padding=‘same‘, kernel_size=2, activation=‘sigmoid‘)) model.add(MaxPooling1D(pool_size=2, strides=1, padding=‘same‘)) model.add(Flatten()) model.add(Dropout(0.5)) model.add(Dense(1)) return model def build5(): model = Sequential(name=‘boston‘) model.add(BatchNormalization(input_shape=(13,))) model.add(Reshape((13, 1,1))) model.add(Conv2D(filters=13, strides=1, padding=‘same‘, kernel_size=2, activation=‘sigmoid‘)) model.add(Conv2D(filters=26, strides=1, padding=‘same‘, kernel_size=2, activation=‘sigmoid‘)) model.add(MaxPooling2D(pool_size=2, strides=1, padding=‘same‘)) model.add(Conv2D(filters=52, strides=1, padding=‘same‘, kernel_size=2, activation=‘sigmoid‘)) model.add(Conv2D(filters=104, strides=1, padding=‘same‘, kernel_size=2, activation=‘sigmoid‘)) model.add(MaxPooling2D(pool_size=2, strides=1, padding=‘same‘)) model.add(Flatten()) model.add(Dropout(0.5)) model.add(Dense(1)) return model for i in range(6): model=eval("build"+str(i)+"()") model.compile(‘adam‘,‘mae‘)#3.0895 history=model.fit(x_train,y_train,batch_size=404,epochs=10000,verbose=1,validation_data=(x_test,y_test)) print(history.history) f=open("result.txt",‘a‘) f.write(str(history.history[‘val_loss‘][-1])+"\n") f.close()
其中build4方法预测最准确mae loss最小
时间: 2024-11-03 00:35:32