训练结果很好,验证结果也很好,但测试的时候性能极差

这是训练的结果


预测标签的准确值

结果

就是我不管用啥预测样本,预测出来的标签全是0.5附近的,问题出在哪里呢?

@snowkylin

我把训练样本的所有标签归一化成0-3的,测试集出来的结果也全是1.5附近

围观。。。如果你把test和valid对调一下训练的话,val的打印会是什么啊?

虽然看不到完整代码不知道你用了什么数据和模型,但我猜是过拟合问题。epoch数过高了,建议不要超过10,以及可以每跑一个epoch都计算一下在测试集的准确率。
以及你这数据量是不是有点少,我看截图写着3/3,每个epoch只有3个batch?你的数据是什么。

不是1000个batch吗?

那是1000个epoch吧

哦,对。。。

我做的是回归问题,一个CNN+MLP并联的神经网络,数据的标签如下文本的名字,15个,

然后归一化成0-1之间

http://localhost:8888/notebooks/5跨梁15因素30水平.ipynb

数据果然很少。过拟合。。

可是我拿训练数据去当测试数据,也是出来全在0.5附近

看来有更多的问题。
ps:这个地址我们无法访问啊。

啊,我看看怎么上传

#!/usr/bin/env python

coding: utf-8

In[104]:

import os
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split

physical_devices = tf.config.experimental.list_physical_devices(‘GPU’)
assert len(physical_devices) > 0, “Not enough GPU hardware devices available”
tf.config.experimental.set_memory_growth(physical_devices[0], True)

#gpus = tf.config.list_physical_devices(device_type=‘GPU’)
#cpus = tf.config.list_physical_devices(device_type=‘CPU’)
#print(gpus, cpus)
#os.environ[‘CUDA_VISIBLE_DEVICES’] = “0”
np.random.seed(1234)
tf.random.set_seed(1234)

In[105]:

#标签归一化
#standrad = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
#test_standrad = [1.66667, 1.66667, 1.66667, 1.66667, 1.66667, 1.66667, 1.66667, 1.66667, 1.66667, 1.66667, 1.66667, 1.66667, 1.66667, 1.66667, 1.66667]
#val_standrad = [3.333, 3.333, 3.333, 3.333, 3.333, 3.333, 3.333, 3.333, 3.333, 3.333, 3.333, 3.333, 3.333, 3.333, 3.333]
standrad = [30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]
test_standrad = [30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30]
#test_standrad = [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
val_standrad = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
#阵型归一化
def normalization(data):
max_value = np.max(abs(data))
return data / max_value
#频率归一化函数
def Z_score_normalization(data):
mean = np.mean(data)
var = np.var(data)
return 10 * (data - mean) / var

In[106]:

#训练数据集
train_shape = []
train_fre = []
train_label = []
train_data_file_dir = “C:/Users/rog/Desktop/data4/data/”
for file in os.listdir(train_data_file_dir):
train_txt_path = train_data_file_dir + file
f = open(train_txt_path)
train_dat1 = f.readlines()
train_all_data = []
for n in train_dat1:
train_all_data.append(float(n))
train_fre_data = train_all_data[0:10]
train_fre_data = np.reshape(Z_score_normalization(np.array(train_fre_data)), (1, 10))
train_fre.append(train_fre_data)
train_shape_data = train_all_data[10:210]
train_shape_data = np.reshape(normalization(np.array(train_shape_data)), (10, 20))

train_shape_data = train_shape_data[:,[0,2,3,5,6,7,9,13,15,17,19]]

train_shape.append(train_shape_data)
train_laber_data = []
for n in file.split("+", 16)[0:15]:
train_laber_data.append(float(n))
train_label_normalization = [a/b for a, b in zip(train_laber_data, standrad)]
train_label.append(train_label_normalization)

