本文整理汇总了Python中keras.backend.eye方法的典型用法代码示例。如果您正苦于以下问题:Python backend.eye方法的具体用法?Python backend.eye怎么用?Python backend.eye使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.eye方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import eye [as 别名]
def call(self, inputs, training=None):
z, gamma_k = inputs
gamma_k_sum = K.sum(gamma_k)
est_phi = K.mean(gamma_k, axis=0)
est_mu = K.dot(K.transpose(gamma_k), z) / gamma_k_sum
est_sigma = K.dot(K.transpose(z - est_mu),
gamma_k * (z - est_mu)) / gamma_k_sum
est_sigma = est_sigma + (K.random_normal(shape=(K.int_shape(z)[1], 1), mean=1e-3, stddev=1e-4) * K.eye(K.int_shape(z)[1]))
self.add_update(K.update(self.phi, est_phi), inputs)
self.add_update(K.update(self.mu, est_mu), inputs)
self.add_update(K.update(self.sigma, est_sigma), inputs)
est_sigma_diag_inv = K.eye(K.int_shape(self.sigma)[0]) / est_sigma
self.add_loss(self.lambd_diag * K.sum(est_sigma_diag_inv), inputs)
phi = K.in_train_phase(est_phi, self.phi, training)
mu = K.in_train_phase(est_mu, self.mu, training)
sigma = K.in_train_phase(est_sigma, self.sigma, training)
return GaussianMixtureComponent._calc_component_density(z, phi, mu, sigma)
示例2: orthonorm_op
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import eye [as 别名]
def orthonorm_op(x, epsilon=1e-7):
'''
Computes a matrix that orthogonalizes the input matrix x
x: an n x d input matrix
eps: epsilon to prevent nonzero values in the diagonal entries of x
returns: a d x d matrix, ortho_weights, which orthogonalizes x by
right multiplication
'''
x_2 = K.dot(K.transpose(x), x)
x_2 += K.eye(K.int_shape(x)[1])*epsilon
L = tf.cholesky(x_2)
ortho_weights = tf.transpose(tf.matrix_inverse(L)) * tf.sqrt(tf.cast(tf.shape(x)[0], dtype=K.floatx()))
return ortho_weights
示例3: gather_indices_2d
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import eye [as 别名]
def gather_indices_2d(x, block_shape, block_stride):
kernel = K.eye(block_shape[0]*block_shape[1])
#kernel = K.reshape(kernel, [block_shape[0], block_shape[1], 1, block_shape[0]*block_shape[1]])
kernel = reshape_range(kernel, 0, 1, [block_shape[0], block_shape[1], 1])
x_shape = K.shape(x)
indices = K.arange(x_shape[2]*x_shape[3])
indices = K.reshape(indices, [1, x_shape[2], x_shape[3], 1])
indices = K.conv2d(tf.cast(indices, tf.float32), kernel, strides=(block_stride[0], block_stride[1]))
i_shape = K.shape(indices)[:3]
n_blocks = tf.reduce_prod(i_shape)
indices = K.reshape(indices, [n_blocks, -1])
return tf.cast(indices, tf.int32)
示例4: create_model
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import eye [as 别名]
def create_model(args, maxlen, vocab):
def ortho_reg(weight_matrix):
### orthogonal regularization for aspect embedding matrix ###
w_n = K.l2_normalize(weight_matrix, axis=-1)
reg = K.sum(K.square(K.dot(w_n, K.transpose(w_n)) - K.eye(w_n.get_shape().as_list()[0])))
return args.ortho_reg * reg
vocab_size = len(vocab)
if args.emb_name:
from w2vEmbReader import W2VEmbReader as EmbReader
emb_reader = EmbReader(os.path.join("..", "preprocessed_data", args.domain), args.emb_name)
aspect_matrix = emb_reader.get_aspect_matrix(args.aspect_size)
args.aspect_size = emb_reader.aspect_size
args.emb_dim = emb_reader.emb_dim
##### Inputs #####
sentence_input = Input(shape=(maxlen,), dtype='int32', name='sentence_input')
neg_input = Input(shape=(args.neg_size, maxlen), dtype='int32', name='neg_input')
##### Construct word embedding layer #####
word_emb = Embedding(vocab_size, args.emb_dim,
mask_zero=True, name='word_emb',
embeddings_constraint=MaxNorm(10))
##### Compute sentence representation #####
e_w = word_emb(sentence_input)
y_s = Average()(e_w)
att_weights = Attention(name='att_weights',
W_constraint=MaxNorm(10),
b_constraint=MaxNorm(10))([e_w, y_s])
z_s = WeightedSum()([e_w, att_weights])
##### Compute representations of negative instances #####
e_neg = word_emb(neg_input)
z_n = Average()(e_neg)
##### Reconstruction #####
p_t = Dense(args.aspect_size)(z_s)
p_t = Activation('softmax', name='p_t')(p_t)
r_s = WeightedAspectEmb(args.aspect_size, args.emb_dim, name='aspect_emb',
W_constraint=MaxNorm(10),
W_regularizer=ortho_reg)(p_t)
##### Loss #####
loss = MaxMargin(name='max_margin')([z_s, z_n, r_s])
model = Model(inputs=[sentence_input, neg_input], outputs=[loss])
### Word embedding and aspect embedding initialization ######
if args.emb_name:
from w2vEmbReader import W2VEmbReader as EmbReader
logger.info('Initializing word embedding matrix')
embs = model.get_layer('word_emb').embeddings
K.set_value(embs, emb_reader.get_emb_matrix_given_vocab(vocab, K.get_value(embs)))
logger.info('Initializing aspect embedding matrix as centroid of kmean clusters')
K.set_value(model.get_layer('aspect_emb').W, aspect_matrix)
return model
示例5: create_model
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import eye [as 别名]
def create_model(args, maxlen, vocab):
def ortho_reg(weight_matrix):
### orthogonal regularization for aspect embedding matrix ###
w_n = weight_matrix / K.cast(K.epsilon() + K.sqrt(K.sum(K.square(weight_matrix), axis=-1, keepdims=True)), K.floatx())
reg = K.sum(K.square(K.dot(w_n, K.transpose(w_n)) - K.eye(w_n.shape[0].eval())))
return args.ortho_reg*reg
vocab_size = len(vocab)
##### Inputs #####
sentence_input = Input(shape=(maxlen,), dtype='int32', name='sentence_input')
neg_input = Input(shape=(args.neg_size, maxlen), dtype='int32', name='neg_input')
##### Construct word embedding layer #####
word_emb = Embedding(vocab_size, args.emb_dim, mask_zero=True, name='word_emb')
##### Compute sentence representation #####
e_w = word_emb(sentence_input)
y_s = Average()(e_w)
att_weights = Attention(name='att_weights')([e_w, y_s])
z_s = WeightedSum()([e_w, att_weights])
##### Compute representations of negative instances #####
e_neg = word_emb(neg_input)
z_n = Average()(e_neg)
##### Reconstruction #####
p_t = Dense(args.aspect_size)(z_s)
p_t = Activation('softmax', name='p_t')(p_t)
r_s = WeightedAspectEmb(args.aspect_size, args.emb_dim, name='aspect_emb',
W_regularizer=ortho_reg)(p_t)
##### Loss #####
loss = MaxMargin(name='max_margin')([z_s, z_n, r_s])
model = Model(input=[sentence_input, neg_input], output=loss)
### Word embedding and aspect embedding initialization ######
if args.emb_path:
from w2vEmbReader import W2VEmbReader as EmbReader
emb_reader = EmbReader(args.emb_path, emb_dim=args.emb_dim)
logger.info('Initializing word embedding matrix')
model.get_layer('word_emb').W.set_value(emb_reader.get_emb_matrix_given_vocab(vocab, model.get_layer('word_emb').W.get_value()))
logger.info('Initializing aspect embedding matrix as centroid of kmean clusters')
model.get_layer('aspect_emb').W.set_value(emb_reader.get_aspect_matrix(args.aspect_size))
return model