Skip to content
This repository has been archived by the owner on Aug 10, 2023. It is now read-only.

Updates rv_net code #17

Open
wants to merge 51 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
5018ece
Add initial RV stellar activity estimator NN
zdebeurs May 31, 2019
38f8f43
changed hparams to self.hparams
zdebeurs May 31, 2019
77e9fb5
changed hparams to self.hparams
zdebeurs May 31, 2019
d495698
changed hparams to self.hparams
zdebeurs May 31, 2019
63c0c8d
imported training from astronet.ops
zdebeurs May 31, 2019
fee434e
Add evaluation metrics
Jun 1, 2019
36252db
Fix root mean square labels/predictions
Jun 1, 2019
a9ce3eb
Add num_examples and num_example_batches to metrics.
Jun 1, 2019
90ce3c2
Fix variable_scope error.
Jun 1, 2019
236505d
Use integer types for counting metrics.
Jun 1, 2019
f08afff
Fix broadcasting bug by adding tf.squeeze.
Jun 1, 2019
35eee5a
Merge pull request #1 from cshallue/master
zdebeurs Jun 1, 2019
d47513e
Allow labels to be re-scaled
cshallue Jun 3, 2019
d5f52e0
Fix typo
cshallue Jun 3, 2019
fddc3d7
Make label_rescale_factor compulsory
cshallue Jun 3, 2019
f5c7f37
Merge pull request #2 from cshallue/master
zdebeurs Jun 4, 2019
cb74d6c
adds model summary functionality
zdebeurs Jul 16, 2019
0692c41
fixes small bug for predictions
zdebeurs Jul 16, 2019
6744064
fixes small bug for predictions
zdebeurs Jul 16, 2019
f001ceb
Adds model summary function
zdebeurs Sep 5, 2019
e3943e3
makes print statement more clear so models for each epoch are more ea…
zdebeurs Sep 5, 2019
bd3d632
prints max pooling layers in model.summary
zdebeurs Sep 5, 2019
bc10fc2
fixes minor spelling error
zdebeurs Sep 5, 2019
d52c654
adds functionality to extract ccf_data and label in addition to predi…
zdebeurs Sep 5, 2019
8398e12
Adds linear model as a test
zdebeurs Sep 13, 2019
427bd9c
Adds summary for linear model
zdebeurs Sep 13, 2019
6a4ef70
resolves syntax error
zdebeurs Sep 13, 2019
afbba93
resolves syntax error
zdebeurs Sep 13, 2019
f3149ab
resolves input shape error
zdebeurs Sep 13, 2019
cd68f8e
returns summary as attribute
zdebeurs Sep 13, 2019
3debe46
Modifies data format to fit data from HARPS-N Spectrograph
zdebeurs Jan 30, 2020
6cab461
adds a folder of images
zdebeurs Sep 19, 2020
63b3e5d
Create README.md
zdebeurs Sep 19, 2020
6f0ccd0
adds fixed gif
zdebeurs Sep 19, 2020
bf5cd04
Merge branch 'master' of https://github.com/zdebeurs/exoplanet-ml
zdebeurs Sep 19, 2020
9a3348b
Update README.md
zdebeurs Sep 19, 2020
48c4de2
Update README.md
zdebeurs Sep 19, 2020
f63656d
Add files via upload
zdebeurs May 26, 2021
1fe8106
Create mu_plotting
zdebeurs May 26, 2021
6c103e0
Rename mu_plotting to mu_plotting.py
zdebeurs May 26, 2021
4aea64a
Update mu_plotting.py
zdebeurs May 26, 2021
417a9fb
Add files via upload
zdebeurs May 26, 2021
eb79d5c
Added modifications for efficiency
zdebeurs May 26, 2021
81d4eb2
Add files via upload
zdebeurs May 26, 2021
2a743b8
Update data_HARPS_N.py
zdebeurs Jun 30, 2021
b6a688f
Update data_HARPS_N.py
zdebeurs Jun 30, 2021
bd918f2
Ds files
zdebeurs Jun 30, 2021
46f91a4
ds
zdebeurs Jun 30, 2021
4029ec9
Update Figure 4
zdebeurs Mar 24, 2022
e77ff1a
Update Figure 4
zdebeurs Mar 24, 2022
1a64257
Update README.md
zdebeurs Mar 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added Figure4.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added exoplanet-ml/.DS_Store
Binary file not shown.
Binary file added exoplanet-ml/astronet/.DS_Store
Binary file not shown.
Binary file added exoplanet-ml/rv_net/.DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions exoplanet-ml/rv_net/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Identifying Exoplanets with Deep Learning. IV. Removing Stellar Activity Signals from Radial Velocity Measurements Using Neural Networks

