使用Tensorflow Datasets class + Keras在训练准确性和评估准确性之间存在巨大差异

时间:2019-03-14 02:36:31

标签: python tensorflow keras neural-network

我根据自己的数据创建了一个数据集,该数据集的格式为(特征,标签)。要素的尺寸为[?,731,7](其中?应该为400),相应的标签的尺寸为[4,],如我的数据集所示。每个[731,7]样本都对应一个[0,1,0,0]之类的4个元素的数组。

enter image description here

一些示例数据: Sampledata1 Sampledata2

构建简单的多层神经网络后,训练过程如下。但是,当我使用相同的数据集进行验证(只是检查算法是否有效)时,实际上却有很大的不同。 我认为这是不对的,但是我不确定是否会发生这种情况,因为我错误地使用了.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)

谢谢!

0 个答案:

没有答案