c++ - 向 ApplyGradientDescent 提供可变 alpha(学习率)
问题描述
这是创建变量、为其创建渐变并使用 ApplyGradientDescent 创建渐变更新操作的最小示例
auto w = Variable(scope, { 3 }, DT_FLOAT);
std::vector<Output> grad_outputs;
TF_CHECK_OK(AddSymbolicGradients(scope, { loss }, { w }, &grad_outputs));
auto apply_w1 = ApplyGradientDescent(scope, w, Cast(scope, 0.01, DT_FLOAT), { grad_outputs[0] });
然而,我真正想要的是有一个可变的学习率,我可以从图外部更新它,同时避免每次都在图上创建新的梯度操作......因此:
auto ph = Placeholder(scope, DT_FLOAT));
auto var = Variable(scope, {1}, DT_FLOAT));
auto assignVar = Assign(scope, var, ph));
Tensor newLr(DT_FLOAT, TensorShape({1}));
newLr.flat<float>().data()[0] = 0.001;
TF_CHECK_OK(session->Run({{ph, newLr}}, {assignVar}, nullptr));
auto apply = ApplyGradientDescent(scope, w, var, { grad_outputs[0] });
问题是上面失败了,因为 ApplyGradientDescent 期望 Scalar 作为 alpha,我不明白。有没有办法解决?
解决方案
推荐阅读
- css - CSS 类不在 iOS 浏览器中显示,但在 Android 中有效
- maven-3 - 如何在命令行中使用 maven 3 设置插件存储库
- python - 从 dbus 对象打印结果
- r - ggplot 抛出错误 `label not found`,而 `label` 显然存在
- jquery - 使用嵌套 JSON 对象自定义(枢轴)显示 Jqgrid
- java - 图片按钮调整大小
- php - 每次用户点击来自 7 的联系人的提交按钮时,如何触发 recaptcha v3(分数测试)
- php - Symfony json 响应返回内容两次
- reactjs - 如何在 React Static typescript 模板中导入 static.config.js 中的 .ts 文件
- java - Maria DB - JPA/Hibernate 更新查询由于错误的查询生成而引发异常