示例#1
0
def local():
    """Base configuration for a CNN model with just local view."""
    config = parent_configs.base()

    # Override the model features to be local_view time series.
    config["inputs"]["features"] = {
        "local_view": {
            "length": 201,
            "is_time_series": True,
        },
    }

    # Add configurations for the convolutional layers of time series features.
    config["hparams"]["time_series_hidden"] = {
        "local_view": {
            "cnn_num_blocks": 2,
            "cnn_block_size": 2,
            "cnn_initial_num_filters": 16,
            "cnn_block_filter_factor": 2,
            "cnn_kernel_size": 5,
            "convolution_padding": "same",
            "pool_size": 7,
            "pool_strides": 2,
        },
    }
    config["hparams"]["num_pre_logits_hidden_layers"] = 2
    config["hparams"]["pre_logits_hidden_layer_size"] = 1024
    return config
def local_global():
    """Base configuration for a CNN model with separate local/global views. 
     In this setup, the configuration is altered to support one single global 
     view, generated via application of a Gaussian pyramid."""
    config = parent_configs.base()

    # Override the model features to be global_view time series.
    config["inputs"]["features"] = {
        "global_view": {
            "length": 251,
            "is_time_series": True,
        },
    }

    # Add configurations for the convolutional layers of time series features.
    config["hparams"]["time_series_hidden"] = {
        "global_view": {
            "cnn_num_blocks": 2,
            "cnn_block_size": 2,
            "cnn_initial_num_filters": 16,
            "cnn_block_filter_factor": 2,
            "cnn_kernel_size": 5,
            "convolution_padding": "same",
            "pool_size": 7,
            "pool_strides": 2,
        },
    }
    config["hparams"]["num_pre_logits_hidden_layers"] = 4
    config["hparams"]["pre_logits_hidden_layer_size"] = 512
    return config
  def testOneTimeSeriesFeature(self):
    # Build config.
    feature_spec = {
        "time_feature_1": {
            "length": 10,
            "is_time_series": True,
        }
    }
    config = configurations.base()
    config["inputs"]["features"] = feature_spec
    config = configdict.ConfigDict(config)

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    model = astro_model.AstroModel(features, labels, config.hparams,
                                   tf.estimator.ModeKeys.TRAIN)
    model.build()

    # Validate hidden layers.
    self.assertItemsEqual(["time_feature_1"],
                          model.time_series_hidden_layers.keys())
    self.assertIs(model.time_series_features["time_feature_1"],
                  model.time_series_hidden_layers["time_feature_1"])
    self.assertEqual(len(model.aux_hidden_layers), 0)
    self.assertIs(model.time_series_features["time_feature_1"],
                  model.pre_logits_concat)
示例#4
0
  def testZeroHiddenLayers(self):
    # Build config.
    feature_spec = {
        "time_feature_1": {
            "length": 10,
            "is_time_series": True,
        },
        "time_feature_2": {
            "length": 10,
            "is_time_series": True,
        },
        "aux_feature_1": {
            "length": 1,
            "is_time_series": False,
        },
    }
    config = configurations.base()
    config["inputs"]["features"] = feature_spec
    config = configdict.ConfigDict(config)
    config.hparams.output_dim = 1
    config.hparams.num_pre_logits_hidden_layers = 0

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    model = astro_model.AstroModel(features, labels, config.hparams,
                                   tf.estimator.ModeKeys.TRAIN)
    model.build()

    # Validate Tensor shapes.
    self.assertShapeEquals((None, 21), model.pre_logits_concat)
    logits_w = testing.get_variable_by_name("logits/kernel")
    self.assertShapeEquals((21, 1), logits_w)
