相关疑难解决方法(0)

如何将py_func与返回dict的函数一起使用

我正在使用编写输入管道tf.data.Dataset。我想使用python代码加载和转换样本,该代码返回张量字典。不幸的是,我看不到如何将其定义为传递给的输出类型tf.py_func

我有一个解决方法,我的函数返回张量列表而不是字典,但是由于我在那个字典中有4个键,因此它使代码的可读性降低。

该代码看起来如下

file_list = ....

def load(file_name):
    return {"image": np.zeros(...,dtype=np.float32),
           "label": 1.0} # there is more labels, in the original code

ds = tf.data.Dataset.from_tensor_slices(file_list)
ds.shuffle(...)
out_type = [{'image':tf.float32, "label":tf.float32 }] # ???? 
ds.map(lambda x: tf.py_func(load, [x], out_type))

ds.batch(...)
ds.prefetch(1)
Run Code Online (Sandbox Code Playgroud)

python tensorflow tensorflow-datasets

8
推荐指数
1
解决办法
898
查看次数

ValueError:没有为任何变量提供梯度 - Tensorflow 2.0/Keras

我正在尝试使用 Keras 实现一个简单的序列到序列模型。但是,我一直看到以下内容ValueError

ValueError: No gradients provided for any variable: ['simple_model/time_distributed/kernel:0', 'simple_model/time_distributed/bias:0', 'simple_model/embedding/embeddings:0', 'simple_model/conv2d/kernel:0', 'simple_model/conv2d/bias:0', 'simple_model/dense_1/kernel:0', 'simple_model/dense_1/bias:0'].
Run Code Online (Sandbox Code Playgroud)

这样的其他问题或在 Github 上查看此问题表明这可能与交叉熵损失函数有关;但我看不到我在这里做错了什么。

我不认为这是问题所在,但我想说的tf-nightly==2.2.0.dev20200410是,准确地说,我正在夜间构建 TensorFlow 。

下面的代码是一个独立的示例,应该重现上面的异常:

import random
from functools import partial

import tensorflow as tf
from tensorflow import keras
from tensorflow_datasets.core.features.text import SubwordTextEncoder

EOS = '<eos>'
PAD = '<pad>'

RESERVED_TOKENS = [EOS, PAD]
EOS_ID = RESERVED_TOKENS.index(EOS)
PAD_ID = RESERVED_TOKENS.index(PAD)

dictionary = [
    'verstehen',
    'verstanden',
    'vergessen',
    'verlegen',
    'verlernen',
    'vertun',
    'vertan',
    'verloren',
    'verlieren',
    'verlassen',
    'verhandeln', …
Run Code Online (Sandbox Code Playgroud)

python machine-learning keras tf.keras tensorflow2.0

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