[ML]keras和tensorflow波士顿房价预测模型
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=1, activation='sigmoid')) model.add(Conv2D(filters=26, strides=2, 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=1, activation='sigmoid')) model.add(Conv2D(filters=104, strides=2, 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(5,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()
其中build5方法预测最准确mae loss最小
tensorflow
import tensorflow as tf import numpy as np from keras.datasets import boston_housing (x_train, y_train), (x_test, y_test) = boston_housing.load_data() X=tf.placeholder('float32',[None,13]) y=tf.placeholder('float32',[None]) keep_prob=tf.placeholder('float32') normal1=tf.layers.batch_normalization(X) reshape1=tf.reshape(normal1,shape=(-1,13,1,1)) conv1_1=tf.layers.conv2d(reshape1,filters=13,strides=1,padding='same',kernel_size=1,activation=tf.nn.sigmoid) conv1_2=tf.layers.conv2d(conv1_1,filters=26,strides=2,padding='same',kernel_size=2,activation=tf.nn.sigmoid) pool1=tf.layers.max_pooling2d(conv1_2,pool_size=2,strides=1,padding='same') conv2_1=tf.layers.conv2d(pool1,filters=52,strides=1,padding='same',kernel_size=1,activation=tf.nn.sigmoid) conv2_2=tf.layers.conv2d(conv2_1,filters=104,strides=2,padding='same',kernel_size=2,activation=tf.nn.sigmoid) pool2=tf.layers.max_pooling2d(conv2_2,pool_size=2,strides=1,padding='same') reshape2=tf.reshape(pool2,shape=(-1,416)) dropout1=tf.nn.dropout(reshape2,keep_prob=keep_prob) dense1=tf.layers.dense(dropout1,units=1) reshape3=tf.reshape(dense1,shape=(-1,)) loss=tf.reduce_mean(tf.abs(y-reshape3)) step=tf.train.AdamOptimizer(0.001).minimize(loss) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(30000): sess.run(step,feed_dict={X:x_train,y:y_train,keep_prob:0.5}) # print(loss_val) loss_val=sess.run(loss,feed_dict={X:x_test,y:y_test,keep_prob:1}) print(str(i)+"=>"+str(loss_val))