【问题标题】:Keras Error: Please provide data which shares the same first dimensionKeras 错误:请提供具有相同第一维的数据
【发布时间】:2020-08-20 12:53:37
【问题描述】:

我尝试在 keras 中使用自定义层。这是一个简单的层,只是一个带有可训练参数的 matmul。

from tensorflow import keras
import numpy as np
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input
from tensorflow.keras.optimizers import RMSprop
from keras.layers import Layer
from tensorflow.keras import backend as K

class MultiLayer(Layer):
    def __init__(self, output_dim, **kwargs):
        self.output_dim = output_dim
        super(MultiLayer, self).__init__(**kwargs)
    def build(self, input_shape):
        # Create a trainable weight variable for this layer
        if self.output_dim[0] != input_shape[1]:
            raise Exception("expect input shape with [{},?], but get input with shape {}".format(self.output_dim[0],input_shape), input_shape)
        self.kernel = self.add_weight(name='kernel', 
                                      shape=(input_shape[2], self.output_dim[0]),
                                      initializer='uniform',
                                      trainable=True)
        super(MultiLayer, self).build(input_shape)  # Be sure to call this at the end
    def call(self, x):
        return K.dot(x, self.kernel)
    def compute_output_shape(self, input_shape):
        return (input_shape[0], self.output_dim)

x_fake = np.random.random((10,28,28))
y_fake = [np.diag(np.ones(28))]*10
input_shape = np.shape(x_fake)[1:]
print(input_shape)
ipt = Input(name='inputs',shape=input_shape)
layer = MultiLayer((input_shape[0],input_shape[0]),name="dev")(ipt)
#layer = Flatten()(layer)
model = Model(inputs=ipt,outputs=layer)
model.summary()
rms = RMSprop()
model.compile(loss="rms", optimizer=rms, metrics=['accuracy'])
model.fit(x_fake,y_fake)

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
inputs (InputLayer)          [(None, 28, 28)]          0         
_________________________________________________________________
dev (MultiLayer)             (None, 28, 28)            784       
=================================================================
Total params: 784
Trainable params: 784
Non-trainable params: 0
_________________________________________________________________

但是当我拟合这个模型时,就会发生错误。

ValueError: Data cardinality is ambiguous:
  x sizes: 10
  y sizes: 28, 28, 28, 28, 28, 28, 28, 28, 28, 28

Please provide data which shares the same first dimension.

我不知道尺寸是什么意思。

如何解决?

【问题讨论】:

    标签: python tensorflow keras tf.keras


    【解决方案1】:

    model.output_shape 必须匹配 y_fake.shape;你的[np.diag(np.ones((28,28)))]*10 是一个包含 10 个数组的列表,每个数组的形状都是 (28, 28),所以 TF 认为你有 10 个输出。我猜你打算np.stack(... * 10)

    接下来,确保所有导入都是一致的(from tensorflow.kerasfrom keras)。最后,“rms”不是受支持的损失,我将其替换为"mse"。以下所有修复。


    import numpy as np
    from tensorflow.keras.models import Model
    from tensorflow.keras.layers import Input
    from tensorflow.keras.optimizers import RMSprop
    from tensorflow.keras.layers import Layer
    from tensorflow.keras import backend as K
    
    class MultiLayer(Layer):
        def __init__(self, output_dim, **kwargs):
            self.output_dim = output_dim
            super(MultiLayer, self).__init__(**kwargs)
    
        def build(self, input_shape):
            # Create a trainable weight variable for this layer
            if self.output_dim[0] != input_shape[1]:
                raise Exception(("expect input shape with [{},?], but get input with "
                                 "shape {}").format(self.output_dim[0],input_shape),
                                input_shape)
            self.kernel = self.add_weight(name='kernel',
                                          shape=(input_shape[2], self.output_dim[0]),
                                          initializer='uniform',
                                          trainable=True)
            super(MultiLayer, self).build(input_shape)  # Be sure to call this at the end
    
        def call(self, x):
            return K.dot(x, self.kernel)
    
        def compute_output_shape(self, input_shape):
            return (input_shape[0], self.output_dim)
    
    x_fake = np.random.random((10,28,28))
    y_fake = np.stack([np.diag(np.ones((28)))]*10)  # (10, 28, 28)
    input_shape = np.shape(x_fake)[1:]
    print(input_shape)
    
    ipt = Input(name='inputs', shape=input_shape)
    layer = MultiLayer((input_shape[0],input_shape[0]),name="dev")(ipt)
    model = Model(inputs=ipt, outputs=layer)
    
    model.summary()
    rms = RMSprop()
    model.compile(loss="mse", optimizer=rms, metrics=['accuracy'])
    
    model.fit(x_fake, y_fake)
    
    

    【讨论】:

    • 应该是“y_fake = [np.diag(np.ones(28))]*10”,形状是(10, 28, 28)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 2018-09-26
    相关资源
    最近更新 更多