ValueError:尺寸必须相等,但输入形状为[?,300,300,3],[?, 300,300]的“ p_softmax / truediv”(op:“ RealDiv”)的尺寸应为3和300

时间:2020-01-23 07:17:24

标签: python tensorflow keras

我当前正在尝试创建Softmax的参数版本,并且在使轴超参数-1表示3D空间(颜色+ x + y)时收到上述错误。我想知道我的实现对于3D空间是否正确,并且如果它仅对2d或1d空间正确,那么我如何概括3d空间的Softmax?

用于层和测试的代码:

import random
import numpy as np
from tensorflow import keras
import tensorflow as tf
from tensorflow.keras import layers as L

class PSoftmax(L.Layer):
    def __init__(self, axis=-1):
        super(PSoftmax, self).__init__()

        self.weight = self.add_weight(shape=(1,1),
                                    initializer='random_normal'
                                    )
        self.axis = axis
    def call(self, inputs):
        y = tf.pow(tf.abs(self.weight), inputs)
        return y / tf.reduce_sum(y, axis=self.axis)

y = L.Input(shape=(300,300,3))
x = PSoftmax(-1)(y)

跟踪:

Traceback (most recent call last):
  File "layers.py", line 22, in <module>
    y = PSoftmax()(y)
  File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 842, in __call__
    outputs = call_fn(cast_inputs, *args, **kwargs)
  File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/api.py", line 237, in wrapper
    raise e.ag_error_metadata.to_exception(e)
ValueError: in converted code:

    layers.py:19 call  *
        return y / tf.reduce_sum(y, axis=self.axis)
    /home/ai/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/math_ops.py:899 binary_op_wrapper
        return func(x, y, name=name)
    /home/ai/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/math_ops.py:1005 _truediv_python3
        return gen_math_ops.real_div(x, y, name=name)
    /home/ai/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/gen_math_ops.py:7954 real_div
        "RealDiv", x=x, y=y, name=name)
    /home/ai/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/op_def_library.py:793 _apply_op_helper
        op_def=op_def)
    /home/ai/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py:548 create_op
        compute_device)
    /home/ai/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:3429 _create_op_internal
        op_def=op_def)
    /home/ai/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:1773 __init__
        control_input_ops)
    /home/ai/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:1613 _create_c_op
        raise ValueError(str(e))

    ValueError: Dimensions must be equal, but are 3 and 300 for 'p_softmax/truediv' (op: 'RealDiv') with input shapes: [?,300,300,3], [?,300,300].

1 个答案:

答案 0 :(得分:1)

调用tf.reduce_sum时必须保持尺寸:

def call(self, inputs):
    y = tf.pow(tf.abs(self.weight), inputs)
    print(y)
    return y / tf.reduce_sum(y, axis=self.axis, keepdims=True)

否则,减小最后一个维度(通道),然后尝试将形状为[None,300,300]的张量除以形状为[None,300,300,3]的张量作为错误状态。