![HARSP Observations Animated](pics/Figure4.gif)

**Figure 1.** HARPS-N ΔCCFs -- Computed residual CCFs (ΔCCFs) by subtracting the mean CCF, highlighting differences in features between CCFs. For training the model, ΔCCFs is the input and the RV from stellar activity is the output. Radial velocity is indicated by its color (red = redshifted, blue = blueshifted)
Binary file not shown.
Binary file not shown.
Binary file not shown.
76 changes: 76 additions & 0 deletions exoplanet-ml/rv_net/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf


class DatasetBuilder(object):
"""Dataset builder class."""

def __init__(self, file_pattern, hparams, mode, repeat=1):
"""Initializes the dataset builder.

Args:
file_pattern: File pattern matching input file shards, e.g.
"/tmp/train-?????-of-00100".
hparams: A ConfigDict.
mode: A tf.estimator.ModeKeys.
repeat: The number of times to repeat the dataset. If None, the dataset
will repeat indefinitely.
"""
valid_modes = [
tf.estimator.ModeKeys.TRAIN, tf.estimator.ModeKeys.EVAL,
tf.estimator.ModeKeys.PREDICT
]
if mode not in valid_modes:
raise ValueError("Expected mode in {}. Got: {}".format(valid_modes, mode))

self.file_pattern = file_pattern
self.hparams = hparams
self.mode = mode
self.repeat = repeat

def __call__(self):
is_training = self.mode == tf.estimator.ModeKeys.TRAIN

# Dataset of file names.
filename_dataset = tf.data.Dataset.list_files(self.file_pattern,
shuffle=is_training)

# Dataset of serialized tf.Examples.
dataset = filename_dataset.flat_map(tf.data.TFRecordDataset)

# Shuffle in training mode.
if is_training:
dataset = dataset.shuffle(self.hparams.shuffle_values_buffer)

# Possibly repeat.
if self.repeat != 1:
dataset = dataset.repeat(self.repeat)

def _example_parser(serialized_example):
"""Parses a single tf.Example into feature and label tensors."""
data_fields = {
self.hparams.ccf_feature_name: tf.FixedLenFeature([401], tf.float32),
self.hparams.label_feature_name: tf.FixedLenFeature([], tf.float32),
}
parsed_fields = tf.parse_single_example(serialized_example, features=data_fields)
ccf_data = parsed_fields[self.hparams.ccf_feature_name]
label = parsed_fields[self.hparams.label_feature_name]
label *= self.hparams.label_rescale_factor # Rescale the label.
return {
"ccf_data": ccf_data,
"label": label,
}

# Map the parser over the dataset.
dataset = dataset.map(_example_parser, num_parallel_calls=4)

# Batch results by up to batch_size.
dataset = dataset.batch(self.hparams.batch_size)

# Prefetch a few batches.
dataset = dataset.prefetch(10)

return dataset
79 changes: 79 additions & 0 deletions exoplanet-ml/rv_net/data_HARPS_N.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf


class DatasetBuilder(object):
"""Dataset builder class."""

def __init__(self, file_pattern, hparams, mode, repeat=1):
"""Initializes the dataset builder.

Args:
file_pattern: File pattern matching input file shards, e.g.
"/tmp/train-?????-of-00100".
hparams: A ConfigDict.
mode: A tf.estimator.ModeKeys.
repeat: The number of times to repeat the dataset. If None, the dataset
will repeat indefinitely.
"""
valid_modes = [
tf.estimator.ModeKeys.TRAIN, tf.estimator.ModeKeys.EVAL,
tf.estimator.ModeKeys.PREDICT
]
if mode not in valid_modes:
raise ValueError("Expected mode in {}. Got: {}".format(valid_modes, mode))

self.file_pattern = file_pattern
self.hparams = hparams
self.mode = mode
self.repeat = repeat

def __call__(self):
is_training = self.mode == tf.estimator.ModeKeys.TRAIN

# Dataset of file names.
filename_dataset = tf.data.Dataset.list_files(self.file_pattern,
shuffle=is_training)

# Dataset of serialized tf.Examples.
dataset = filename_dataset.flat_map(tf.data.TFRecordDataset)

