我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.__name__()。
def create_input_layer(self, batch_input_shape, input_dtype=None, name=None): if not name: prefix = self.__class__.__name__.lower() + '_input_' name = prefix + str(K.get_uid(prefix)) if not input_dtype: input_dtype = K.floatx() self.batch_input_shape = batch_input_shape self.input_dtype = input_dtype # Instantiate the input layer. x = Input(batch_shape=batch_input_shape, dtype=input_dtype, name=name) # This will build the current layer # and create the node connecting the current layer # to the input layer we just created. self(x)
def to_json(self, **kwargs): '''Returns a JSON string containing the network configuration. To load a network from a JSON save file, use `keras.models.model_from_json(json_string, custom_objects={})`. ''' import json def get_json_type(obj): # If obj is any numpy type if type(obj).__module__ == np.__name__: return obj.item() # If obj is a python 'type' if type(obj).__name__ == type.__name__: return obj.__name__ raise TypeError('Not JSON Serializable:', obj) model_config = self._updated_config() return json.dumps(model_config, default=get_json_type, **kwargs)
def get_config(self): '''Returns the model configuration as a Python list. ''' config = [] if isinstance(self.layers[0], Merge): assert hasattr(self.layers[0], 'layers') layers = [] for layer in self.layers[0].layers: layer_config = {'class_name': layer.__class__.__name__, 'config': layer.get_config()} layers.append(layer_config) merge_config = self.layers[0].get_config() merge_config['layers'] = layers config.append({'class_name': 'Merge', 'config': merge_config}) else: config.append({'class_name': self.layers[0].__class__.__name__, 'config': self.layers[0].get_config()}) for layer in self.layers[1:]: config.append({'class_name': layer.__class__.__name__, 'config': layer.get_config()}) return copy.deepcopy(config)
def test_linear_regression_numpy(self): """ Test the linear regression using numpy (if installed). """ # test that numpy is installed try: import numpy numpy.__name__ except ImportError: raise unittest.SkipTest('cannot test as optional numpy is not installed') # perform the test with numpy from supvisors.utils import get_linear_regression, get_simple_linear_regression xdata = [2, 4, 6, 8, 10, 12] ydata = [3, 4, 5, 6, 7, 8] # test linear regression a, b = get_linear_regression(xdata, ydata) self.assertAlmostEqual(0.5, a) self.assertAlmostEqual(2.0, b) # test simple linear regression a, b = get_simple_linear_regression(ydata) self.assertAlmostEqual(1.0, a) self.assertAlmostEqual(3.0, b)
def to_json(self, **kwargs): """Returns a JSON string containing the network configuration. To load a network from a JSON save file, use `keras.models.model_from_json(json_string, custom_objects={})`. """ import json def get_json_type(obj): # If obj is any numpy type if type(obj).__module__ == np.__name__: return obj.item() # If obj is a python 'type' if type(obj).__name__ == type.__name__: return obj.__name__ raise TypeError('Not JSON Serializable:', obj) model_config = self._updated_config() return json.dumps(model_config, default=get_json_type, **kwargs)
def get_config(self): """Returns the model configuration as a Python list. """ config = [] if isinstance(self.layers[0], Merge): assert hasattr(self.layers[0], 'layers') layers = [] for layer in self.layers[0].layers: layer_config = {'class_name': layer.__class__.__name__, 'config': layer.get_config()} layers.append(layer_config) merge_config = self.layers[0].get_config() merge_config['layers'] = layers config.append({'class_name': 'Merge', 'config': merge_config}) else: config.append({'class_name': self.layers[0].__class__.__name__, 'config': self.layers[0].get_config()}) for layer in self.layers[1:]: config.append({'class_name': layer.__class__.__name__, 'config': layer.get_config()}) return copy.deepcopy(config)
def __init__(self, xs, ys, name=None): """ Input data sets. :param xs: data, shape(n_samples, n_xs), accept numpy, pandas, list :param ys: labels, shape(n_samples, n_ys), accept numpy, pandas, list """ if (type(xs).__module__ == np.__name__) & (type(ys).__module__ == np.__name__): self.module = 'numpy_data' elif ('pandas' in type(xs).__module__) & ('pandas' in type(ys).__module__): xs, ys = np.asarray(xs), np.asarray(ys) elif (type(xs) == list) & (type(ys) == list): xs, ys = np.asarray(xs), np.asarray(ys) else: raise TypeError('all data type must be numpy or pandas') if ys.ndim < 2: ys = ys[:, np.newaxis] if xs.ndim < 2: xs = xs[:, np.newaxis] self.n_xfeatures = xs.shape[-1] # col for 2 dims, channel for 3 dims self.n_yfeatures = ys.shape[-1] # col for 2 dims, self.data = np.hstack((xs, ys)) self.n_samples = ys.shape[0] self.name = name
def serialise_dict(dd): out = {} for kk, vv in dd.iteritems(): if isinstance(vv, type): continue try: if vv is None: out[kk] = None elif type(vv).__module__ == numpy_name: out[kk] = vv.tolist() elif isinstance(vv, dict): out[kk] = serialise_dict(vv) elif isinstance(vv, (str, list, bool, int, float)): out[kk] = vv except Exception as ex: raise ex return out
def get(identifier): if identifier is None: return linear if isinstance(identifier, six.string_types): identifier = str(identifier) return deserialize(identifier) elif callable(identifier): if isinstance(identifier, Layer): logging.warning( 'Do not pass a layer instance (such as {identifier}) as the ' 'activation argument of another layer. Instead, advanced ' 'activation layers should be used just like any other ' 'layer in a model.'.format(identifier=identifier.__class__.__name__)) return identifier else: raise ValueError('Could not interpret ' 'activation function identifier:', identifier) ## backend.py
def count_params(self): """Count the total number of scalars composing the weights. Returns: An integer count. Raises: RuntimeError: if the layer isn't yet built (in which case its weights aren't yet defined). """ if not self.built: if self.__class__.__name__ == 'Sequential': self.build() # pylint: disable=no-value-for-parameter else: raise RuntimeError('You tried to call `count_params` on ' + self.name + ', but the layer isn\'t built. ' 'You can build it manually via: `' + self.name + '.build(batch_input_shape)`.') return sum([K.count_params(p) for p in self.weights])
def _updated_config(self): """Util hared between different serialization methods. Returns: Model config with Keras version information added. """ from tensorflow.contrib.keras.python.keras import __version__ as keras_version # pylint: disable=g-import-not-at-top config = self.get_config() model_config = { 'class_name': self.__class__.__name__, 'config': config, 'keras_version': keras_version, 'backend': K.backend() } return model_config
def binary_one_hot(x): try: if type(x).__module__ == np.__name__: dim0 = x.shape[0] elif isinstance(x, list): dim0 = len(x) else: raise TypeError except TypeError: print("Expecting input type to be one of {list, numpy.ndarray}. Received %s" % type(x)) dim1 = 2 output = np.zeros((dim0, dim1)) for i in range(dim0): output[i, x[i]] = 1 return output
def compute_output_shape(self, input_shape): """Computes the output shape of the layer. Assumes that the layer will be built to match that input shape provided. # Arguments input_shape: Shape tuple (tuple of integers) or list of shape tuples (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer. # Returns An input shape tuple. """ if hasattr(self, 'get_output_shape_for'): msg = "Class `{}.{}` defines `get_output_shape_for` but does not override `compute_output_shape`. " + \ "If this is a Keras 1 layer, please implement `compute_output_shape` to support Keras 2." warnings.warn(msg.format(type(self).__module__, type(self).__name__), stacklevel=2) return input_shape
def count_params(self): """Count the total number of scalars composing the weights. # Returns An integer count. # Raises RuntimeError: if the layer isn't yet built (in which case its weights aren't yet defined). """ if not self.built: if self.__class__.__name__ == 'Sequential': self.build() else: raise RuntimeError('You tried to call `count_params` on ' + self.name + ', but the layer isn\'t built. ' 'You can build it manually via: `' + self.name + '.build(batch_input_shape)`.') return sum([K.count_params(p) for p in self.weights])
def _updated_config(self): """Util hared between different serialization methods. # Returns Model config with Keras version information added. """ from .. import __version__ as keras_version config = self.get_config() model_config = { 'class_name': self.__class__.__name__, 'config': config, 'keras_version': keras_version, 'backend': K.backend() } return model_config
def run_impl(self, params): # Convert numpy types. for k in list(params.keys()): a = params[k] if type(a).__module__ == np.__name__: params[k] = a.item() # Check if the paramset has not already run. run = self.runset.find_run(params) if run: return run # Run the function. r = self.to_optimise(params) if r is not None: # Merge params and result dicts. run = params.copy() run.update(r) return run
def create_input_layer(self, batch_input_shape, input_dtype=None, name=None): if not name: prefix = self.__class__.__name__.lower() + '_input_' name = prefix + str(K.get_uid(prefix)) if not input_dtype: input_dtype = K.floatx() self.batch_input_shape = batch_input_shape self.input_dtype = input_dtype # instantiate the input layer x = Input(batch_shape=batch_input_shape, dtype=input_dtype, name=name) # this will build the current layer # and create the node connecting the current layer # to the input layer we just created. self(x)
def to_json(self, **kwargs): '''Returns a JSON string containing the network configuration. To load a network from a JSON save file, use `keras.models.model_from_json(json_string, custom_objects={})`. ''' import json def get_json_type(obj): # if obj is any numpy type if type(obj).__module__ == np.__name__: return obj.item() # if obj is a python 'type' if type(obj).__name__ == type.__name__: return obj.__name__ raise TypeError('Not JSON Serializable:', obj) model_config = self._updated_config() return json.dumps(model_config, default=get_json_type, **kwargs)
def get_config(self): '''Returns the model configuration as a Python list. ''' config = [] if self.layers[0].__class__.__name__ == 'Merge': assert hasattr(self.layers[0], 'layers') layers = [] for layer in self.layers[0].layers: layer_config = {'class_name': layer.__class__.__name__, 'config': layer.get_config()} layers.append(layer_config) merge_config = self.layers[0].get_config() merge_config['layers'] = layers config.append({'class_name': 'Merge', 'config': merge_config}) else: config.append({'class_name': self.layers[0].__class__.__name__, 'config': self.layers[0].get_config()}) for layer in self.layers[1:]: config.append({'class_name': layer.__class__.__name__, 'config': layer.get_config()}) return copy.deepcopy(config)
def __hash__(self): if type(self.data).__module__ == np.__name__: # Numpy arrays return hash(str(self.data)) elif self.data.__hash__ is None: return hash(tuple(self.data)) else: return hash(self.data)
def get_size(variable): '''Get bytes of variable ''' if type(variable).__module__ == np.__name__: variable = variable.tobytes() elif type(variable) is str: assert (all(ord(c) < 256) for c in variable) else: raise ValueError('Data type not supported') # checking the length of a bytestring is more accurate return len(variable)
def dyplot(data, **kwargs): """ Plot data as line chart with dygraph Params: data: either a 2-d numpy array or a list of lists. win: pane id labels: list of series names, first series is always the X-axis see http://dygraphs.com/options.html for other supported options """ win = kwargs.get('win') or uid() dataset = {} if type(data).__module__ == np.__name__: dataset = data.tolist() else: dataset = data # clone kwargs into options options = dict(kwargs) options['file'] = dataset if options.get('labels'): options['xlabel'] = options['labels'][0] # Don't pass our options to dygraphs. options.pop('win', None) return pane('plot', kwargs.get('win'), kwargs.get('title'), content=options)
def count_params(self): '''Returns the total number of floats (or ints) composing the weights of the layer. ''' if not self.built: if self.__class__.__name__ == 'Sequential': self.build() else: raise Exception('You tried to call `count_params` on ' + self.name + ', but the layer isn\'t built. ' 'You can build it manually via: `' + self.name + '.build(batch_input_shape)`.') return sum([K.count_params(p) for p in self.weights])
def get_config(self): if isinstance(self.mode, python_types.LambdaType): mode = func_dump(self.mode) mode_type = 'lambda' elif callable(self.mode): mode = self.mode.__name__ mode_type = 'function' else: mode = self.mode mode_type = 'raw' if isinstance(self._output_shape, python_types.LambdaType): output_shape = func_dump(self._output_shape) output_shape_type = 'lambda' elif callable(self._output_shape): output_shape = self._output_shape.__name__ output_shape_type = 'function' else: output_shape = self._output_shape output_shape_type = 'raw' if isinstance(self._output_mask, python_types.LambdaType): output_mask = func_dump(self._output_mask) output_mask_type = 'lambda' elif callable(self._output_mask): output_mask = self._output_mask.__name__ output_mask_type = 'function' else: output_mask = self._output_mask output_mask_type = 'raw' return {'name': self.name, 'mode': mode, 'mode_type': mode_type, 'concat_axis': self.concat_axis, 'dot_axes': self.dot_axes, 'output_shape': output_shape, 'output_shape_type': output_shape_type, 'output_mask': output_mask, 'output_mask_type': output_mask_type, 'arguments': self.arguments}
def test_normalize(normalize): test_shape = (np.random.choice(range(1000)), 32, 32, 3) test_numbers = np.random.choice(range(256), test_shape) normalize_out = normalize(test_numbers) assert type(normalize_out).__module__ == np.__name__,\ 'Not Numpy Object' assert normalize_out.shape == test_shape,\ 'Incorrect Shape. {} shape found'.format(normalize_out.shape) assert normalize_out.max() <= 1 and normalize_out.min() >= 0,\ 'Incorect Range. {} to {} found'.format(normalize_out.min(), normalize_out.max()) _print_success_message()
def test_one_hot_encode(one_hot_encode): test_shape = np.random.choice(range(1000)) test_numbers = np.random.choice(range(10), test_shape) one_hot_out = one_hot_encode(test_numbers) assert type(one_hot_out).__module__ == np.__name__,\ 'Not Numpy Object' assert one_hot_out.shape == (test_shape, 10),\ 'Incorrect Shape. {} shape found'.format(one_hot_out.shape) n_encode_tests = 5 test_pairs = list(zip(test_numbers, one_hot_out)) test_indices = np.random.choice(len(test_numbers), n_encode_tests) labels = [test_pairs[test_i][0] for test_i in test_indices] enc_labels = np.array([test_pairs[test_i][1] for test_i in test_indices]) new_enc_labels = one_hot_encode(labels) assert np.array_equal(enc_labels, new_enc_labels),\ 'Encodings returned different results for the same numbers.\n' \ 'For the first call it returned:\n' \ '{}\n' \ 'For the second call it returned\n' \ '{}\n' \ 'Make sure you save the map of labels to encodings outside of the function.'.format(enc_labels, new_enc_labels) _print_success_message()
def save_model(model, filepath, overwrite=True): def get_json_type(obj): if hasattr(obj, 'get_config'): return {'class_name': obj.__class__.__name__, 'config': obj.get_config()} if type(obj).__module__ == np.__name__: return obj.item() if callable(obj) or type(obj).__name__ == type.__name__: return obj.__name__ raise TypeError('Not JSON Serializable:', obj) import h5py from keras import __version__ as keras_version if not overwrite and os.path.isfile(filepath): proceed = keras.models.ask_to_proceed_with_overwrite(filepath) if not proceed: return f = h5py.File(filepath, 'w') f.attrs['keras_version'] = str(keras_version).encode('utf8') f.attrs['generator_config'] = json.dumps({ 'class_name': model.discriminator.__class__.__name__, 'config': model.generator.get_config(), }, default=get_json_type).encode('utf8') f.attrs['discriminator_config'] = json.dumps({ 'class_name': model.discriminator.__class__.__name__, 'config': model.discriminator.get_config(), }, default=get_json_type).encode('utf8') generator_weights_group = f.create_group('generator_weights') discriminator_weights_group = f.create_group('discriminator_weights') model.generator.save_weights_to_hdf5_group(generator_weights_group) model.discriminator.save_weights_to_hdf5_group(discriminator_weights_group) f.flush() f.close()
def test_suite(): return unittest.findTestCases(sys.modules[__name__])
def count_params(self): """Returns the total number of floats (or ints) composing the weights of the layer. """ if not self.built: if self.__class__.__name__ == 'Sequential': self.build() else: raise RuntimeError('You tried to call `count_params` on ' + self.name + ', but the layer isn\'t built. ' 'You can build it manually via: `' + self.name + '.build(batch_input_shape)`.') return sum([K.count_params(p) for p in self.weights])
def serialise_to_json(obj): """ :param model: :return: """ if obj is dict: return serialise_dict(obj) params = {} for kk, vv in obj.__dict__.iteritems(): try: if isinstance(vv, type): continue if vv is None: params[kk] = None elif type(vv).__module__ == numpy_name: params[kk] = vv.tolist() elif isinstance(vv, dict): params[kk] = serialise_dict(vv) elif isinstance(vv, (str, list, bool, int, float)): params[kk] = vv except Exception as ex: raise ex return params
def get_config(self): config = [] for layer in self.layers: config.append({ 'class_name': layer.__class__.__name__, 'config': layer.get_config() }) return copy.deepcopy(config)
def fit(self, x, y, **kwargs): """Constructs a new model with `build_fn` & fit the model to `(x, y)`. Arguments: x : array-like, shape `(n_samples, n_features)` Training samples where n_samples in the number of samples and n_features is the number of features. y : array-like, shape `(n_samples,)` or `(n_samples, n_outputs)` True labels for X. **kwargs: dictionary arguments Legal arguments are the arguments of `Sequential.fit` Returns: history : object details about the training history at each epoch. """ if self.build_fn is None: self.model = self.__call__(**self.filter_sk_params(self.__call__)) elif (not isinstance(self.build_fn, types.FunctionType) and not isinstance(self.build_fn, types.MethodType)): self.model = self.build_fn( **self.filter_sk_params(self.build_fn.__call__)) else: self.model = self.build_fn(**self.filter_sk_params(self.build_fn)) loss_name = self.model.loss if hasattr(loss_name, '__name__'): loss_name = loss_name.__name__ if loss_name == 'categorical_crossentropy' and len(y.shape) != 2: y = to_categorical(y) fit_args = copy.deepcopy(self.filter_sk_params(Sequential.fit)) fit_args.update(kwargs) history = self.model.fit(x, y, **fit_args) return history
def score(self, x, y, **kwargs): """Returns the mean accuracy on the given test data and labels. Arguments: x: array-like, shape `(n_samples, n_features)` Test samples where n_samples in the number of samples and n_features is the number of features. y: array-like, shape `(n_samples,)` or `(n_samples, n_outputs)` True labels for x. **kwargs: dictionary arguments Legal arguments are the arguments of `Sequential.evaluate`. Returns: score: float Mean accuracy of predictions on X wrt. y. Raises: ValueError: If the underlying model isn't configured to compute accuracy. You should pass `metrics=["accuracy"]` to the `.compile()` method of the model. """ y = np.searchsorted(self.classes_, y) kwargs = self.filter_sk_params(Sequential.evaluate, kwargs) loss_name = self.model.loss if hasattr(loss_name, '__name__'): loss_name = loss_name.__name__ if loss_name == 'categorical_crossentropy' and len(y.shape) != 2: y = to_categorical(y) outputs = self.model.evaluate(x, y, **kwargs) if not isinstance(outputs, list): outputs = [outputs] for name, output in zip(self.model.metrics_names, outputs): if name == 'acc': return output raise ValueError('The model is not configured to compute accuracy. ' 'You should pass `metrics=["accuracy"]` to ' 'the `model.compile()` method.')
def serialize_keras_object(instance): _, instance = tf_decorator.unwrap(instance) if instance is None: return None if hasattr(instance, 'get_config'): return { 'class_name': instance.__class__.__name__, 'config': instance.get_config() } if hasattr(instance, '__name__'): return instance.__name__ else: raise ValueError('Cannot serialize', instance)
def get_config(self): if isinstance(self.function, python_types.LambdaType): function = func_dump(self.function) function_type = 'lambda' else: function = self.function.__name__ function_type = 'function' config = { 'function': function, 'function_type': function_type, 'arguments': self.arguments } base_config = super(Lambda, self).get_config() return dict(list(base_config.items()) + list(config.items()))
def serialize(layer): return {'class_name': layer.__class__.__name__, 'config': layer.get_config()}
def get_config(self): config = { 'layer': { 'class_name': self.layer.__class__.__name__, 'config': self.layer.get_config() } } base_config = super(Wrapper, self).get_config() return dict(list(base_config.items()) + list(config.items()))
def count_params(self): '''Returns the total number of floats (or ints) composing the weights of the layer. ''' if not self.built: if self.__class__.__name__ == 'Sequential': self.build() else: raise RuntimeError('You tried to call `count_params` on ' + self.name + ', but the layer isn\'t built. ' 'You can build it manually via: `' + self.name + '.build(batch_input_shape)`.') return sum([K.count_params(p) for p in self.weights])
def get_data(self,): if not self.file_lst2 and type(self.file_lst2[0][0]).__module__ == np.__name__: self._vectorize_labels() for pair in zip(self.file_lst2,self.labels): #TODO