TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python s

本文介绍了一种在使用TensorFlow实现“字嵌入+LSTM+CRF”的分词任务中遇到的TypeError问题,详细解释了错误原因,并给出了正确的feed_dict参数设置方式。
部署运行你感兴趣的模型镜像

一. 背景

今天继续完成“字嵌入+LSTM+CRF”实现分词的大业, 不过一上来又出现了新的问题。

二. 代码

Chars, vectors, labels = read(path)
vectors = vectors.reshape((batch_size, max_seq_len, 128))
labels = labels.reshape((batch_size, max_seq_len))

with tf.Graph().as_default() as g:
    inputs = tf.placeholder('float', [batch_size, max_seq_len, 128])
    tags = tf.placeholder('float', [batch_size, max_seq_len])
    model = Model(inputs, tags)
    initer = tf.global_variables_initializer()

with tf.Session(graph=g) as sess:
    sess.run(initer)
    train_batch_size = batch_size * 0.9
    test_batch_size = batch_size * 0.1
    # batch_size = train_batch_size
    for i in range(1000):
        print(type(vectors))
        print(type(labels))
        loss, accuracy, size, _ = sess.run([model.loss, model.accuracy, model.batch_size, model.optimizer], feed_dict={inputs : inputs, tags : labels})
        if i % 100 == 0:
            print('loss:', loss, 'accuracy', accuracy, 'size', size)

三. 错误 

以上代码报如下错误:

TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles.For reference, the tensor object was Tensor("Placeholder:0", shape=(1000, 10, 128), dtype=float32) which was passed to the feed with key Tensor("Placeholder:0", shape=(1000, 10, 128), dtype=float32).

这是什么鬼,怎么会这样, 明明传进入的是np.ndarray啊。

四. 解决方案

后来仔细看了好久,原来是是这里没有写对:

 feed_dict={inputs : inputs, tags : labels}

改成如下就解决问题了:

 feed_dict={inputs : vectors, tags : labels}

 

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值