【问题标题】:ValueError: Cannot feed value of shape (64,) for Tensor 'TargetsData/Y:0', which has shape '(?, 1)'ValueError:无法为具有形状“(?,1)”的张量“TargetsData / Y:0”提供形状(64,)的值
【发布时间】:2021-07-01 04:28:37
【问题描述】:

总的来说,我在 Python 编程方面有相当多的经验,但对神经网络和深度学习非常陌生。在完成了 Tech With Tim 的 Mega AI 课程后,我决定自己创建一个神经网络。这很简单 - 它接受一个名字并根据其性别输出 0 或 1 - 0 = 男性,1 = 女性。

这是我的完整代码。

import numpy as np
import pandas as pd
import tensorflow as tf
import tflearn

encode_alphabet = '0ABCDEFGHIJKLMNOPQRSTUVWXYZ'

data = pd.read_csv("names.csv")
X = data.drop(["OUT"], 1).values.tolist()
y = [int("".join([str(j) for j in i])) for i in data[["OUT"]].values.tolist()]

print(len(X))
print(len(y))

tf.compat.v1.reset_default_graph()

net = tflearn.input_data(shape=[None, 12])
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 1, activation="linear")
net = tflearn.regression(net)

model = tflearn.DNN(net)

model.fit(X, y, n_epoch=100, show_metric=True)

print(data)

names.csv 文件看起来像这样(前 5 行):

L0  L1  L2  L3  L4  L5  L6  L7  L8  L9  L10 L11 OUT
0   0   0   0   0   0   15  12  9   22  5   18  0
0   0   0   0   0   0   7   5   15  18  7   5   0
0   0   0   0   0   0   0   8   1   18  18  25  0
0   0   0   0   0   0   0   0   14  15  1   8   0
0   0   0   0   0   0   0   0   10  1   3   11  0

这是我得到的错误:

ValueError: Cannot feed value of shape (64,) for Tensor 'TargetsData/Y:0', which has shape '(?, 1)'

我尝试了各种模型形状,但似乎没有任何效果。 如果需要的话,我很高兴为你们提供更多信息,但我对这一切太陌生了,RTFM 回复真的无济于事。

提前非常感谢

【问题讨论】:

    标签: python pandas numpy tensorflow neural-network


    【解决方案1】:

    错误不言自明

    ValueError: Cannot feed value of shape (64,) for Tensor 'TargetsData/Y:0', which has shape '(?, 1)'
    

    这意味着您传递的y 形状(64,) 与预期的形状(?, 1) 不匹配。要修复它,您只需要更改y 的形状:

    y = [[int("".join([str(j) for j in i]))] for i in data[["OUT"]].values.tolist()]
    

    或者

    y = np.array([int("".join([str(j) for j in i])) for i in data[["OUT"]].values])
    y = y.reshape(len(y), 1)
    

    【讨论】:

    • 但是,我遇到了学习问题。 binary_acc 在 epoch 1 上从大约 42% 开始,在保持 1000 个 epoch 后稳定在 42%-58% 的范围内。你知道这是为什么吗?
    • @macaquedev 对不起,我没有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    • 2018-08-03
    相关资源
    最近更新 更多