相关疑难解决方法(0)

ValueError:没有为任何变量提供渐变

我正面临着tensorFlow的麻烦.执行以下代码

import tensorflow as tf
import input_data

learning_rate = 0.01
training_epochs = 25
batch_size = 100
display_step = 1

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

# tensorflow graph input
X = tf.placeholder('float', [None, 784]) # mnist data image of shape 28 * 28 = 784
Y = tf.placeholder('float', [None, 10]) # 0-9 digits recognition = > 10 classes

# set model weights
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

# Our hypothesis
activation = tf.add(tf.matmul(X, W),b)  # Softmax

# Cost function: cross …
Run Code Online (Sandbox Code Playgroud)

tensorflow

13
推荐指数
2
解决办法
1万
查看次数

有没有办法在 tf.data.Dataset w/tf.py_func 中传递字典?

我在数据处理中使用 tf.data.Dataset,我想用 tf.py_func 应用一些 python 代码。

顺便说一句,我发现在 tf.py_func 中,我无法返回字典。有没有办法做到这一点或解决方法?

我有如下所示的代码

def map_func(images, labels):
    """mapping python function"""
    # do something
    # cannot be expressed as a tensor graph
    return {
        'images': images,
        'labels': labels,
        'new_key': new_value}
def tf_py_func(images, labels):
    return tf.py_func(map_func, [images, labels], [tf.uint8, tf.string], name='blah')

return dataset.map(tf_py_func)
Run Code Online (Sandbox Code Playgroud)

================================================== ==========================

已经有一段时间了,我忘记我问过这个问题了。我以另一种方式解决了它,它是如此简单,以至于我觉得我几乎是个傻瓜。问题是:

  1. tf.py_func 不能返回字典。
  2. dataset.map 可以返回字典。

答案是:映射两次。

def map_func(images, labels):
    """mapping python function"""
    # do something
    # cannot be expressed as a tensor graph
    return {
        'images': images,
        'labels': labels,
        'new_key': new_value} …
Run Code Online (Sandbox Code Playgroud)

python tensorflow tensorflow-datasets

6
推荐指数
1
解决办法
2643
查看次数

标签 统计

tensorflow ×2

python ×1

tensorflow-datasets ×1