使用TFlearn整形数据以进行线性回归 [英] Shaping data for linear regression with TFlearn

查看:204
本文介绍了使用TFlearn整形数据以进行线性回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试扩展线性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.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抱怨:

However, tflearn complains:

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()的默认批处理大小.

I found the shape (64, ) comes from the default batch size of tflearn.regression().

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

Do I need to transform the labels (Y)? In what way?

谢谢!

推荐答案

我试图做同样的事情.我进行了这些更改以使其正常工作

I tried to do the same. I made these changes to get it to work

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

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

My guess is that with features >1 you cannot use tflearn.single_unit(). You can add additional fully_connected layers, but the last one must have only 1 neuron because Y.shape=(?,1)

这篇关于使用TFlearn整形数据以进行线性回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