def get_hyperparameter(name, initializer=None, shape=None, dtype=None, collections=None, scalar=False): """ Creates an hyperparameter variable, which is a GLOBAL_VARIABLE and HYPERPARAMETER. Mirrors the behavior of `tf.get_variable`. :param name: name of this hyperparameter :param initializer: initializer or initial value (can be also np.array or float) :param shape: optional shape, may be not needed depending on initializer :param dtype: optional type, may be not needed depending on initializer :param collections: optional additional collection or list of collections, which will be added to HYPERPARAMETER and GLOBAL_VARIABLES :param scalar: default False, if True splits the hyperparameter in its scalar components, i.e. each component will be a single scalar hyperparameter. In this case the method returns a tensor which of the desired shape (use this option with `ForwardHG`) :return: the newly created variable, or, if `scalar` is `True` a tensor composed by scalar variables. """ _coll = HYPERPARAMETERS_COLLECTIONS if collections: _coll += as_list(collections) if not scalar: return tf.get_variable(name, shape, dtype, initializer, trainable=False, collections=_coll) else: with tf.variable_scope(name + '_components'): _shape = shape or initializer.shape if isinstance(_shape, tf.TensorShape): _shape = _shape.as_list() # _tmp_lst = reduce(lambda a, v: [a]*v, shape[::-1], None) _tmp_lst = np.empty(_shape, object) for k in range(np.multiply.reduce(_shape)): indices = np.unravel_index(k, _shape) # print(indices) _ind_name = '_'.join([str(ind) for ind in indices]) _tmp_lst[indices] = tf.get_variable( _ind_name, (), dtype, initializer if callable(initializer) else initializer[indices], trainable=False, collections=_coll) return tf.convert_to_tensor(_tmp_lst.tolist(), name=name)
def get_hyperparameter(name, initializer=None, shape=None, dtype=None, collections=None, scalar=False): """ Creates an hyperparameter variable, which is a GLOBAL_VARIABLE and HYPERPARAMETER. Mirrors the behavior of `tf.get_variable`. :param name: name of this hyperparameter :param initializer: initializer or initial value (can be np.array or float) :param shape: optional shape, may be not needed depending on initializer :param dtype: optional type, may be not needed depending on initializer :param collections: optional additional collection or list of collections, which will be added to HYPERPARAMETER and GLOBAL_VARIABLES :param scalar: default False, if True splits the hyperparameter in its scalar components, i.e. each component will be a single scalar hyperparameter. In this case the method returns a tensor which of the desired shape (use this option with `ForwardHG`) :return: the newly created variable, or, if `scalar` is `True` a tensor composed by scalar variables. """ _coll = HYPERPARAMETERS_COLLECTIONS if collections: _coll += as_list(collections) if not scalar: return tf.get_variable(name, shape, dtype, initializer, trainable=False, collections=_coll) else: with tf.variable_scope(name + '_components'): _shape = shape or initializer.shape if isinstance(_shape, tf.TensorShape): _shape = _shape.as_list() # _tmp_lst = reduce(lambda a, v: [a]*v, shape[::-1], None) _tmp_lst = np.empty(_shape, object) for k in range(np.multiply.reduce(_shape)): indices = np.unravel_index(k, _shape) # print(indices) _ind_name = '_'.join([str(ind) for ind in indices]) _tmp_lst[indices] = tf.get_variable(_ind_name, (), dtype, initializer if callable(initializer) else initializer[indices], trainable=False, collections=_coll) return tf.convert_to_tensor(_tmp_lst.tolist(), name=name)