在下面的代码中。
@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()
当使用wihtout时,输出 tf.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'
预期的输出是一个2-tuple of(tensorA,tensorB)的形状。(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)>)
有人知道原因吗?我怀疑这是因为数据集的形状是: <MapDataset shapes: (128, None, 78), types: tf.float32>
然而,我不明白为什么 max_y
是 None
因为在循环之前已经知道了批处理的形状?
我希望我理解的很好:你的问题在于 max_y
作为none,并且它是作为大小参数的一部分,用于 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')