示例#5
0
  def testOneTimeSeriesFeature(self):
    # Build config.
    feature_spec = {
        "time_feature_1": {
            "length": 10,
            "is_time_series": True,
        }
    }
    config = configurations.base()
    config["inputs"]["features"] = feature_spec
    config = configdict.ConfigDict(config)

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    model = astro_model.AstroModel(features, labels, config.hparams,
                                   tf.estimator.ModeKeys.TRAIN)
    model.build()

    # Validate hidden layers.
    self.assertItemsEqual(["time_feature_1"],
                          model.time_series_hidden_layers.keys())
    self.assertIs(model.time_series_features["time_feature_1"],
                  model.time_series_hidden_layers["time_feature_1"])
    self.assertEqual(len(model.aux_hidden_layers), 0)
    self.assertIs(model.time_series_features["time_feature_1"],
                  model.pre_logits_concat)
  def testZeroHiddenLayers(self):
    # Build config.
    feature_spec = {
        "time_feature_1": {
            "length": 10,
            "is_time_series": True,
        },
        "time_feature_2": {
            "length": 10,
            "is_time_series": True,
        },
        "aux_feature_1": {
            "length": 1,
            "is_time_series": False,
        },
    }
    config = configurations.base()
    config["inputs"]["features"] = feature_spec
    config = configdict.ConfigDict(config)
    config.hparams.output_dim = 1
    config.hparams.num_pre_logits_hidden_layers = 0

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    model = astro_model.AstroModel(features, labels, config.hparams,
                                   tf.estimator.ModeKeys.TRAIN)
    model.build()

    # Validate Tensor shapes.
    self.assertShapeEquals((None, 21), model.pre_logits_concat)
    logits_w = testing.get_variable_by_name("logits/kernel")
    self.assertShapeEquals((21, 1), logits_w)
  def testInvalidModeRaisesError(self):
    # Build config.
    config = configdict.ConfigDict(configurations.base())

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    with self.assertRaises(ValueError):
      _ = astro_model.AstroModel(features, labels, config.hparams, "training")
示例#8
0
  def testInvalidModeRaisesError(self):
    # Build config.
    config = configdict.ConfigDict(configurations.base())

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    with self.assertRaises(ValueError):
      _ = astro_model.AstroModel(features, labels, config.hparams, "training")
  def testZeroFeaturesRaisesError(self):
    # Build config.
    config = configurations.base()
    config["inputs"]["features"] = {}
    config = configdict.ConfigDict(config)

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    model = astro_model.AstroModel(features, labels, config.hparams,
                                   tf.estimator.ModeKeys.TRAIN)
    with self.assertRaises(ValueError):
      # Raises ValueError because at least one feature is required.
      model.build()
示例#10
0
def local_global():
  """Base configuration for a CNN model with separate local/global views."""
  config = parent_configs.base()

  # Override the model features to be local_view and global_view time series.
  config["inputs"]["features"] = {
      "local_view": {
          "length": 51,
          "is_time_series": True,
      },
      "global_view": {
          "length": 701,
          "is_time_series": True,
      },
      "tce_depth": {
          "length": 1,
          "is_time_series": False,
      },
      "tce_impact": {
          "length": 1,
          "is_time_series": False,
      }
  }

  # Add configurations for the convolutional layers of time series features.
  config["hparams"]["time_series_hidden"] = {
      "local_view": {
          "cnn_num_blocks": 2,
          "cnn_block_size": 2,
          "cnn_initial_num_filters": 16,
          "cnn_block_filter_factor": 2,
          "cnn_kernel_size": 5,
          "convolution_padding": "same",
          "pool_size": 3,
          "pool_strides": 2,
      },
      "global_view": {
          "cnn_num_blocks": 4,
          "cnn_block_size": 2,
          "cnn_initial_num_filters": 16,
          "cnn_block_filter_factor": 2,
          "cnn_kernel_size": 5,
          "convolution_padding": "same",
          "pool_size": 3,
          "pool_strides": 2,
      },
  }
  config["hparams"]["num_pre_logits_hidden_layers"] = 4
  config["hparams"]["pre_logits_hidden_layer_size"] = 512
  return config
