Beispiel #1
0
 def test_copy_layers(self):
     """Test copying layers."""
     tg = dc.models.TensorGraph()
     features = Feature(shape=(None, 10))
     dense = Dense(10,
                   in_layers=features,
                   biases_initializer=tf.random_normal_initializer)
     constant = Constant(10.0)
     output = dense + constant
     tg.add_output(output)
     tg.set_loss(output)
     tg.fit_generator([])
     replacements = {constant: Constant(20.0)}
     copy = output.copy(replacements, tg)
     assert isinstance(copy, Add)
     assert isinstance(copy.in_layers[0], Dense)
     assert isinstance(copy.in_layers[0].in_layers[0], Feature)
     assert copy.in_layers[1] == replacements[constant]
     variables = tg.get_layer_variables(dense)
     with tg._get_tf("Graph").as_default():
         if tfe.in_eager_mode():
             values = [v.numpy() for v in variables]
         else:
             values = tg.session.run(variables)
     for v1, v2 in zip(values, copy.in_layers[0].variable_values):
         assert np.array_equal(v1, v2)
Beispiel #2
0
 def test_copy_layers_shared(self):
     """Test copying layers with shared variables."""
     tg = dc.models.TensorGraph()
     features = Feature(shape=(None, 10))
     dense = Dense(10,
                   in_layers=features,
                   biases_initializer=tf.random_normal_initializer)
     constant = Constant(10.0)
     output = dense + constant
     tg.add_output(output)
     tg.set_loss(output)
     replacements = {features: features, constant: Constant(20.0)}
     copy = output.copy(replacements, shared=True)
     tg.add_output(copy)
     assert isinstance(copy, Add)
     assert isinstance(copy.in_layers[0], Dense)
     assert isinstance(copy.in_layers[0].in_layers[0], Feature)
     assert copy.in_layers[1] == replacements[constant]
     variables1 = tg.get_layer_variables(dense)
     variables2 = tg.get_layer_variables(copy.in_layers[0])
     for v1, v2, in zip(variables1, variables2):
         assert v1 == v2
     feed_dict = {features: np.random.random((5, 10))}
     v1, v2 = tg.predict_on_generator([feed_dict], outputs=[output, copy])
     assert_true(np.all(np.isclose(v1 + 10, v2)))
 def test_operators(self):
     """Test math operators on Layers."""
     v1 = np.random.uniform(size=(2, 3)).astype(np.float32)
     v2 = np.random.uniform(size=(2, 3)).astype(np.float32)
     c1 = Constant(v1)
     c2 = Constant(v2)
     tg = dc.models.TensorGraph()
     tg.set_loss(c1)
     expected = []
     tg.add_output(c1 + c2)
     expected.append(v1 + v2)
     tg.add_output(c1 + v2)
     expected.append(v1 + v2)
     tg.add_output(1 + c2)
     expected.append(1 + v2)
     tg.add_output(c1 - c2)
     expected.append(v1 - v2)
     tg.add_output(c1 - v2)
     expected.append(v1 - v2)
     tg.add_output(1 - c2)
     expected.append(1 - v2)
     tg.add_output(c1 * c2)
     expected.append(v1 * v2)
     tg.add_output(c1 * v2)
     expected.append(v1 * v2)
     tg.add_output(2 * c2)
     expected.append(2 * v2)
     tg.add_output(-c1)
     expected.append(-v1)
     for o, e in zip(tg.outputs, expected):
         value = tg.predict_on_batch(np.array([0]), outputs=o)
         assert np.array_equal(e, value)
Beispiel #4
0
def test_Constant_pickle():
  tg = TensorGraph()
  feature = Feature(shape=(tg.batch_size, 1))
  layer = Constant(np.array([15.0]))
  output = Add(in_layers=[feature, layer])
  tg.add_output(output)
  tg.set_loss(output)
  tg.build()
  tg.save()
Beispiel #5
0
 def create_layers(self, state, **kwargs):
     action_mean = Dense(1,
                         in_layers=state,
                         weights_initializer=tf.zeros_initializer)
     action_std = Constant([10.0])
     value = Dense(1, in_layers=state)
     return {
         'action_mean': action_mean,
         'action_std': action_std,
         'value': value
     }
Beispiel #6
0
    def __init__(self,
                 tensorboard=False,
                 tensorboard_log_frequency=100,
                 batch_size=100,
                 random_seed=None,
                 use_queue=True,
                 graph=None,
                 learning_rate=0.001,
                 **kwargs):
        """
    Parameters
    ----------
    tensorboard: bool
      Should we log to model_dir data for tensorboard?
    tensorboard_log_frequency: int
      How many training batches before logging tensorboard?
    batch_size: int
      default batch size for training and evaluating
    use_queue: boolean
      if True when building we will create a tf.FIFO queue, which will hold
      all features, weights, and labels.  We will feed the inputs into this
      queue in batches of self.batch_size in a separate thread from the
      thread training the model.  You cannot use a queue when
      batches are not of consistent size
    graph: tensorflow.Graph
      the Graph in which to create Tensorflow objects.  If None, a new Graph
      is created.
    learning_rate: float or LearningRateSchedule
      the learning rate to use for optimization
    kwargs
    """

        # Layer Management
        self.layers = dict()
        self.features = list()
        self.labels = list()
        self.outputs = list()
        self.task_weights = list()
        self.submodels = list()
        self.loss = Constant(0)
        self.built = False
        self.queue_installed = False
        self.optimizer = Adam(learning_rate=learning_rate,
                              beta1=0.9,
                              beta2=0.999,
                              epsilon=1e-7)

        # Singular place to hold Tensor objects which don't serialize
        # These have to be reconstructed on restoring from pickle
        # See TensorGraph._get_tf() for more details on lazy construction
        self.tensor_objects = {
            "FileWriter": None,
            "Graph": graph,
            "train_op": None,
            "summary_op": None,
        }
        self.tensorboard = tensorboard
        self.tensorboard_log_frequency = tensorboard_log_frequency
        self.tensorboard_step = 0
        self.global_step = 0
        self.use_queue = use_queue

        self.batch_size = batch_size
        self.random_seed = random_seed
        super(TensorGraph, self).__init__(**kwargs)
        self.save_file = "%s/%s" % (self.model_dir, "model")
        self.model_class = None

        self.rnn_initial_states = []
        self.rnn_final_states = []
        self.rnn_zero_states = []
        if self.use_queue and self.tensorboard:
            raise ValueError(
                "Currently TensorGraph cannot both use_queue and tensorboard at the same time"
            )
Beispiel #7
0
 def test_constant(self):
     """Test that Constant can be invoked."""
     value = np.random.uniform(size=(2, 3)).astype(np.float32)
     with self.session() as sess:
         out_tensor = Constant(value)()
         assert np.array_equal(value, out_tensor.eval())
Beispiel #8
0
 def test_constant(self):
   """Test that Constant can be invoked."""
   value = np.random.uniform(size=(2, 3)).astype(np.float32)
   with self.session() as sess:
     out_tensor = Constant(value)()
     assert np.array_equal(value, out_tensor.eval())