码迷,mamicode.com
首页 > 其他好文 > 详细

[ML]keras波士顿房价预测模型

时间:2017-09-22 16:35:49      阅读:642      评论:0      收藏:0      [点我收藏+]

标签:att   layer   sig   write   log   div   房价   rbo   print   

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最小

[ML]keras波士顿房价预测模型

标签:att   layer   sig   write   log   div   房价   rbo   print   

原文地址:http://www.cnblogs.com/xmxkkk/p/7575506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!