示例#11
0
  def testZeroFeaturesRaisesError(self):
    # Build config.
    config = configurations.base()
    config["inputs"]["features"] = {}
    config = configdict.ConfigDict(config)

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    model = astro_model.AstroModel(features, labels, config.hparams,
                                   tf.estimator.ModeKeys.TRAIN)
    with self.assertRaises(ValueError):
      # Raises ValueError because at least one feature is required.
      model.build()
示例#12
0
    def testEvalMode(self):
        # Build config.
        feature_spec = {
            "time_feature_1": {
                "length": 10,
                "is_time_series": True,
            },
            "time_feature_2": {
                "length": 10,
                "is_time_series": True,
            },
            "aux_feature_1": {
                "length": 1,
                "is_time_series": False,
            },
        }
        config = configurations.base()
        config["inputs"]["features"] = feature_spec
        config = configdict.ConfigDict(config)
        config.hparams.output_dim = 1

        # Build model.
        features = input_ops.build_feature_placeholders(config.inputs.features)
        labels = input_ops.build_labels_placeholder()
        model = astro_model.AstroModel(features, labels, config.hparams,
                                       tf.estimator.ModeKeys.TRAIN)
        model.build()

        # Validate Tensor shapes.
        self.assertShapeEquals((None, 21), model.pre_logits_concat)
        self.assertShapeEquals((None, 1), model.logits)
        self.assertShapeEquals((None, 1), model.predictions)
        self.assertShapeEquals((None, ), model.batch_losses)
        self.assertShapeEquals((), model.total_loss)

        # Execute the TensorFlow graph.
        scaffold = tf.train.Scaffold()
        scaffold.finalize()
        with self.session() as sess:
            sess.run([scaffold.init_op, scaffold.local_init_op])
            step = sess.run(model.global_step)
            self.assertEqual(0, step)

            # Fetch total loss.
            features = testing.fake_features(feature_spec, batch_size=16)
            labels = testing.fake_labels(config.hparams.output_dim,
                                         batch_size=16)
            feed_dict = input_ops.prepare_feed_dict(model, features, labels)
            total_loss = sess.run(model.total_loss, feed_dict=feed_dict)
            self.assertShapeEquals((), total_loss)
示例#13
0
  def testEvalMode(self):
    # Build config.
    feature_spec = {
        "time_feature_1": {
            "length": 10,
            "is_time_series": True,
        },
        "time_feature_2": {
            "length": 10,
            "is_time_series": True,
        },
        "aux_feature_1": {
            "length": 1,
            "is_time_series": False,
        },
    }
    config = configurations.base()
    config["inputs"]["features"] = feature_spec
    config = configdict.ConfigDict(config)
    config.hparams.output_dim = 1

    # Build model.
    features = input_ops.build_feature_placeholders(config.inputs.features)
    labels = input_ops.build_labels_placeholder()
    model = astro_model.AstroModel(features, labels, config.hparams,
                                   tf.estimator.ModeKeys.TRAIN)
    model.build()

    # Validate Tensor shapes.
    self.assertShapeEquals((None, 21), model.pre_logits_concat)
    self.assertShapeEquals((None, 1), model.logits)
    self.assertShapeEquals((None, 1), model.predictions)
    self.assertShapeEquals((None,), model.batch_losses)
    self.assertShapeEquals((), model.total_loss)

    # Execute the TensorFlow graph.
    scaffold = tf.train.Scaffold()
    scaffold.finalize()
    with self.test_session() as sess:
      sess.run([scaffold.init_op, scaffold.local_init_op])
      step = sess.run(model.global_step)
      self.assertEqual(0, step)

      # Fetch total loss.
      features = testing.fake_features(feature_spec, batch_size=16)
      labels = testing.fake_labels(config.hparams.output_dim, batch_size=16)
      feed_dict = input_ops.prepare_feed_dict(model, features, labels)
      total_loss = sess.run(model.total_loss, feed_dict=feed_dict)
      self.assertShapeEquals((), total_loss)
示例#14
0
    def testTwoHiddenLayers(self):
        # Build config.
        feature_spec = {
            "time_feature_1": {
                "length": 10,
                "is_time_series": True,
            },
            "time_feature_2": {
                "length": 10,
                "is_time_series": True,
            },
            "aux_feature_1": {
                "length": 1,
                "is_time_series": False,
            },
        }
        config = configurations.base()
        config["inputs"]["features"] = feature_spec
        config = configdict.ConfigDict(config)
        config.hparams.output_dim = 1
        config.hparams.num_pre_logits_hidden_layers = 2
        config.hparams.pre_logits_hidden_layer_size = 5

        # Build model.
        features = input_ops.build_feature_placeholders(config.inputs.features)
        labels = input_ops.build_labels_placeholder()
        model = astro_model.AstroModel(features, labels, config.hparams,
                                       tf.estimator.ModeKeys.TRAIN)
        model.build()

        # TODO(shallue): TensorFlow 2.0 doesn't have global variable collections.
        # If we want to keep testing variable shapes in 2.0, we must keep track of
        # the individual Keras Layer objects in the model class.
        variables = {v.op.name: v for v in tf.global_variables()}

        # Validate Tensor shapes.
        self.assertShapeEquals((None, 21), model.pre_logits_concat)
        fc1 = variables["pre_logits_hidden/fully_connected_1/kernel"]
        self.assertShapeEquals((21, 5), fc1)
        fc2 = variables["pre_logits_hidden/fully_connected_2/kernel"]
        self.assertShapeEquals((5, 5), fc2)
        logits_w = variables["logits/kernel"]
        self.assertShapeEquals((5, 1), logits_w)
示例#15
0
def local_global():
  """Base configuration for a CNN model with separate local/global views."""
  config = parent_configs.base()

  # Override the model features to be local_view and global_view time series.
  config["inputs"]["features"] = {
      "local_view": {
          "length": 201,
          "is_time_series": True,
      },
      "global_view": {
          "length": 2001,
          "is_time_series": True,
      },
  }

  # Add configurations for the convolutional layers of time series features.
  config["hparams"]["time_series_hidden"] = {
      "local_view": {
          "cnn_num_blocks": 2,
          "cnn_block_size": 2,
          "cnn_initial_num_filters": 16,
          "cnn_block_filter_factor": 2,
          "cnn_kernel_size": 5,
          "convolution_padding": "same",
          "pool_size": 7,
          "pool_strides": 2,
      },
      "global_view": {
          "cnn_num_blocks": 5,
          "cnn_block_size": 2,
          "cnn_initial_num_filters": 16,
          "cnn_block_filter_factor": 2,
          "cnn_kernel_size": 5,
          "convolution_padding": "same",
          "pool_size": 5,
          "pool_strides": 2,
      },
  }
  config["hparams"]["num_pre_logits_hidden_layers"] = 4
  config["hparams"]["pre_logits_hidden_layer_size"] = 512
  return config
示例#16
0
def base():
  """Base configuration for a CNN model with a single global view."""
  config = parent_configs.base()

  # Add configuration for the convolutional layers of the global_view feature.
  config["hparams"]["time_series_hidden"] = {
      "global_view": {
          "cnn_num_blocks": 5,
          "cnn_block_size": 2,
          "cnn_initial_num_filters": 16,
          "cnn_block_filter_factor": 2,
          "cnn_kernel_size": 5,
          "convolution_padding": "same",
          "pool_size": 5,
          "pool_strides": 2,
      },
  }
  config["hparams"]["num_pre_logits_hidden_layers"] = 4
  config["hparams"]["pre_logits_hidden_layer_size"] = 1024
  return config
示例#17
0
def base():
    """Base configuration for an RNN model with a single global view."""
    config = parent_configs.base()

    # Add configuration for the recurrent layers of the global_view feature.
    config["hparams"]["time_series_hidden"] = {
        "global_view": {
            "rnn_num_layers": 3,
            "rnn_num_units": 128,
            "rnn_memory_cells":
            None,  # None for standard RNN or "lstm" or "gru"
            "rnn_activation":
            "tanh",  # "tanh" or "relu". ReLU not available for CuDNN LSTM/GRU.
            "rnn_dropout": 0.0,
            "rnn_direction": "uni"  # "uni" or "bi"
        },
    }
    config["hparams"]["num_pre_logits_hidden_layers"] = 2
    config["hparams"]["pre_logits_hidden_layer_size"] = 128
    return config
