【问题标题】:TensorFlow: while learning parameters getting NANTensorFlow:在学习参数的同时获得 NAN
【发布时间】:2017-07-27 10:19:28
【问题描述】:

我正在尝试下面的简单线性 reg 梯度下降示例是我的代码...

def gardientDecent(features,target):
     alpha = tf.constant(0.001,dtype=tf.float64)
     length = tf.cast(tf.size(target),dtype=tf.float64)
     feature_one = features[:,0]
     features_Val = tf.transpose(features)

     dummy_theta = tf.zeros([2,1],dtype=tf.float64)

     dot_product = tf.multiply(features_Val, dummy_theta)
     diff = tf.subtract(dot_product, target)
     diff_one = tf.multiply(diff, feature_one)

     theta1 = tf.reduce_sum(diff)
     theta2 = tf.reduce_sum(diff_one)

     final1 = tf.div(theta1, length)
     final2 = tf.div(theta2, length)

     t1 = tf.subtract(theta1, tf.multiply(alpha, final1))
     t2 = tf.subtract(theta2, tf.multiply(alpha, final2))

     newArray = np.array([[0],[0]],dtype=np.float64)
     for i in range(1000):
            temp1,temp2 = vas.run([t1,t2],{dummy_theta:newArray})
            print i ,temp1,temp2
            newArray = np.array([[temp1],[temp2]],dtype=np.float64)
     print vas.run(tf.cast(newArray,dtype=tf.float64))

但是在经过几步之后运行时,我得到了这样的 Nan 值...

请帮助我......我是 ML 和 Tensor 流的新手......提前 TY

【问题讨论】:

  • 请不要张贴代码图片,它们不能被复制粘贴并且难以阅读。编辑您的帖子。
  • @gionni 现已修改

标签: python arrays machine-learning tensorflow


【解决方案1】:

这似乎是您从参数中减去的损失,而不是该损失相对于 theta 的实际梯度。您应该为theta2 调用相同的grad1 = tf.gradients(tf.reduce_sum(diff), theta1)(使用diff_one),然后从theta1theta2 中减去它(使用alpha 权重):t1 = tf.subtract(theta1, tf.multiply(alpha, grad1))theta2 相同

【讨论】:

    猜你喜欢
    • 2018-04-28
    • 2022-01-10
    • 2020-09-12
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    • 2017-06-22
    • 1970-01-01
    相关资源
    最近更新 更多