Python 使用TFlearn为线性回归塑造数据

Python 使用TFlearn为线性回归塑造数据,python,tensorflow,linear-regression,tflearn,Python,Tensorflow,Linear Regression,Tflearn,我试图通过将列数增加到21来扩展 from trafficdata import X,Y import tflearn print(X.shape) #(1054, 21) print(Y.shape) #(1054,) # Linear Regression graph input_ = tflearn.input_data(shape=[None,21]) linear = tflearn.single_unit(input_) regression = tflearn.regress

我试图通过将列数增加到21来扩展

from trafficdata import X,Y

import tflearn

print(X.shape) #(1054, 21)
print(Y.shape) #(1054,)

# Linear Regression graph
input_ = tflearn.input_data(shape=[None,21])
linear = tflearn.single_unit(input_)
regression = tflearn.regression(linear, optimizer='sgd', loss='mean_square',
                                metric='R2', learning_rate=0.01)
m = tflearn.DNN(regression)
m.fit(X, Y, n_epoch=1000, show_metric=True, snapshot_epoch=False)

print("\nRegression result:")
print("Y = " + str(m.get_weights(linear.W)) +
      "*X + " + str(m.get_weights(linear.b)))
然而,tflearn抱怨:

Traceback (most recent call last):
  File "linearregression.py", line 16, in <module>
    m.fit(X, Y, n_epoch=1000, show_metric=True, snapshot_epoch=False)
  File "/usr/local/lib/python3.5/dist-packages/tflearn/models/dnn.py", line 216, in fit
    callbacks=callbacks)
  File "/usr/local/lib/python3.5/dist-packages/tflearn/helpers/trainer.py", line 339, in fit
    show_metric)
  File "/usr/local/lib/python3.5/dist-packages/tflearn/helpers/trainer.py", line 818, in _train
    feed_batch)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 789, in run
    run_metadata_ptr)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 975, in _run
    % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (64,) for Tensor 'TargetsData/Y:0', which has shape '(21,)'
回溯(最近一次呼叫最后一次):
文件“linearregression.py”,第16行,在
m、 拟合(X,Y,n_epoch=1000,show_metric=True,snapshot_epoch=False)
文件“/usr/local/lib/python3.5/dist-packages/tflearn/models/dnn.py”,第216行,适合
回调=回调)
文件“/usr/local/lib/python3.5/dist-packages/tflearn/helpers/trainer.py”,第339行,适合
显示(单位:公制)
文件“/usr/local/lib/python3.5/dist-packages/tflearn/helpers/trainer.py”,第818行,in\u-train
进料(每批)
文件“/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py”,第789行,正在运行
运行_元数据_ptr)
文件“/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py”,第975行,正在运行
%(np_val.shape,subfeed_t.name,str(subfeed_t.get_shape()))
ValueError:无法为张量'TargetsData/Y:0'提供形状(64,)的值,该张量具有形状'(21,)'
我发现形状(64,)来自默认的批量大小tflearn.regression()

我是否需要转换标签(Y)?以什么方式


谢谢

我也试着这么做。我做了这些改变来让它工作

# linear = tflearn.single_unit(input_)
linear = tflearn.fully_connected(input_, 1, activation='linear')

我的猜测是,如果功能>1,则不能使用
tflearn.single\u unit()
。您可以添加其他完全连接的层,但最后一层必须只有1个神经元,因为Y.shape=(?,1)

您有21个特征。因此,不能使用
linear=tflearn.single\u单元(输入)

请尝试以下操作:
linear=tflearn.fully\u connected(输入、21、激活='linear')

您得到的错误是因为您的标签,即Y的形状为(1054,)。 你必须先对它进行预处理

在使用线性回归图之前,请尝试使用下面给出的代码:

Y=np.展开尺寸(Y,-1)

现在在
回归=tflearn.回归(线性,优化器='sgd',损失='mean_square',度量='R2',学习率=0.01)
之前,键入以下代码:

linear=tflearn.完全连接(linear,1,activation='linear')