【发布时间】:2020-05-11 23:15:05
【问题描述】:
我正在学习在 R 中求解方程,并且我有兴趣通过 TensorFlow 来完成它(我已经知道如何使用 GA 和模拟退火来完成它)。 我正在 R 中为这个执行 Y = X + Z 并求解 Z 的 Python 程序构建等效代码(在 blog 中找到它)。我花了几个小时试图找到许多函数变体,在互联网博客和 CRAN 文档中搜索它们。
import tensorflow as tf
x = tf.constant([[1., 2.]])
y = tf.constant([[12., 4.]])
Z = tf.Variable(tf.zeros([1, 2]))
yy = tf.add(x, Z)
deviation = tf.square(y - yy)
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(deviation)
init = tf.global_variables_initializer()
sess = tf.Session();
sess.run(init)
for i in range(5000):
sess.run(train_step)
print(sess.run(Z))
下面是我开发到现在的R代码,在这个过程中尝试了很多变种的程序:
library(tensorflow)
x = tf$constant(c(1.,2.))
y = tf$constant(c(12,4))
Z = tf$Variable(tf$zeros(2,1))
yy = tf$add(x, Z)
deviation = tf$square(y - yy)
train_step = tf$optimizers$Adam(0.01)
现在我无法将 minimize() 函数添加到 train_step,因为我收到一个错误,指出它不是优化器的属性。我尝试过使用 tensorflow 和 keras 中的评估、编译和其他功能,但无法弄清楚如何做到这一点。
【问题讨论】:
标签: python r tensorflow keras rstudio