简体   繁体   English

ValueError:尺寸必须相等,但对于具有输入形状的“p_softmax/truediv”(操作:“RealDiv”)为 3 和 300:[?,300,300,3]、[?,300,300]

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

I am currently trying to create a parametric version of Softmax, and I receive the above error, when making my axis hyperparam -1 for 3D space (color+x+y).我目前正在尝试创建 Softmax 的参数版本,并且在为 3D 空间(颜色 + x + y)设置我的轴​​超参数 -1 时收到上述错误。 I'd like to know whether my implementation is correct for 3D space, and if it is only correct for 2d or 1d space, then how can I generalize Softmax for 3d spaces?我想知道我的实现是否适用于 3D 空间,如果它只适用于 2d 或 1d 空间,那么我如何将 Softmax 推广到 3d 空间?

Code for layer and testing:层和测试代码:

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:追溯:

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].

You have to keep the dimension, when you call tf.reduce_sum :当您调用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)

Otherwise, the last dimension (channel) is reduced and you try to divide a tensor of shape [None, 300 , 300] with a tensor of shape [None, 300, 300, 3] as the error states.否则,最后一个维度(通道)会减少,并且您尝试将形状为 [None, 300 , 300] 的张量与形状为 [None, 300, 300, 3] 的张量分开作为错误状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:无法将输入数组从形状 (300,300,3) 广播到形状 (300,300) - ValueError: could not broadcast input array from shape (300,300,3) into shape (300,300) Keras 错误:无法将输入数组从形状 (300,300,3) 广播到形状 (300,300) - Keras Error: could not broadcast input array from shape (300,300,3) into shape (300,300) Keras ValueError:尺寸必须相等,但对于具有输入形状的“{{node Equal}}”,尺寸为 9 和 400:[?,9], [?,300,400] - Keras ValueError: Dimensions must be equal, but are 9 and 400 for '{{node Equal}}' with input shapes: [?,9], [?,300,400] Model.predict()ValueError:无法为形状为(?,300,300,3)的Tensor输入形状(300,300,3)的值 - Model.predict() ValueError: Cananot feed value of shape (300,300,3) for Tensor which has shape (?,300,300,3) 为什么300 * 300椭圆形无法完美贴合300 * 300 Canvas? - Why a 300*300 Oval cannot fit a 300*300 Canvas perfectly? ValueError:发现样本数量不一致的输入变量:[100, 300] - ValueError: Found input variables with inconsistent numbers of samples: [100, 300] python regex阅读[1/300] - python regex reading [1/300] 大熊猫合并300个数据框 - pandas merging 300 dataframes 用 Python 合并 300 个变量 - Merging 300 variables with Python Python - 将 300 个变量插入 SQLite - Python - Insert 300 variables into SQLite
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM