本文整理汇总了Python中keras.metrics方法的典型用法代码示例。如果您正苦于以下问题:Python keras.metrics方法的具体用法?Python keras.metrics怎么用?Python keras.metrics使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras
的用法示例。
在下文中一共展示了keras.metrics方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: lstm_model
# 需要导入模块: import keras [as 别名]
# 或者: from keras import metrics [as 别名]
def lstm_model(input_shape, hidden = 256, targets = 1, learn_rate = 1e-4, multiclass=False):
model = Sequential()
model.add(Bidirectional(LSTM(hidden), merge_mode = 'concat'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
if (targets > 1) and not multiclass:
model.add(Bidirectional(LSTM(hidden), merge_mode = 'concat'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(targets))
if multiclass:
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer=Adam(lr=learn_rate, beta_1 =.5 ), metrics=['categorical_accuracy'])
else:
model.add(Activation ('sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer=Adam(lr=learn_rate, beta_1 =.5 ), metrics=['accuracy'])
return (model)
示例2: cnn_model
# 需要导入模块: import keras [as 别名]
# 或者: from keras import metrics [as 别名]
def cnn_model(input_shape, hidden = 256, targets = 1, learn_rate = 1e-4):
model = Sequential()
model.add(Convolution1D(input_shape = input_shape, nb_filter = 64, filter_length = 3, border_mode = 'same', activation = 'relu'))
model.add(MaxPooling1D(pool_length = 3))
model.add(Bidirectional(LSTM(hidden), merge_mode = 'concat'))
model.add(Activation('tanh'))
model.add(Dropout(0.5))
model.add(Dense(targets))
if multiclass:
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer=Adam(lr=learn_rate, beta_1 =.5 ), metrics=['categorical_accuracy'])
else:
model.add(Activation ('sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer=Adam(lr=learn_rate, beta_1 =.5 ), metrics=['accuracy'])
return (model)
示例3: mlp_model
# 需要导入模块: import keras [as 别名]
# 或者: from keras import metrics [as 别名]
def mlp_model(input_shape, hidden =512, targets = 1, multiclass = False, learn_rate = 1e-4):
model = Sequential()
model.add(Dense(hidden, activation = 'relu', input_shape = input_shape))
model.add(Dropout(.5))
model.add(Dense(hidden, activation = 'relu'))
model.add(Dropout(.5))
model.add(Dense(hidden, activation = 'relu'))
model.add(Dropout(.5))
model.add(Dense(targets))
if multiclass:
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=learn_rate, beta_1 =.5 ),
metrics=['categorical_accuracy'])
else:
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy', optimizer=Adam(lr=learn_rate, beta_1 =.5 ), metrics=['accuracy'])
return (model)
示例4: hierarchical_cnn
# 需要导入模块: import keras [as 别名]
# 或者: from keras import metrics [as 别名]
def hierarchical_cnn (input_shape, aux_shape, targets = 1, hidden = 256, multiclass = False, learn_rate=1e-4):
x = Input(shape = input_shape, name = 'x')
xx = Convolution1D(nb_filter = 64, filter_length = 3, border_mode = 'same', activation = 'relu') (x)
xx = MaxPooling1D(pool_length = 3) (xx)
xx = Bidirectional(LSTM (256, activation = 'relu'), merge_mode = 'concat') (xx)
xx = Dropout(0.5)(xx)
dx = Input(shape = aux_shape, name = 'aux')
xx = concatenate([xx, dx])
if multiclass:
y = Dense(targets, activation = 'softmax') (xx)
model = Model(inputs = [x, dx], outputs = [y])
model.compile (loss = 'categorical_crossentropy', optimizer = Adam(lr = learn_rate), metrics = ['categorical_accuracy'])
else:
y = Dense(targets, activation = 'sigmoid') (xx)
model = Model(inputs = [x, dx], outputs = [y])
model.compile (loss = 'binary_crossentropy', optimizer = Adam(lr = learn_rate), metrics = ['accuracy'])
return (model)
示例5: hierarchical_lstm
# 需要导入模块: import keras [as 别名]
# 或者: from keras import metrics [as 别名]
def hierarchical_lstm (input_shape, aux_shape, targets = 1, hidden = 256, multiclass = False, learn_rate = 1e-4):
x = Input(shape = input_shape, name = 'x')
xx = Bidirectional(LSTM (hidden, activation = 'relu'), merge_mode = 'concat') (x)
xx = Dropout(0.5)(xx)
dx = Input(shape = aux_shape, name = 'aux')
xx = concatenate([xx, dx])
xx = Dense(512, activation = 'relu') (xx)
if multiclass:
y = Dense(targets, activation = 'softmax') (xx)
model = Model(inputs = [x, dx], outputs = [y])
model.compile (loss = 'categorical_crossentropy', optimizer = Adam(lr = learn_rate), metrics = ['categorical_accuracy'])
else:
y = Dense(targets, activation = 'sigmoid') (xx)
model = Model(inputs = [x, dx], outputs = [y])
model.compile (loss = 'binary_crossentropy', optimizer = Adam(lr = learn_rate), metrics = ['accuracy'])
return (model)
示例6: infer_on_batch
# 需要导入模块: import keras [as 别名]
# 或者: from keras import metrics [as 别名]
def infer_on_batch(self, batch, labels=None):
"""
Method infers the model on the given batch
Args:
batch - list of texts
labels - list of labels
Returns:
loss and metrics values on the given batch, if labels are given
predictions, otherwise
"""
texts = batch
if labels:
features = self.texts2vec(texts)
onehot_labels = labels2onehot(labels, classes=self.classes)
metrics_values = self.model.test_on_batch(features, onehot_labels.reshape(-1, self.n_classes))
return metrics_values
else:
features = self.texts2vec(texts)
predictions = self.model.predict(features)
return predictions
示例7: evaluate
# 需要导入模块: import keras [as 别名]
# 或者: from keras import metrics [as 别名]
def evaluate(self, data, fn_inverse=None, horizon=1, fn_plot=None):
"""
Evaluate model
:return:
"""
encoder_input_data, decoder_input_exog, y = data
y_hat = self.predict(encoder_inputs=encoder_input_data,
pred_steps=horizon,
decoder_input_exog=decoder_input_exog)
if fn_inverse is not None:
y = fn_inverse(y)
y_hat = fn_inverse(y_hat)
y = np.float32(y)
y_hat = np.float32(y_hat)
if fn_plot is not None:
fn_plot([y,y_hat])
results = []
for m in self.model.metrics:
if isinstance(m, str):
results.append(K.eval(K.mean(get(m)(y, y_hat))))
else:
results.append(K.eval(K.mean(m(y, y_hat))))
return results
示例8: train_on_batch
# 需要导入模块: import keras [as 别名]
# 或者: from keras import metrics [as 别名]
def train_on_batch(self, batch):
"""
Method trains the intent_model on the given batch
Args:
batch - list of tuples (preprocessed text, labels)
Returns:
loss and metrics values on the given batch
"""
texts = list(batch[0])
labels = list(batch[1])
features = self.texts2vec(texts)
onehot_labels = labels2onehot(labels, classes=self.classes)
metrics_values = self.model.train_on_batch(features, onehot_labels)
return metrics_values