猿问

通过哈雷方法使用张量流求四次多项式的根

我刚刚开始学习 tensorflow,基本上我正在学习使用 tensorflow 进行各种数值计算,而不是直接在 ML 中使用它。我正在 Google Cloud Platform 上执行此操作,在那里我遇到了这个问题并被卡住了。

我正在使用惰性求值,我可以使用占位符在张量流图中创建 a0、a1、a2...、a4 的实例,也可以写出函数。但是如何使用 tensorflow 进行初始猜测?而且,即使我得到 x0 的值,我如何使用tf.while_loop应用循环, 我浏览了它的文档和这篇文章,但我仍然不知道如何进行。我试图找到具有类似问题或内容的帖子,但找不到使用 tensorflow 的帖子。如果我能获得洞察力或使用内在 tensorflow 函数和命令的方法,那就太好了 :) 提前致谢!


小唯快跑啊
浏览 154回答 2
2回答

GCT1015

当我从这里执行第一个示例时,我得到了这些值。请注意,方程式是不同的。1.49999999696126451.4111888803781981.41421320166699951.4142135623730898但这似乎是一个很好的例子。import tensorflow as tfh = tf.constant(.00000001, dtype='float64')eps = tf.constant(.000001, dtype='float64')b = tf.constant(2.0, tf.float64)def f(x):&nbsp; &nbsp; return tf.subtract( tf.multiply(x , x ) , 2. )def fp(x):&nbsp; &nbsp; return&nbsp; tf.divide( tf.subtract( f(tf.add(x, h)) ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f(x)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;h&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)def fpp(x):&nbsp; &nbsp; return tf.divide( tf.subtract( fp( tf.add(x , h)) ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fp(x)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;h&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)def cond(i, x_new, x_prev):&nbsp; &nbsp; return tf.logical_and( i < 5,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tf.less_equal( tf.abs( tf.cast(tf.subtract( x_new ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x_prev&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;),dtype='float64')),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; eps&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )def body( i, x_new, x_prev ):&nbsp; &nbsp; fx = f( x_prev )&nbsp; &nbsp; fpx = fp( x_prev )&nbsp; &nbsp; x_new = tf.subtract( x_prev ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tf.divide( b * fx * fpx&nbsp; ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tf.subtract(b * fpx * fpx,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fx * fpp( x_prev )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)&nbsp; &nbsp; xnew = tf.Print(x_new, [x_new], message="The root is : ")&nbsp; &nbsp; with tf.control_dependencies([x_new,xnew]):&nbsp; &nbsp; &nbsp; &nbsp; x_prev = tf.identity(xnew)&nbsp; &nbsp; return [i + 1, xnew, x_prev ]sess = tf.Session()sess.run(tf.global_variables_initializer())print( sess.run(tf.while_loop(cond, body, [1, b - fpp(b), b])) )根是:[1.4999999969612645]根是:[1.411188880378198]根是:[1.4142132016669995]根是:[1.4142135623730898][5,1.4142135623730898,1.4142135623730898]
随时随地看视频慕课网APP

相关分类

Python
我要回答