【发布时间】:2018-10-15 21:17:33
【问题描述】:
我的问题是关于使用 tflearn 编写一个进行回归(而不是分类)的神经网络。
数据集:
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
我想建立一个神经网络,它包含 11 个特征(葡萄酒中的化学值)并输出或预测一个分数,即质量(满分 10)。我不想将葡萄酒分类为 quality_1、quality_2、... 我希望模型对我的特征执行回归函数并预测 10 的值(甚至可能是浮点数)。
我的数据中的质量列只有值 = [3, 4, 5, 6, 7, 8, 9]。 它不包含 1、2 和 10。
由于缺乏经验,我只能编写一个神经网络,将葡萄酒分类为 [score_3, score_4,...] 之类的类,并且我使用了一种热编码来做到这一点。
处理过的数据:
特点:
[[ 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. ]]
标签:
[[ 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.]]
分类为不同类别的神经网络的代码:
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)
上面的神经网络很差(准确度=0.40)。此外,它将数据分类为不同的类别。我想知道如何编写一个回归神经网络,它为输入特征(而不是分类)给出 10 分。我也更喜欢 tflearn,因为我对它很满意。
希望我只需要对我的代码进行一些更改。谢谢。
【问题讨论】:
标签: machine-learning neural-network classification regression tflearn