【问题标题】:Predicting values using TFLearn neural networks使用 TFLearn 神经网络预测值
【发布时间】:2017-01-09 18:59:12
【问题描述】:

我是 TFLearn 的新手,我正在尝试一个简单的神经网络来预测给定输入数组时的输出数组值。

此代码的实际输入可能是灰度图像的像素值或从灰度图像中提取的特征。因此输入是二维数组格式。输出将是每个像素的预测颜色。

在示例代码中,我使用了两个大小为 9 的随机数组。当输入“t_x”数组时,我需要训练网络来预测“t_y”数组。 代码运行,但是预测很差。

代码改编自 TFLearn 发现的 MNIST 示例here

这是我的代码

from random import randint
import numpy as np
import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression



#input
t_x = [3, 8, 7, 4, 0, 7, 9, 5, 1]
#output
t_y = [9, 5, 1, 4, 7, 9, 7, 3, 6]

x = []
y = []

for i in range(1000):
  x.append(t_x)
  y.append(t_y)

#array of input values
x = np.reshape(x,(-1,3,3,1))

#array of output values
y = np.reshape(y,(-1,9))

network = input_data(shape=[None, 3, 3, 1], name='input')
network = conv_2d(network, 32, 3, activation='relu', regularizer="L2")
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = conv_2d(network, 64, 3, activation='relu', regularizer="L2")
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = fully_connected(network, 128, activation='tanh')
network = dropout(network, 0.8)
network = fully_connected(network, 256, activation='tanh')
network = dropout(network, 0.8)
network = fully_connected(network, 9, activation='softmax')
network = regression(network, optimizer='adam', learning_rate=0.01,
                     loss='categorical_crossentropy', name='target')


# Training
model = tflearn.DNN(network, tensorboard_verbose=0)
model.fit({'input': x}, {'target': y}, n_epoch=20)

pred = model.predict(np.reshape(t_x,(-1,3,3,1)))
print "Prediction :", pred[0]

我假设它与“conv_2d”和“fully_connnected”函数中指定的参数值有关。

我必须设置哪些值才能获得准确的预测?

【问题讨论】:

    标签: python machine-learning tensorflow computer-vision tflearn


    【解决方案1】:

    输出格式

    代码的最后一层 (fully_connected(network, 9, activation='softmax')) 产生 9 个具有 softmax 函数的神经元,即归一化以使它们的总和加起来为 1。这通常是可用的 (并在 MNIST 中使用)用于选择/优化选择 9 个可能输出值之一的函数 - 网络将输出类似 [0.01 0.01 0.01 0.9 0.03 0.01 0.01 0.01 0.01],“预测”第四个值是正确的值,这将与单热目标向量匹配(例如 [0 0 0 1 0 0 0 0 0])。

    不用说,softmax 的输出永远不可能等于 [9, 5, 1, 4, 7, 9, 7, 3, 6],甚至不能接近,因为所有值 softmax 的输出都会加起来等于 1。即使之前的层也不能输出这样的值,因为 tanh 只能产生介于 -1 和 1 之间的值,并且永远不会产生 9。

    如果您想预测 1-9 范围内的 9 个数字,那么您可能需要使用全连接层而不是 softmax,并缩放您的输出,以使预期输出在 0 到 1 的范围内。有更重要的是,但这将是一个好的开始。

    【讨论】:

    • 有没有办法将网络层的输出值限制在一个值范围内,比如 0 - 9?
    • @AkheelKM 有一个标准的 0-1 激活函数并将其乘以 9;这是一种经常使用的机器学习实践,可以将所有变量缩放(或对数缩放)到 0-1 之类的值,然后在输出时重新缩放它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 2018-10-15
    • 2012-05-06
    • 2017-12-28
    • 1970-01-01
    相关资源
    最近更新 更多