【问题标题】:Shaping data for linear regression with TFlearn使用 TFlearn 为线性回归塑造数据
【发布时间】:2017-06-26 16:06:04
【问题描述】:

我正在尝试通过将列数增加到 21 来扩展 the tflearn example for linear regression

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,)'

我发现形状 (64, ) 来自 tflearn.regression() 的默认批量大小。

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

谢谢!

【问题讨论】:

    标签: python tensorflow linear-regression tflearn


    【解决方案1】:

    我也尝试过这样做。我进行了这些更改以使其正常工作

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

    我的猜测是,如果功能 >1,您将无法使用 tflearn.single_unit()。您可以添加额外的全连接层,但最后一层必须只有 1 个神经元,因为 Y.shape=(?,1)

    【讨论】:

      【解决方案2】:

      您有 21 个功能。因此,您不能使用linear = tflearn.single_unit(input_)

      试试这个:linear = tflearn.fully_connected(input_, 21, activation='linear')

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

      尝试在# linear regression graph之前使用下面给出的代码:

      Y = np.expand_dims(Y,-1)

      现在在regression = tflearn.regression(linear, optimizer='sgd', loss='mean_square',metric='R2', learning_rate=0.01)之前,输入以下代码:

      linear = tflearn.fully_connected(linear, 1, activation='linear')

      【讨论】:

        猜你喜欢
        • 2019-12-29
        • 2017-04-29
        • 1970-01-01
        • 2017-02-25
        • 2018-10-07
        • 1970-01-01
        • 2018-07-31
        • 2018-04-24
        • 2013-10-23
        相关资源
        最近更新 更多