gpt4 book ai didi

python - Tensorflow:我的虹膜分类器(没有估计器)无法工作

转载 作者:行者123 更新时间:2023-12-01 01:56:17 24 4
gpt4 key购买 nike

我正在学习 tensorflow ,作为练习,我正在尝试为虹膜数据集训练分类器。网络架构我暂定取自the official tensorflow's iris example我正在尝试使用图层 API 重新创建它:它应该是一个具有三个大小分别为 10、20 和 10 的隐藏层的神经网络;之后,由于虹膜是一个 3 路分类问题,我将最后一个大小为 3 的密集层与 softmax 激活一起放置。这是代码:

def parse_csv(line):
data = tf.decode_csv(line, record_defaults=[[]] * 5)
return tf.stack(data[:4]), data[4]

trn_data, trn_targ = tf.data.TextLineDataset("../data/train.csv").map(parse_csv).shuffle(200).repeat().batch(32).make_one_shot_iterator().get_next()
evl_data, evl_targ = tf.data.TextLineDataset("../data/test.csv").map(parse_csv).shuffle(200).repeat().batch(32).make_one_shot_iterator().get_next()

x = tf.placeholder(tf.float32, [None, 4], name="input")
y_ = tf.placeholder(tf.int64, [None, ], name="target")

# definition of the neural network
a1 = tf.layers.dense(x, 10, activation=tf.nn.relu)
a2 = tf.layers.dense(a1, 20, activation=tf.nn.relu)
a3 = tf.layers.dense(a2, 10, activation=tf.nn.relu)
y = tf.layers.dense(a3, 3, activation=tf.nn.softmax)

# training step
loss = tf.losses.softmax_cross_entropy(tf.one_hot(y_, 3), y)
train_op = tf.train.AdamOptimizer(0.001).minimize(loss, global_step=tf.train.get_or_create_global_step())

# evaluation of the results
predictions = tf.argmax(y, 1, name="predictions")
correct_prediction = tf.equal(y_, predictions) # boolean tensor that says if we did good
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
cm = tf.confusion_matrix(y_, predictions)

with tf.Session() as sess:
tf.global_variables_initializer().run()

for n in range(401):
if n % 50 == 0: # see what's going on every 50 steps
feed_train = {x: sess.run(evl_data), y_: sess.run(evl_targ)}
acc, c = sess.run([accuracy, cm], feed_dict=feed_train)
print(c, acc)
else: # train
feed_eval = {x: sess.run(trn_data), y_: sess.run(trn_targ)}
_ = sess.run(train_op, feed_dict=feed_eval)

然而,准确率确实很差,并且与网络没有学习任何内容的假设大致相符(徘徊在 0.33 左右)。为了更好地理解,我在不同步骤和不同运行中打印了混淆矩阵:它们通常表明网络对所有输入预测相同的结果,无论其特征或标签如何。使用张量板对网络参数进行可视化显示,偏差随时间变化,但权重(或内核,如文档中定义的那样)不会变化。

我传递数据或进行训练或其他什么方式肯定有错误,但我找不到它。你能帮忙吗?

最佳答案

解决方案是使用可供给迭代器,如 https://www.tensorflow.org/programmers_guide/datasets#creating_an_iterator 中所述。 。问题中发布的代码应更正为:

def parse_csv(line):
data = tf.decode_csv(line, record_defaults=[[]] * 5)
return tf.stack(data[:4]), data[4]

# notice that there is no `get_next()` at the end
training_iterator = tf.data.TextLineDataset("../data/train.csv").map(parse_csv).shuffle(200).repeat().batch(
32).make_one_shot_iterator()
validation_iterator = tf.data.TextLineDataset("../data/test.csv").map(parse_csv).shuffle(200).repeat().batch(
32).make_one_shot_iterator()

handle = tf.placeholder(tf.string, shape=[])
x, y_ = tf.data.Iterator.from_string_handle(handle, training_iterator.output_types, training_iterator.output_shapes).get_next()

## [...more code...]

with tf.Session() as sess:
tf.global_variables_initializer().run()

training_handle = sess.run(training_iterator.string_handle())
validation_handle = sess.run(validation_iterator.string_handle())

for n in range(401):
if n % 50 == 0: # see what's going on every 50 steps
acc, c = sess.run([accuracy, cm], feed_dict={handle: validation_handle})
print(c, acc)
else: # train
_ = sess.run(train_op, feed_dict={handle: training_handle})

关于python - Tensorflow:我的虹膜分类器(没有估计器)无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50152572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com