1 # Copyright 2016 The TensorFlow Authors. All Rights Reserved. 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 """Neural network components for hybrid models.""" 16 from __future__ import absolute_import 17 from __future__ import division 18 from __future__ import print_function 19 20 from tensorflow.contrib import layers 21 from tensorflow.contrib.tensor_forest.hybrid.python import hybrid_layer 22 23 from tensorflow.python.framework import ops 24 25 from tensorflow.python.ops import array_ops 26 27 28 class FullyConnectedLayer(hybrid_layer.HybridLayer): 29 """A stacked, fully-connected feed-forward neural network layer.""" 30 31 def _define_vars(self, params): 32 pass 33 34 def inference_graph(self, data): 35 with ops.device(self.device_assigner): 36 # Compute activations for the neural network. 37 nn_activations = layers.fully_connected(data, self.params.layer_size) 38 39 for _ in range(1, self.params.num_layers): 40 # pylint: disable=W0106 41 nn_activations = layers.fully_connected(nn_activations, 42 self.params.layer_size) 43 return nn_activations 44 45 46 class ManyToOneLayer(hybrid_layer.HybridLayer): 47 48 def _define_vars(self, params): 49 pass 50 51 def inference_graph(self, data): 52 with ops.device(self.device_assigner): 53 # Compute activations for the neural network. 54 nn_activations = layers.fully_connected(data, 1) 55 56 # There is always one activation per instance by definition, so squeeze 57 # away the extra dimension. 58 return array_ops.squeeze(nn_activations, squeeze_dims=[1]) 59 60 61 class FlattenedFullyConnectedLayer(hybrid_layer.HybridLayer): 62 """A stacked, fully-connected flattened feed-forward neural network layer.""" 63 64 def _define_vars(self, params): 65 pass 66 67 def inference_graph(self, data): 68 with ops.device(self.device_assigner): 69 # Compute activations for the neural network. 70 nn_activations = [layers.fully_connected(data, self.params.layer_size)] 71 72 for _ in range(1, self.params.num_layers): 73 # pylint: disable=W0106 74 nn_activations.append( 75 layers.fully_connected( 76 nn_activations[-1], 77 self.params.layer_size)) 78 79 nn_activations_tensor = array_ops.concat( 80 nn_activations, 1, name="flattened_nn_activations") 81 82 return nn_activations_tensor 83