Python Tflearn - ValueError:不能为u' inputdata /X:0'张量提供形状(16,1)的值,它有形状(?2)'

时间:2021-11-14 12:50:43

Being new to machine learning and tflearn/tensorflow I was trying to follow the quickstart tutorial of tflearn (the titanic one).

作为机器学习和tflearn/tensorflow的新手,我尝试着学习tflearn(泰坦尼克号)的快速入门教程。

Modifying it to suit my need I got to this code:

修改它以适应我的需要,我得到了这个代码:

from __future__ import print_function

import numpy as np
import tflearn

# Load CSV file, indicate that the first column represents labels
from tflearn.data_utils import load_csv
data, labels = load_csv('nowcastScaled.csv', target_column=1, n_classes=2)

# Preprocessing function
def preprocess(data):
    return np.array(data, dtype=np.float32)

# Preprocess data
data = preprocess(data)

# Build neural network
net = tflearn.input_data(shape=[None, 2])
net = tflearn.fully_connected(net, 32)
net = tflearn.fully_connected(net, 32)
net = tflearn.fully_connected(net, 2, activation='softmax')
net = tflearn.regression(net, optimizer='adam', learning_rate=0.001,
                         loss='categorical_crossentropy')

# Define model
model = tflearn.DNN(net)
# Start training (apply gradient descent algorithm)
model.fit(data, labels, n_epoch=10, batch_size=16, show_metric=True)

But I got this error:

但我犯了一个错误:

ValueError: Cannot feed value of shape (16, 1) for Tensor u'InputData/X:0', which has shape '(?, 2)'

ValueError:不能为u' inputdata /X:0'张量提供形状(16,1)的值,它有形状'(?2)'

My csv file is composed of 2 column, one being an index (number of the entry, since it's only a test I limited myself to 100 entry) and the other being a congestion score (what I'm trying to predict, between 0 and 200), both are numerical values.

我的csv文件由两列组成,一列是索引(条目的数量,因为我只对100个条目进行了测试),另一列是拥塞分数(我要预测的是0到200之间),它们都是数值。

I kinda understand that I'm trying to feed it a bad value (or at least something he's not waiting for) but I don't see how to correct it.

我有点明白我在试图给它增加一个坏的价值(或者至少是他没有等待的东西),但我不知道如何去纠正它。

1 个解决方案

#1


0  

Adding

添加

data = np.reshape(data, (-1, 2))

Corrected my problem but gave me the same error but with Y this time so I did:

修正了我的问题,但我犯了同样的错误,但这次我做了:

labels = np.reshape(labels, (-1, 2))

both before regression and it seem to have done the trick. I don't know if it is the best or even a good way to do it but for now I managed to got ride of the error.

在回归之前,它似乎都成功了。我不知道这是最好的还是最好的方法,但现在我设法克服了这个错误。

#1


0  

Adding

添加

data = np.reshape(data, (-1, 2))

Corrected my problem but gave me the same error but with Y this time so I did:

修正了我的问题,但我犯了同样的错误,但这次我做了:

labels = np.reshape(labels, (-1, 2))

both before regression and it seem to have done the trick. I don't know if it is the best or even a good way to do it but for now I managed to got ride of the error.

在回归之前,它似乎都成功了。我不知道这是最好的还是最好的方法,但现在我设法克服了这个错误。