提交 0cfae9f9 authored 作者: Frederic Bastien's avatar Frederic Bastien

scan nextml doc

上级 680275ad
...@@ -862,15 +862,16 @@ print f([0, 1, 2]) ...@@ -862,15 +862,16 @@ print f([0, 1, 2])
\item Allow looping (for, map, while) \item Allow looping (for, map, while)
\item Allow recursion (reduce) \item Allow recursion (reduce)
\item Allow recursion with dependency on many of the previous time step \item Allow recursion with dependency on many of the previous time step
\item Optimize some cases like moving computation outside of scan. \item Optimize some cases like moving computation outside of scan
\item The Scan grad is done via Backpropagation\_through\_time(BPTT) \item The Scan grad is done via Backpropagation Through Time(BPTT)
\end{itemize} \end{itemize}
\end{frame} \end{frame}
\begin{frame}{When not to use scan} \begin{frame}{When not to use scan}
\begin{itemize} \begin{itemize}
\item If only needed for ``vectorization'' or ``broadcasting''. tensor \item If you only need for ``vectorization'' or
and numpy.ndarray support them natively. This will be much better. ``broadcasting''. tensor and numpy.ndarray support them
natively. This will be much better for that use case.
\item You do a fixed number of iteration that is very small (2,3). You \item You do a fixed number of iteration that is very small (2,3). You
are probably better to just unroll the graph to do it. are probably better to just unroll the graph to do it.
...@@ -892,22 +893,36 @@ import theano.tensor as T ...@@ -892,22 +893,36 @@ import theano.tensor as T
import numpy as np import numpy as np
# define tensor variables # define tensor variables
X = T.matrix("X")
W = T.matrix("W") W = T.matrix("W")
X = T.matrix("X")
b_sym = T.vector("b_sym") b_sym = T.vector("b_sym")
# define shared random stream # define shared random stream
trng = T.shared_randomstreams.RandomStreams(1234) trng = T.shared_randomstreams.RandomStreams(1234)
d=trng.binomial(size=W[1].shape) d=trng.binomial(size=W[1].shape)
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Scan Example1: Computing tanh(v.dot(W) + b) * d where d is binomial (2)}
results, updates = theano.scan(lambda v: T.tanh(T.dot(v, W) + b_sym) * d, sequences=X) \lstset{language=Python,
compute_with_bnoise = theano.function(inputs=[X, W, b_sym], outputs=[results], commentstyle=\itshape\color{blue},
updates=updates, allow_input_downcast=True) stringstyle=\color{violet},
}
\begin{lstlisting}
results, updates = theano.scan(
lambda v: T.tanh(T.dot(v, W) + b_sym) * d,
sequences=X)
f = theano.function(inputs=[X, W, b_sym],
outputs=[results],
updates=updates)
x = np.eye(10, 2, dtype=theano.config.floatX) x = np.eye(10, 2, dtype=theano.config.floatX)
w = np.ones((2, 2), dtype=theano.config.floatX) w = np.ones((2, 2), dtype=theano.config.floatX)
b = np.ones((2), dtype=theano.config.floatX) b = np.ones((2), dtype=theano.config.floatX)
print compute_with_bnoise(x, w, b) print f(x, w, b)
\end{lstlisting} \end{lstlisting}
\end{frame} \end{frame}
...@@ -928,19 +943,28 @@ A = T.vector("A") ...@@ -928,19 +943,28 @@ A = T.vector("A")
def inner_fct(prior_result, B): def inner_fct(prior_result, B):
return prior_result * B return prior_result * B
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Scan Example2: Computing pow(A, k) (2)}
# Symbolic description of the result \lstset{language=Python,
result, updates = theano.scan(fn=inner_fct, commentstyle=\itshape\color{blue},
outputs_info=T.ones_like(A), stringstyle=\color{violet},
non_sequences=A, n_steps=k) }
\begin{lstlisting}
result, updates = theano.scan(
fn=inner_fct,
outputs_info=T.ones_like(A),
non_sequences=A, n_steps=k)
# Scan has provided us with A ** 1 through A ** k. Keep only the last # Scan provide us with A ** 1 through A ** k.
# value. Scan notices this and does not waste memory saving them. # Keep only the last value. Scan optimize memory.
final_result = result[-1] final = result[-1]
power = theano.function(inputs=[A, k], outputs=final_result, power = theano.function(inputs=[A, k], outputs=final,
updates=updates) updates=updates)
print power(range(10), 2) print power(range(10), 2)
#[ 0. 1. 4. 9. 16. 25. 36. 49. 64. 81.] #[ 0. 1. 4. 9. 16. 25. 36. 49. 64. 81.]
\end{lstlisting} \end{lstlisting}
...@@ -954,18 +978,19 @@ print power(range(10), 2) ...@@ -954,18 +978,19 @@ print power(range(10), 2)
stringstyle=\color{violet}, stringstyle=\color{violet},
} }
\begin{lstlisting} \begin{lstlisting}
result, updates = theano.scan(fn=inner_fct, result, updates = theano.scan(
outputs_info=T.ones_like(A), fn=inner_fct,
non_sequences=A, n_steps=k) sequences=[]
outputs_info=[T.ones_like(A)],
non_sequences=A,
n_steps=k)
\end{lstlisting} \end{lstlisting}
\begin{itemize} \begin{itemize}
\item updates are needed if there is random number generated in the \item Updates are needed if there is random number generated in the
\item Just pass them to the call theano.function(..., updates=updates) \item Just pass them to the call theano.function(..., updates=updates)
\item The innfer function of scan take argument like this: \item The innfer function of scan take argument like this:
scan: sequences, outputs\_info, non sequences
\item scan: sequence, outputs\_info, nonsequence
\end{itemize} \end{itemize}
\end{frame} \end{frame}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论