Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 逻辑回归Tensorflow的损失函数-公式和Tensorflow函数结果不匹配_Python_Tensorflow_Machine Learning_Deep Learning - Fatal编程技术网

Python 逻辑回归Tensorflow的损失函数-公式和Tensorflow函数结果不匹配

Python 逻辑回归Tensorflow的损失函数-公式和Tensorflow函数结果不匹配,python,tensorflow,machine-learning,deep-learning,Python,Tensorflow,Machine Learning,Deep Learning,我尝试在Tensorflow中使用两个成本函数进行逻辑回归: dim = train_X.shape[1] X = tf.placeholder(tf.float32, shape=(None, dim)) y = tf.placeholder(tf.float32, shape=(None,1)) W = tf.Variable(tf.zeros(shape=(dim,1))) b = tf.Variable(tf.zeros([1])) y_pred = tf.nn.sigmoid(tf

我尝试在Tensorflow中使用两个成本函数进行逻辑回归:

dim = train_X.shape[1]
X = tf.placeholder(tf.float32, shape=(None, dim))
y = tf.placeholder(tf.float32, shape=(None,1))

W = tf.Variable(tf.zeros(shape=(dim,1)))
b = tf.Variable(tf.zeros([1]))

y_pred = tf.nn.sigmoid(tf.add(tf.matmul(X,W), b)) # using matmul for matrix multiplication. x.shape(768,8) w.shape(8,1)

cost = tf.reduce_mean(tf.add(-tf.multiply(y, tf.log(y_pred)), -tf.multiply(1-y, tf.log(1-y_pred))))
cost2 = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=y_pred, labels=y))

train = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
train2 = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost2)
这两个成本函数给出了不同的结果,尽管我的理解是它们应该给出相同的结果

session = tf.Session()
session.run(init)
print(session.run(cost, feed_dict={X:train_X, y:train_y}))
print(session.run(cost2, feed_dict={X:train_X, y:train_y}))

有人能解释一下为什么会发生这种情况,以及我应该做什么改变才能让他们显示相同的结果吗?

cost2变量的预测变量应该是:


y_pred2 = tf.add(tf.matmul(X,W2), b2)


由于
tf.nn.sigmoid\u cross\u entropy\u与\u logits
已经合并了sigmoid函数。

您的结果有多大不同?他们的价值观有什么不同?