neural-network - 在 Dynet 中添加 numpy 数组和表达式的正确方法?
问题描述
在 Dynet 中添加参数和 numpy 生成的数组的正确方法是什么?例如,当我尝试以下操作时:
from dynet import *
import numpy as np
import numpy.matlib as npm
model = ParameterCollection()
....
LayerA = model.add_parameters((1, 100))
LayerA = model.add_parameters((1, 100))
...
normal = np.random.normal(npm.zeros((1, 100)), npm.ones((1, 100)))
Layer = LayerA + cmult(LayerB,normal) # cmult is Dynet component-wise multiplication
...
output = Layer.expr() * activation(...)
它给了我以下错误,
TypeError: Argument 'x' has incorrect type (expected _dynet.Expression, got _dynet.Parameters)
所以,我尝试了这个而不是我上面的,
Layer = LayerA + cmult(LayerB.expr(),normal)
但它给了我以下错误,
TypeError: Argument 'y' has incorrect type (expected _dynet.Expression, got numpy.ndarray)
所以我想我可能需要将 numpy 数组转换为 dynet 表达式,
Layer = LayerA.expr() + cmult(LayerB.expr(),inputTensor(normal))
...
output = Layer * activation(...)
它通过没有任何错误,但它在我打电话的其他地方抱怨output.scalar_value()
,
terminate called after throwing an instance of 'std::runtime_error'
what(): Input tensor has more than one element, cannot convert to scalar.
Aborted (core dumped)
解决方案
推荐阅读
- python - 打印在同一行 CMD
- ms-word - 错误 - applescript 中的“未授权将 Apple 事件发送到 Microsoft Word”
- javascript - 从 api 中提取更好的格式数据
- xcode - 如何从 Xcode 中的命令行工具访问 MacOS 安全飞地
- microsoft-graph-api - 如何创建 Microsoft Teams 附带的日历事件链接到在线会议
- html - Bootstrap 按百分比展开菜单/导航栏,如果需要可滚动
- python - Python步进电机最短路径
- android - 如何在 Android 中更改起始目的地动态?
- git - Git合并覆盖我的本地分支文件而不发出合并冲突的信号
- sql - Bulk Loading CSV From Blob Storage to SQL Database