Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
41 changes: 24 additions & 17 deletions cosmopower/cosmopower_NN.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,32 @@ def __init__(self, parameters: Optional[Sequence[str]] = None,
self.alphas = []
self.betas = []
for i in range(self.n_layers):
self.W.append(tf.Variable(
tf.random.normal([
self.architecture[i], self.architecture[i + 1]
], 0.0, 1e-3),
name="W_" + str(i), trainable=trainable))
self.b.append(tf.Variable(tf.zeros([self.architecture[i + 1]]),
name="b_" + str(i),
trainable=trainable))
self.W.append(self.add_weight(
initializer=tf.keras.initializers.RandomNormal(stddev=1e-3),
shape=(self.architecture[i], self.architecture[i + 1]),
name="W_" + str(i),
trainable=trainable
))
self.b.append(self.add_weight(
initializer="zeros",
shape=(self.architecture[i + 1],),
name="b_" + str(i),
trainable=trainable
))

for i in range(self.n_layers - 1):
self.alphas.append(tf.Variable(
tf.random.normal([self.architecture[i + 1]]),
name="alphas_" + str(i), trainable=trainable))
self.betas.append(tf.Variable(
tf.random.normal([self.architecture[i + 1]]),
name="betas_" + str(i), trainable=trainable))
self.alphas.append(self.add_weight(
initializer="random_normal",
shape=(self.architecture[i + 1],),
name="alphas_" + str(i),
trainable=trainable
))
self.betas.append(self.add_weight(
initializer="random_normal",
shape=(self.architecture[i + 1],),
name="betas_" + str(i),
trainable=trainable
))

# restore weights if restoring
if restore_filename is not None:
Expand Down Expand Up @@ -546,7 +556,6 @@ def ten_to_rescaled_predictions_np(self, parameters_dict: dict
return 10.**self.rescaled_predictions_np(parameters_dict)

# Infrastructure for network training

@tf.function
def compute_loss(self, training_parameters: tf.Tensor,
training_features: tf.Tensor) -> tf.Tensor:
Expand Down Expand Up @@ -595,7 +604,6 @@ def compute_loss_and_gradients(self,
"""
# compute loss on the tape
with tf.GradientTape() as tape:

# loss
loss = tf.sqrt(
tf.reduce_mean(
Expand Down Expand Up @@ -888,7 +896,6 @@ def train(self, training_data: Sequence,
for epoch in t:
# loop over batches
for theta, feats in training_data:

# training step: check whether to accumulate gradients
# or not (only worth doing this for very large batch
# sizes)
Expand Down
43 changes: 27 additions & 16 deletions cosmopower/cosmopower_PCAplusNN.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,32 @@ def __init__(self, cp_pca: Optional[object] = None,
self.alphas = []
self.betas = []
for i in range(self.n_layers):
self.W.append(tf.Variable(
tf.random.normal([
self.architecture[i], self.architecture[i + 1]
], 0.0, np.sqrt(2.0 / self.n_parameters)),
name="W_" + str(i), trainable=trainable))
self.b.append(tf.Variable(tf.zeros([self.architecture[i + 1]]),
name="b_" + str(i),
trainable=trainable))
self.W.append(self.add_weight(
initializer=tf.keras.initializers.RandomNormal(stddev=1e-3),
shape=(self.architecture[i], self.architecture[i + 1]),
name="W_" + str(i),
trainable=trainable
))
self.b.append(self.add_weight(
initializer="zeros",
shape=(self.architecture[i + 1],),
name="b_" + str(i),
trainable=trainable
))

for i in range(self.n_layers - 1):
self.alphas.append(tf.Variable(
tf.random.normal([self.architecture[i + 1]]),
name="alphas_" + str(i), trainable=trainable))
self.betas.append(tf.Variable(
tf.random.normal([self.architecture[i + 1]]),
name="betas_" + str(i), trainable=trainable))
self.alphas.append(self.add_weight(
initializer="random_normal",
shape=(self.architecture[i + 1],),
name="alphas_" + str(i),
trainable=trainable
))
self.betas.append(self.add_weight(
initializer="random_normal",
shape=(self.architecture[i + 1],),
name="betas_" + str(i),
trainable=trainable
))

# restore weights if restoring
if restore_filename is not None:
Expand Down Expand Up @@ -923,8 +934,8 @@ def train(self, training_data: Sequence,
).shuffle(n_training).batch(batch_sizes[i])

# set up training loss
validation_loss = [np.infty]
best_loss = np.infty
validation_loss = [np.inf]
best_loss = np.inf
early_stopping_counter = 0

# loop over epochs
Expand Down
6 changes: 3 additions & 3 deletions cosmopower/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def read_parameters(self, indices: Union[int, np.ndarray]) -> dict:
cast_1d = (type(indices) is int)
indices = np.atleast_1d(indices)

entries = np.where(np.in1d(self.file["data"]["indices"][:],
entries = np.where(np.isin(self.file["data"]["indices"][:],
indices))[0]

parameters = {
Expand All @@ -179,7 +179,7 @@ def read_parameters(self, indices: Union[int, np.ndarray]) -> dict:
}

if cast_1d:
parameters = {k: float(v) for k, v in parameters.items()}
parameters = {k: float(v[0]) for k, v in parameters.items()}

return parameters

Expand All @@ -193,7 +193,7 @@ def read_spectra(self, indices: Union[int, np.ndarray]) -> np.ndarray:
cast_1d = (type(indices) is int)
indices = np.atleast_1d(indices)

entries = np.where(np.in1d(self.file["data"]["indices"][:],
entries = np.where(np.isin(self.file["data"]["indices"][:],
indices))[0]
spec = self.file["data"]["spectra"][entries, :]
if cast_1d:
Expand Down
11 changes: 6 additions & 5 deletions cosmopower/examples/1_create_training_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Author: A. Spurio Mancini, H. T. Jense

import tqdm
from tqdm import tqdm
import numpy as np
from cosmopower.parser import YAMLParser
from cosmopower.spectra import init_boltzmann_code, get_boltzmann_spectra
Expand Down Expand Up @@ -51,7 +51,8 @@
samples, validation_samples, testing_samples = \
parser.get_parameter_samples()

print(samples)
for k in samples:
print(k, samples[k][:10])

parser.save_samples_to_file(samples, validation_samples)

Expand Down Expand Up @@ -128,7 +129,7 @@
# This function returns False if the sample is invalid, so it's a
# simple check to discard invalid datapoints.
network_params = np.array([
params[k]
samples[k][n]
for k in parser.network_input_parameters("Pk/lin")
])
dataset.write_data(n, network_params,
Expand All @@ -147,7 +148,7 @@
# This function returns False if the sample is invalid, so it's a
# simple check to discard invalid datapoints.
network_params = np.array([
params[k]
samples[k][n]
for k in parser.network_input_parameters("Pk/lin")
])
dataset.write_data(n, network_params,
Expand All @@ -163,7 +164,7 @@
# This function returns False if the sample is invalid, so it's a
# simple check to discard invalid datapoints.
network_params = np.array([
params[k]
samples[k][n]
for k in parser.network_input_parameters("Pk/lin")
])
dataset.write_data(n, network_params,
Expand Down
2 changes: 1 addition & 1 deletion cosmopower/examples/2b_train_PCA_emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
network = cosmopower_PCAplusNN(cp_pca=pca, verbose=True, trainable=True,
**settings.get("n_traits", {}))

with tf.device("/device:CPU:0"):
with tf.device(None):
network.train(training_data=datasets,
filename_saved_model=output_file,
validation=validation,
Expand Down
2 changes: 1 addition & 1 deletion cosmopower/examples/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ path: example
emulated_code:
name: camb
# which parameters are passed to camb
inputs: [ombh2, omch2, H0, ns, As]
inputs: [ombh2, omch2, H0, ns, As, z]
extra_args:
# Some extra accuracy and speed parameters
kmax: 20.0
Expand Down
4 changes: 0 additions & 4 deletions cosmopower/examples/planck_cmb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ networks:
- quantity: "derived"
type: NN
inputs: [ombh2, omch2, cosmomc_theta, ns, logA, tau]
log: True
modes:
label: l
range: [2, 2508]
n_traits:
n_hidden: [512,512,512,512]
training:
Expand Down
6 changes: 3 additions & 3 deletions cosmopower/spectra.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def init_boltzmann_code(parser: YAMLParser) -> dict:
res = {}

code = parser.boltzmann_code
extra_args = parser.boltzmann_extra_args
extra_args = parser.boltzmann_extra_args or {}

# Try to import the correct python module for spectrum generation
try:
Expand Down Expand Up @@ -256,7 +256,7 @@ def generate_spectra(args: list = None) -> None:
f"files {first_file}--{last_file}.")

state = init_boltzmann_code(parser)
extra_args = parser.boltzmann_extra_args
extra_args = parser.boltzmann_extra_args or {}

accepted = 0
tbar = tqdm.tqdm(np.arange(first, last + 1))
Expand All @@ -265,7 +265,7 @@ def generate_spectra(args: list = None) -> None:

for n in tbar:
tbar.set_description(("" if MPI is None else f"[{rank}] ")
+ f"{accepted/max(n,1):.1%} success rate")
+ f"{accepted/max(n, 1):.1%} success rate")

boltzmann_params = {k: training[k][n] for k in parser.boltzmann_inputs}

Expand Down
24 changes: 12 additions & 12 deletions cosmopower/spectra_camb.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ def initialize(parser: YAMLParser, extra_args: dict = {}) -> dict:

# Check that the maximum values for mode ranges matches
# the requested outputs.
if parser.modes_label(quantity) == "l" and \
extra_args["lmax"] < parser.modes(quantity).max():
extra_args["lmax"] = parser.modes(quantity).max()
if parser.modes_label(quantity) == "l":
extra_args["lmax"] = max(extra_args["lmax"],
parser.modes(quantity).max())

if parser.modes_label(quantity) == "k" and \
extra_args["kmax"] < parser.modes(quantity).max():
extra_args["kmax"] = parser.modes(quantity).max()
if parser.modes_label(quantity) == "k":
extra_args["kmax"] = max(extra_args["kmax"],
parser.modes(quantity).max())

if parser.modes_label(quantity) == "z":
z1 = extra_args["redshifts"]
Expand Down Expand Up @@ -223,12 +223,12 @@ def get_spectra(parser: YAMLParser, state: dict, args: dict = {},
except (camb.CAMBError, camb.CAMBFortranError):
return False

state["derived"] = get_camb_derived(state["params"], state["results"],
list(state["derived"].keys()))

for quantity in quantities:
qpath = quantity.split("/")

state["derived"] = get_camb_derived(state["params"], state["results"],
list(state["derived"].keys()))

if qpath[0] == "derived":
continue
elif qpath[0] == "Cl":
Expand Down Expand Up @@ -275,14 +275,14 @@ def get_spectra(parser: YAMLParser, state: dict, args: dict = {},
state["params"], state["results"], parser.modes(qpath[0])
)
elif qpath[0] == "DA" or qpath[0] == "angular_diameter_distance":
# angular diameter distance
state[quantity] = get_camb_sigma8(
# angular diameter distance DA(z)
state[quantity] = get_camb_angular_diameter_distance(
state["params"], state["results"], parser.modes(qpath[0])
)
elif qpath[0] in ["Omega_b", "Omega_cdm"]:
# background evolution of densities
var = {"Omega_b": "baryon", "Omega_cdm": "cdm"}.get(qpath[0])
state[quantity] = get_camb_sigma8(
state[quantity] = get_camb_Omega(
state["params"], state["results"], parser.modes(qpath[0]), var
)
else:
Expand Down
Loading