0

在以下代码中:

@tf.function
def get_x_y(dataset,count=1):
  X = tf.TensorArray(tf.float32,count)
  Y = tf.TensorArray(tf.float32,count)
  idx = tf.Variable(0,dtype=tf.int32)
  for batch in dataset.take(count):
    max_x,max_y,max_z = batch.shape
    x = tf.slice(batch,[0,0,0],[-1,max_y-1,-1])
    y = tf.slice(batch,[0,1,0],[-1,-1,-1 ]) 
    y = tf.argmax(y,-1)
    y = tf.cast(y,tf.float32)
    X = X.write(idx.numpy(),x)
    Y = Y.write(idx.numpy(),y)
    idx.assign_add(1)
  return X.stack(),Y.stack()

使用 wihtouttf.function装饰器时的输出与预期的一样,但是使用tf.function装饰器时会出现以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-56-2306b6173e70> in <module>()
----> 1 print(get_x_y(dataset))nt 

8 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    966           except Exception as e:  # pylint:disable=broad-except
    967             if hasattr(e, "ag_error_metadata"):
--> 968               raise e.ag_error_metadata.to_exception(e)
    969             else:
    970               raise

TypeError: in user code:

    <ipython-input-55-4a32428fa07d>:8 get_x_y  *
        x = tf.slice(batch,[0,0,0],[-1,max_y-1,-1])

    TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

预期输出是(张量A,张量B)形状的2元组,(count,batch_size,batch_seq_len,vocab_size)分别(count,batch_size,batch_seq_len)具有

count = argument provided to the function
batch_size = 128
batch_seq_len = ?, max_len(seq_i) for all seq in batch
vocab_size = 78

例如,当函数没有用 修饰时的预期输出tf.function

(<tf.Tensor: shape=(1, 128, 262, 78), dtype=float32, numpy=
array([[[[0., 1., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         ...,
         [1., 0., 0., ..., 0., 0., 0.],
         [1., 0., 0., ..., 0., 0., 0.],
         [1., 0., 0., ..., 0., 0., 0.]],

        [[0., 1., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         ...,
         [1., 0., 0., ..., 0., 0., 0.],
         [1., 0., 0., ..., 0., 0., 0.],
         [1., 0., 0., ..., 0., 0., 0.]],

        [[0., 1., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 1.],
         ...,
         [1., 0., 0., ..., 0., 0., 0.],
         [1., 0., 0., ..., 0., 0., 0.],
         [1., 0., 0., ..., 0., 0., 0.]],

        ...,

        [[0., 1., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 1.],
         ...,
         [1., 0., 0., ..., 0., 0., 0.],
         [1., 0., 0., ..., 0., 0., 0.],
         [1., 0., 0., ..., 0., 0., 0.]],

        [[0., 1., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 1.],
         ...,
         [1., 0., 0., ..., 0., 0., 0.],
         [1., 0., 0., ..., 0., 0., 0.],
         [1., 0., 0., ..., 0., 0., 0.]],

        [[0., 1., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 1.],
         ...,
         [1., 0., 0., ..., 0., 0., 0.],
         [1., 0., 0., ..., 0., 0., 0.],
         [1., 0., 0., ..., 0., 0., 0.]]]], dtype=float32)>, <tf.Tensor: shape=(1, 128, 262), dtype=float32, numpy=
array([[[ 7., 53., 69., ...,  0.,  0.,  0.],
        [15., 21.,  5., ...,  0.,  0.,  0.],
        [15., 77.,  5., ...,  0.,  0.,  0.],
        ...,
        [15., 77.,  5., ...,  0.,  0.,  0.],
        [15., 77.,  5., ...,  0.,  0.,  0.],
        [15., 77.,  5., ...,  0.,  0.,  0.]]], dtype=float32)>)

有没有人知道为什么?我怀疑这是因为数据集是 shape <MapDataset shapes: (128, None, 78), types: tf.float32>,但是我不明白为什么max_yNone因为在循环之前就知道批处理形状?

4

1 回答 1

0

我希望我能很好地理解你:你有一个max_yas none 的问题,它是作为 size 参数的一部分来tf.slice引发异常的。

strided_slice() 需要 4 个参数 input_、begin、end、strides。

该方法的功能非常简单:它的工作方式类似于对循环进行迭代,其中 begin 是张量中元素的位置,循环从这里开始,end 是循环停止的地方。

所以请尝试:这里的步骤或步幅可以是dataset.take(count)

tf.strided_slice(input, [start1, start2, ..., startN],
    [end1, end2, ..., endN], [step1, step2, ..., stepN])


max_y = tf.Variable(np.array(batch) 
s = tf.strided_slice(max_y, begin, end, max_y, name='var_slice')
于 2020-05-29T10:49:21.817 回答