本文整理汇总了Python中keras.backend.one_hot方法的典型用法代码示例。如果您正苦于以下问题:Python backend.one_hot方法的具体用法?Python backend.one_hot怎么用?Python backend.one_hot使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.one_hot方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: optimizer
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import one_hot [as 别名]
def optimizer(self):
a = K.placeholder(shape=(None,), dtype='int32')
y = K.placeholder(shape=(None,), dtype='float32')
prediction = self.model.output
a_one_hot = K.one_hot(a, self.action_size)
q_value = K.sum(prediction * a_one_hot, axis=1)
error = K.abs(y - q_value)
quadratic_part = K.clip(error, 0.0, 1.0)
linear_part = error - quadratic_part
loss = K.mean(0.5 * K.square(quadratic_part) + linear_part)
optimizer = RMSprop(lr=0.00025, epsilon=0.01)
updates = optimizer.get_updates(self.model.trainable_weights, [], loss)
train = K.function([self.model.input, a, y], [loss], updates=updates)
return train
# 상태가 입력, 큐함수가 출력인 인공신경망 생성
示例2: optimizer
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import one_hot [as 别名]
def optimizer(self):
a = K.placeholder(shape=(None, ), dtype='int32')
y = K.placeholder(shape=(None, ), dtype='float32')
py_x = self.model.output
a_one_hot = K.one_hot(a, self.action_size)
q_value = K.sum(py_x * a_one_hot, axis=1)
error = K.abs(y - q_value)
quadratic_part = K.clip(error, 0.0, 1.0)
linear_part = error - quadratic_part
loss = K.mean(0.5 * K.square(quadratic_part) + linear_part)
optimizer = RMSprop(lr=0.00025, epsilon=0.01)
updates = optimizer.get_updates(self.model.trainable_weights, [], loss)
train = K.function([self.model.input, a, y], [loss], updates=updates)
return train
# approximate Q function using Convolution Neural Network
# state is input and Q Value of each action is output of network
示例3: optimizer
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import one_hot [as 别名]
def optimizer(self):
a = K.placeholder(shape=(None,), dtype='int32')
y = K.placeholder(shape=(None,), dtype='float32')
py_x = self.model.output
a_one_hot = K.one_hot(a, self.action_size)
q_value = K.sum(py_x * a_one_hot, axis=1)
error = K.abs(y - q_value)
quadratic_part = K.clip(error, 0.0, 1.0)
linear_part = error - quadratic_part
loss = K.mean(0.5 * K.square(quadratic_part) + linear_part)
optimizer = RMSprop(lr=0.00025, epsilon=0.01)
updates = optimizer.get_updates(self.model.trainable_weights, [], loss)
train = K.function([self.model.input, a, y], [loss], updates=updates)
return train
# approximate Q function using Convolution Neural Network
# state is input and Q Value of each action is output of network
示例4: optimizer
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import one_hot [as 别名]
def optimizer(self):
a = K.placeholder(shape=(None, ), dtype='int32')
y = K.placeholder(shape=(None, ), dtype='float32')
py_x = self.model.output
a_one_hot = K.one_hot(a, self.action_size)
q_value = K.sum(py_x * a_one_hot, axis=1)
error = K.abs(y - q_value)
quadratic_part = K.clip(error, 0.0, 1.0)
linear_part = error - quadratic_part
loss = K.mean(0.5 * K.square(quadratic_part) + linear_part)
optimizer = RMSprop(lr=0.00025, epsilon=0.01)
updates = optimizer.get_updates(self.model.trainable_weights, [], loss)
train = K.function([self.model.input, a, y], [loss], updates=updates)
return train
# approximate Q function using Convolution Neural Network
# state is input and Q Value of each action is output of network
# dueling network's Q Value is sum of advantages and state value
示例5: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import one_hot [as 别名]
def call(self, inputs, **kwargs):
if type(inputs) is list: # true label is provided with shape = [None, n_classes], i.e. one-hot code.
assert len(inputs) == 2
inputs, mask = inputs
else: # if no true label, mask by the max length of capsules. Mainly used for prediction
# compute lengths of capsules
x = K.sqrt(K.sum(K.square(inputs), -1))
# generate the mask which is a one-hot code.
# mask.shape=[None, n_classes]=[None, num_capsule]
mask = K.one_hot(indices=K.argmax(x, 1), num_classes=x.get_shape().as_list()[1])
# inputs.shape=[None, num_capsule, dim_capsule]
# mask.shape=[None, num_capsule]
# masked.shape=[None, num_capsule * dim_capsule]
masked = K.batch_flatten(inputs * K.expand_dims(mask, -1))
return masked
示例6: labelembed_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import one_hot [as 别名]
def labelembed_loss(out1, out2, tar, targets, tau = 2., alpha = 0.9, beta = 0.5, num_classes = 100):
out2_prob = K.softmax(out2)
tau2_prob = K.stop_gradient(K.softmax(out2 / tau))
soft_tar = K.stop_gradient(K.softmax(tar))
L_o1_y = K.sparse_categorical_crossentropy(output = K.softmax(out1), target = targets)
pred = K.argmax(out2, axis = -1)
mask = K.stop_gradient(K.cast(K.equal(pred, K.cast(targets, 'int64')), K.floatx()))
L_o1_emb = -cross_entropy(out1, soft_tar) # pylint: disable=invalid-unary-operand-type
L_o2_y = K.sparse_categorical_crossentropy(output = out2_prob, target = targets)
L_emb_o2 = -cross_entropy(tar, tau2_prob) * mask * (K.cast(K.shape(mask)[0], K.floatx())/(K.sum(mask)+1e-8)) # pylint: disable=invalid-unary-operand-type
L_re = K.relu(K.sum(out2_prob * K.one_hot(K.cast(targets, 'int64'), num_classes), axis = -1) - alpha)
return beta * L_o1_y + (1-beta) * L_o1_emb + L_o2_y + L_emb_o2 + L_re
示例7: softmax_sparse_crossentropy_ignoring_last_label
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import one_hot [as 别名]
def softmax_sparse_crossentropy_ignoring_last_label(y_true, y_pred):
'''
Softmax cross-entropy loss function for pascal voc segmentation
and models which do not perform softmax.
tensorlow only
'''
y_pred = KB.reshape(y_pred, (-1, KB.int_shape(y_pred)[-1]))
log_softmax = tf.nn.log_softmax(y_pred)
y_true = KB.one_hot(tf.to_int32(KB.flatten(y_true)),
KB.int_shape(y_pred)[-1]+1)
unpacked = tf.unstack(y_true, axis=-1)
y_true = tf.stack(unpacked[:-1], axis=-1)
cross_entropy = -KB.sum(y_true * log_softmax, axis=1)
cross_entropy_mean = KB.mean(cross_entropy)
return cross_entropy_mean
示例8: set_batch_function
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import one_hot [as 别名]
def set_batch_function(self, model, input_shape, batch_size, nb_actions, gamma):
input_dim = np.prod(input_shape)
samples = K.placeholder(shape=(batch_size, input_dim * 2 + 3))
S = samples[:, 0 : input_dim]
a = samples[:, input_dim]
r = samples[:, input_dim + 1]
S_prime = samples[:, input_dim + 2 : 2 * input_dim + 2]
game_over = samples[:, 2 * input_dim + 2 : 2 * input_dim + 3]
r = K.reshape(r, (batch_size, 1))
r = K.repeat(r, nb_actions)
r = K.reshape(r, (batch_size, nb_actions))
game_over = K.repeat(game_over, nb_actions)
game_over = K.reshape(game_over, (batch_size, nb_actions))
S = K.reshape(S, (batch_size, ) + input_shape)
S_prime = K.reshape(S_prime, (batch_size, ) + input_shape)
X = K.concatenate([S, S_prime], axis=0)
Y = model(X)
Qsa = K.max(Y[batch_size:], axis=1)
Qsa = K.reshape(Qsa, (batch_size, 1))
Qsa = K.repeat(Qsa, nb_actions)
Qsa = K.reshape(Qsa, (batch_size, nb_actions))
delta = K.reshape(self.one_hot(a, nb_actions), (batch_size, nb_actions))
targets = (1 - delta) * Y[:batch_size] + delta * (r + gamma * (1 - game_over) * Qsa)
self.batch_function = K.function(inputs=[samples], outputs=[S, targets])
示例9: sparse_categorical_crossentropy
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import one_hot [as 别名]
def sparse_categorical_crossentropy(gt_ids, pred_one_hot_post_softmax):
"""
K.sparse_categorical_crossentropyだと結果がNaNになる。。。
0割り算が発生しているかも。
https://qiita.com/4Ui_iUrz1/items/35a8089ab0ebc98061c1
対策として、微少値を用いてlog(0)にならないよう調整した本関数を作成。
"""
gt_ids = log.tfprint(gt_ids, "cross:gt_ids:")
pred_one_hot_post_softmax = log.tfprint(pred_one_hot_post_softmax,
"cross:pred_one_hot_post_softmax:")
gt_one_hot = K.one_hot(gt_ids, K.shape(pred_one_hot_post_softmax)[-1])
gt_one_hot = log.tfprint(gt_one_hot, "cross:gt_one_hot:")
epsilon = K.epsilon() # 1e-07
loss = -K.sum(
gt_one_hot * K.log(
tf.clip_by_value(pred_one_hot_post_softmax, epsilon, 1 - epsilon)),
axis=-1)
loss = log.tfprint(loss, "cross:loss:")
return loss
示例10: Mask
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import one_hot [as 别名]
def Mask(self, inputs, seq_len, mode='mul'):
"""
# Arguments:
inputs: input tensor with shape (batch_size, seq_len, input_size)
seq_len: Each sequence's actual length with shape (batch_size,)
mode:
mul: mask the rest dim with zero, used before fully-connected layer
add: subtract a big constant from the rest, used before softmax layer
# Reutrns:
Masked tensors with the same shape of input tensor
"""
if seq_len is None:
return inputs
else:
mask = K.one_hot(seq_len[:, 0], K.shape(inputs)[1])
mask = 1 - K.cumsum(mask, 1)
for _ in range(len(inputs.shape) - 2):
mask = K.expand_dims(mask, 2)
if mode == 'mul':
return inputs * mask
if mode == 'add':
return inputs - (1 - mask) * 1e12
示例11: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import one_hot [as 别名]
def call(self, inputs, mask=None):
if self.use_token_type:
assert len(inputs) == 2, "`token_type_ids` must be specified if `use_token_type` is True."
output = inputs[0]
_, seq_length, input_width = K.int_shape(output)
# print(inputs)
assert seq_length == K.int_shape(inputs[1])[1], "width of `token_type_ids` must be equal to `seq_length`"
token_type_ids = inputs[1]
# assert K.int_shape(token_type_ids)[1] <= self.token_type_vocab_size
flat_token_type_ids = K.reshape(token_type_ids, [-1])
flat_token_type_ids = K.cast(flat_token_type_ids, dtype='int32')
token_type_one_hot_ids = K.one_hot(flat_token_type_ids, num_classes=self.token_type_vocab_size)
token_type_embeddings = K.dot(token_type_one_hot_ids, self.token_type_table)
token_type_embeddings = K.reshape(token_type_embeddings, shape=[-1, seq_length, input_width])
# print(token_type_embeddings)
output += token_type_embeddings
else:
output = inputs
seq_length = K.int_shape(inputs)[1]
if self.use_position_embeddings:
position_embeddings = K.slice(self.full_position_embeddings, [0, 0], [seq_length, -1])
output += position_embeddings
return output
示例12: loss_function
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import one_hot [as 别名]
def loss_function(self):
if self.learn_mode == 'join':
def loss(y_true, y_pred):
assert self._inbound_nodes, 'CRF has not connected to any layer.'
assert not self._outbound_nodes, 'When learn_model="join", CRF must be the last layer.'
if self.sparse_target:
y_true = K.one_hot(K.cast(y_true[:, :, 0], 'int32'), self.units)
X = self._inbound_nodes[0].input_tensors[0]
mask = self._inbound_nodes[0].input_masks[0]
nloglik = self.get_negative_log_likelihood(y_true, X, mask)
return nloglik
return loss
else:
if self.sparse_target:
return sparse_categorical_crossentropy
else:
return categorical_crossentropy
示例13: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import one_hot [as 别名]
def call(self, inputs, **kwargs):
if isinstance(inputs, list): # true label is provided with shape = [None, n_classes], i.e. one-hot code.
assert len(inputs) == 2
inputs, mask = inputs
else: # if no true label, mask by the max length of capsules. Mainly used for prediction
# compute lengths of capsules
x = K.sqrt(K.sum(K.square(inputs), -1))
# generate the mask which is a one-hot code.
# mask.shape=[None, n_classes]=[None, num_capsule]
mask = K.one_hot(indices=K.argmax(x, 1), num_classes=x.get_shape().as_list()[1])
# inputs.shape=[None, num_capsule, dim_capsule]
# mask.shape=[None, num_capsule]
# masked.shape=[None, num_capsule * dim_capsule]
masked = K.batch_flatten(inputs * K.expand_dims(mask, -1))
return masked
示例14: softmax_sparse_crossentropy_ignoring_last_label
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import one_hot [as 别名]
def softmax_sparse_crossentropy_ignoring_last_label(y_true, y_pred):
y_pred = K.reshape(y_pred, (-1, K.int_shape(y_pred)[-1]))
log_softmax = tf.nn.log_softmax(y_pred)
y_true = K.one_hot(tf.to_int32(K.flatten(y_true)), K.int_shape(y_pred)[-1]+1)
unpacked = tf.unstack(y_true, axis=-1)
y_true = tf.stack(unpacked[:-1], axis=-1)
cross_entropy = -K.sum(y_true * log_softmax, axis=1)
cross_entropy_mean = K.mean(cross_entropy)
return cross_entropy_mean
# Softmax cross-entropy loss function for coco segmentation
# and models which expect but do not apply sigmoid on each entry
# tensorlow only
示例15: mean_acc
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import one_hot [as 别名]
def mean_acc(y_true, y_pred):
s = K.shape(y_true)
# reshape such that w and h dim are multiplied together
y_true_reshaped = K.reshape( y_true, tf.stack( [-1, s[1]*s[2], s[-1]] ) )
y_pred_reshaped = K.reshape( y_pred, tf.stack( [-1, s[1]*s[2], s[-1]] ) )
# correctly classified
clf_pred = K.one_hot( K.argmax(y_pred_reshaped), nb_classes = s[-1])
equal_entries = K.cast(K.equal(clf_pred,y_true_reshaped), dtype='float32') * y_true_reshaped
correct_pixels_per_class = K.sum(equal_entries, axis=1)
n_pixels_per_class = K.sum(y_true_reshaped,axis=1)
acc = correct_pixels_per_class / n_pixels_per_class
acc_mask = tf.is_finite(acc)
acc_masked = tf.boolean_mask(acc,acc_mask)
return K.mean(acc_masked)