train_dataset = tf.data.Dataset.from_tensor_slices(({“shape”: train_shape, “fre”: train_fre}, train_label))
train_dataset = train_dataset.shuffle(buffer_size=30).batch(10)#将数据集划分批次并打乱,每批大小为40
train_dataset = train_dataset.prefetch(tf.data.experimental.AUTOTUNE)#并行化策略提高训练流程效率

#print(train_label)

In[107]:

#验证数据集
v_shape = []
v_fre = []
v_label = []
val_data_file_dir = “C:/Users/rog/Desktop/data4/val/”
for file in os.listdir(val_data_file_dir):
val_txt_path = val_data_file_dir + file
f = open(val_txt_path)
val_dat1 = f.readlines()
val_all_data = []
for n in val_dat1:
val_all_data.append(float(n))
val_fre_data = val_all_data[0:10]
val_fre_data = np.reshape(Z_score_normalization(np.array(val_fre_data)), (1, 10))
v_fre.append(val_fre_data)
val_shape_data = val_all_data[10:210]
val_shape_data = np.reshape(normalization(np.array(val_shape_data)), (10, 20))

val_shape_data = val_shape_data[:,[0,2,3,5,6,7,9,13,15,17,19]]

v_shape.append(val_shape_data)
val_laber_data = []
for n in file.split("+", 16)[0:15]:
val_laber_data.append(float(n))
val_label_normalization = [a/b for a, b in zip(val_laber_data, val_standrad)]
v_label.append(val_label_normalization)

val_dataset = tf.data.Dataset.from_tensor_slices(({“shape”: v_shape, “fre”: v_fre}, v_label))
val_dataset = val_dataset.shuffle(buffer_size=10).batch(4)#将数据集划分批次并打乱,每批大小为4
val_dataset = val_dataset.prefetch(tf.data.experimental.AUTOTUNE)#并行化策略提高训练流程效率

#print(v_label)

In[108]:

#测试数据集
test_shape = []
test_fre = []
test_label = []
test_data_file_dir = “C:/Users/rog/Desktop/data4/test3/”
for file in os.listdir(test_data_file_dir):
test_txt_path = test_data_file_dir + file
f = open(test_txt_path)
test_dat = f.readlines()
test_all_data = []
for n in test_dat:
test_all_data.append(float(n))
test_fre_data = test_all_data[0:10]
test_fre_data = np.reshape(Z_score_normalization(np.array(test_fre_data)), (1, 10))
test_fre.append(test_fre_data)
test_shape_data = test_all_data[10:210]
test_shape_data = np.reshape(normalization(np.array(test_shape_data)), (10, 20))

test_shape_data = test_shape_data[:,[0,2,3,5,6,7,9,13,15,17,19]]

test_shape.append(test_shape_data)
test_laber_data = []
for n in file.split("+", 16)[0:15]:
test_laber_data.append(float(n))
test_label_normalization = [a/b for a, b in zip(test_laber_data, test_standrad)]
test_label.append(test_label_normalization)

test_dataset = tf.data.Dataset.from_tensor_slices(({“shape”: test_shape, “fre”: test_fre}, test_label))
test_dataset = test_dataset.shuffle(buffer_size=1).batch(1)#将数据集划分批次并打乱,每批大小为1
test_dataset = test_dataset.prefetch(tf.data.experimental.AUTOTUNE)#并行化策略提高训练流程效率

print(test_label)

In[109]:

