简体   繁体   English

使用tflearn进行回归的神经网络

[英]Neural Network for Regression with tflearn

My question is about coding a neural network which does regression(and NOT classification) using tflearn. 我的问题是关于使用tflearn进行回归(而不是分类)的神经网络编码。

Dataset: 数据集:

fixed acidity  volatile acidity  citric acid  ...  alcohol  quality   
7.4             0.700            0.00         ...  9.4        5    
7.8             0.880            0.00         ...  9.8        5  
7.8             0.760            0.04         ...  9.8        5     
11.2            0.280            0.56         ...  9.8        6      
7.4             0.700            0.00         ...  9.4        5

I want to build a neural network which takes in 11 features (chemical values in wine) and outputs or predicts a score ie, quality(out of 10). 我想建立一个具有11个特征(葡萄酒中的化学值)并输出或预测得分(即质量(满分10分))的神经网络。 I DON'T want to classify the wine like quality_1, quality_2,... I want the model to perform a regression function for my features and predict a value out of 10(could be even a float). 我不想像quality_1,quality_2等那样对葡萄酒进行分类...我希望模型对我的特征执行回归函数,并预测值不超过10(甚至可能是浮点数)。

The quality column in my data only has values = [3, 4, 5, 6, 7, 8, 9]. 我的数据中的quality列仅具有值[3、4、5、6、7、8、9]。 It does not contain 1, 2, and 10. 它不包含1、2和10。

Due to the lack in experience, I could only code a neural network that CLASSIFIES the wine into classes like [score_3, score_4,...] and I used one hot encoding to do so. 由于经验不足,我只能编码一个神经网络,将酒分类为[score_3,score_4,...]之类,并且我使用一种热编码来做到这一点。

Processed Data: 处理的数据:

Features: 特征:

[[  7.5999999    0.23         0.25999999 ...,   3.02999997   0.44
    9.19999981]
 [  6.9000001    0.23         0.34999999 ...,   2.79999995   0.54000002
   11.        ]
 [  6.69999981   0.17         0.37       ...,   3.25999999   0.60000002
   10.80000019]
 ..., 
 [  6.30000019   0.28         0.47       ...,   3.11999989   0.50999999
    9.5       ]
 [  5.19999981   0.64499998   0.         ...,   3.77999997   0.61000001
   12.5       ]
 [  8.           0.23999999   0.47999999 ...,   3.23000002   0.69999999
   10.        ]]

Labels: 标签:

[[ 0.  1.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  1.  0.  0.]
 ..., 
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  1. ...,  0.  0.  0.]]

Code for a neural network which CLASSIFIES into different classes: 用于神经网络的代码,可分为以下几类:

import pandas as pd
import numpy as np
import tflearn
from tflearn.layers.core import input_data, fully_connected
from tflearn.layers.estimator import regression
from sklearn.model_selection import train_test_split


def preprocess():

    data_source_red = 'F:\Gautam\...\Datasets\winequality-red.csv'

    data_red = pd.read_csv(data_source_red, index_col=False, sep=';')

    data = pd.get_dummies(data, columns=['quality'], prefix=['score'])

    x = data[data.columns[0:11]].values
    y = data[data.columns[11:18]].values

    x = np.float32(x)
    y = np.float32(y)

    return (x, y)


x, y = preprocess()

train_x, test_x, train_y, test_y = train_test_split(x, y, test_size = 0.2)

network = input_data(shape=[None, 11], name='Input_layer')

network = fully_connected(network, 10, activation='relu', name='Hidden_layer_1')

network = fully_connected(network, 10, activation='relu', name='Hidden_layer_2')

network = fully_connected(network, 7, activation='softmax', name='Output_layer')

network = regression(network, batch_size=2, optimizer='adam', learning_rate=0.01)

model = tflearn.DNN(network)

model.fit(train_x, train_y, show_metric=True, run_id='wine_regression',
          validation_set=0.1, n_epoch=1000)

The neural network above is a poor one(accuracy=0.40). 上面的神经网络很差(精度= 0.40)。 Moreover, it classifies the data into different classes. 此外,它将数据分为不同的类别。 I would like to know how to code a regression neural network which gives a score out of 10 for the input features(and NOT CLASSIFICATION). 我想知道如何编写一个回归神经网络,该神经网络的输入特征得分为10分(非分类)。 I would also prefer tflearn as I'm quite comfortable with it. 我也更喜欢tflearn,因为我对此很满意。

Hopefully, a few alterations to my code is all I need. 希望我需要对我的代码进行一些改动。 Thanks. 谢谢。

This is the line in your code which makes your network a classifier with seven categories, instead of a regressor: 这是代码中的行,使您的网络成为具有七个类别的分类器,而不是回归器:

network = fully_connected(network, 7, activation='softmax', name='Output_layer')

I don't use TFLearn any more, I have switched over to Keras (which is similar, and has better support). 我不再使用TFLearn,我已切换到Keras(类似,并且具有更好的支持)。 However, I will suggest that you want the following output layer instead: 但是,我建议您改用以下输出层:

network = fully_connected(network, 1, activation='linear', name='Output_layer')

Also, your training data will need to change. 此外,您的训练数据也需要更改。 If you want to perform a regression, you want a one-dimensional scalar label instead. 如果要执行回归,则需要一维标量标签。 I assume that you still have the original data, which you say that you altered? 我假设您仍然拥有原始数据,即您说已更改的数据? If not, the UC Irvine Machine Learning Data Repository has the wine quality data with a single, numerical Quality column . 如果不是, 则UC Irvine机器学习数据存储库具有带有单个数字“质量”列的葡萄酒质量数据

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

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