forked from AlexanderFengler/nn_likelihoods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnnreg_model_keras.py
63 lines (52 loc) · 2.65 KB
/
dnnreg_model_keras.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import tensorflow as tf
from tensorflow import keras
import os
import pandas as pd
# Function asks for a dictionary as input with the following keys (and associated datatypes)
# params = {'input_shape': 3,
# 'output_shape': 1,
# 'output_activation': 'sigmoid',
# 'hidden_layers': [20, 20, 20],
# 'hidden_activations': ['relu', 'relu', 'relu'],
# 'l1_activation': [0.0, 0.0, 0.0],
# 'l2_activation': [0.0, 0.0, 0.0],
# 'l1_kernel': [0.0, 0.0, 0.0, 0.0],
# 'l2_kernel': [0.0, 0.0, 0.0, 0.0],
# 'optimizer': 'Nadam',
# 'loss': 'mse',
# 'metrics': ['mse'],
# 'batch_size': 100,
# 'max_epoch': 1000,
# 'eval_after_n_epochs': 10,
# 'data_type': 'choice_probabilities',
# 'model_directory': '',
# 'training_data_size': 'online'
# }
def kera_model_generate(params = {}):
# This returns a tensor
inputs = keras.layers.Input(shape = (params['input_shape'], ))
# Model hidden
op = keras.layers.Dense(params['hidden_layers'][0],
activation = params['hidden_activations'][0],
kernel_regularizer = keras.regularizers.l1_l2(l1 = params['l1_kernel'][0],
l2 = params['l2_kernel'][0]),
activity_regularizer = keras.regularizers.l1_l2(l1 = params['l1_activation'][0],
l2 = params['l2_activation'][0])
)(inputs)
for cnt in range(1, len(params['hidden_layers']), 1):
op = keras.layers.Dense(params['hidden_layers'][cnt],
activation = params['hidden_activations'][cnt],
kernel_regularizer = keras.regularizers.l1_l2(l1 = params['l1_kernel'][cnt],
l2 = params['l2_kernel'][cnt]),
activity_regularizer = keras.regularizers.l1_l2(l1 = params['l1_activation'][cnt],
l2 = params['l2_activation'][cnt]))(op)
# Model output
outputs = keras.layers.Dense(params['output_shape'], params['output_activation'])(op)
# Make model
model = keras.models.Model(inputs = inputs, outputs = outputs)
model.compile(
optimizer = model_params['optimizer'],
loss = model_params['loss'],
metrics = model_params['metrics']
)
return model