尝试创建简单的加法模型时,TFLearn无法提供形状值(ValueError)

时间:2017-06-16 00:01:20

标签: python machine-learning tensorflow regression tflearn

我是TFLearn的新手,并尝试创建一个简单的添加程序。 输入为2个值,输出为一个值,即输入的总和。我得到的错误是"ValueError: Cannot feed value of shape (100,) for Tensor 'TargetsData/Y:0', which has shape '(?, 1)"似乎形状/批量大小在代码中匹配,所以我不知道列车/测试数据的生成方式是问题,或者如果NN创建代码错误。 这是代码:

import numpy as np
import tflearn


def generate_answers(data):
    answers = []
    for row in data:
        answers.append(sum(row))
    return np.array(answers).astype(float)

train_data_count = 1000
test_data_count = 100

net = tflearn.input_data(shape=(None, 2))
net = tflearn.fully_connected(net, 100)
net = tflearn.fully_connected(net, 100)
net = tflearn.fully_connected(net, 1, activation="linear")
net = tflearn.regression(net, optimizer='sgd', loss='mean_square', metric='R2', learning_rate=0.1)
model = tflearn.DNN(net)

train_data = np.random.randint(500, size=(train_data_count, 2)).astype(float)
train_answers = generate_answers(train_data)
print(train_data.shape)
print(train_answers.shape)
model.fit(train_data, train_answers, n_epoch=100, batch_size=100, show_metric=True)

test_data = np.random.randint(500, size=(test_data_count, 2)).astype(float)
test_answers = generate_answers(test_data)
predictions = model.predict(test_data)

count = 0
for i in range(len(predictions)):
    if test_answers[i] == predictions[i]:
        count += 1
print(count, "/", len(predictions))

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

事实证明,在回归之前添加net = tflearn.reshape(net, [-1])有助于解决该问题。该程序仍有一些错误,但至少已经解决了。