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提前
答案 0 :(得分:0)
似乎是你从参数中减去的损失,而不是相对于θ的实际的损失梯度。您应该为grad1 = tf.gradients(tf.reduce_sum(diff), theta1)
(theta2
)调用diff_one
相同的内容,然后从theta1
和theta2
中减去(带有alpha权重):{{1}和t1 = tf.subtract(theta1, tf.multiply(alpha, grad1))