gpt4 book ai didi

tensorflow - tensorflow 中的基本神经网络

转载 作者:行者123 更新时间:2023-12-02 03:03:14 26 4
gpt4 key购买 nike

我一直在尝试在 tensorflow 中实现一个基本的神经网络,输入只是 (x,y,z) 中 1/0 的随机数据,但是我希望我的网络在 x = 1 时输出 1 并输出 0否则。

这是我的网络代码

import tensorflow as tf
import numpy as np

x_data = np.array([[0,0,1],
[0,1,1],
[1,0,0],
[0,1,0],
[1,1,1],
[0,1,1],
[1,1,1]])

x_test = np.array([[1,1,1], [0,1,0], [0,0,0]])
y_data = np.array([0,0,1,0,1,0,1])


iters = 1000
learning_rate = 0.1
weights = {
'w1': tf.Variable(tf.random_normal([3, 5])),
'w2': tf.Variable(tf.random_normal([5, 1])),
}
bias = {
'b1': tf.Variable(tf.random_normal([5])),
'b2': tf.Variable(tf.random_normal([1])),
}

def predict(x, weights, bias):
l1 = tf.add(tf.matmul(x, weights['w1']), bias['b1'])
l1 = tf.nn.sigmoid(l1)
out = tf.add(tf.matmul(l1, weights['w2']), bias['b2'])
return out


x = tf.placeholder(tf.float32, shape=(None,3))
y = tf.placeholder(tf.float32, shape=(None))

pred = predict(x, weights, bias)

cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=pred, labels=y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)

init = tf.global_variables_initializer()

# graph
with tf.Session() as sess:
sess.run(init)

for i in range(0, iters):
_, c = sess.run([optimizer, cost], feed_dict={x: x_data, y: y_data})
if i % 100 == 0:
print("cost: " + str(c))

print(sess.run(weights['w1']))
print(sess.run(pred, feed_dict={x: x_test}))

哪个输出
[-0.37119362]
[-0.23264697]
[-0.14701667]

但是我的测试数据应该输出 [1,0,0],我真的不确定这里有什么问题。我尝试过使用超参数并查看 stackoverflow。我也尝试使用 softmax_cross_entropy 作为成本函数,尽管它给我一个错误,说 logits 与标签的形状不同。

有谁知道为什么这不输出我所期望的?

最佳答案

首先,您需要在输出之前通过一个激活函数(即 tf.nn.sigmoid )。

确保 tf.nn.sigmoid_cross_entropy_with_logits获取 logits(在 sigmoid 激活之前)。

您的输入也有形状问题 y_data那是 (7)而不是 (7, 1)
这是您的代码的工作版本:

import tensorflow as tf
import numpy as np

x_data = np.array([[0,0,1],
[0,1,1],
[1,0,0],
[0,1,0],
[1,1,1],
[0,1,1],
[1,1,1]])

x_test = np.array([[1,1,1], [0,1,0], [0,0,0]])
y_data = np.array([[0],[0],[1],[0],[1],[0],[1]])


iters = 1000
learning_rate = 0.1
weights = {
'w1': tf.Variable(tf.random_normal([3, 5])),
'w2': tf.Variable(tf.random_normal([5, 1])),
}
bias = {
'b1': tf.Variable(tf.random_normal([5])),
'b2': tf.Variable(tf.random_normal([1])),
}

def predict(x, weights, bias):
l1 = tf.add(tf.matmul(x, weights['w1']), bias['b1'])
l1 = tf.nn.sigmoid(l1)
out = tf.add(tf.matmul(l1, weights['w2']), bias['b2'])
return out


x = tf.placeholder(tf.float32, shape=(None,3))
y = tf.placeholder(tf.float32, shape=(None,1))

pred = predict(x, weights, bias)
pred_postactivation = tf.nn.sigmoid(pred)

cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=pred, labels=y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)

init = tf.global_variables_initializer()

# graph
with tf.Session() as sess:
sess.run(init)

for i in range(0, iters):
_, c = sess.run([optimizer, cost], feed_dict={x: x_data, y: y_data})
if i % 100 == 0:
print("cost: " + str(c))

print(sess.run(weights['w1']))
print(sess.run(pred_postactivation, feed_dict={x: x_test}))

哪些输出:
cost: 1.23954
cost: 0.583582
cost: 0.455403
cost: 0.327644
cost: 0.230051
cost: 0.165296
cost: 0.123712
cost: 0.0962315
cost: 0.0772587
cost: 0.0636141
[[ 0.94488049 0.78105074 0.81608331 1.75763154 -4.47565413]
[-2.61545444 0.26020721 0.151407 1.33066297 1.00578034]
[-1.2027328 0.05413296 -0.13530347 -0.39841765 0.16014417]]
[[ 0.92521071]
[ 0.05481482]
[ 0.07227208]]

关于tensorflow - tensorflow 中的基本神经网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44484366/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com