python - tensorflow : result of training data(using sigmoid) came out reversely

标签 python python-3.x tensorflow sigmoid

我尝试使用“梯度下降算法”训练数据以最小化成本值,

奇怪的是,根据步数的不同,结果也不同。

下面是我的训练代码:

import tensorflow as tf
X = tf.placeholder(tf.float32, shape=[None, 2], name="X")
Y = tf.placeholder(tf.float32, shape=[None, 1], name="Y")
W = tf.Variable(tf.random_normal([2, 1]), name="weight")
b = tf.Variable(tf.random_normal([1]), name="bias")

hypo = tf.sigmoid(tf.matmul(X, W) +b)

cost = -tf.reduce_mean(Y*(tf.log*(hypo)) + (1-Y)*(tf.log(1-hypo)))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-3)
train = optimizer.minimize(cost)

#### Saving model
SAVER_DIR = "model"
saver = tf.train.Saver()
checkpoint_path = os.path.join(SAVER_DIR, "model")
ckpt = tf.train.get_checkpoint_state(SAVER_DIR)

sess = tf.Session()
sess.run(tf.global_variables_initializer())
for step in range(4201):
    cost_val, hy_val, _ = sess.run([cost, hypo, train], feed_dict={X:x_data, Y=y_data})

saver.save(sess, checkpoint_path, global_step=step)

并恢复模型:

saver = tf.train.import_meta_graph('./model/model-4200.meta')
saver.restore(sess,'./model/model-4200')

result = sess.run(hypo, feed_dict={X: x_data_test})

fig, ax = plt.subplots()
ax.plot(Julian_test,y_data_test,'ro-') # Correct answer. all items are one of the two:0 or 1.
ax.plot(Julian_test,result,'bo-')      # Result of training. Predict answer within
plt.show() #                            sigmoid function, so all items are in range of 0 ~ 1.

plot where step = 4200

如图所示,sigmoid的结果是相反的。

但是,当我将步数更改为 5000 时,(在上面的代码中,我只更改了步数。)

结果正确。

plot where step = 5000

我不明白为什么它会有所不同。我错过了什么?确实需要帮助!

最佳答案

简单来说,通过增加步骤,您可以允许 tensorflow 代码/模型多次查看数据,从而使其能够了解有关数据的更多见解。并概括其表示。

EG 假设您为模型指定了 2000 步,在 2000 步结束时,它找到了最小值,并且模型停在那里。但是,如果模型到目前为止找到的最小成本不是全局最小成本怎么办,我们不能说,因为我们将其限制为 2000 步。假设您将步数增加到 20000,模型现在会找到另一个最小值,从而提供更准确的结果。

但是您需要确保您的模型不会过度拟合,即在训练数据上提供准确性,但在验证集上不提供准确性。 (因此请确保不要将 num 步骤增加太多)。

关于python - tensorflow : result of training data(using sigmoid) came out reversely,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54998338/

相关文章:

python - 传递数学方程作为参数(numpy)

python - 使用 ValueError : Layer sequential expects 1 inputs, 构建自定义联合平均过程,但它收到 3 个输入张量

Python:在静态上下文中嵌套一行for循环时出错

python - 在 Pandas 中格式化 DataFrame - 堆叠列

python - django如何异步执行函数,即将任务移交给子进程并返回响应

python - tf.transform : add preprocessing to Keras model?

python - 进程结束,退出代码为 -1073740791 (0xC0000409) STATUS_STACK_BUFFER_OVERRUN

python - 如何水平连接 2 个数据框(按行和按列)?

python - 在tensorflow/models/research(不是/object_dectio)中编译.proto

python - 如何将列表绑定(bind)到 SQLAlchemy 中自定义查询中的参数?