2017-04-20 67 views
0

我尝试从GitHub使用MLPClassifier:使用MLPClassifier Tensorflow

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

但其实我不知道怎样才能用我自己的数据使用它。我有尺寸为20000x100的特征矩阵X和尺寸为20000的5个类别的目标矢量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? 

回答

0

如果你看看你的变量batch_x,你会看到它的简单形状[batch_size, 784]的numpy的数组,所以batch_size压扁的图像和batch_y是形状[batch_size, 10]的数组,所以在batch_x

所以,如果你想用这个模型中使用自己的数据每个图像1独热编码的标签,你必须:

  • 或是在同一WA已格式化的数据Y([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) 
+0

谢谢,但我仍然有问题与我y。当我用n_classes = 5定义y = tf.placeholder(“float”,[None,n_classes])时,那么我的y的维度是?x5而不是?x1?另外我不确定total_batch?我认为这只是一个例子,对吧? – HansPeterLoft

+0

我用y解决了这个问题,它被表示为一个矩阵。 – HansPeterLoft

+0

我编辑了我的答案以清除 – ted

0

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

+0

在我的示例中,batch_size究竟是什么?从我的理解应该是一个特点,对吧?所以它应该是平等的? – HansPeterLoft

+0

批处理大小是执行参数更新之前由神经网络处理的事件的数量。如果您选择较高的批量大小,则训练将不太容易出现数据集的波动,但可能会更慢一些。你可能想看看http://sebastianruder.com/optimizing-gradient-descent/,特别是有关梯度下降变体的章节。在那里你会找到一个很好的解释是什么小批量。 – ml4294