def feed_dict_builder(X, Y, net_inputs, net_targets): """ Format provided data to a dictionary format compatible with Tensorflow data feeding. It match all X and Y data provided with net_inputs and net_targets provided placeholders. In case of inputs data list, matching is made respectively. Examples: ```python # Building feed dictionary >> feed_dict = feed_dict_builder(X, Y, input1, output1) >> {input1: X, output1: Y} >> feed_dict = feed_dict_builder({input1: X}, Y, input1, output1) >> {input1: X, output1: Y} >> feed_dict = feed_dict_builder([X1, X2], Y, [in1, in2], out1) >> {in1: X1, in2: X2, output1: Y} # For validation split: >> val_feed_dict = feed_dict_builder(0.1, 0.1, input1, output1) >> {input1: 0.1, output1: 0.1} ``` Arguments: X: `array` or `dict`. The input data. Y: `array`, `dict` or `float`. The targets (labels). net_inputs: `list`. The network data inputs `Placeholders`. net_targets: `list`. The network targets `Placeholders`. Returns: `dict`. A Tensorflow-ready dictionary to feed data. Raises: Exception if X and net_inputs or Y and net_targets list length doesn't match. """ feed_dict = {} if not (is_none(X) or is_none(net_inputs)): # If input data are not a dict, we match them by creation order if not isinstance(X, dict): # If validation split, copy that value to the whole placeholders if isinstance(X, float): X = [X for _i in net_inputs] elif len(net_inputs) > 1: try: #TODO: Fix brodcast issue if different if np.ndim(X) < 2: raise ValueError("Multiple inputs but only one data " "feeded. Please verify number of " "inputs and data provided match.") elif len(X) != len(net_inputs): raise Exception( str(len(X)) + " inputs feeded, " "but expected: " + str(len(net_inputs)) + ". If you are using notebooks, please " "make sure that you didn't run graph " "construction cell multiple time, " "or try to enclose your graph within " "`with tf.Graph().as_default():` or " "use `tf.reset_default_graph()`") except Exception: # Skip verif pass else: X = [X] for i, x in enumerate(X): feed_dict[net_inputs[i]] = x else: # If a dict is provided for key, val in X.items(): # Do nothing if dict already fits {placeholder: data} template if isinstance(key, tf.Tensor): continue else: # Else retrieve placeholder with its name var = vs.get_inputs_placeholder_by_name(key) if var is None: raise Exception( "Feed dict asks for variable named '%s' but no " "such variable is known to exist" % key) feed_dict[var] = val if not (is_none(Y) or is_none(net_targets)): if not isinstance(Y, dict): # Verify network has targets if len(net_targets) == 0: return feed_dict # If validation split, copy that value to every target placeholder. if isinstance(Y, float): Y = [Y for _t in net_targets] elif len(net_targets) > 1: try: #TODO: Fix brodcast issue if different if np.ndim(Y) < 2: raise ValueError( "Multiple outputs but only one data " "feeded. Please verify number of outputs " "and data provided match.") elif len(Y) != len(net_targets): raise Exception( str(len(Y)) + " outputs feeded, " "but expected: " + str(len(net_targets))) except Exception: # skip verif pass else: Y = [Y] for i, y in enumerate(Y): feed_dict[net_targets[i]] = y else: # If a dict is provided for key, val in Y.items(): # Do nothing if dict already fits {placeholder: data} template if isinstance(key, tf.Tensor): continue else: # Else retrieve placeholder with its name var = vs.get_targets_placeholder_by_name(key) if var is None: raise Exception( "Feed dict asks for variable named '%s' but no " "such variable is known to exist" % key) feed_dict[var] = val return feed_dict
def feed_dict_builder(X, Y, net_inputs, net_targets): """ Format provided data to a dictionary format compatible with Tensorflow data feeding. It match all X and Y data provided with net_inputs and net_targets provided placeholders. In case of inputs data list, matching is made respectively. Examples: ```python # Building feed dictionary >> feed_dict = feed_dict_builder(X, Y, input1, output1) >> {input1: X, output1: Y} >> feed_dict = feed_dict_builder({input1: X}, Y, input1, output1) >> {input1: X, output1: Y} >> feed_dict = feed_dict_builder([X1, X2], Y, [in1, in2], out1) >> {in1: X1, in2: X2, output1: Y} # For validation split: >> val_feed_dict = feed_dict_builder(0.1, 0.1, input1, output1) >> {input1: 0.1, output1: 0.1} ``` Arguments: X: `array` or `dict`. The input data. Y: `array`, `dict` or `float`. The targets (labels). net_inputs: `list`. The network data inputs `Placeholders`. net_targets: `list`. The network targets `Placeholders`. Returns: `dict`. A Tensorflow-ready dictionary to feed data. Raises: Exception if X and net_inputs or Y and net_targets list length doesn't match. """ feed_dict = {} if not (is_none(X) or is_none(net_inputs)): # If input data are not a dict, we match them by creation order if not isinstance(X, dict): # If validation split, copy that value to the whole placeholders if isinstance(X, float): X = [X for _i in net_inputs] elif len(net_inputs) > 1: if np.ndim(X) < 2: raise ValueError("Multiple inputs but only one data " "feeded. Please verify number of inputs " "and data provided match.") elif len(X) != len(net_inputs): raise Exception(str(len(X)) + " inputs feeded, " "but expected: " + str(len(net_inputs)) + ". If you are using notebooks, please " "make sure that you didn't run graph " "construction cell multiple time, " "or try to enclose your graph within " "`with tf.Graph().as_default():`") else: X = [X] for i, x in enumerate(X): feed_dict[net_inputs[i]] = x else: # If a dict is provided for key, val in X.items(): # Do nothing if dict already fits {placeholder: data} template if isinstance(key, tf.Tensor): continue else: # Else retrieve placeholder with its name feed_dict[vs.get_inputs_placeholder_by_name(key)] = val if not (is_none(Y) or is_none(net_targets)): if not isinstance(Y, dict): # Verify network has targets if len(net_targets) == 0: return feed_dict # If validation split, copy that value to every target placeholder. if isinstance(Y, float): Y = [Y for _t in net_targets] elif len(net_targets) > 1: if np.ndim(Y) < 2: raise ValueError("Multiple outputs but only one data " "feeded. Please verify number of outputs " "and data provided match.") elif len(Y) != len(net_targets): raise Exception(str(len(Y)) + " outputs feeded, " "but expected: " + str(len(net_targets))) else: Y = [Y] for i, y in enumerate(Y): feed_dict[net_targets[i]] = y else: # If a dict is provided for key, val in Y.items(): # Do nothing if dict already fits {placeholder: data} template if isinstance(key, tf.Tensor): continue else: # Else retrieve placeholder with its name feed_dict[vs.get_targets_placeholder_by_name(key)] = val return feed_dict
def feed_dict_builder(X, Y, net_inputs, net_targets): """ Format provided data to a dictionary format compatible with Tensorflow data feeding. It match all X and Y data provided with net_inputs and net_targets provided placeholders. In case of inputs data list, matching is made respectively. Examples: ```python # Building feed dictionary >> feed_dict = feed_dict_builder(X, Y, input1, output1) >> {input1: X, output1: Y} >> feed_dict = feed_dict_builder({input1: X}, Y, input1, output1) >> {input1: X, output1: Y} >> feed_dict = feed_dict_builder([X1, X2], Y, [in1, in2], out1) >> {in1: X1, in2: X2, output1: Y} # For validation split: >> val_feed_dict = feed_dict_builder(0.1, 0.1, input1, output1) >> {input1: 0.1, output1: 0.1} ``` Arguments: X: `array` or `dict`. The input data. Y: `array`, `dict` or `float`. The targets (labels). net_inputs: `list`. The network data inputs `Placeholders`. net_targets: `list`. The network targets `Placeholders`. Returns: `dict`. A Tensorflow-ready dictionary to feed data. Raises: Exception if X and net_inputs or Y and net_targets list length doesn't match. """ feed_dict = {} if not (is_none(X) or is_none(net_inputs)): # If input data are not a dict, we match them by creation order if not isinstance(X, dict): # If validation split, copy that value to the whole placeholders if isinstance(X, float): X = [X for _i in net_inputs] elif len(net_inputs) > 1: if np.ndim(X) < 2: raise ValueError("Multiple inputs but only one data " "feeded. Please verify number of inputs " "and data provided match.") elif len(X) != len(net_inputs): raise Exception( str(len(X)) + " inputs feeded, " "but expected: " + str(len(net_inputs))) else: X = [X] for i, x in enumerate(X): feed_dict[net_inputs[i]] = x else: # If a dict is provided for key, val in X.items(): # Do nothing if dict already fits {placeholder: data} template if isinstance(key, tf.Tensor): continue else: # Else retrieve placeholder with its name feed_dict[vs.get_inputs_placeholder_by_name(key)] = val if not (is_none(Y) or is_none(net_targets)): if not isinstance(Y, dict): # Verify network has targets if len(net_targets) == 0: return feed_dict # If validation split, copy that value to every target placeholder. if isinstance(Y, float): Y = [Y for _t in net_targets] elif len(net_targets) > 1: if np.ndim(Y) < 2: raise ValueError("Multiple outputs but only one data " "feeded. Please verify number of outputs " "and data provided match.") elif len(Y) != len(net_targets): raise Exception( str(len(Y)) + " outputs feeded, " "but expected: " + str(len(net_targets))) else: Y = [Y] for i, y in enumerate(Y): feed_dict[net_targets[i]] = y else: # If a dict is provided for key, val in Y.items(): # Do nothing if dict already fits {placeholder: data} template if isinstance(key, tf.Tensor): continue else: # Else retrieve placeholder with its name feed_dict[vs.get_targets_placeholder_by_name(key)] = val return feed_dict