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)
python类__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 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 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 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_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 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