def fit(self, epochs=1000, alpha=0.001, beta=0, verbose=False): """ Find optimal weights to fit to samples. :param epochs: Number of epochs to train model for \\ :param alpha: Learning rate hyperparameter \\ :param beta: Regularization hyperparameter \\ :param verbose: Flag for verbose logging \\ :return: Optimal weights \\ """ X, y = self.data, self.labels optimizer = tf.train.GradientDescentOptimizer(learning_rate=alpha) gradient = tfe.implicit_gradients(self._loss) for epoch in range(epochs): optimizer.apply_gradients(gradient(beta)) if verbose: once_every = epochs // 10 if (epoch + 1) % once_every == 0: loss = self._loss(beta) print('Epoch: %i --> loss = %0.3f' % (epoch + 1, loss)) return self.w.numpy()
def train(): batch_size = 10 num_epoches = 2 data_size = 500 * 10 display_iter = 100 dir = r"E:\code\python\deeplearning\tensorflow1.x\data\mnist_digits_images" save_dir = r"E:\\code\\python\\deeplearning\\tensorflow1.x\\data\\ck\\" dataset = get_dataset(directory=dir, size=[28, 28], batch_size=batch_size) iterator = dataset.make_one_shot_iterator() data = iterator.get_next() model = MNISTModel(name='net') global_step = tf.train.get_or_create_global_step() optimizer = tf.train.AdamOptimizer(learning_rate=1e-4) grad_fn = tfe.implicit_gradients(compute_loss) while global_step * batch_size / data_size < num_epoches: step = int(global_step * batch_size / data_size) x, y = tf.cast(data[0], dtype=tf.float32), data[1] grads_and_vars = grad_fn(model, x, y) optimizer.apply_gradients( grads_and_vars=grads_and_vars, global_step=tf.train.get_or_create_global_step()) # 获取要保存的变量 if global_step % display_iter == 0: all_variables = (model.variables + optimizer.variables() + [global_step]) tfe.Saver(all_variables).save(save_dir, global_step=global_step) # 检查点文件 print("Epoch:{}, Iteration:{}, loss:{}".format( step, global_step, compute_loss(model, x, y))) global_step = tf.train.get_or_create_global_step()
def optimize_policy(self, policy, env, n_samples_per_rollout, time_horizon, n_iter=1000, verbose=1): if policy.type == 'discrete': loss = self.loss_discrete elif policy.type == 'continuous': loss = self.loss_continuous grads = tfe.implicit_gradients(loss) for i in range(n_iter): observations, actions, q_values, mean_reward = self.policy_rollout( policy, env, n_samples_per_rollout, time_horizon) if verbose >= 1 and (i+1) % 1 == 0: print('Iteration {0}. Loss: {1}. Average reward: {2}.' .format(i+1, loss(policy, observations, actions, q_values), mean_reward)) if verbose >= 2 and (i+1) % 10 == 0: render_policy(policy, env) self.optimizer.apply_gradients( grads(policy, observations, actions, q_values)) return n_iter
def call(self, inputs): """Calculates logits and action. Args: inputs: Observations from a step in the cart-pole environment, of shape `(batch_size, input_size)` Returns: logits: the logits output by the output layer. This can be viewed as the likelihood vales of choosing the left (0) action. Shape: `(batch_size, 1)`. actions: randomly selected actions ({0, 1}) based on the logits. Shape: `(batch_size, 1)`. """ hidden = self._hidden_layer(inputs) logits = self._output_layer(hidden) left_prob = tf.nn.sigmoid(logits) action_probs = tf.concat([left_prob, 1.0 - left_prob], 1) self._grad_fn = eager.implicit_gradients( self._get_cross_entropy_and_save_actions) actions = tf.multinomial(tf.log(action_probs), 1) return logits, actions
def __init__(self, hidden_size): super(EagerPolicyNetwork, self).__init__() self._hidden_layer = tf.keras.layers.Dense(hidden_size, activation=tf.nn.elu) self._output_layer = tf.keras.layers.Dense(1) self._grad_fn = eager.implicit_gradients( self._get_cross_entropy_and_save_actions)
def __init__(self, params): super(SimpleNN, self).__init__() self.params = params self.hidden_layers = [ self.track_layer(tf.layers.Dense(params.state_size, activation=tf.nn.relu)) for _ in range(params.num_layers)] self.output_layer = self.track_layer(tf.layers.Dense(params.num_classes, name='output_layer')) self.optimizer = tf.train.AdamOptimizer(1e-3) self.grads_fn = tfe.implicit_gradients(self.loss_fn)
def __init__(self, env): self.actions = list(range(env.action_space.n)) self.num_states = env.observation_space.n self.rewards = tfe.Variable(tf.random_uniform( [env.observation_space.n]), name="rewards") """ self.rewards = tfe.Variable(initial_value=[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0,], name="rewards") """ self._updater = tfe.implicit_gradients(self.loss)
def train(): datasize = 100 train_X, train_y = generate_data(datasize=datasize) dataset = gen_datast(train_X, train_y) iterator = dataset.make_one_shot_iterator() data = iterator.get_next() optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01) grad_fn = tfe.implicit_gradients(compute_loss) global_step = tf.train.get_or_create_global_step() iter = 0 training_epoches = 20 display_step = 2 batch_size = 10 plot_steps = [] plot_losses = [] while global_step * batch_size / datasize < training_epoches: step = int(global_step * batch_size / datasize) X = tf.cast(data['X'], dtype=tf.float32) y = tf.cast(data['y'], dtype=tf.float32) grads_and_vars = grad_fn(linear_regression, X, y) optimizer.apply_gradients( grads_and_vars=grads_and_vars, global_step=global_step ) # compute_cost的输入为linear_regression, data['X'], data['y'] loss = compute_loss(linear_regression, X, y) if step % display_step == 0: print("Epoch:", step + 1, "loss=", loss, "weight=", weight, "b=", b) if not (loss == "NA"): # plot_steps.append(global_step.eval()) 不行 plot_steps.append(iter) plot_losses.append(loss) data = iterator.get_next() global_step = tf.train.get_or_create_global_step() iter += 1 print("Done!!!") print(plot_steps) show_data(plot_steps, plot_losses)
def optimize_policy(self, env, n_samples_per_rollout, time_horizon=200, n_iter=1000, early_stop=True, verbose=True): grads = tfe.implicit_gradients(self.loss) for i in range(n_iter): observations, actions, q_values, mean_reward = self.policy_rollout( env, n_samples_per_rollout, time_horizon) if verbose and (i + 1) % 10 == 0: print('Iteration {0}. Average reward: {1}'.format( i + 1, mean_reward)) if mean_reward == 200 and early_stop: return i + 1 self.optimizer.apply_gradients( grads(observations, actions, q_values)) return n_iter
#定义优化器 optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) #定义函数计算loss def _grad_fn(yolo_v3, images_tensor, list_y_trues): logits = yolo_v3(images_tensor) loss = loss_fn(list_y_trues, logits, anchors=COCO_ANCHORS, image_size=[imgsize, imgsize]) return loss grad = tfe.implicit_gradients(_grad_fn) #获得计算梯度的函数 history = [] for i in range(num_epoches): _loop_train(yolo_v3, optimizer, generator, grad) #训练 loss_value = _loop_validation(yolo_v3, generator) #验证 print("{}-th loss = {}".format(i, loss_value)) #收集loss history.append(loss_value) if loss_value == min(history): #只有loss创新低时再保存模型 print(" update weight {}".format(loss_value)) yolo_v3.save_weights("{}.h5".format(save_fname)) ################################################################ #使用模型
def all_loss(encoder, decoder, img_tensor, target): loss = 0 hidden = decoder.reset_state(batch_size=target.shape[0]) dec_input = tf.expand_dims([word_index['<start>']] * target.shape[0], 1) feature = encoder(img_tensor) # (batch_size,49,256) for i in range(1, target.shape[1]): predictions, hidden, _ = decoder(dec_input, feature, hidden) loss += loss_mask(target[:, i], predictions) dec_input = tf.expand_dims(target[:, i], 1) return loss grad = tfe.implicit_gradients(all_loss) # 实现单步训练过程 def train_one_epoch(encoder, decoder, optimizer, step_counter, dataset, epoch): total_loss = 0 for (step, (img_tensor, target)) in enumerate(dataset): loss = 0 optimizer.apply_gradients(grad(encoder, decoder, img_tensor, target), step_counter) loss = all_loss(encoder, decoder, img_tensor, target) total_loss += (loss / int(target.shape[1])) if step % 5 == 0: print('Epoch {} Batch {} Loss {:.4f}'.format(epoch + 1, step,
# Using sparse_softmax cross entropy return tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits( logits=inference_fn(inputs), labels=labels)) # Calculate accuracy def accuracy_fn(inference_fn, inputs, labels): prediction = tf.nn.softmax(inference_fn(inputs)) correct_pred = tf.equal(tf.argmax(prediction, 1), labels) return tf.reduce_mean(tf.cast(correct_pred, tf.float32)) # SGD Optimizer optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) # Compute gradients grad = tfe.implicit_gradients(loss_fn) # Training average_loss = 0. average_acc = 0. for step in range(num_steps): # Iterate through the dataset try: d = dataset_iter.next() except StopIteration: # Refill queue dataset_iter = tfe.Iterator(dataset) d = dataset_iter.next() # Images
global_step = tf.train.get_or_create_global_step() def getcost(x, y): #定义函数,计算loss值 # 前向结构 z = tf.cast(tf.multiply(np.asarray(x, dtype=np.float32), W) + b, dtype=tf.float32) cost = tf.reduce_mean(tf.square(y - z)) #loss值 return cost learning_rate = 0.01 # 随机梯度下降法作为优化器 optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) grad = tfe.implicit_gradients(getcost) #获得计算梯度的函数 #定义saver,演示两种方法处理检查点文件 savedir = "logeager/" savedirx = "logeagerx/" saver = tf.train.Saver([W, b], max_to_keep=1) #生成saver。 max_to_keep=1,表明最多只保存一个检查点文件 saverx = tfe.Saver([W, b]) #生成saver。 max_to_keep=1,表明最多只保存一个检查点文件 kpt = tf.train.latest_checkpoint(savedir) #找到检查点文件 kptx = tf.train.latest_checkpoint(savedirx) #找到检查点文件 if kpt != None: saver.restore(None, kpt) #两种加载方式都可以 saverx.restore(kptx)
# Create Linear Regression function def linear_regression(theta): return theta * W + b # Create MSE function (y-yi)^2 / 2*n_samples def MSE(model, theta, labels): #return tf.reduce_sum(tf.pow(model(theta) - labels, 2) / (2*n_samples)) return tf.reduce_mean(tf.square(tf.subtract(model(theta), labels))) # Define SGD Optimizer optimizer = tf.train.GradientDescentOptimizer(learning_rate) # Computation of gradients grad = tfe.implicit_gradients(MSE) # Print initial cost cost = MSE(linear_regression, train_X, train_Y) print("Cost: %.9f" % cost) print("Weight: %s" % W.numpy()) print("Bias: %s" % b.numpy()) for step in range(num_steps): optimizer.apply_gradients(grad(linear_regression, train_X, train_Y)) if (step + 1) % display_step == 0 or step == 0: print("Epoch:", '%04d' % (step + 1), "cost=", "{:.9f}".format(MSE(linear_regression, train_X, train_Y)), "W=", W.numpy(), "b=", b.numpy())
if batch < n_batch - 1: batch_X = train_list[batch * batch_size:(batch + 1) * batch_size, :, :, :] batch_Y = train_label[batch * batch_size:(batch + 1) * batch_size, :, :] else: batch_X = train_list[batch * batch_size:, :, :, :] batch_Y = train_label[batch * batch_size:, :, :] return batch_X, batch_Y # Adam Optimizer optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) # Compute gradients grad = tfe.implicit_gradients(mean_square_fn) # Initial cost, before optimizing print("Initial cost = {:.9f}".format(mean_square_fn(pred, batch_Y))) # Training costs = [] params = stage_1_util_v1.params for epoch in range(epochs): batch_cost = 0.0 for batch in range(n_batch): batch_X, batch_Y = next_batch(batch, batch_size) pred = stage_1_model(batch_X, n_joints, is_training,
def train(self, global_step): if self.use_difference_critic: # Sample replay buffer for training batchA = self.memory.sample(batch_size=self.batch_size * 2) batchB = self.memory.sample(batch_size=self.batch_size * 2) # Prepare training data for critic actionA1s = self.actor(tf.convert_to_tensor(batchA['obs1'])) actionB1s = self.actor(tf.convert_to_tensor(batchB['obs1'])) diff_Qs = self.difference_critic( tf.convert_to_tensor(batchA['obs1']), actionA1s, tf.convert_to_tensor(batchB['obs1']), actionB1s) target_diff_Qs = batchA['rewards'] - batchB[ 'rewards'] + self.gamma * diff_Qs target_diff_Qs = tf.clip_by_value( tf.convert_to_tensor(target_diff_Qs), 2 * self.return_range[0], 2 * self.return_range[1]) target_diff_Qs = tf.constant( target_diff_Qs ) # target_diff_Qs should be treated as a constant (like supervised learning labels) # Gradient descent step on critic diff_critic_grad_fn = tfe.implicit_gradients( self.difference_critic_loss) difference_critic_grad = diff_critic_grad_fn( tf.convert_to_tensor(batchA['obs0']), tf.convert_to_tensor(batchA['actions']), tf.convert_to_tensor(batchB['obs0']), tf.convert_to_tensor(batchB['actions']), target_diff_Qs) difference_critic_grad = filter_grads_and_vars_by_scope( difference_critic_grad, 'difference_critic') self.difference_critic_optimizer.apply_gradients( difference_critic_grad, global_step=global_step) # Gradient descent step on actor actor_grad_fn = tfe.implicit_gradients( self.actor_loss_with_difference_critic) actor_grad = actor_grad_fn(tf.convert_to_tensor(batchA['obs0']), tf.convert_to_tensor(batchB['obs0']), tf.convert_to_tensor(batchB['actions'])) actor_grad = filter_grads_and_vars_by_scope( actor_grad, 'actor') # We only want to optimize the actor at this stage self.actor_optimizer.apply_gradients(actor_grad, global_step=global_step) else: # Sample replay buffer for training batch = self.memory.sample(batch_size=self.batch_size) # Prepare training data for critic action1s = self.actor(tf.convert_to_tensor(batch['obs1'])) Qs = self.critic(tf.convert_to_tensor(batch['obs1']), action1s) terminals = batch['terminals1'].astype('float32') target_Qs = batch['rewards'] + (1. - terminals) * self.gamma * Qs target_Qs = tf.clip_by_value(tf.convert_to_tensor(target_Qs), self.return_range[0], self.return_range[1]) target_Qs = tf.constant( target_Qs ) # target_Qs should be treated as a constant (like supervised learning labels) # Gradient descent step on critic_loss critic_grad_fn = tfe.implicit_gradients(self.critic_loss) critic_grad = critic_grad_fn( tf.convert_to_tensor(batch['obs0']), tf.convert_to_tensor(batch['actions']), target_Qs) critic_grad = filter_grads_and_vars_by_scope(critic_grad, 'critic') self.critic_optimizer.apply_gradients(critic_grad, global_step=global_step) # Gradient descent step on actor actor_grad_fn = tfe.implicit_gradients(self.actor_loss) actor_grad = actor_grad_fn(tf.convert_to_tensor(batch['obs0'])) actor_grad = filter_grads_and_vars_by_scope(actor_grad, 'actor') self.actor_optimizer.apply_gradients(actor_grad, global_step=global_step)
grad_f = tfe.gradients_function(f) print(f(2., 1.)) # 2.0 print(grad_f(2., 1.)) # [Tensor(3.0), Tensor(-2.0)] # It's also possible to calculate partial derivatives wrt. Variables implicitly # involved in the computation of the function x = tfe.Variable(1.0, name='x') y = tfe.Variable(2.0, name='y') def g(z): return z * x + z * y**2 grad_g = tfe.gradients_function(g) imp_grad_g = tfe.implicit_gradients(g) print(g(3.)) # Tensor(15.0) print(grad_g(3.)) # [Tensor(5.0)] is [dg/dz] print(imp_grad_g(3.)) # [(Tensor(3.0), Variable(name='x:0')), # (Tensor(12.0), Variable(name='y:0'))] is [dg/dx, dg/dy] # You can also calculate partial derivatives of one computation inside a # GradientTape context manager x = tf.constant(3.) y = tfe.Variable(3.) with tfe.GradientTape() as tape: tape.watch(x) # Trainable Variables are automatically watched f = x**2 + y**2 print(tape.gradient(f, [x, y]))
# Linear regression (Wx + b) def linear_regression(inputs): return inputs * W + b # Mean square error def mean_square_fn(model_fn, inputs, labels): return tf.reduce_sum(tf.pow(model_fn(inputs) - labels, 2)) / (2 * n_samples) # SGD Optimizer optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) # Compute gradients grad = tfe.implicit_gradients(mean_square_fn) # Initial cost, before optimizing print("Initial cost= {:.9f}".format( mean_square_fn(linear_regression, train_X, train_Y)), "W=", W.numpy(), "b=", b.numpy()) # Training for step in range(num_steps): optimizer.apply_gradients(grad(linear_regression, train_X, train_Y)) if (step + 1) % display_step == 0 or step == 0: print("Epoch:", '%04d' % (step + 1), "cost=", "{:.9f}".format(mean_square_fn(linear_regression, train_X, train_Y)), "W=", W.numpy(), "b=", b.numpy())
# 必须在loss_fn函数里面进行前项传播,然后经implicit_gradients包装后,才能进行梯度计算 def loss_fn(forward, inputs, ys, keep_prob, is_training, momentum=0.9): # tf.nn.softmax_cross_entropy_with_logits_v2等价于tf.nn.softmax+cross_entropy # 1.最后一层不需要使用激活函数softmax,直接使用softmax_cross_entropy_with_logits_v2即可 return tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits_v2(labels=ys, logits=forward( inputs, keep_prob, is_training, momentum))) '''--------定义loss函数 end--------''' '''--------定义优化器与梯度函数 start--------''' # ys与out_layer都是one-hot类型的向量 optimizer = tf.train.AdamOptimizer(0.01) grad_fn = tfe.implicit_gradients(loss_fn) '''--------定义优化器与梯度函数 end--------''' '''--------训练start--------''' model = CNNModel() for i in range(100000): # 获取要训练的数据 batch_xs, batch_ys = mnist.train.next_batch(32) # 计算每一个变量的梯度 grad = grad_fn(model.forward, batch_xs.reshape(-1, 28, 28, 1), batch_ys, 0.8, True) # 优化梯度 optimizer.apply_gradients(grad) if i % 50 == 0: # 测试阶段 y_pre = model.forward(tf.cast(x_test, dtype=tf.float32), 1, False) # 测试正确率
## Gradients - Automatic differentiation is built into eager execution # Under the hood ... # - Operations are recorded on a tap # - The tape is Played back to compute gradients # - This is reverse-mode differentiation (backpropagation) # Eg: 01 def square(x): return x**2 grad = tfe.gradients_function(square) # differentiation w.r.t input of square print(square(3.)) print(grad(3.)) # Eg: 02 x = tfe.Variable(2.0) # use when eager execution is enabled def loss(y): return (y - x**2)**2 grad = tfe.implicit_gradients( loss) # Differentiation w.r.t variables used to compute loss print(loss(7.)) print(grad(7.)) # [gradient w.r.t x, x]
for i in x: print(i) def square(x): return x**2 grad = tfe.gradients_function(square) print(square(3.)) print(grad(3.)) x = tfe.Variable(2.0) def loss(y): return (y - x**2)**2 grad = tfe.implicit_gradients(loss) print(loss(7.)) print(grad(7.)) #APIs for computing gradients work #tfe.gradients_function() #tfe.value_and_gradients_function() #tfe.implicit_gradients() #tfe.implicit_value_and_gradients()
def __init__(self): super(MNISTModel, self).__init__() self.layer1 = self.track_layer(tf.layers.Dense(units=10)) self.layer2 = self.track_layer(tf.layers.Dense(units=10)) def call(self, input): """Actually runs the model.""" result = self.layer1(input) result = self.layer2(result) return result # Let's make up a blank input image model = MNISTModel() batch = tf.zeros([1, 1, 784]) print(batch.shape) result = model(batch) print(result) def loss_function(model, x, y): y_ = model(x) return tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=y_) optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001) for (x, y) in tfe.Iterator(batch): grads = tfe.implicit_gradients(loss_function)(model, x, y) optimizer.apply_gradients(grads)