【问题标题】:TensorFlow: Implementing Single layer perceptron / Multi layer perceptron using own data setTensorFlow:使用自己的数据集实现单层感知器/多层感知器
【发布时间】:2023-03-03 11:31:01
【问题描述】:

我是 TensorFlow 的新手。我正在寻找有关使用 tensorflow 实现多层感知器的示例,但我得到 仅在 MNIST 图像上的示例 数据集,除了 MNIST,我是否可以使用相同的优化和成本函数来构建神经网络模型并训练数字格式的数据,意味着,我可以使用 tensorflow 训练自己的数字数据集吗?强>

是否有任何训练新数据集的示例?

【问题讨论】:

    标签: python tensorflow neural-network perceptron tensorflow-datasets


    【解决方案1】:

    修改 TensorFlow multilayer perceptron example 很容易。您需要在此处指定输入和输出大小

    n_input = # Input size (MNIST uses 28*28 size images = 784 pixels)
    n_classes = # Output size (MNIST classifies into 0-9 digits)
    

    最后,您必须更改训练周期代码才能使用您的数据集。由于我对您的数据集一无所知,因此无法为您提供更多帮助。

    【讨论】:

    【解决方案2】:

    我终于明白了。使用带有 tensorflow、numpy、matplotlib 包的单层感知器构建、训练和最小化人工神经网络的成本/损失。 数据以数组的形式使用而不是MNIST。 这是代码。

    import tensorflow as tf
    import numpy as np
    import matplotlib.pyplot as plt
    learning_rate = 0.0008
    training_epochs = 2000
    display_step = 50
    # taking input as array from numpy package and converting it into tensor
    inputX = np.array([[  2,   3],
                      [  1,   3]])
    inputY = np.array([[  2,   3],
                      [  1,   3]])
    x = tf.placeholder(tf.float32, [None, 2])
    y_ = tf.placeholder(tf.float32, [None, 2])
    
    W = tf.Variable([[0.0,0.0],[0.0,0.0]])
    b = tf.Variable([0.0,0.0])
    
    layer1 = tf.add(tf.matmul(x, W), b)
    y = tf.nn.softmax(layer1)
    
    cost = tf.reduce_sum(tf.pow(y_-y,2))
    
    optimizer =tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)
    
    init = tf.global_variables_initializer()
    sess = tf.Session()
    sess.run(init)
    
    avg_set = []
    epoch_set = []
    
    for i in range(training_epochs):
       sess.run(optimizer, feed_dict = {x: inputX, y_:inputY})
    
       #log training
       if i % display_step == 0:
           cc = sess.run(cost, feed_dict = {x: inputX, y_:inputY})
           #check what it thinks when you give it the input data
           print(sess.run(y, feed_dict = {x:inputX}))
    
    
           print("Training step:", '%04d' % (i), "cost=", "{:.9f}".format(cc))
           avg_set.append(cc)
           epoch_set.append(i + 1)
    
    print("Optimization Finished!")
    training_cost = sess.run(cost, feed_dict = {x: inputX, y_: inputY})
    print("Training cost = ", training_cost, "\nW=", sess.run(W),
          "\nb=", sess.run(b))
    plt.plot(epoch_set,avg_set,'o',label = 'SLP Training phase')
    plt.ylabel('cost')
    plt.xlabel('epochs')
    plt.legend()
    plt.show()
    

    稍后通过添加隐藏层也可以使用多层感知器实现

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-14
      • 2012-04-30
      • 2017-01-06
      • 1970-01-01
      • 2010-10-24
      • 1970-01-01
      • 2019-04-13
      相关资源
      最近更新 更多