本文整理汇总了Python中keras.backend.function方法的典型用法代码示例。如果您正苦于以下问题:Python backend.function方法的具体用法?Python backend.function怎么用?Python backend.function使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.function方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_deep_representations
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import function [as 别名]
def get_deep_representations(model, X, batch_size=256):
"""
TODO
:param model:
:param X:
:param batch_size:
:return:
"""
# last hidden layer is always at index -4
output_dim = model.layers[-4].output.shape[-1].value
get_encoding = K.function(
[model.layers[0].input, K.learning_phase()],
[model.layers[-4].output]
)
n_batches = int(np.ceil(X.shape[0] / float(batch_size)))
output = np.zeros(shape=(len(X), output_dim))
for i in range(n_batches):
output[i * batch_size:(i + 1) * batch_size] = \
get_encoding([X[i * batch_size:(i + 1) * batch_size], 0])[0]
return output
示例2: generate_pattern
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import function [as 别名]
def generate_pattern(layer_name, filter_index, size=150):
# 过滤器可视化函数
layer_output = model.get_layer(layer_name).output
loss = K.mean(layer_output[:, :, :, filter_index])
grads = K.gradients(loss, model.input)[0]
grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)
iterate = K.function([model.input], [loss, grads])
input_img_data = np.random.random((1, size, size, 3)) * 20 + 128.
step = 1
for _ in range(40):
loss_value, grads_value = iterate([input_img_data])
input_img_data += grads_value * step
img = input_img_data[0]
return deprocess_image(img)
示例3: reverse_generator
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import function [as 别名]
def reverse_generator(generator, X_sample, y_sample, title):
"""Gradient descent to map images back to their latent vectors."""
latent_vec = np.random.normal(size=(1, 100))
# Function for figuring out how to bump the input.
target = K.placeholder()
loss = K.sum(K.square(generator.outputs[0] - target))
grad = K.gradients(loss, generator.inputs[0])[0]
update_fn = K.function(generator.inputs + [target], [grad])
# Repeatedly apply the update rule.
xs = []
for i in range(60):
print('%d: latent_vec mean=%f, std=%f'
% (i, np.mean(latent_vec), np.std(latent_vec)))
xs.append(generator.predict_on_batch([latent_vec, y_sample]))
for _ in range(10):
update_vec = update_fn([latent_vec, y_sample, X_sample])[0]
latent_vec -= update_vec * update_rate
# Plots the samples.
xs = np.concatenate(xs, axis=0)
plot_as_gif(xs, X_sample, title)
示例4: optimizer
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import function [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
# 상태가 입력, 큐함수가 출력인 인공신경망 생성
示例5: actor_optimizer
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import function [as 别名]
def actor_optimizer(self):
action = K.placeholder(shape=[None, self.action_size])
advantages = K.placeholder(shape=[None, ])
policy = self.actor.output
# 정책 크로스 엔트로피 오류함수
action_prob = K.sum(action * policy, axis=1)
cross_entropy = K.log(action_prob + 1e-10) * advantages
cross_entropy = -K.sum(cross_entropy)
# 탐색을 지속적으로 하기 위한 엔트로피 오류
entropy = K.sum(policy * K.log(policy + 1e-10), axis=1)
entropy = K.sum(entropy)
# 두 오류함수를 더해 최종 오류함수를 만듬
loss = cross_entropy + 0.01 * entropy
optimizer = RMSprop(lr=self.actor_lr, rho=0.99, epsilon=0.01)
updates = optimizer.get_updates(self.actor.trainable_weights, [],loss)
train = K.function([self.actor.input, action, advantages],
[loss], updates=updates)
return train
# 가치신경망을 업데이트하는 함수
示例6: optimizer
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import function [as 别名]
def optimizer(self):
action = K.placeholder(shape=[None, 5])
discounted_rewards = K.placeholder(shape=[None, ])
# 크로스 엔트로피 오류함수 계산
action_prob = K.sum(action * self.model.output, axis=1)
cross_entropy = K.log(action_prob) * discounted_rewards
loss = -K.sum(cross_entropy)
# 정책신경망을 업데이트하는 훈련함수 생성
optimizer = Adam(lr=self.learning_rate)
updates = optimizer.get_updates(self.model.trainable_weights,[],
loss)
train = K.function([self.model.input, action, discounted_rewards], [],
updates=updates)
return train
# 정책신경망으로 행동 선택
示例7: __init__
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import function [as 别名]
def __init__(self,
x_train,
x_mean,
x_sigma,
y_train,
sigma_noise,
batch_size,
input_gradients,
eps):
self.bs = batch_size
self.x_train = x_train
self.x_mean = x_mean
self.x_sigma = x_sigma
self.y_train = y_train
self.sigma_noise = sigma_noise
self.indices = np.random.permutation(x_train.shape[0])
self.i = 0
# compile gradient function
bs2 = self.bs // 2
self.input_gradients = input_gradients
self.eps = eps
示例8: sample_posterior
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import function [as 别名]
def sample_posterior(self, x, n=1):
r"""
Generates :code:`n` samples from the estimated posterior
distribution for the input vector :code:`x`. The sampling
is performed by the inverse CDF method using the estimated
CDF obtained from the :code:`cdf` member function.
Arguments:
x(np.array): Array of shape `(n, m)` containing `n` inputs for which
to predict the conditional quantiles.
n(int): The number of samples to generate.
Returns:
Tuple (xs, fs) containing the :math: `x`-values in `xs` and corresponding
values of the posterior CDF :math: `F(x)` in `fs`.
"""
y_pred, qs = self.cdf(x)
p = np.random.rand(n)
y = np.interp(p, qs, y_pred)
return y
示例9: optimizer
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import function [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
示例10: optimizer
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import function [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
示例11: build_model
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import function [as 别名]
def build_model(self):
input = Input(shape=self.state_size)
conv = Conv2D(16, (8, 8), strides=(4, 4), activation='relu')(input)
conv = Conv2D(32, (4, 4), strides=(2, 2), activation='relu')(conv)
conv = Flatten()(conv)
fc = Dense(256, activation='relu')(conv)
policy = Dense(self.action_size, activation='softmax')(fc)
value = Dense(1, activation='linear')(fc)
actor = Model(inputs=input, outputs=policy)
critic = Model(inputs=input, outputs=value)
actor._make_predict_function()
critic._make_predict_function()
actor.summary()
critic.summary()
return actor, critic
# make loss function for Policy Gradient
# [log(action probability) * advantages] will be input for the back prop
# we add entropy of action probability to loss
示例12: actor_optimizer
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import function [as 别名]
def actor_optimizer(self):
action = K.placeholder(shape=[None, self.action_size])
advantages = K.placeholder(shape=[None, ])
policy = self.actor.output
good_prob = K.sum(action * policy, axis=1)
eligibility = K.log(good_prob + 1e-10) * advantages
actor_loss = -K.sum(eligibility)
entropy = K.sum(policy * K.log(policy + 1e-10), axis=1)
entropy = K.sum(entropy)
loss = actor_loss + 0.01*entropy
optimizer = RMSprop(lr=self.actor_lr, rho=0.99, epsilon=0.01)
updates = optimizer.get_updates(self.actor.trainable_weights, [], loss)
train = K.function([self.actor.input, action, advantages], [loss], updates=updates)
return train
# make loss function for Value approximation
示例13: optimizer
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import function [as 别名]
def optimizer(self):
action = K.placeholder(shape=[None, 5])
discounted_rewards = K.placeholder(shape=[None, ])
# Calculate cross entropy error function
action_prob = K.sum(action * self.model.output, axis=1)
cross_entropy = K.log(action_prob) * discounted_rewards
loss = -K.sum(cross_entropy)
# create training function
optimizer = Adam(lr=self.learning_rate)
updates = optimizer.get_updates(self.model.trainable_weights, [],
loss)
train = K.function([self.model.input, action, discounted_rewards], [],
updates=updates)
return train
# get action from policy network
示例14: build_model
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import function [as 别名]
def build_model(self):
state = Input(batch_shape=(None, self.state_size))
shared = Dense(self.hidden1, input_dim=self.state_size, activation='relu', kernel_initializer='glorot_uniform')(state)
actor_hidden = Dense(self.hidden2, activation='relu', kernel_initializer='glorot_uniform')(shared)
action_prob = Dense(self.action_size, activation='softmax', kernel_initializer='glorot_uniform')(actor_hidden)
value_hidden = Dense(self.hidden2, activation='relu', kernel_initializer='he_uniform')(shared)
state_value = Dense(1, activation='linear', kernel_initializer='he_uniform')(value_hidden)
actor = Model(inputs=state, outputs=action_prob)
critic = Model(inputs=state, outputs=state_value)
actor._make_predict_function()
critic._make_predict_function()
actor.summary()
critic.summary()
return actor, critic
# make loss function for Policy Gradient
# [log(action probability) * advantages] will be input for the back prop
# we add entropy of action probability to loss
示例15: actor_optimizer
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import function [as 别名]
def actor_optimizer(self):
action = K.placeholder(shape=(None, self.action_size))
advantages = K.placeholder(shape=(None, ))
policy = self.actor.output
good_prob = K.sum(action * policy, axis=1)
eligibility = K.log(good_prob + 1e-10) * K.stop_gradient(advantages)
loss = -K.sum(eligibility)
entropy = K.sum(policy * K.log(policy + 1e-10), axis=1)
actor_loss = loss + 0.01*entropy
optimizer = Adam(lr=self.actor_lr)
updates = optimizer.get_updates(self.actor.trainable_weights, [], actor_loss)
train = K.function([self.actor.input, action, advantages], [], updates=updates)
return train
# make loss function for Value approximation