【问题标题】:Use MLPClassifier Tensorflow使用 MLPClassifier TensorFlow
【发布时间】:2017-09-16 19:13:45
【问题描述】:

我尝试使用来自 github 的 MLPClassifier:

https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/3_NeuralNetworks/multilayer_perceptron.py

但我实际上不知道如何将它与我自己的数据一起使用。我有一个维度为 20000x100 的特征矩阵 X 和一个 5 类大小为 20000 的目标向量 y。

X 和 y 保存在一个 numpy 数组中。我感到困惑的是:

x = tf.placeholder("float", [None, n_input]) #n_input is 100 here, right?
y = tf.placeholder("float", [None, n_classes])


total_batch = int(mnist.train.num_examples/batch_size) #What is that for my data?


batch_x, batch_y = mnist.train.next_batch(batch_size)#what are these?

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    除了@ted 的回答,你还得修改total_batch 的计算。 total_batch 是您的网络将生成的批次数。假设 X 包含您的数据,您必须将 int(mnist.train.num_examples/batch_size) 替换为 int(20000/batch_size)int(X.shape[0]/batch_size)。您可以在那里选择批量大小,例如 200。

    【讨论】:

    • 在我的示例中,batch_size 到底是什么?根据我的理解,它应该是一个功能,对吧?所以应该等于一?
    • 批量大小是神经网络在执行参数更新之前处理的事件数。如果您选择更高的批量大小,训练将不太容易受到数据集波动的影响,但收敛速度可能会更慢一些。你可能想看看sebastianruder.com/optimizing-gradient-descent,尤其是关于梯度下降变体的部分。在那里你会找到关于什么是小批量的很好的解释。
    【解决方案2】:

    如果您查看变量batch_x,您会发现它只是一个形状为[batch_size, 784] 的numpy 数组,所以batch_size 是扁平图像,batch_y 是一个形状为[batch_size, 10] 的数组,所以1 个单热batch_x中每个图像的编码标签

    所以如果你想在这个模型中使用你自己的数据,你必须:

    • 以相同的方式格式化您的数据([batch_size, 784][batch_size, 10]
    • 或更改您的占位符 xy,以便它们可以采用您自己的数据形式

    在您的情况下,只需使用以下代码更改代码:

    n_input = 100
    n_classes = 5
    total_batch = int(20000/batch_size)
    

    此外,最好不要使用上述数字,而是从您的数据中获取这些值:

    n_input = your_x_data.shape[1]
    n_classes = your_y_data.shape[1]
    # or n_classes = your_y_data.max(axis=1) 
    # if your y data array is not already one-hot encoded
    total_batch = int(your_x_data.shape[0]/batch_size)
    

    【讨论】:

    • 谢谢,但我的 y 还是有问题。当我用 n_classes=5 定义 y = tf.placeholder("float", [None, n_classes]) 时,我的 y 的维度是 ?x5 而不是 ?x1?我也不确定total_batch?我认为这只是我的情况,对吧?
    • 我用y解决了这个问题,它被表示为一个矩阵。
    猜你喜欢
    • 2018-04-17
    • 2019-09-02
    • 2016-06-15
    • 1970-01-01
    • 2020-08-13
    • 2017-03-15
    • 2018-01-27
    • 2016-03-05
    • 2019-07-26
    相关资源
    最近更新 更多