在教堂中使用矩阵创建域 [英] Create domain with matrices in Chapel

查看:103
本文介绍了在教堂中使用矩阵创建域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个域D,我想用它为几个矩阵<​​c1>编制索引.形式的东西

I have a domain D, and I want to use it to index several matrices A. Something of the form

var dom: domain(1) = {0..5};
var mats: [dom] <?>;

var a0 = [[0.0, 0.1, 0.2], [0.3, 0.4, 0.5]];
var a1 = [[1.0, 1.1, 1.2, 1.3], [1.4, 1.5, 1.6, 1.7]];

mats[0] = a0;
mats[1] = a1;

每个a将是2D的,但大小不同.是的,其中一些将是稀疏的(但不必出于这个问题的目的)

Each a will be 2D but have different sizes. Yes, some of these will be sparse (but need not be for purposes of this question)

==更新==

为清楚起见,我有一系列的层(这是一个神经网络),例如1..15.我创建了var layerDom = {1..15},每一层都有多个与之关联的对象,例如错误,所以我有

For clarity, I have a series of layers (it's a neural net), say 1..15. I created var layerDom = {1..15} Each layer has multiple objects associated with it, like error so I have

var errors: [layerDom] real;  // Just a number

我想拥有

var Ws: [layerDom] <matrixy thingy>;  // Weight matrices all of different shape.

推荐答案

从教堂1.15开始,还没有一种优雅的方法来创建内部数组具有不同大小的数组.这是因为内部数组都共享相同的域,这意味着更改一个数组的域会更改所有数组.

As of Chapel 1.15 there isn't an elegant way to create an array of arrays where the inner arrays have different sizes. This is because the inner arrays all share the same domain, meaning that changing one array's domain changes all arrays.

要获得理想的效果,您需要创建一个包含数组的记录/类的数组:

To achieve the desired effect, you need to create an array of records/classes that contain an array:

record Weight {
  var D : domain(2);
  var A : [D] real;
}

var layers = 4;
var weights : [1..layers] Weight;
for i in 1..layers {
  weights[i].D = {1..i, 1..i};
  weights[i].A = i;
}

for w in weights do writeln(w.A, "\n");

// 1.0
// 
// 2.0 2.0
// 2.0 2.0
// 
// 3.0 3.0 3.0
// 3.0 3.0 3.0
// 3.0 3.0 3.0
// 
// 4.0 4.0 4.0 4.0
// 4.0 4.0 4.0 4.0
// 4.0 4.0 4.0 4.0
// 4.0 4.0 4.0 4.0
// 

这篇关于在教堂中使用矩阵创建域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