示例#18
0
def base():
    """Base configuration for a CNN model with a single global view."""
    config = parent_configs.base()

    # Add configuration for the convolutional layers of the global_view feature.
    config["hparams"]["time_series_hidden"] = {
        "global_view": {
            "cnn_num_blocks": 5,
            "cnn_block_size": 2,
            "cnn_initial_num_filters": 16,
            "cnn_block_filter_factor": 2,
            "cnn_kernel_size": 5,
            "convolution_padding": "same",
            "pool_size": 5,
            "pool_strides": 2,
        },
    }
    config["hparams"]["num_pre_logits_hidden_layers"] = 4
    config["hparams"]["pre_logits_hidden_layer_size"] = 1024
    return config
示例#19
0
def local_global():
    """Base configuration for an RNN model with separate local/global views."""
    config = parent_configs.base()

    # Override the model features to be local_view and global_view time series.
    config["inputs"]["features"] = {
        "local_view": {
            "length": 201,
            "is_time_series": True,
        },
        "global_view": {
            "length": 2001,
            "is_time_series": True,
        },
    }
    # Add configuration for the recurrent layers of the local_view feature.
    config["hparams"]["time_series_hidden"] = {
        "local_view": {
            "rnn_num_layers": 2,
            "rnn_num_units": 128,
            "rnn_memory_cells":
            "lstm",  # None for standard RNN or "lstm" or "gru"
            "rnn_activation":
            "tanh",  # "tanh" or "relu". ReLU not available for CuDNN LSTM/GRU.
            "rnn_dropout": 0.3,
            "rnn_direction": "uni"  # "uni" or "bi"
        },
        "global_view": {
            "rnn_num_layers": 3,
            "rnn_num_units": 128,
            "rnn_memory_cells":
            "lstm",  # None for standard RNN or "lstm" or "gru"
            "rnn_activation":
            "tanh",  # "tanh" or "relu". ReLU not available for CuDNN LSTM/GRU.
            "rnn_dropout": 0.3,
            "rnn_direction": "uni"  # "uni" or "bi"
        },
    }
    config["hparams"]["num_pre_logits_hidden_layers"] = 2
    config["hparams"]["pre_logits_hidden_layer_size"] = 128
    return config
示例#20
0
def base():
  """Base config for a fully connected model with a single global view."""
  config = parent_configs.base()

  # Add configuration for the fully-connected layers of the global_view feature.
  config["hparams"]["time_series_hidden"] = {
      "global_view": {
          "num_local_layers": 0,
          "local_layer_size": 128,

          # If > 0, the first layer is implemented as a wide convolutional layer
          # for invariance to small translations.
          "translation_delta": 0,

          # Pooling type following the wide convolutional layer.
          "pooling_type": "max",

          # Dropout rate for the fully connected layers.
          "dropout_rate": 0.0,
      },
  }
  return config
示例#21
0
def base():
    """Base config for a fully connected model with a single global view."""
    config = parent_configs.base()

    # Add configuration for the fully-connected layers of the global_view feature.
    config["hparams"]["time_series_hidden"] = {
        "global_view": {
            "num_local_layers": 0,
            "local_layer_size": 128,

            # If > 0, the first layer is implemented as a wide convolutional layer
            # for invariance to small translations.
            "translation_delta": 0,

            # Pooling type following the wide convolutional layer.
            "pooling_type": "max",

            # Dropout rate for the fully connected layers.
            "dropout_rate": 0.0,
        },
    }
    return config
