Skip to content

Commit

Permalink
Simplify numpy_to_input, removing unused no_reshape and setting gridp…
Browse files Browse the repository at this point in the history
…oints to None in shape always
  • Loading branch information
APJansen committed Jul 5, 2023
1 parent f82b407 commit 80ef5d7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 26 deletions.
23 changes: 6 additions & 17 deletions n3fit/src/n3fit/backends/keras_backend/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ def batchit(x, batch_dimension=0, **kwarg):
# layer generation
def numpy_to_input(
numpy_array: np.ndarray,
no_reshape: bool = False,
name: str = None,
custom_shape: tuple = None,
):
"""
Takes a numpy array and generates a Input layer.
Expand All @@ -126,26 +124,17 @@ def numpy_to_input(
Parameters
----------
numpy_array: np.ndarray
no_reshape: bool
if true, don't add batch dimension, take the first dimension of the array as the batch
name: bool
name to give to the layer
custom_shape: tuple
To specify a more general shape with None values
"""
if no_reshape:
batched_array = numpy_array
batch_size = numpy_array.shape[0]
shape = numpy_array.shape[1:]
else:
batched_array = np.expand_dims(numpy_array, 0)
batch_size = 1
shape = numpy_array.shape
if custom_shape is not None:
shape = custom_shape
batched_array = np.expand_dims(numpy_array, 0)
batch_size = 1
shape = list(numpy_array.shape)
# set the number of gridpoints to None, otherwise shapes don't show in model.summary
shape[0] = None

input_layer = Input(batch_size=batch_size, shape=shape, name=name)
input_layer.tensor_content = batched_array
input_layer.original_shape = no_reshape
return input_layer


Expand Down
4 changes: 1 addition & 3 deletions n3fit/src/n3fit/model_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,7 @@ def pdfNN_layer_generator(
input_x_eq_1 = scaler(input_x_eq_1)[0]
# the layer that subtracts 1 from the NN output
subtract_one_layer = Lambda(op.op_subtract, name='subtract_one')
layer_x_eq_1 = op.numpy_to_input(
np.array(input_x_eq_1).reshape(1, 1), name='x_ones', custom_shape=(None, 1)
) # Just to make shapes consistent
layer_x_eq_1 = op.numpy_to_input(np.array(input_x_eq_1).reshape(1, 1), name='x_eq_1')
model_input["layer_x_eq_1"] = layer_x_eq_1

# the layer that multiplies the NN output by the preprocessing factor
Expand Down
8 changes: 2 additions & 6 deletions n3fit/src/n3fit/msr.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,8 @@ def generate_msr_model_and_grid(
if scaler:
xgrid_integration = scaler(xgrid_integration)

# Turn into input layer. Specify a custom shape here using None,
# so shapes will display properly in the model summary
grid_shape = 2 if scaler is not None else 1
xgrid_integration = op.numpy_to_input(
xgrid_integration, name="integration_grid", custom_shape=(None, grid_shape)
)
# Turn into input layer.
xgrid_integration = op.numpy_to_input(xgrid_integration, name="integration_grid")

# 1c Get the original grid
if scaler:
Expand Down

0 comments on commit 80ef5d7

Please sign in to comment.