Python 使用Tensorflow数据集class+;凯拉斯

Python 使用Tensorflow数据集class+;凯拉斯,python,tensorflow,keras,neural-network,Python,Tensorflow,Keras,Neural Network,我根据数据创建了一个数据集,该数据集的形式为(要素、标签)。特征的维度是[?,731,7](其中?应该是400),相应标签的维度是[4,],如我的数据集中所示。每个[731,7]样本对应一个4元素数组,如[0,1,0,0] 一些示例数据: 在建立一个简单的多层神经网络后,训练过程如下。但当我使用相同的数据集进行验证(只是检查算法是否有效)时,我实际上得到了巨大的差异。 我认为这是不对的,但我不确定这是因为我使用了.eval()错误还是因为我的数据集错误。 我的数据集创建代码: filena

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

一些示例数据:

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

我的数据集创建代码:

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)
谢谢