【问题标题】:What is the difference between sigmoid functions inplemented in tensorflow and that from scratch在 tensorflow 中实现的 sigmoid 函数与从头开始实现的函数有什么区别
【发布时间】:2019-10-05 17:56:08
【问题描述】:

数值稳定的sigmoid函数的实现和TensorFlow中实现的有什么区别?

在实现sigmoid()tf.nn.sigmoid()(或tf.sigmoid())这两个函数时,我得到了不同的结果。第一个给出了nan 和非常差的准确度(大约 0.93%),而第二个给出了非常好的准确度(大约 99.99%)。

数值稳定的 sigmoid 函数 sigmoid() 由下式给出:

def sigmoid(z):
    return tf.where(z >= 0, 1 / (1 + tf.exp(-z)), tf.exp(z) / (1 + tf.exp(z)))

我希望这两种方法都能获得相同的结果(准确度),无论是由 TensorFlow 实现的还是从头创建的 sigmoid()

注意:我用同一型号测试了tf.sigmoidsigmoid()这两个函数。

【问题讨论】:

  • 什么输入给你 NaN?
  • 谢谢,@DavisHerring。 i epochs 之后的测试成本。当epoch = 11他测试cost = NaN时可能是我的情况
  • 要准确查看 tf.nn.sigmoid 方法中发生的情况,如果对其进行了一些额外的修改,您可以打开模块并查看源代码。
  • 谢谢,@ShubhamPanchal。不幸的是,我找不到他们从头开始定义sigmoid 函数的位置。文档字符串y = 1 / (1 + exp(-x)) 中提到了它,但我尝试实现它但仍然没有得到预期的分数。

标签: python python-3.x tensorflow neural-network


【解决方案1】:

我尝试使用以下代码和简单的 Iris 数据集重现您的案例。 l 的值是使用tf.sigmoid 计算的costl2 的值是使用自定义sigmoid 函数计算的cost (cost2)ll2 的值几乎相同为了我。

如果您可以提供代码和数据(如果可以共享),我们可以对此进行更深入的研究。

import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn import preprocessing
from sklearn import model_selection
import sys

iris_data = pd.read_csv('iris_species/Iris.csv',header=0,delimiter = ',')

data_set_y = pd.DataFrame(iris_data['Species'])
data_set_X = iris_data.drop(['Species'],axis=1)
num_samples = iris_data.shape[0]
num_features = iris_data.shape[1]
num_labels = 1

X = tf.placeholder('float',[None,4])
y = tf.placeholder('float',[None,1])

W = tf.Variable(tf.zeros([4,2]),dtype=tf.float32)
b = tf.Variable(tf.zeros([1]),dtype=tf.float32)

train_X,test_X,train_y,test_y = model_selection.train_test_split(data_set_X,data_set_y,random_state=0)

train_y = np.reshape(train_y,(-1,1))

prediction = tf.add(tf.matmul(X,W),b)
cost = tf.sigmoid(prediction)
optimizer = tf.train.GradientDescentOptimizer(0.001).minimize(cost)
num_epochs = 1000

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for epoch in range(num_epochs):
        _,l = sess.run([optimizer,cost],feed_dict = {X: train_X, y: train_y})
        if epoch % 50 == 0:
            #print (type(l))
            #print (l.shape)
            print (l)


def sigmoid(z):
    return tf.where(z >= 0, 1 / (1 + tf.exp(-z)), tf.exp(z) / (1 + tf.exp(z)))

prediction = tf.add(tf.matmul(X,W),b)
cost2 = sigmoid(prediction)
optimizer2 = tf.train.GradientDescentOptimizer(0.001).minimize(cost2)
num_epochs = 1000


print ('Shape of train_y is: ',train_y.shape)


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for epoch in range(num_epochs):
        _,l2 = sess.run([optimizer2,cost2],feed_dict = {X: train_X, y: train_y})
        if epoch % 50 == 0:
            #print (type(l))
            #print (l.shape)
            print (l2)

【讨论】:

    猜你喜欢
    • 2019-06-12
    • 2018-10-20
    • 2017-07-28
    • 1970-01-01
    • 2020-08-30
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    相关资源
    最近更新 更多