为什么使用Pearsonr作为损失函数时,为什么第一个时期的损失大于张量
我想在DNN网络中使用Pearsonr作为损失功能,我遵循Scipy.stats.pearsonr来编写自己的损失功能,并且结果与Scipy相同。这是我的功能,结果被
def correlationLoss2(x, y):
x = tf.convert_to_tensor(x)
y = math_ops.cast(y, x.dtype)
xmean = tf.reduce_mean(x)
ymean = tf.reduce_mean(y)
xsqsum = tf.reduce_sum( tf.math.squared_difference(x, xmean))
ysqsum = tf.reduce_sum( tf.math.squared_difference(y, ymean))
cov = tf.reduce_sum( (x - xmean) * (y - ymean))
corr = cov / tf.sqrt(xsqsum * ysqsum)
return - corr
x = data[:, 22]
y = data[:, -1]
print(- float(correlationLoss(x, y)))
print(stats.pearsonr(x, y)[0])
忽略了x和y,它们是我项目的数据集,结果是:
-0.017431042527298025
-0.017431042527298084
事情似乎还不错,但是当涉及到我的网络时,第一个时代说(我的指标func是(1- corr)):
Epoch 1/50
196/196 [==============================] - 35s 172ms/step - loss: 1.0501 - correlationMetric: 0.8766 - val_loss: -0.0295 - val_correlationMetric: 0.9123
Epoch 2/50
196/196 [==============================] - 11s 55ms/step - loss: -0.1150 - correlationMetric: 0.8465 - val_loss: -0.0598 - val_correlationMetric: 0.9115
Epoch 3/50
196/196 [==============================] - 11s 56ms/step - loss: -0.1299 - correlationMetric: 0.8352 - val_loss: -0.0674 - val_correlationMetric: 0.9061
Epoch 4/50
196/196 [==============================] - 10s 53ms/step - loss: -0.1383 - correlationMetric: 0.8270 - val_loss: -0.0734 - val_correlationMetric: 0.9013
网络的结果很棒,但是我仍然想知道为什么第一个时期的损失大于一个时代,这是没有意义的。这是我的网络:
def get_model():
features_inputs = tf.keras.Input((Config.input_size, ), dtype=tf.float32)
gn = tf.keras.layers.GaussianNoise(0.035)(features_inputs)
feature_x = layers.Dense(256, activation='swish')(gn)
feature_x = layers.Dropout(0.5)(feature_x)
feature_x = layers.Dense(512, activation='swish', kernel_regularizer="l2")(feature_x)
feature_x = layers.Dropout(0.44)(feature_x)
feature_x = layers.Dense(128, activation='swish', kernel_regularizer="l2")(feature_x)
feature_x = layers.Dropout(0.33)(feature_x)
feature_x = layers.Dense(32, activation='swish', kernel_regularizer="l2")(feature_x)
feature_x = layers.Dropout(0.3)(feature_x)
output = layers.Dense(1)(feature_x)
model = tf.keras.Model(inputs=[features_inputs], outputs=[output])
model.compile(optimizer=tf.optimizers.Adam(0.001), loss=correlationLoss2,
metrics=[correlationMetric])
return model
I want to use pearsonr as loss function in my DNN network, I followed the scipy.stats.pearsonr to write my own loss function, and it gets the same result as scipy. Heres my func and the result it get
def correlationLoss2(x, y):
x = tf.convert_to_tensor(x)
y = math_ops.cast(y, x.dtype)
xmean = tf.reduce_mean(x)
ymean = tf.reduce_mean(y)
xsqsum = tf.reduce_sum( tf.math.squared_difference(x, xmean))
ysqsum = tf.reduce_sum( tf.math.squared_difference(y, ymean))
cov = tf.reduce_sum( (x - xmean) * (y - ymean))
corr = cov / tf.sqrt(xsqsum * ysqsum)
return - corr
x = data[:, 22]
y = data[:, -1]
print(- float(correlationLoss(x, y)))
print(stats.pearsonr(x, y)[0])
ignore x and y, they are the dataset of my project, and heres the result:
-0.017431042527298025
-0.017431042527298084
Things seem to be all right, but when it comes to my network, the first epoch says(my metric func is (1 - corr)):
Epoch 1/50
196/196 [==============================] - 35s 172ms/step - loss: 1.0501 - correlationMetric: 0.8766 - val_loss: -0.0295 - val_correlationMetric: 0.9123
Epoch 2/50
196/196 [==============================] - 11s 55ms/step - loss: -0.1150 - correlationMetric: 0.8465 - val_loss: -0.0598 - val_correlationMetric: 0.9115
Epoch 3/50
196/196 [==============================] - 11s 56ms/step - loss: -0.1299 - correlationMetric: 0.8352 - val_loss: -0.0674 - val_correlationMetric: 0.9061
Epoch 4/50
196/196 [==============================] - 10s 53ms/step - loss: -0.1383 - correlationMetric: 0.8270 - val_loss: -0.0734 - val_correlationMetric: 0.9013
The result of network is great, but I still wonder why the loss of first epoch is greater than one, that does not make sense. Heres my network:
def get_model():
features_inputs = tf.keras.Input((Config.input_size, ), dtype=tf.float32)
gn = tf.keras.layers.GaussianNoise(0.035)(features_inputs)
feature_x = layers.Dense(256, activation='swish')(gn)
feature_x = layers.Dropout(0.5)(feature_x)
feature_x = layers.Dense(512, activation='swish', kernel_regularizer="l2")(feature_x)
feature_x = layers.Dropout(0.44)(feature_x)
feature_x = layers.Dense(128, activation='swish', kernel_regularizer="l2")(feature_x)
feature_x = layers.Dropout(0.33)(feature_x)
feature_x = layers.Dense(32, activation='swish', kernel_regularizer="l2")(feature_x)
feature_x = layers.Dropout(0.3)(feature_x)
output = layers.Dense(1)(feature_x)
model = tf.keras.Model(inputs=[features_inputs], outputs=[output])
model.compile(optimizer=tf.optimizers.Adam(0.001), loss=correlationLoss2,
metrics=[correlationMetric])
return model
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论