【问题标题】:Python Tflearn - ValueError: Cannot feed value of shape (16, 1) for Tensor u'InputData/X:0', which has shape '(?, 2)'Python Tflearn - ValueError:无法为具有形状“(?,2)”的张量u'InputData / X:0'提供形状(16、1)的值
【发布时间】:2017-06-30 10:09:24
【问题描述】:

作为机器学习和 tflearn/tensorflow 的新手,我试图按照 tflearn(泰坦尼克号)的快速入门教程进行操作。

修改它以满足我的需要我得到了这个代码:

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)

但是我收到了这个错误:

ValueError: 无法为张量提供形状 (16, 1) 的值 u'InputData/X:0',形状为'(?, 2)'

我的 csv 文件由 2 列组成,一个是索引(条目的编号,因为这只是一个测试,我将自己限制为 100 个条目),另一个是拥塞分数(我试图预测的内容, 0 到 200 之间),都是数值。

我有点理解我试图给它一个错误的价值(或者至少是他没有等待的东西),但我不知道如何纠正它。

【问题讨论】:

    标签: python machine-learning tensorflow tflearn


    【解决方案1】:

    添加

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

    纠正了我的问题,但给了我同样的错误,但这次是 Y,所以我这样做了:

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

    在回归之前,它似乎已经成功了。我不知道这是否是最好的,甚至是好的方法,但现在我设法解决了这个错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 2018-12-15
      • 1970-01-01
      • 2023-03-19
      • 2018-04-29
      • 1970-01-01
      • 2018-04-05
      相关资源
      最近更新 更多