示例#22
0
def local_global():
    """Base config for a locally fully connected model with local/global views."""
    config = parent_configs.base()

    # Override the model features to be local_view and global_view time series.
    config["inputs"]["features"] = {
        "local_view": {
            "length": 201,
            "is_time_series": True,
            "subcomponents": [],
        },
        "global_view": {
            "length": 2001,
            "is_time_series": True,
            "subcomponents": [],
        },
    }

    # Add configurations for the fully-connected layers of time series features.
    config["hparams"]["time_series_hidden"] = {
        "local_view": {
            "num_local_layers": 0,
            "local_layer_size": 128,
            "translation_delta": 0,  # For wide convolution.
            "pooling_type": "max",  # For wide convolution.
            "dropout_rate": 0.0,
        },
        "global_view": {
            "num_local_layers": 0,
            "local_layer_size": 128,
            "translation_delta": 0,  # For wide convolution.
            "pooling_type": "max",  # For wide convolution.
            "dropout_rate": 0.0,
        },
    }
    return config
示例#23
0
def local_global():
  """Base config for a locally fully connected model with local/global views."""
  config = parent_configs.base()

  # Override the model features to be local_view and global_view time series.
  config["inputs"]["features"] = {
      "local_view": {
          "length": 201,
          "is_time_series": True,
      },
      "global_view": {
          "length": 2001,
          "name_in_proto": "light_curve",
          "is_time_series": True,
          "data_source": "",
      },
  }

  # Add configurations for the fully-connected layers of time series features.
  config["hparams"]["time_series_hidden"] = {
      "local_view": {
          "num_local_layers": 0,
          "local_layer_size": 128,
          "translation_delta": 0,  # For wide convolution.
          "pooling_type": "max",  # For wide convolution.
          "dropout_rate": 0.0,
      },
      "global_view": {
          "num_local_layers": 0,
          "local_layer_size": 128,
          "translation_delta": 0,  # For wide convolution.
          "pooling_type": "max",  # For wide convolution.
          "dropout_rate": 0.0,
      },
  }
  return config
def local_global():
  """Base configuration for a CNN model with separate local/global views."""
  config = parent_configs.base()

  # Override the model features to be local_view and global_view time series.
  config["inputs"]["features"] = {
      "local_view": {
          "length": 61,
          "is_time_series": True,
      },
      "global_view": {
          "length": 201,
          "is_time_series": True,
      },
      # "secondary_view": {
      #     "length": 81,
      #     "is_time_series": True,
      # },
      # "tce_steff": {
      #     "length": 1,
      #     "is_time_series": False,
      # },
      # "tce_slogg": {
      #     "length": 1,
      #     "is_time_series": False,
      # },
      # "tce_sradius": {
      #     "length": 1,
      #     "is_time_series": False,
      # },
      # "Period": {
      #     "length": 1,
      #     "is_time_series": False,
      # },
  }

  # Add configurations for the convolutional layers of time series features.
  # need fine tuning!
  config["hparams"]["time_series_hidden"] = {
      "local_view": {
          "cnn_num_blocks": 2,
          "cnn_block_size": 2,
          "cnn_initial_num_filters": 16,
          "cnn_block_filter_factor": 2,
          "cnn_kernel_size": 5,
          "convolution_padding": "same",
          "pool_size": 7,
          "pool_strides": 2,
      },
      "global_view": {
          "cnn_num_blocks": 5,
          "cnn_block_size": 2,
          "cnn_initial_num_filters": 16,
          "cnn_block_filter_factor": 2,
          "cnn_kernel_size": 5,
          "convolution_padding": "same",
          "pool_size": 5,
          "pool_strides": 2,
      },
      # "secondary_view": {
      #     "cnn_num_blocks": 2,
      #     "cnn_block_size": 2,
      #     "cnn_initial_num_filters": 16,
      #     "cnn_block_filter_factor": 2,
      #     "cnn_kernel_size": 5,
      #     "convolution_padding": "same",
      #     "pool_size": 7,
      #     "pool_strides": 2,
      # },
  }
  config["hparams"]["num_pre_logits_hidden_layers"] = 4
  config["hparams"]["pre_logits_hidden_layer_size"] = 512
  return config