简体   繁体   English

Tensorflow Addons R2 ValueError:两个形状中的尺寸 0 必须相等,但 1 和 5

[英]Tensorflow Addons R2 ValueError: Dimension 0 in both shapes must be equal, but are 1 and 5

I have been trying to add a tfa metric to my model compile to be tracked throughout the training.我一直在尝试将 tfa 指标添加到我的 model 编译中,以便在整个培训过程中进行跟踪。 However, when I add the R2 metric, I get the following error.但是,当我添加 R2 指标时,出现以下错误。 I thought y_shape=(1,) would fix this, however it did not.我以为y_shape=(1,)会解决这个问题,但事实并非如此。

    ValueError: Dimension 0 in both shapes must be equal, but are 1 and 5. Shapes are [1] and [5]. for '{{node AssignAddVariableOp_8}} = AssignAddVariableOp[dtype=DT_FLOAT](AssignAddVariableOp_8/resource, Sum_6)' with input shapes: [], [5].

My code is shown below:我的代码如下所示:

    model = Sequential()
    model.add(Input(shape=(4,)))
    model.add(Normalization())
    model.add(Dense(5, activation="relu", kernel_regularizer=l2(l2=1e-2)))
    print(model.summary())

    opt = Adam(learning_rate = 1e-2)
    model.compile(loss="mean_squared_error", optimizer=tf.keras.optimizers.Adam(learning_rate=1e-2), metrics=[MeanSquaredError(name="mse"), MeanAbsoluteError(name="mae"), tfa.metrics.RSquare(name="R2", y_shape=(1,))])

    history = model.fit(x = training_x,
                        y = training_y,
                        epochs = 10,
                        batch_size = 64,
                        validation_data = (validation_x, validation_y)
                        )

Any help is greatly appreciated, Note, I also tried changing the y_shape to (5,), but then I get the error that the dimensions are not equal.非常感谢任何帮助,请注意,我也尝试将 y_shape 更改为 (5,),但随后出现尺寸不相等的错误。 but are 5 and 1...但是 5 和 1...

You need to add an output layer to your model like the below:您需要向 model 添加一个 output 层,如下所示:

model.add(Dense(1))

then your model will be like below:那么您的 model 将如下所示:

model = Sequential()
model.add(Input(shape=(4,)))
model.add(Normalization())
model.add(Dense(5, activation="relu", kernel_regularizer=regularizers.l2(l2=1e-2)))
model.add(Dense(1))
print(model.summary())

Output: Output:

Model: "sequential_10"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 normalization_10 (Normaliza  (None, 4)                9         
 tion)                                                           
                                                                 
 dense_12 (Dense)            (None, 5)                 25        
                                                                 
 dense_13 (Dense)            (None, 1)                 6         
                                                                 
=================================================================
Total params: 40
Trainable params: 31
Non-trainable params: 9

暂无
暂无

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

相关问题 tensorflow ValueError:两个形状的尺寸0必须相等 - tensorflow ValueError: Dimension 0 in both shapes must be equal SHAP ValueError:两个形状中的维度 1 必须相等,但为 2 和 1。形状为 [?,2] 和 [?,1] - SHAP ValueError: Dimension 1 in both shapes must be equal, but are 2 and 1. Shapes are [?,2] and [?,1] 两个形状的维度 2 必须相等,但是是 3 和 1 - Dimension 2 in both shapes must be equal, but are 3 and 1 ValueError:两个形状中的维度 2 必须相等,但分别为 512 和 511。形状为 [?,384,512] 和 [?,384,511] - ValueError: Dimension 2 in both shapes must be equal, but are 512 and 511. Shapes are [?,384,512] and [?,384,511] keras使用权重加载模型,发出ValueError:两个形状的尺寸1必须相等,但分别为124和121 - keras use weight to load model, issue ValueError: Dimension 1 in both shapes must be equal, but are 124 and 121 Tensorflow LSTM错误(ValueError:形状必须等于等级,但为2和1) - Tensorflow LSTM Error (ValueError: Shapes must be equal rank, but are 2 and 1 ) Tensorflow ValueError:必须完全定义所有形状:[TensorShape([Dimension(None),Dimension(None),Dimension(3)]),TensorShape([]) - Tensorflow ValueError: All shapes must be fully defined: [TensorShape([Dimension(None), Dimension(None), Dimension(3)]), TensorShape([]) Tensorflow错误:ValueError:形状必须等于等级,但是2和1从形状1与其他形状合并 - Tensorflow Error: ValueError: Shapes must be equal rank, but are 2 and 1 From merging shape 1 with other shapes Tensorflow Transformer ValueError:维度必须为 5 但为 4 - Tensorflow Transformer ValueError: Dimension must be 5 but is 4 Tensorflow 尺寸问题:ValueError:形状 (3, 1) 和 (None, 3) 不兼容 - Tensorflow dimension issue: ValueError: Shapes (3, 1) and (None, 3) are incompatible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM