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