Caffe2 - Python API
A deep learning, cross platform ML framework
rnn_cell_test_util.py
1 # Copyright (c) 2016-present, Facebook, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 ##############################################################################
15 
16 from __future__ import absolute_import
17 from __future__ import division
18 from __future__ import print_function
19 from __future__ import unicode_literals
20 
21 from caffe2.python import workspace, scope
22 from caffe2.python.model_helper import ModelHelper
23 
24 import numpy as np
25 
26 
27 def sigmoid(x):
28  return 1.0 / (1.0 + np.exp(-x))
29 
30 
31 def tanh(x):
32  return 2.0 * sigmoid(2.0 * x) - 1
33 
34 
35 def _prepare_rnn(
36  t, n, dim_in, create_rnn, outputs_with_grads,
37  forget_bias, memory_optim=False,
38  forward_only=False, drop_states=False, T=None,
39  two_d_initial_states=None, dim_out=None,
40  num_states=2,
41  **kwargs
42 ):
43  if dim_out is None:
44  dim_out = [dim_in]
45  print("Dims: ", t, n, dim_in, dim_out)
46 
47  model = ModelHelper(name='external')
48 
49  if two_d_initial_states is None:
50  two_d_initial_states = np.random.randint(2)
51 
52  def generate_input_state(n, d):
53  if two_d_initial_states:
54  return np.random.randn(n, d).astype(np.float32)
55  else:
56  return np.random.randn(1, n, d).astype(np.float32)
57 
58  states = []
59  for layer_id, d in enumerate(dim_out):
60  for i in range(num_states):
61  state_name = "state_{}/layer_{}".format(i, layer_id)
62  states.append(model.net.AddExternalInput(state_name))
63  workspace.FeedBlob(
64  states[-1], generate_input_state(n, d).astype(np.float32))
65 
66  # Due to convoluted RNN scoping logic we make sure that things
67  # work from a namescope
68  with scope.NameScope("test_name_scope"):
69  input_blob, seq_lengths = model.net.AddScopedExternalInputs(
70  'input_blob', 'seq_lengths')
71 
72  outputs = create_rnn(
73  model, input_blob, seq_lengths, states,
74  dim_in=dim_in, dim_out=dim_out, scope="external/recurrent",
75  outputs_with_grads=outputs_with_grads,
76  memory_optimization=memory_optim,
77  forget_bias=forget_bias,
78  forward_only=forward_only,
79  drop_states=drop_states,
80  static_rnn_unroll_size=T,
81  **kwargs
82  )
83 
84  workspace.RunNetOnce(model.param_init_net)
85 
86  workspace.FeedBlob(
87  seq_lengths,
88  np.random.randint(1, t + 1, size=(n,)).astype(np.int32)
89  )
90  return outputs, model.net, states + [input_blob]