本文整理汇总了Python中keras.models方法的典型用法代码示例。如果您正苦于以下问题:Python keras.models方法的具体用法?Python keras.models怎么用?Python keras.models使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras
的用法示例。
在下文中一共展示了keras.models方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: make_model_yaml
# 需要导入模块: import keras [as 别名]
# 或者: from keras import models [as 别名]
def make_model_yaml(template_yaml, model_json, output_yaml_path):
#
with open(template_yaml, 'r') as f:
model_yaml = yaml.load(f)
#
# get the model config:
json_file = open(model_json, 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = keras.models.model_from_json(loaded_model_json)
#
model_yaml["schema"]["targets"] = []
for oname, oshape in zip(loaded_model.output_names, loaded_model.output_shape):
append_el ={"name":oname , "shape":str(oshape)#replace("None,", "")
, "doc":"Methylation probability for %s"%oname}
model_yaml["schema"]["targets"].append(append_el)
#
with open(output_yaml_path, 'w') as f:
yaml.dump(model_yaml, f, default_flow_style=False)
示例2: make_secondary_dl_yaml
# 需要导入模块: import keras [as 别名]
# 或者: from keras import models [as 别名]
def make_secondary_dl_yaml(template_yaml, model_json, output_yaml_path):
with open(template_yaml, 'r') as f:
model_yaml = yaml.load(f)
#
# get the model config:
json_file = open(model_json, 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = keras.models.model_from_json(loaded_model_json)
#
model_yaml["output_schema"]["targets"] = []
for oname, oshape in zip(loaded_model.output_names, loaded_model.output_shape):
append_el ={"name":oname , "shape":str(oshape)#replace("None,", "")
, "doc":"Methylation probability for %s"%oname}
model_yaml["output_schema"]["targets"].append(append_el)
#
with open(output_yaml_path, 'w') as f:
yaml.dump(model_yaml, f, default_flow_style=False)
示例3: __init__
# 需要导入模块: import keras [as 别名]
# 或者: from keras import models [as 别名]
def __init__(self, model_save_path='../models',
model_structure_name='seq2seq_model_demo',
model_weights_name='seq2seq_model_demo',
model_name=None):
super().__init__()
self.model_save_path = model_save_path
self.model_structure_name=model_structure_name + self.model_name_format_str +'.json'
self.model_weights_name=model_weights_name + self.model_name_format_str +'.h5'
print('model_structure_name:', self.model_structure_name)
print('model_weights_name:', self.model_weights_name)
self.pred_result = None # Predicted mean value
self.pred_var_result = None # Predicted variance value
self.current_mean_val_loss = None
self.EARLY_STOP=False
self.val_loss_list=[]
self.train_loss_list=[]
self.pred_var_result = []
示例4: get_residual_model
# 需要导入模块: import keras [as 别名]
# 或者: from keras import models [as 别名]
def get_residual_model(is_mnist=True, img_channels=1, img_rows=28, img_cols=28):
model = keras.models.Sequential()
first_layer_channel = 128
if is_mnist: # size to be changed to 32,32
model.add(ZeroPadding2D((2,2), input_shape=(img_channels, img_rows, img_cols))) # resize (28,28)-->(32,32)
# the first conv
model.add(Convolution2D(first_layer_channel, 3, 3, border_mode='same'))
else:
model.add(Convolution2D(first_layer_channel, 3, 3, border_mode='same', input_shape=(img_channels, img_rows, img_cols)))
model.add(Activation('relu'))
# [residual-based Conv layers]
residual_blocks = design_for_residual_blocks(num_channel_input=first_layer_channel)
model.add(residual_blocks)
model.add(BatchNormalization(axis=1))
model.add(Activation('relu'))
# [Classifier]
model.add(Flatten())
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
# [END]
return model
示例5: compute_backbone_shapes
# 需要导入模块: import keras [as 别名]
# 或者: from keras import models [as 别名]
def compute_backbone_shapes(config, image_shape):
"""Computes the width and height of each stage of the backbone network.
Returns:
[N, (height, width)]. Where N is the number of stages
"""
if callable(config.BACKBONE):
return config.COMPUTE_BACKBONE_SHAPE(image_shape)
# Currently supports ResNet only
assert config.BACKBONE in ["resnet50", "resnet101"]
return np.array(
[[int(math.ceil(image_shape[0] / stride)),
int(math.ceil(image_shape[1] / stride))]
for stride in config.BACKBONE_STRIDES])
############################################################
# Resnet Graph
############################################################
# Code adopted from:
# https://github.com/fchollet/deep-learning-models/blob/master/resnet50.py
示例6: predict
# 需要导入模块: import keras [as 别名]
# 或者: from keras import models [as 别名]
def predict(self, x):
r"""
Predict quantiles of the conditional distribution P(y|x).
Forward propagates the inputs in `x` through the network to
obtain the predicted quantiles `y`.
Arguments:
x(np.array): Array of shape `(n, m)` containing `n` m-dimensional inputs
for which to predict the conditional quantiles.
Returns:
Array of shape `(n, k)` with the columns corresponding to the k
quantiles of the network.
"""
predictions = np.stack(
[m.predict((x - self.x_mean) / self.x_sigma) for m in self.models])
return np.mean(predictions, axis=0)
示例7: test_ShapGradientExplainer
# 需要导入模块: import keras [as 别名]
# 或者: from keras import models [as 别名]
def test_ShapGradientExplainer(self):
# model = VGG16(weights='imagenet', include_top=True)
# X, y = shap.datasets.imagenet50()
# to_explain = X[[39, 41]]
#
# url = "https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json"
# fname = shap.datasets.cache(url)
# with open(fname) as f:
# class_names = json.load(f)
#
# def map2layer(x, layer):
# feed_dict = dict(zip([model.layers[0].input], [preprocess_input(x.copy())]))
# return K.get_session().run(model.layers[layer].input, feed_dict)
#
# e = GradientExplainer((model.layers[7].input, model.layers[-1].output),
# map2layer(preprocess_input(X.copy()), 7))
# shap_values, indexes = e.explain_instance(map2layer(to_explain, 7), ranked_outputs=2)
#
print("Skipped Shap GradientExplainer")
示例8: compute_backbone_shapes
# 需要导入模块: import keras [as 别名]
# 或者: from keras import models [as 别名]
def compute_backbone_shapes(config, image_shape):
"""Computes the width and height of each stage of the backbone network.
Returns:
[N, (height, width)]. Where N is the number of stages
"""
if callable(config.BACKBONE):
return config.COMPUTE_BACKBONE_SHAPE(image_shape)
# Currently supports ResNet only
assert config.BACKBONE in ["resnet50", "resnet101"]
return np.array(
[[int(math.ceil(image_shape[0] / stride)),
int(math.ceil(image_shape[1] / stride))]
for stride in config.BACKBONE_STRIDES])
############################################################
# Resnet Graph
############################################################
# Code adopted from:
# https://github.com/fchollet/deep-learning-models/blob/master/resnet50.py
示例9: _build_model
# 需要导入模块: import keras [as 别名]
# 或者: from keras import models [as 别名]
def _build_model(self, num_features, num_actions, max_history_len):
"""Build a keras model and return a compiled model.
:param max_history_len: The maximum number of historical
turns used to decide on next action
"""
from keras.layers import LSTM, Activation, Masking, Dense
from keras.models import Sequential
n_hidden = 32 # Neural Net and training params
batch_shape = (None, max_history_len, num_features)
# Build Model
model = Sequential()
model.add(Masking(-1, batch_input_shape=batch_shape))
model.add(LSTM(n_hidden, batch_input_shape=batch_shape))
model.add(Dense(input_dim=n_hidden, units=num_actions))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
logger.debug(model.summary())
return model
示例10: tagSentences
# 需要导入模块: import keras [as 别名]
# 或者: from keras import models [as 别名]
def tagSentences(self, sentences):
# Pad characters
if 'characters' in self.params['featureNames']:
self.padCharacters(sentences)
labels = {}
for modelName, model in self.models.items():
paddedPredLabels = self.predictLabels(model, sentences)
predLabels = []
for idx in range(len(sentences)):
unpaddedPredLabels = []
for tokenIdx in range(len(sentences[idx]['tokens'])):
if sentences[idx]['tokens'][tokenIdx] != 0: # Skip padding tokens
unpaddedPredLabels.append(paddedPredLabels[idx][tokenIdx])
predLabels.append(unpaddedPredLabels)
idx2Label = self.idx2Labels[modelName]
labels[modelName] = [[idx2Label[tag] for tag in tagSentence] for tagSentence in predLabels]
return labels
示例11: computeF1
# 需要导入模块: import keras [as 别名]
# 或者: from keras import models [as 别名]
def computeF1(self, modelName, sentences):
labelKey = self.labelKeys[modelName]
model = self.models[modelName]
idx2Label = self.idx2Labels[modelName]
correctLabels = [sentences[idx][labelKey] for idx in range(len(sentences))]
predLabels = self.predictLabels(model, sentences)
labelKey = self.labelKeys[modelName]
encodingScheme = labelKey[labelKey.index('_')+1:]
pre, rec, f1 = BIOF1Validation.compute_f1(predLabels, correctLabels, idx2Label, 'O', encodingScheme)
pre_b, rec_b, f1_b = BIOF1Validation.compute_f1(predLabels, correctLabels, idx2Label, 'B', encodingScheme)
if f1_b > f1:
logging.debug("Setting wrong tags to B- improves from %.4f to %.4f" % (f1, f1_b))
pre, rec, f1 = pre_b, rec_b, f1_b
return pre, rec, f1
示例12: saveModel
# 需要导入模块: import keras [as 别名]
# 或者: from keras import models [as 别名]
def saveModel(self, modelName, epoch, dev_score, test_score):
import json
import h5py
if self.modelSavePath == None:
raise ValueError('modelSavePath not specified.')
savePath = self.modelSavePath.replace("[DevScore]", "%.4f" % dev_score).replace("[TestScore]", "%.4f" % test_score).replace("[Epoch]", str(epoch+1)).replace("[ModelName]", modelName)
directory = os.path.dirname(savePath)
if not os.path.exists(directory):
os.makedirs(directory)
if os.path.isfile(savePath):
logging.info("Model "+savePath+" already exists. Model will be overwritten")
self.models[modelName].save(savePath, True)
with h5py.File(savePath, 'a') as h5file:
h5file.attrs['mappings'] = json.dumps(self.mappings)
h5file.attrs['params'] = json.dumps(self.params)
h5file.attrs['modelName'] = modelName
h5file.attrs['labelKey'] = self.datasets[modelName]['label']
示例13: shufflenetv2b_wd2
# 需要导入模块: import keras [as 别名]
# 或者: from keras import models [as 别名]
def shufflenetv2b_wd2(**kwargs):
"""
ShuffleNetV2(b) 0.5x model from 'ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design,'
https://arxiv.org/abs/1807.11164.
Parameters:
----------
pretrained : bool, default False
Whether to load the pretrained weights for model.
root : str, default '~/.keras/models'
Location for keeping the model parameters.
Returns
-------
functor
Functor for model graph creation with extra fields.
"""
return get_shufflenetv2b(
width_scale=(12.0 / 29.0),
model_name="shufflenetv2b_wd2",
**kwargs)
示例14: shufflenetv2b_w1
# 需要导入模块: import keras [as 别名]
# 或者: from keras import models [as 别名]
def shufflenetv2b_w1(**kwargs):
"""
ShuffleNetV2(b) 1x model from 'ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design,'
https://arxiv.org/abs/1807.11164.
Parameters:
----------
pretrained : bool, default False
Whether to load the pretrained weights for model.
root : str, default '~/.keras/models'
Location for keeping the model parameters.
Returns
-------
functor
Functor for model graph creation with extra fields.
"""
return get_shufflenetv2b(
width_scale=1.0,
model_name="shufflenetv2b_w1",
**kwargs)
示例15: shufflenetv2b_w3d2
# 需要导入模块: import keras [as 别名]
# 或者: from keras import models [as 别名]
def shufflenetv2b_w3d2(**kwargs):
"""
ShuffleNetV2(b) 1.5x model from 'ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design,'
https://arxiv.org/abs/1807.11164.
Parameters:
----------
pretrained : bool, default False
Whether to load the pretrained weights for model.
root : str, default '~/.keras/models'
Location for keeping the model parameters.
Returns
-------
functor
Functor for model graph creation with extra fields.
"""
return get_shufflenetv2b(
width_scale=(44.0 / 29.0),
model_name="shufflenetv2b_w3d2",
**kwargs)