使用 tflearn 进行回归的神经网络
Neural Network for Regression with tflearn
我的问题是关于使用 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... ).
我数据中的质量列只有值 = [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,因为我对它很满意。
您代码中的这一行使您的网络成为具有七个类别的分类器,而不是回归器:
network = fully_connected(network, 7, activation='softmax', name='Output_layer')
我不再使用 TFLearn,我已经切换到 Keras(类似,并且支持更好)。但是,我建议您改用以下输出层:
network = fully_connected(network, 1, activation='linear', name='Output_layer')
此外,您的训练数据需要更改。如果要执行回归,则需要一维标量标签。我假设您仍然拥有原始数据,您说您更改了这些数据?如果没有,the UC Irvine Machine Learning Data Repository has the wine quality data with a single, numerical Quality column.
我的问题是关于使用 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... ).
我数据中的质量列只有值 = [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,因为我对它很满意。
您代码中的这一行使您的网络成为具有七个类别的分类器,而不是回归器:
network = fully_connected(network, 7, activation='softmax', name='Output_layer')
我不再使用 TFLearn,我已经切换到 Keras(类似,并且支持更好)。但是,我建议您改用以下输出层:
network = fully_connected(network, 1, activation='linear', name='Output_layer')
此外,您的训练数据需要更改。如果要执行回归,则需要一维标量标签。我假设您仍然拥有原始数据,您说您更改了这些数据?如果没有,the UC Irvine Machine Learning Data Repository has the wine quality data with a single, numerical Quality column.