# Shuffle in training mode.
if is_training:
dataset = dataset.shuffle(self.hparams.shuffle_values_buffer)

# Possibly repeat.
if self.repeat != 1:
dataset = dataset.repeat(self.repeat)

def _example_parser(serialized_example):
"""Parses a single tf.Example into feature and label tensors."""
data_fields = {
self.hparams.ccf_feature_name: tf.io.FixedLenFeature([161], tf.float32),
self.hparams.label_feature_name: tf.io.FixedLenFeature([], tf.float32),
self.hparams.label_feature_name2: tf.io.FixedLenFeature([], tf.float32),
}
parsed_fields = tf.io.parse_single_example(serialized_example, features=data_fields)
ccf_data = parsed_fields[self.hparams.ccf_feature_name]
label = parsed_fields[self.hparams.label_feature_name]
label *= self.hparams.label_rescale_factor # Rescale the label.
label2 = parsed_fields[self.hparams.label_feature_name2]
return {
"ccf_data": ccf_data,
"label": label,
"bjd": label2,
}

# Map the parser over the dataset.
dataset = dataset.map(_example_parser, num_parallel_calls=4)

# Batch results by up to batch_size.
dataset = dataset.batch(self.hparams.batch_size)

# Prefetch a few batches.
dataset = dataset.prefetch(10)

return dataset
94 changes: 94 additions & 0 deletions exoplanet-ml/rv_net/estimator_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf

from astronet.ops import training

def create_learning_rate(hparams, global_step):
"""Creates a learning rate Tensor.

Args:
hparams: ConfigDict containing the learning rate configuration.
global_step: The global step Tensor.

Returns:
A learning rate Tensor.
"""
if hparams.get("learning_rate_decay_steps"):
# Linear decay from hparams.learning_rate to 0.
learning_rate = tf.train.polynomial_decay(
learning_rate=hparams.learning_rate,
global_step=global_step,
decay_steps=hparams.learning_rate_decay_steps,
end_learning_rate=0,
power=1.0)
else:
learning_rate = tf.constant(hparams.learning_rate)

return learning_rate


def sum_metric(values, name=None):
with tf.variable_scope(name, 'sum', (values,)):
values = tf.convert_to_tensor(values)
total = tf.get_variable(
'total',
initializer=tf.zeros([], dtype=values.dtype),
trainable=False,
collections=[tf.GraphKeys.LOCAL_VARIABLES,
tf.GraphKeys.METRIC_VARIABLES])
update_total = tf.assign_add(total, tf.reduce_sum(values))
return total.value(), update_total


class ModelFn(object):
"""Class that acts as a callable model function for Estimator train / eval."""

def __init__(self, model_class, hparams):
"""Initializes the model function.

Args:
model_class: Model class.
hparams: A HParams object containing hyperparameters for building and
training the model.
"""
self.model_class = model_class
self.hparams = hparams

def __call__(self, features, mode):
"""Builds the model and returns an EstimatorSpec."""
model = self.model_class(features, self.hparams, mode)
model.build()
print(model.summary)
print("___________ starting new epoch___________")
# Possibly create train_op.
train_op = None
if mode == tf.estimator.ModeKeys.TRAIN:
learning_rate = create_learning_rate(self.hparams, model.global_step)
optimizer = training.create_optimizer(self.hparams, learning_rate)
train_op = training.create_train_op(model, optimizer)

# Possibly create evaluation metrics.
eval_metrics = None
if mode == tf.estimator.ModeKeys.EVAL:
eval_metrics = {
"num_examples": sum_metric(tf.ones_like(model.label, dtype=tf.int32)),
"num_eval_batches": sum_metric(1),
"rmse": tf.metrics.root_mean_squared_error(
model.label, model.predicted_rv),
"root_mean_label": tf.metrics.root_mean_squared_error(
model.label, tf.zeros_like(model.label)),
"root_mean_pred": tf.metrics.root_mean_squared_error(
model.predicted_rv, tf.zeros_like(model.predicted_rv)),
}

return tf.estimator.EstimatorSpec(
mode=mode,
predictions={"ccf_data": model.ccf_data,
"label": model.label,
"predicted_rv": model.predicted_rv},
loss=model.total_loss,
train_op=train_op,
eval_metric_ops=eval_metrics)
1 change: 1 addition & 0 deletions exoplanet-ml/rv_net/exoplanet-ml
Submodule exoplanet-ml added at 63c0c8
Loading