python - tensorflow 中的单神经元前馈网络

标签 python machine-learning tensorflow feed-forward

我制作了一个前馈单神经元网络。预测打印 0.5,而它应该打印 0.0。我对 tensorflow 很陌生。请帮我。这是我的代码:

"""
O---(w1)-\
          \
O---(w2)-->Sum ---> Sigmoid ---> O  3 inputs and 1 output
          /
O---(w3)-/

          |   Input     | Output
Example 1 | 0   0   1   |   0   
Example 2 | 1   1   1   |   1
Example 3 | 1   0   1   |   1
Exmaple 4 | 0   1   1   |   0

"""

import tensorflow as tf

features = tf.placeholder(tf.float32, [None, 3])
labels = tf.placeholder(tf.float32, [None])

#Random weights
W = tf.Variable([[-0.16595599], [0.44064899], [-0.99977125]], tf.float32)

init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

predict = tf.nn.sigmoid(tf.matmul(features, W))

error = labels - predict

# Training
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(error)

for i in range(10000):
    sess.run(train, feed_dict={features: [[0, 1, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]], labels: [0, 1, 1, 0]})

training_cost = sess.run(error, feed_dict={features: [[0, 1, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]], labels: [0, 1, 1, 0]})
print('Training cost = ', training_cost, 'W = ', sess.run(W))

print(sess.run(predict, feed_dict={features:[[0, 1, 1]]}))

我还仅使用 numpy 手动制作了这个模型,效果很好。

编辑:我尝试了所有类型的成本函数,包括 tf.reduce_mean(predict-labels)**2)

最佳答案

你有两个错误

(a) 你原来的误差函数优化了错误的目标

(b) 您的目标向量已转置 下面的行使其可见 print(sess.run(预测标签, feed_dict={特征: [[0, 1, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]], 标签:[0, 1, 1, 0]})}))

结果是一个 4x4 矩阵。

使用以下代码即可达到想要的结果

import tensorflow as tf

features = tf.placeholder(tf.float32, [None, 3])
labels = tf.placeholder(tf.float32, [None,1])

#Random weights
W = tf.Variable([[10.0], [000.0], [0.200]], tf.float32)
init = tf.initialize_all_variables()
with tf.Session() as sess:
    sess.run(init)

    predict = tf.nn.sigmoid(tf.matmul(features, W))

    print(sess.run(predict, feed_dict={features:[[0, 1, 1]]}))

    lbls= [[0], [1], [1], [0]]
    print(sess.run(predict,
                 feed_dict={features: [[0, 1, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]], labels:lbls}))


    #    error = labels - predict
    error = tf.reduce_mean((labels - predict)**2) 
    # Training
    optimizer = tf.train.GradientDescentOptimizer(10)
    train = optimizer.minimize(error)

    for i in range(100):
        sess.run(train,
        feed_dict={features: [[0, 1, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]], labels: lbls})
        training_cost = sess.run(error,
                             feed_dict={features: [[0, 1, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]],
                                        labels: lbls})
        classe = sess.run((labels-predict),
                             feed_dict={features: [[0, 1, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]],
                                        labels: lbls})
        print('Training cost = ', training_cost, 'W = ', classe)

    print(sess.run(predict,
                 feed_dict={features: [[0, 1, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]]}))

关于python - tensorflow 中的单神经元前馈网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44168353/

相关文章:

python - 如何使用python中的elementtree处理xml文件中格式不正确的字符

python - 如何在 python 2.7 中执行此 CURL 以从 Elasticsearch 中删除文档?

python - 我可以在不破坏线程安全的情况下读取队列中最旧元素的内容吗?

python - 对 sklearn Pipeline + ColumnTransformer 中的列应用多个预处理步骤

python - 在卡住图上使用 optimize_for_inference.py 后使用模型时出错

python - QtOpenGL : No such file or directory opencv python

r - 在 R 中创建空间滞后项

python - 如何解决 ValueError : bad input shape (11, 11)?

tensorflow - 在 TensorFlow 中,如何查看批量归一化参数?

Tensorflow tf.contrib.data.prefetch_to_device() 多个 GPU