#定义模型
l2_regularizers = 0.1
input_model_shape = keras.Input(shape=(10, 11),name=“shape”)
input_frequency = keras.Input(shape=(1, 10), name=“fre”)
shape = layers.Conv1D(16, 10, activation=‘tanh’, input_shape=(10, 11), name=“Conv1D_1”)(input_model_shape)
#shape = layers.BatchNormalization(name=“BN”)(shape)
#shape = layers.MaxPooling1D(pool_size=2, name=“MaxPool1D_1”)(shape)
#shape = layers.Conv1D(32, 5, activation=‘tanh’, name=“Conv1D_2”)(shape)
#shape = layers.Conv1D(32, 3, activation=‘tanh’, name=“Conv1D_3”)(shape)
#shape = layers.MaxPooling1D(pool_size=2, name=“MaxPool1D_2”)(shape)
#shape = layers.BatchNormalization(name=“BN_shape”)(shape)
#fre = layers.Dense(32, activation=“tanh”, kernel_regularizer=keras.regularizers.l2(l2_regularizers), name=“Fre_Dense_1”)(input_frequency)
#fre = layers.BatchNormalization(name=“BN_fre_1”)(fre)
#fre = layers.Dense(32, activation=“tanh”, name=“Fre_Dense_2”)(fre)
#fre = layers.Dense(32, activation=“tanh”, name=“Fre_Dense_3”)(fre)
#fre = layers.Dense(32, activation=“tanh”, name=“Fre_Dense_4”)(fre)
#fre = layers.BatchNormalization(name=“BN_fre_2”)(fre)
#combine = layers.concatenate([shape, fre], axis=2, name=“Combine_Layer”)
combine = layers.concatenate([shape, input_frequency], axis=2, name=“Combine_Layer”)
layer = layers.Dense(512, activation=“tanh”, kernel_regularizer=keras.regularizers.l2(l2_regularizers), name=“Dense_1”)(combine)
#layer = layers.Dropout(0.1,name=“Dropout_1”)(layer)
layer = layers.Dense(512, activation=“tanh”, kernel_regularizer=keras.regularizers.l2(l2_regularizers), name=“Dense_2”)(layer)
layer = layers.Dense(512, activation=“tanh”, kernel_regularizer=keras.regularizers.l2(l2_regularizers), name=“Dense_3”)(layer)
layer = layers.Dense(512, activation=“tanh”, kernel_regularizer=keras.regularizers.l2(l2_regularizers), name=“Dense_4”)(layer)
outputs = layers.Dense(15, name=“Output”)(layer)
model = keras.Model(inputs=[input_model_shape, input_frequency], outputs=outputs)

In[110]:

#keras.utils.plot_model(model, ‘CNN+MLP.png’, show_shapes=True, show_layer_names=True, dpi=256)

In[111]:

#训练模型
num_epochs = 1100#对完整训练数据迭代的次数
model.compile(optimizer=keras.optimizers.SGD(),
loss=tf.keras.losses.mean_squared_error,
metrics=[‘mse’])
train_history = model.fit(train_dataset, epochs=num_epochs, validation_data=val_dataset)
print(model.metrics_names)

In[112]:

def show_train_history(train_history, train, val):
plt.plot(train_history.history[train])
plt.plot(train_history.history[val])
plt.title(‘train history’)
plt.ylabel(train)
plt.xlabel(‘epoch’)
plt.legend([‘train’,‘validation’], loc=‘upper right’)
plt.show()
show_train_history(train_history, ‘loss’,‘val_loss’)

In[113]:

print(model.evaluate(test_dataset))
predict_label = np.reshape(model.predict(test_dataset),15)
#predict_label = [a*b for a, b in zip(predict_label, test_standrad)]
print(predict_label)

使用训练数据集去作为测试集得到的标签也是0.5附近

从这些信息我不能很清楚地看到数据的量(训练集有多少个样本?)。如果模型是CNN而且直接训练,我的粗略建议是训练集样本数一般在10000以上会较好,低于1000几乎无法成功。以及回归问题比最基础的分类一般会难训练一些。

只有30个样本吧,训练集。样本数确实上不去,但是用训练集来做测试也是一样的话这个我真的不太理解啊、

训练集只有30个样本吧。样本数上不去,对这个有限制。
但是用训练集的一部分来做测试数据,也是得到一样的结果(也是中间值,十分不理想),这个我真的不太理解啊。
用什么样本来做测试都是得到一样的预测标签,好奇怪啊

的确挺奇怪的。可惜我也看不出所以然来。。。