Message Boards Message Boards


Avoid backticks on the right of matrix elements?

Posted 3 months ago
9 Replies
0 Total Likes

Hi, I have a problem with the output of an evaluated function.

I am doing a school project that involves matrices and the logistic sigmoid. I've created a function that takes as parameters four matrices and should give back a tensor from which I'm supposed to extract a matrix. The problem is this: when I evaluate the function the result is a tensor where each element is an expression with a backtick (this one: ` ) on the right that doesn't allow any further calculation. To evaluate the expressions in the tensor, and get what I want, I have to copy the output in another input space and remove manually all the backticks.

Here is the code:

(* Matrices *)
A = {{.2, .8}} 
W = {{.3}, {.6}}
Q = {{.4, .6 , .2}}
T = {{.5, .1, .1}}

(* Functions *)
Output[a_, w_, q_] := LogisticSigmoid[LogisticSigmoid[a.w].q]
Error[a_, w_, q_, t_] := (t - Output[a, w, q])^2
Derror[a_, w_, q_, t_] = D[Error[a, w, q, t], q]
(* And until here it's all right *)

(* Here come the troubles *)
Derror[A, W, Q, T]

The output when executing Derror is:

{{0.0309271 {{0.631812}}.1,
  0.238167 {{0.631812}}.1, 
  0.214915 {{0.631812}}.1}}

And if i put that in the input space (just pasting, without executing it) I get:

{{0.030927085534456576` {{0.6318124177361016`}}.1, 
  0.23816694971195315` {{0.6318124177361016`}}.1, 
  0.21491527050353018` {{0.6318124177361016`}}.1}}

The expected result is:

{{{{0.00195401}}, {{0.0150477}}, {{0.0135786}}}}

But to get to that I have to input:

(* The output of Derror but with backticks removed *)
{{0.030927085534456576 {{0.6318124177361016}}.1, 0.23816694971195315 {{0.6318124177361016}}.1, 0.21491527050353018 {{0.6318124177361016}}.1}}

Why is this happening? I'm not very trained in Wolfram Mathematica so I really don't know what to do. I searched the web but I found nothing. I tried with converting the output to string, replacing "`" with "", and than converting the string to expression again but nothing, it's useless. Look forward to find some answer here and sorry for my english (i'm not from an english-speaking country).

9 Replies

The backtick denotes the precision of an approximate real number. If no number follows the tick as in your case, then the number is a machine-precision floating-point number. My Mathematica is busy right now, and will be for a while, but I'm fairly certain that the backtick is not the problem. (It's only a problem when one is outputting numeric results for use in a different program.)

I can see that your definition of Derror seems wrong. The operator D is not going to work, as far as I know, when the second argument is a numeric array such as {{.4, .6 , .2}}. But that's what will be executed when you enter Derror[A, W, Q, T].

Posted 3 months ago

If D doesn't work in this case, is there any other way to do it? The output seems to be correct but just not evaluated until the end.

You've defined Q. It is not a variable, it is the constant {{.4, .6 , .2}}. D doesn't do anything sensible with such a thing as its variable.

Posted 3 months ago

An approximate real number result is by default displayed with 6 digits. But when the output is copied to an input cell more digits may be displayed. Ending in a backtick to show that the decimal expansion can involve more digits.

The square root of 2. :

In[1]:= Sqrt[2.]
Out[1]= 1.41421

Copy Out[1] to an input cell and evaluate that cell:

In[2]:= 1.4142135623730951`
Out[2]= 1.41421
Posted 3 months ago

Got it. But what should I do to get as output from Derror the result I need:

{{{{0.00195401}}, {{0.0150477}}, {{0.0135786}}}}

Instead of:

{{0.0309271 {{0.631812}}.1,
  0.238167 {{0.631812}}.1, 
  0.214915 {{0.631812}}.1}}

The result is correct only when copying the output and removing the backticks. Is there any way to get what I expect just executing Derror?

You should not use Q to represent both a constant and a variable. Use a different symbol for one or the other.

Posted 3 months ago

Ok. I tried this but I still get the result not evaluated.

MatrixForm[A = {{.2, .8}} ]
MatrixForm[W = {{.3, .4, .2}, {.6, .5, .1}}]
MatrixForm[R = {{.4, .6 }, {.2, .9}, {.4, .2}}] (* It's no longer Q *)
MatrixForm[T = {{.5, .1}}]

Output[a_, w_, q_] := LogisticSigmoid[LogisticSigmoid[a.w].q]
Error[a_, w_, q_, t_] := (t - Output[a, w, q])^2
Derror[a_, w_, q_, t_] = D[Error[a, w, q, t], q]

And executing Derror:

In[1]:= Derror[A, W, R, T]
Out[1]= {{0.0656387 {{0.631812, 0.617748, 0.529964}}.1, 0.246487 {{0.631812, 0.617748, 0.529964}}.1}}

When I should get:

{{{{0.00414713, 0.00405482, 0.00347862}}, {{0.0155734, 0.0152267, 0.0130629}}}}

I got that the same way I did before: copying the output in another input cell and removing the backticks.
What am I doing wrong?

Look at the definition of Derror. It evaluates to:

-2 LogisticSigmoid[a.w].1 (1 - 
   LogisticSigmoid[LogisticSigmoid[a.w].q]) (t - 
   LogisticSigmoid[LogisticSigmoid[a.w].q]) LogisticSigmoid[

Note the crazy term LogisticSigmoid[a.w].1.

You appear to want some sort of array derivative. If that's what you want, you must give D a list containing an array of variables to differentiate against, not simply a scalar variable like q. See the documentation for D.

Posted 3 months ago

I gave a look to the documentation. The closest thing to an array derivative, as much as I understand, is this:

In[2]:= Derror[a_, w_, q_, t_] = D[Error[a, w, q, t], {{w, q}}]
Out[2]= {-2 (a.1 (1 - LogisticSigmoid[a.w]) LogisticSigmoid[a.w]).q (1 - 
    LogisticSigmoid[LogisticSigmoid[a.w].q]) (t - 
    LogisticSigmoid[LogisticSigmoid[a.w].q]) LogisticSigmoid[
   LogisticSigmoid[a.w].q], -2 LogisticSigmoid[a.w].1 (1 - 
    LogisticSigmoid[LogisticSigmoid[a.w].q]) (t - 
    LogisticSigmoid[LogisticSigmoid[a.w].q]) LogisticSigmoid[

Or a little better:

$$\left\{-\frac{2 \left(1-\frac{1}{e^{-\frac{1}{e^{-a.w}+1}.q}+1}\right) \frac{a.1\left(1-\frac{1}{e^{-a.w}+1}\right)}{e^{-a.w}+1}.q\left(t-\frac{1}{e^{-\frac{1}{e^{-a.w}+1}.q}+1}\right)}{e^{-\frac{1}{e^{-a.w}+1}.q}+1},-\frac{2 \frac{1}{e^{-a.w}+1}.1 \left(1-\frac{1}{e^{-\frac{1}{e^{-a.w}+1}.q}+1}\right)\left(t-\frac{1}{e^{-\frac{1}{e^{-a.w}+1}.q}+1}\right)}{e^{-\frac{1}{e^{-a.w}+1}.q}+1}\right\}$$
I still get LogisticSigmoid[a.w].1. Is this what you meant? It's starting to get messy here.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract