13

6

I have to speed up a matrix-calculation and would like to use `Compile`

for it, though it fails and produces an unknown error message:

```
compiledFunc =
Compile[{{w, _Real, 2}, {v, _Integer, 1}, {hb, _Real,
1}, {vb, _Real, 1}}, Module[{h, hs, vr, hr},
h = 1./(1. + Exp[-(w.v + hb)]);
hs = Boole@Thread[h > RandomReal[{0, 1}, {Length@hb}]];
vr = 1./(1. + Exp[-((Transpose@w).hs + vb)]);
hr = 1./(1. + Exp[-(w.vr + hb)]);
w + (0.01*(Outer[Times, h, v] - Outer[Times, hr, vr]))
], Parallelization -> True];
(*
==> Compile::argcompten: The comparison, Greater, is invalid for tensor arguments. >>
*)
```

Any idea how I can overcome this and/or speed it up?

**Update:**

According to the answers, the problem is that `Thread`

is not compilable. Substituting it with `MapThread`

indeed allows compilation. But then running the code gives another error. (I thought that since it is about the same piece of code, I won't start a new post.)

```
cFunc = Compile[{{w, _Real, 2}, {v, _Integer, 1}, {hb, _Real,
1}, {vb, _Real, 1}}, Module[{h, hs, vr, hr},
h = 1./(1. + Exp[-(w.v + hb)]);
hs = Boole@
MapThread[Greater, {h, RandomReal[{0, 1}, {Length@hb}]}];
vr = 1./(1. + Exp[-((Transpose@w).hs + vb)]);
hr = 1./(1. + Exp[-(w.vr + hb)]);
w + (0.01*(Outer[Times, h, v] - Outer[Times, hr, vr]))],
Parallelization -> True];
cFunc[w, v, hb, vb]
(*
CompiledFunction::cfse: Compiled expression 1.7+3.2 I should be a machine-size real number. >>
CompiledFunction::cfex: Could not complete external evaluation at instruction 2; proceeding with uncompiled evaluation. >>
*)
```

You probably need to do the threading manually. – Mr.Wizard – 2012-02-14T19:46:49.180

MMA is assuming that

`h`

is a tensor. How do you determine that a tensor is larger than some real number? You need to provide a metric in order for`Greater`

to work. – Matariki – 2012-02-14T19:53:51.6631@Matariki The purpose of the

`Thread`

is to thread the`>`

over elements, so what you say is not a problem. However,`Thread`

does not get compiled, and that is actually the problem. – acl – 2012-02-14T19:58:11.333