具有自定义关注层的tensorflow CTC错误

时间:2019-06-20 01:50:19

标签: tensorflow keras tensorflow2.0 tf.keras

我尝试通过以下代码吸引人们的注意,我试图推断出我创建的模型,它具有我想要的形状和值,并且可以与model.summary()完美配合。但是,它不会通过CTC损失计算。有人知道我的自定义图层有什么问题吗?     def compute_atten(data,atten_w):         input_vector = data.numpy()         atten_vectors = atten_w.numpy()         all_batch = []         对于索引,枚举(输入向量)中的one_batch:             tmp_w = atten_vectors [index]             all_vector = []             对于j,enumerate(one_batch)中的向量:                 tmp = np.zeros(input_vector.shape [2])                 对于tmp_w [j]中的w:                     tmp + =向量* w                 all_vector.append(tmp)             all_batch.append(all_vector)         all_batch = np.asarray(all_batch,dtype = np.float32)         返回all_batch

class AttentionLayer(tf.keras.layers.Layer):
    def __init__(self,**kwargs):
        super(AttentionLayer, self).__init__(**kwargs)

    def build(self,input_shape):
        super().build(input_shape)
        self.built = True

    def call(self,inputs):
        output_shape = self.compute_output_shape(tf.shape(inputs))
        transpose_input = tf.transpose(inputs,perm=[0,2,1])
        atten_w = K.backend.batch_dot(inputs,transpose_input)
        atten_w = tf.linalg.set_diag(atten_w,tf.zeros(tf.shape(atten_w)[0:-1],dtype=tf.float32))
        atten_w = tf.nn.softmax(atten_w,axis=1)
        atten_v = tf.py_function(calculate_atten,inp=[inputs,atten_w],Tout=[tf.float32])
        atten_v = K.backend.reshape(tf.convert_to_tensor(atten_v),output_shape)
        return atten_v

    def compute_output_shape(self, input_shape):
        return input_shape

2019-06-20 10:42:14.753458: W tensorflow/core/framework/op_kernel.cc:1431] OP_REQUIRES failed at transpose_op.cc:157 : Invalid argument: transpose expects a vector of size 0. But input(1) is a vector of size 3 2019-06-20 10:42:14.753536: W 
tensorflow/core/common_runtime/base_collective_executor.cc:214] BaseCollectiveExecutor::StartAbort Invalid argument: transpose expects a vector of size 0. But input(1) is a vector of size 3 [[{{node transpose}}]] [[replica_1/StatefulPartitionedCall/ExpandDims/_240]] 2019-06-20 10:42:14.753980: E 
tensorflow/core/common_runtime/process_function_library_runtime.cc:764] Component function execution failed: Invalid argument: transpose expects a vector of size 0. But input(1) is a vector of size 3 [[{{node transpose}}]] [[replica_1/StatefulPartitionedCall/ExpandDims/_240]] 2019-06-20 10:42:14.754115: W 
tensorflow/core/common_runtime/base_collective_executor.cc:214] BaseCollectiveExecutor::StartAbort Invalid argument: transpose expects a vector of size 0. But input(1) is a vector of size 3 [[{{node transpose}}]] 2019-06-20 10:42:14.754457: E 
tensorflow/core/common_runtime/process_function_library_runtime.cc:764] Component function execution failed: Invalid argument: transpose expects a vector of size 0. But input(1) is a vector of size 3 [[{{node transpose}}]] 2019-06-20 10:42:14.765733: W 
tensorflow/core/common_runtime/base_collective_executor.cc:214] BaseCollectiveExecutor::StartAbort Invalid argument: transpose expects a vector of size 0. But input(1) is a vector of size 3 [[{{node transpose}}]] [[replica_1/StatefulPartitionedCall/CTCLoss/_237]] 2019-06-20 10:42:14.765808: E 
tensorflow/core/common_runtime/process_function_library_runtime.cc:764] Component function execution failed: Invalid argument: transpose expects a vector of size 0. But input(1) is a vector of size 3 [[{{node transpose}}]] [[replica_1/StatefulPartitionedCall/CTCLoss/_237]] Traceback (most recent call last): File "ctc_model4.py", line 1060, in <module> train_ops = ditributred_train(train_iterator) File 
"/misc/home/usr16/cheesiang_leow/.virtualenvs/tensorflow-2.0/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 438, in __call__ return self._stateless_fn(*args, **kwds) File "/misc/home/usr16/cheesiang_leow/.virtualenvs/tensorflow-2.0/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 1288, in __call__ return graph_function._filtered_call(args, kwargs) # pylint: disable=protected-access File 
"/misc/home/usr16/cheesiang_leow/.virtualenvs/tensorflow-2.0/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 574, in _filtered_call (t for t in nest.flatten((args, kwargs)) File 
"/misc/home/usr16/cheesiang_leow/.virtualenvs/tensorflow-2.0/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 627, in _call_flat outputs = self._inference_function.call(ctx, args) File 
"/misc/home/usr16/cheesiang_leow/.virtualenvs/tensorflow-2.0/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 415, in call ctx=ctx) File 
"/misc/home/usr16/cheesiang_leow/.virtualenvs/tensorflow-2.0/lib/python3.6/site-packages/tensorflow/python/eager/execute.py", line 66, in quick_execute six.raise_from(core._status_to_exception(e.code, message), None) File "<string>", line 3, in raise_from 
tensorflow.python.framework.errors_impl.InvalidArgumentError: transpose expects a vector of size 0. But input(1) is a vector of size 3 [[{{node transpose}}]] [[replica_1/StatefulPartitionedCall/ExpandDims/_240]] [Op:__inference_ditributred_train_24926]

0 个答案:

没有答案