简体   繁体   English

理解线性回归keras模型图

[英]Making sense of Linear regression keras model plot

I'm using Keras boston dataset, using single feature and trying to perform a linear regression model. 我正在使用Keras波士顿数据集,使用单个特征并尝试执行线性回归模型。 I have normalized the input feature. 我已经标准化了输入功能。 The output plot appears to be a straight line and not aligned to the data distribution: 输出图似乎是一条直线,未与数据分布对齐:
情节
- what am I missing here? -我在这里想念什么? Skipped the lines to load and normalize data below. 跳过这些行以加载和规范化下面的数据。

model = Sequential()
model.add(Dense(units=1,input_dim=1, activation='sigmoid'))
model.compile(optimizer=optimizers.SGD(lr=0.0001,clipvalue=0.5), loss='binary_crossentropy', metrics=['mae','accuracy'])
history = model.fit(x_train, y_train, validation_split=0.25,batch_size=64, epochs=200,shuffle=True)
loss = model.evaluate(x_test,y_test,batch_size=None)
predict=model.predict(x_test)
plt.plot()
plt.plot(y_test, predict, 'b', x_test , y_test, 'k.')

I implemented simple linear regression model with keras. 我用keras实现了简单的线性回归模型。

from keras.models import Sequential
from keras.layers import Dense, Dropout
import numpy as np
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt

class Train:
    def __init__(self):
        self.__lr = 0.1
        self.x_train, self.y_train, self.x_test, self.y_test, self.y_scaler = self.load_data('train.csv')
        self.train()

This is the part of preprocessing as following: 这是预处理的一部分,如下所示:

    def load_data(self, fname):
        data = np.loadtxt(fname, skiprows=1, delimiter=',')

        x_data = data[:, 1:-1]
        x_scaler = MinMaxScaler(feature_range=(0, 1))
        x_data = x_scaler.fit_transform(x_data)

        y_data = data[:, [-1]]
        y_scaler = MinMaxScaler(feature_range=(0, 1))
        y_data = y_scaler.fit_transform(y_data)

        train_size = int(len(x_data)*0.7)
        test_size = len(x_data) - train_size
        x_train, x_test = x_data[0:train_size], x_data[train_size:len(x_data)]
        y_train, y_test = y_data[0:train_size], y_data[train_size:len(y_data)]

        return x_train, y_train, x_test, y_test, y_scaler

And, It's the training part include layers. 并且,它的训练部分包括层次。 You must use the relu and mse . 您必须使用relumse
Please refer the code as following: 请参考以下代码:

    def train(self):
        model = Sequential()
        model.add(Dense(128, input_dim=len(self.x_train[0]), activation='relu'))
        model.add(Dropout(0.2))
        model.add(Dense(64, activation='relu'))   
        model.add(Dropout(0.2))
        model.add(Dense(32, activation='relu'))   
        model.add(Dropout(0.2))
        model.add(Dense(1, activation='relu'))                        
        model.compile(loss='mse', optimizer='sgd', metrics=['mse'])
        model.fit(self.x_train, self.y_train, epochs=5000)
        result = model.evaluate(self.x_test, self.y_test)
        predictions = model.predict(self.x_test)
        predictions = self.y_scaler.inverse_transform(predictions)
        print (predictions)

        y_test = self.y_scaler.inverse_transform(self.y_test)

        plt.plot(y_test)
        plt.plot(predictions)
        plt.show()

You know it's the main: 您知道这是主要的:

if __name__ == "__main__":
    train = Train()

The result is 结果是 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM