使用Tensorflow数据集class+Keras,训练精度和评估精度之间存在巨大差异

2024-04-20 12:02:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我从我的数据创建了一个数据集,这个数据集的形式是(特征、标签)。features的维度是[?,731,7](其中?应该是400),相应标签的维度是[4,],如我的数据集所示。每个[731,7]样本对应一个类似[0,1,0,0]的4元素数组。你知道吗

enter image description here

一些示例数据: Sampledata1Sampledata2

建立简单的多层神经网络后,训练过程正常如下。但是,当我使用相同的数据集进行验证(只是检查算法是否有效)时,我实际上得到了巨大的差异。 我不认为这是对的,但我不确定这是因为我使用了.eval()错误还是我的数据集出错。 enter image description here

我的数据集创建代码:

filenames = glob.glob(main_dir+keywords)
# filenames = ['test.txt','test2.txt']
length = len(filenames) # num of files
length_samesat = 100 # happen to be this... I designed in propogation
batch_num = 731 # happen to be this...

dataset = tf.data.Dataset.from_tensor_slices(filenames)

dataset = dataset.flat_map(lambda filename: tf.data.TextLineDataset(filename).skip(3))
dataset = dataset.map(lambda string: tf.string_split([string],delimiter=', ').values)
dataset = dataset.map(lambda x: tf.strings.to_number(x))
dataset = dataset.batch(batch_num)
dataset = dataset.map(lambda tensor: tf.reshape(tensor,[batch_num,7]))
dataset = dataset.batch(1).repeat()

然后我用标签数据集压缩数据集,创建NN并运行

dataset_all = tf.data.Dataset.zip((dataset, datalabel))
dataset_all = dataset_all.shuffle(400)
visual_dataset(dataset_all,0,20)
# NN Model
inputs = tf.keras.Input(shape=(731,7,))  # Returns a placeholder tensor

# A layer instance is callable on a tensor, and returns a tensor.
x = tf.keras.layers.Flatten()(inputs)
x = tf.keras.layers.Dense(400, activation='tanh')(x)
x = tf.keras.layers.Dense(400, activation='tanh')(x)
# x = tf.keras.layers.Dense(450, activation='tanh')(x)
# x = tf.keras.layers.Dense(300, activation='tanh')(x)
# x = tf.keras.layers.Dense(450, activation='tanh')(x)
# x = tf.keras.layers.Dense(200, activation='relu')(x)
# x = tf.keras.layers.Dense(100, activation='relu')(x)
predictions = tf.keras.layers.Dense(4, activation='softmax')(x)

# Instantiate the model given inputs and outputs.
model = tf.keras.Model(inputs=inputs, outputs=predictions)

# The compile step specifies the training configuration.
model.compile(optimizer=tf.train.RMSPropOptimizer(0.001),
          loss='categorical_crossentropy',
          metrics=['accuracy'])

# Trains for 5 epochs
model.fit(dataset_all, epochs=5, steps_per_epoch=400)
model.evaluate(dataset_all, steps=400)

谢谢!你知道吗


Tags: 数据modellayerstfbatchallactivationdataset