Referencing defined symbols when simplifying equations

Question: How can I tell Mathematica to replace a sub-expression (in a simplified expression) with a symbol if the symbol is defined to be that sub-expression?

Say you have defined:

f[x_, y_] := some_complicated_expr_using_x_and_y_1
g[x_, y_] := some_complicated_expr_using_x_and_y_2
h := some_complicated_expr_using_f_and_g

then you do D[h,x] // Simplify, you would get an expression that has sub-expressions some_complicated_expr_using_x_and_y_1 and some_complicated_expr_using_x_and_y_2.

How can I tell Mathematica to replace some_complicated_expr_using_x_and_y_1 with f[x,y]?

For a concrete example, consider:

h[μ_, r_] := Sum[x[μ, s]*w[s, r], {s, 1, n}]
yhat[μ_, r_] := σ[h[μ, r]]
e := (1/(2 m)) Sum[Sum[(y[μ, r] - yhat[μ, r])^2, {r, 1, p}], {μ, 1, m}]
h[μ, r]
yhat[μ, r]
e
D[e, w[i, j]]

I you evaluate the above you will get How can I get it to produce: (which equals to Out)

I have tried ReplaceRepeated but got no luck: • to get halfway there you need to have the unevaluated forms of the expressions on the rhs of ruleh and ruleyhat. That is, use HoldForm[h[μ, r]] and HoldForm[yhat[μ, r]]. – kglr Jan 20 '18 at 5:55

Here is the result of the operation D[e, w[i, j]]:

Clear[yhat];
expr=(1/(2 m) (\!$$\*UnderoverscriptBox[\(\[Sum]$$, $$\[Mu] = 1$$, $$m$$]$$\*UnderoverscriptBox[\(\[Sum]$$, $$r = 1$$, $$p$$]$$(\(-2$$\ $$( \*UnderoverscriptBox[\(\[Sum]$$, $$s = 1$$, $$n$$]\*
TemplateBox[{RowBox[{"i", ",", "s"}]},
"KroneckerDeltaSeq"]\ \*
TemplateBox[{RowBox[{"j", ",", "r"}]},
"KroneckerDeltaSeq"]\ x[\[Mu], s])\)\ y[\[Mu], r]\ \*
SuperscriptBox["\[Sigma]", "\[Prime]",
MultilineFunction->None][
\*UnderoverscriptBox[$$\[Sum]$$, $$s = 1$$, $$n$$]w[s, r]\ x[\[Mu],
s]] + 2\ $$( \*UnderoverscriptBox[\(\[Sum]$$, $$s = 1$$, $$n$$]\*
TemplateBox[{RowBox[{"i", ",", "s"}]},
"KroneckerDeltaSeq"]\ \*
TemplateBox[{RowBox[{"j", ",", "r"}]},
"KroneckerDeltaSeq"]\ x[\[Mu], s])\)\ \[Sigma][
\*UnderoverscriptBox[$$\[Sum]$$, $$s = 1$$, $$n$$]w[s, r]\ x[\[Mu],
s]]\ \*
SuperscriptBox["\[Sigma]", "\[Prime]",
MultilineFunction->None][
\*UnderoverscriptBox[$$\[Sum]$$, $$s = 1$$, $$n$$]w[s, r]\ x[\[Mu],
s]])\)\)\))) /. \[Sigma][a_] -> yhat[\[Mu], r] /.
Derivative[\[Sigma]][a_] -> yhat'[\[Mu], r]

Try this:

expr /. \[Sigma][a_] -> yhat[\[Mu], r] /.Derivative[\[Sigma]][a_] -> yhat'[\[Mu], r]

yielding thew following: Have fun!