Shaping data for linear regression with TFlearn

2019-05-30 11:46发布

I'm trying to expand the tflearn example for linear regression by increasing the number of columns to 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)))

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

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

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

Thanks!

2条回答
再贱就再见
2楼-- · 2019-05-30 11:53

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

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)

查看更多
时光不老,我们不散
3楼-- · 2019-05-30 12:12

You have 21 features. Therefore, you cannot use linear = tflearn.single_unit(input_)

Instead try this: linear = tflearn.fully_connected(input_, 21, activation='linear')

The error you get is because your labels, i.e., Y has a shape of (1054,). You have to first preprocess it.

Try using the code given below before # linear regression graph:

Y = np.expand_dims(Y,-1)

Now before regression = tflearn.regression(linear, optimizer='sgd', loss='mean_square',metric='R2', learning_rate=0.01), type the below code:

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

查看更多
登录 后发表回答