\tikzstyle{input_neuron}=[circle,draw=red!50,fill=red!10,thick,minimum size=6mm]
\tikzstyle{hidden_neuron}=[circle,draw=blue!50,fill=cyan!10,thick,minimum size=6mm]
\tikzstyle{output_neuron}=[circle,draw=green!50,fill=green!10,thick,minimum size=6mm]
\tikzstyle{faded}=[draw=gray!30,fill=gray!20,thick,minimum size=6mm]

\tikzstyle{input}=[circle,draw=black!50,fill=black!20,thick,minimum size=6mm]
% \hspace*{-50pt}
\begin{tikzpicture}
  \newcommand*{\xbase}{-1}

  \node (masks) at (12,9.5) {\textbf{Masks}};

  \def\MASKA{0,1,2,3}
  \def\MASKB{1,1,2,1,3}
  \def\MASKC{1,2,1,2,3}
  \def\MASKD{1,2,3,4}

  \node (mask1) at (12,8.5) {
    \begin{tikzpicture}[scale=0.35, every node/.style={scale=0.35}]
      \newcommand*{\size}{1cm}%
      \tikzstyle{box}=[rectangle,draw=gray, thin, minimum size=\size]
      \foreach \x in {0,...,4}{
          \foreach \y in {0,...,3}
              \node[box] at (11+\x,8-\y) {};
      }
      \foreach \y [count=\yi] in \MASKA {
        \foreach \x [count=\xi] in \MASKB {
            \ifnum \y<\x %
            \else %
              \node[box,fill=black] at (11+\xi-1,8-\yi+1){};  
            \fi
          }
        }

      \node at (11+5.5,8-1.3) {\scalebox{2}{$=\mathbf{M}^\mathbf{V}$}};
    \end{tikzpicture}
  };

  \node (mask2) at (12,6.7) {
    \begin{tikzpicture}[scale=0.35, every node/.style={scale=0.35}]
      \newcommand*{\size}{1cm}%
      \tikzstyle{box}=[rectangle,draw=gray, thin, minimum size=\size]
      \foreach \x in {0,...,4}{
          \foreach \y in {0,...,4}
              \node[box] at (11+\x,8-\y) {};
      }
      \foreach \y [count=\yi] in \MASKB {
        \foreach \x [count=\xi] in \MASKC {
            \ifnum \y<\x %
            \else %
              \node[box,fill=black] at (11+\xi-1,8-\yi+1){};  
            \fi
          }
        }
      \node at (11+5.5,8-1.3) {\scalebox{2}{$=\mathbf{M}^{\mathbf{W}^2}$}};
    \end{tikzpicture}
  };

  \node (mask3) at (12,4.7) {
    \begin{tikzpicture}[scale=0.35, every node/.style={scale=0.35}]
      \newcommand*{\size}{1cm}%
      \tikzstyle{box}=[rectangle,draw=gray, thin, minimum size=\size]
      \foreach \x in {0,...,3}{
          \foreach \y in {0,...,4}
              \node[box] at (11+\x,8-\y) {};
      }
      \foreach \y [count=\yi] in \MASKC {
        \foreach \x [count=\xi] in \MASKD {
            \ifnum \y<\x %
            \else %
              \node[box,fill=black] at (11+\xi-1,8-\yi+1){};  
            \fi
          }
        }
      \node at (11+5.5,8-1.3) {\scalebox{2}{$=\mathbf{M}^{\mathbf{W}^1}$}};
    \end{tikzpicture}
  };

  \renewcommand*{\xbase}{7}
  \node (Xneuron01l) at (\xbase+7.5,4) {\small $x_1$};
  \node (Xneuron02l) at (\xbase+8.5,4) {\small $x_2$};
  \node (Xneuron03l) at (\xbase+9.5,4) {\small $x_3$};
  \node (Xneuron03l) at (\xbase+10.5,4) {\small $x_4$};

  \node [input_neuron,faded] (Xneuron01) at (\xbase+7.5,4.5) {1};
  \node [input_neuron,faded] (Xneuron02) at (\xbase+8.5,4.5) {2};
  \node [input_neuron,faded] (Xneuron03) at (\xbase+9.5,4.5) {3};
  \node [input_neuron,faded] (Xneuron04) at (\xbase+10.5,4.5) {4};
  \visible<5->{\node [input_neuron,fill=red] (Xneuron01) at (\xbase+7.5,4.5) {1};}
  \visible<6->{\node [input_neuron,fill=green] (Xneuron02) at (\xbase+8.5,4.5) {2};}
  \visible<7->{\node [input_neuron,fill=blue] (Xneuron03) at (\xbase+9.5,4.5) {3};}

  \node [hidden_neuron,faded] (Xneuron11) at (\xbase+7,6)    {1};
  \node [hidden_neuron,faded] (Xneuron12) at (\xbase+8,6)    {2};
  \node [hidden_neuron,faded] (Xneuron13) at (\xbase+9,6)    {1};
  \node [hidden_neuron,faded] (Xneuron14) at (\xbase+10,6)   {2};
  \node [hidden_neuron,faded] (Xneuron15) at (\xbase+11,6)   {3};

  \node [hidden_neuron,faded] (Xneuron21) at (\xbase+7, 7.5) {1};
  \node [hidden_neuron,faded] (Xneuron22) at (\xbase+8, 7.5) {1};
  \node [hidden_neuron,faded] (Xneuron23) at (\xbase+9, 7.5) {2};
  \node [hidden_neuron,faded] (Xneuron24) at (\xbase+10,7.5) {1};
  \node [hidden_neuron,faded] (Xneuron25) at (\xbase+11,7.5) {3};

  \node [output_neuron,faded] (Xneuron31) at (\xbase+7.5,9)  {1};
  \node [output_neuron,faded] (Xneuron32) at (\xbase+8.5,9)  {2};
  \node [output_neuron,faded] (Xneuron33) at (\xbase+9.5,9)  {3};
  \node [output_neuron,faded] (Xneuron34) at (\xbase+10.5,9) {4};
  \visible<4->{\node [output_neuron,fill=red] (Xneuron31) at (\xbase+7.5,9)  {1};}
  \visible<5->{\node [output_neuron,fill=green] (Xneuron32) at (\xbase+8.5,9)  {2};}
  \visible<6->{\node [output_neuron,fill=blue] (Xneuron33) at (\xbase+9.5,9)  {3};}
  \visible<7->{\node [output_neuron,fill=brown] (Xneuron34) at (\xbase+10.5,9) {4};}

  \visible<5->
  {
    \foreach \y [count=\yi] in \MASKC
    {
      \ifnum \y<2
        \node [hidden_neuron,fill=green] (Xneuron1\yi) at (\xbase+6+\yi,6) {\y};
      \fi
    }
    \foreach \y [count=\yi] in \MASKB
    {
      \ifnum \y<2
        \node [hidden_neuron,fill=green] (Xneuron2\yi) at (\xbase+6+\yi,7.5) {\y};
      \fi
    }
  }

  \visible<6->
  {
    \foreach \y [count=\yi] in \MASKC
    {
      \ifnum \y<3
        \node [hidden_neuron,fill=blue] (Xneuron1\yi) at (\xbase+6+\yi,6) {\y};
      \fi
    }
    \foreach \y [count=\yi] in \MASKB
    {
      \ifnum \y<3
        \node [hidden_neuron,fill=blue] (Xneuron2\yi) at (\xbase+6+\yi,7.5) {\y};
      \fi
    }
  }

  \visible<7->
  {
    \foreach \y [count=\yi] in \MASKC
    {
      \ifnum \y<4
        \node [hidden_neuron,fill=brown] (Xneuron1\yi) at (\xbase+6+\yi,6) {\y};
      \fi
    }
    \foreach \y [count=\yi] in \MASKB
    {
      \ifnum \y<4
        \node [hidden_neuron,fill=brown] (Xneuron2\yi) at (\xbase+6+\yi,7.5) {\y};
      \fi
    }
  }

  \node [rotate=45, text width=1.5] (Xneuron31l) at (\xbase+7.5, 9.5) {\small $p(x_1)$};
  \node [rotate=45, text width=1.5] (Xneuron32l) at (\xbase+8.5, 9.5) {\small $p(x_2|x_1)$};
  \node [rotate=45, text width=1.5] (Xneuron33l) at (\xbase+9.5, 9.5) {\small $p(x_3|x_1,x_2)$};
  \node [rotate=45, text width=1.5] (Xneuron34l) at (\xbase+10.5,9.5) {\small $p(x_4|x_1,x_2,x_3)$};

  \visible<4->{\node [rotate=45, text width=1.5, text=red] (Xneuron31l) at (\xbase+7.5, 9.5) {\small $p(x_1)$};}
  \visible<5->{\node [rotate=45, text width=1.5, text=green] (Xneuron32l) at (\xbase+8.5, 9.5) {\small $p(x_2|x_1)$};}
  \visible<6->{\node [rotate=45, text width=1.5, text=blue] (Xneuron33l) at (\xbase+9.5, 9.5) {\small $p(x_3|x_1,x_2)$};}
  \visible<7->{\node [rotate=45, text width=1.5, text=brown] (Xneuron34l) at (\xbase+10.5,9.5) {\small $p(x_4|x_1,x_2,x_3)$};}

  \foreach \y [count=\yi] in \MASKC {
    \foreach \x [count=\xi] in \MASKD {
        \ifnum\y<\x %
        \else %
          \draw[->,thick,gray!30] (Xneuron0\xi) -- (Xneuron1\yi);
          \ifnum\y<2 \ifnum\x<2%
            \visible<5>{\draw[->,thick,green] (Xneuron0\xi) -- (Xneuron1\yi);}
          \fi \fi
          \ifnum\y<3 \ifnum\x<3%
            \visible<6>{\draw[->,thick,blue] (Xneuron0\xi) -- (Xneuron1\yi);}
          \fi \fi
          \ifnum\y<4 \ifnum\x<4%
            \visible<7>{\draw[->,thick,brown] (Xneuron0\xi) -- (Xneuron1\yi);}
          \fi \fi
        \fi
      }
    }

  \foreach \y [count=\yi] in \MASKB {
    \foreach \x [count=\xi] in \MASKC {
        \ifnum\y<\x %
        \else %
          \draw[->,thick,gray!30] (Xneuron1\xi) -- (Xneuron2\yi);
          \ifnum\y<2 \ifnum\x<2%
            \visible<5>{\draw[->,thick,green] (Xneuron1\xi) -- (Xneuron2\yi);}
          \fi \fi
          \ifnum\y<3 \ifnum\x<3%
            \visible<6>{\draw[->,thick,blue] (Xneuron1\xi) -- (Xneuron2\yi);}
          \fi \fi
          \ifnum\y<4 \ifnum\x<4%
            \visible<7>{\draw[->,thick,brown] (Xneuron1\xi) -- (Xneuron2\yi);}
          \fi \fi
        \fi
      }
    }

  \foreach \y [count=\yi] in \MASKA {
    \foreach \x [count=\xi] in \MASKB {
        \ifnum\y<\x %
        \else %
          \draw[->,thick,gray!30] (Xneuron2\xi) -- (Xneuron3\yi);
          \ifnum\y=1 \ifnum\x<2%
            \visible<5>{\draw[->,thick,green] (Xneuron2\xi) -- (Xneuron3\yi);}
          \fi \fi
          \ifnum\y=2 \ifnum\x<3%
            \visible<6>{\draw[->,thick,blue] (Xneuron2\xi) -- (Xneuron3\yi);}
          \fi \fi
          \ifnum\y=3 \ifnum\x<4%
            \visible<7>{\draw[->,thick,brown] (Xneuron2\xi) -- (Xneuron3\yi);}
          \fi \fi
        \fi
      }
    }
\end{tikzpicture}  