我正在使用CNN进行回归,并尝试比较预测和训练标签的分布(例如股票的价格变化)。预测的直方图看起来不错,但是训练数据标签的直方图看起来像有线的。标签应在(0.92〜1.08)范围内并且呈正态分布,但直方图在该区域看起来很平坦,如下所示。enter image description here
我认为它是有线的原因
对于每个批次或步骤,直方图的中心和形状都应该像预测之一一样发生变化。enter image description here
从每一步训练标签的分布图中,我确实输入了不同批次的训练数据,这可以排除训练继续使用同一批次数据的可能性。enter image description here
所以我只是想知道是什么原因导致训练标签的直方图在训练期间几乎冻结。任何帮助将不胜感激!
代码如下:
def cnn_model_fn_regression(features, labels, mode):
BN = batch_norm_wrapper(features,mode)
_stride=[1.0,1.0,1.0,1.0]
_strides=[float(i) for i in _stride]
conv1 = tf.nn.depthwise_conv2d(
input=BN,
filter=tf.get_variable(shape=[1, 5,20,4], dtype=tf.float64,
name='filters'),
padding="SAME",
strides=_strides,
name="Input"
)
prelu_layer1 = parametric_relu(conv1)
conv2 = tf.layers.conv2d(
inputs=batch_norm_wrapper(prelu_layer1,mode),
filters=64,
kernel_size=[1, 5],
padding="same",
activation=parametric_relu)
pool2_flat = tf.reshape(conv2, [-1, 30 * 1 * 64])
dense = tf.layers.dense(inputs=pool2_flat, units=1920, activation=parametric_relu)
dropout = tf.layers.dropout(
inputs=dense, rate=0.1, training=mode == tf.estimator.ModeKeys.TRAIN)
predictions = {
"predict":tf.layers.dense(inputs=dropout,units=1,name='predict_layer')
}
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions["predict"])
# Calculate Loss (for both TRAIN and EVAL modes)
loss = tf.losses.mean_squared_error(labels=labels, predictions=predictions["predict"])
MSE=tf.metrics.mean_squared_error(labels=labels, predictions=predictions["predict"])
# Configure the Training Op (for TRAIN mode)
if mode == tf.estimator.ModeKeys.TRAIN:
tf.summary.scalar('MSE_ToTal', MSE[0])
tf.summary.scalar('MSE_Batch', MSE[1])
tf.summary.histogram('Training_Prediction',predictions["predict"])
tf.summary.histogram('Training_Labels',labels)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.000005)
train_op = optimizer.minimize(
loss=loss,
global_step=tf.train.get_global_step())
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)
# Add evaluation metrics (for EVAL mode)
if mode == tf.estimator.ModeKeys.EVAL:
eval_metric_ops = {
"mse": tf.metrics.mean_squared_error(labels=labels, predictions=predictions["predict"])}
tf.summary.histogram('Eval_Prediction',predictions["predict"])
tf.summary.histogram('Eval_Labels',labels)
return tf.estimator.EstimatorSpec(
mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
if mode == tf.estimator.ModeKeys.PREDICT:
eval_metric_ops = {
"mse": tf.metrics.mean_squared_error(labels=labels, predictions=predictions["predict"])}
return tf.estimator.EstimatorSpec(
mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
def main():
# Load training and eval data
[label,feature] = test()
# Create the Estimator
mnist_classifier = tf.estimator.Estimator(
model_fn=cnn_model_fn_regression, model_dir="./regression12")
# Train the model
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x=feature,
y=label,
batch_size=1000,
num_epochs=300,
shuffle=True)
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
x=feature,
y=label,
batch_size=1000,
num_epochs=1,
shuffle=True)
trainspec=tf.estimator.TrainSpec(
input_fn=train_input_fn,
max_steps=None
)
evalspec=tf.estimator.EvalSpec(
input_fn=eval_input_fn
)
tf.estimator.train_and_evaluate(
mnist_classifier,
trainspec,
evalspec
)