我正在尝试构建一个模型,遇到了一个问题。我一直在尝试调试它,但遇到了一个奇怪的问题,我认为这可能是原因,但我不确定我做错了什么。我已经将我认为的问题简化为一小段代码,您可以在colab上运行。
这是一个colab,你可以尝试运行这个:https://colab.research.google.com/drive/1pSTwCwMFGlWgJOP3gn9WF6pZq2CiP4XJ
import keras
from keras.layers import Layer, Dense, Input, Reshape
import keras.backend as K
class SimplePermute(Layer):
def __init__(self, **kwargs):
super(SimplePermute, self).__init__(**kwargs)
def call(self, inputs, **kwargs):
return K.permute_dimensions(inputs, [0,2,1])
test_i = Input(shape=(10, 256))
test = SimplePermute()(test_i)
print(test.get_shape())
print(K.int_shape(test))
test = Dense(units=100, activation="softmax", name="sft2")(test)
print(test.get_shape())
print(K.int_shape(test))我希望第二系列的print输出的是排列后的张量形状--即?,256,10。然而,K.int_shape()返回?,10,256,而TF的get_shape()返回正确排列的形状。
我认为这种内部不匹配导致了我在模型下游看到的错误。
发布于 2019-09-13 11:06:40
您的自定义层没有实现compute_output_shape方法。这是Keras用来确定张量的_keras_shape属性的方法,该属性由K.int_shape返回。
Permute((2,1))层。Lambda(lambda x: K.permute_dimensions(x, [0,2,1]))层。compute_output_shape方法:。
def compute_output_shape(self, input_shape):
return (input_shape[0], input_shape[2], input_shape[1])https://stackoverflow.com/questions/57915767
复制相似问题