我目前正在尝试创建Softmax的参数化版本,当为3D空间(color+x+y)创建我的轴超参数-1时,我收到上述错误。我想知道我的实现对于3D空间是否正确,如果它只适用于2d或1d空间,那么我如何将Softmax推广到3d空间?
用于分层和测试的代码:
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].发布于 2020-01-23 23:22:25
当您调用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的张量分开作为错误状态。
https://stackoverflow.com/questions/59873313
复制相似问题