【问题标题】:Unexpected prediction values, tflearn意外的预测值,tflearn
【发布时间】:2017-02-25 19:36:51
【问题描述】:

使用 tflearn,我正在尝试对图像进行分类。

我的代码是

import tflearn
dataset_file = 'my_dataset.txt'

X, Y = tflearn.data_utils.image_preloader(dataset_file, image_shape=(128, 128),categorical_labels=True, mode='file', grayscale=True,  normalize=True)

net = tflearn.input_data(shape=(128,128))
net = tflearn.fully_connected(net, 2, activation='linear')
net = tflearn.regression(net, optimizer='adam', loss='categorical_crossentropy')

model = tflearn.DNN(net, tensorboard_verbose=3)
model.fit(X, Y)

print(model.predict(X))

数据集文件如下:

1.jpg 1
2.jpg 1
3.jpg 1
4.jpg 1
5.jpg 0
6.jpg 0
7.jpg 0

...其中 1 和 0 是图像类别。

不过,我的预测并不如我所愿:

[[2.9711711406707764, -3.049826145172119], [9.435855865478516, -11.466367721557617], [-3.7774205207824707, -4.090094089508057], [-7.006657600402832, -3.4418578147888184], [-18.654706954956055, -0.9354709982872009], [-17.237045288085938, -3.1278553009033203], [-18.066274642944336, -1.6454157829284668]]

我希望看到图像类型 1 或 0 的匹配项。 只是 tflearn 的初学者,不知道该怎么做。

【问题讨论】:

  • 在tflearn.regression 中你试过设置to_one_hot=True, n_classes=2吗?
  • 这给了我 ValueError: Cannot feed value of shape (7, 2) for Tensor 'TargetsData/Y:0', which has shape '(?,)'

标签: python-3.x tensorflow


【解决方案1】:

尝试更改您的fully_connected 层以使用'softmax' 激活

net = tflearn.fully_connected(net, 2, activation='softmax')

现在,您应该得到以下形式的输出:

predictions = [[0.999962568283081, 3.739727253559977e-05],
               [0.999962568283081, 3.739727253559977e-05],
               ...]

这里predictions[i][j] 是测试集中ith 元素具有类j 的概率。您会注意到所有i 的sum(predictions[i]) == 1.0

【讨论】:

  • 预测总和[i] 不能为 1,因为前 2 个 i 约为 0.999。
  • sum([0.999962568283081, 3.739727253559977e-05]) ~ 1.0 是我的意思。 predictions[i] 是每个类的概率列表。
猜你喜欢
  • 2017-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多