def get_optimizer(learning_rate, hparams): """Get the tf.train.Optimizer for this optimizer string. Args: learning_rate: The learning_rate tensor. hparams: tf.contrib.training.HParams object with the optimizer and momentum values. Returns: optimizer: The tf.train.Optimizer based on the optimizer string. """ return { "rmsprop": tf.RMSPropOptimizer(learning_rate, decay=0.95, momentum=hparams.momentum, epsilon=1e-4), "adam": tf.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-8), "adagrad": tf.AdagradOptimizer(learning_rate, initial_accumulator_value=1.0), "mom": tf.MomentumOptimizer(learning_rate, momentum=hparams.momentum), "sgd": tf.GradientDescentOptimizer(learning_rate) }.get(hparams.optimizer)
def __init__(self, learning_rate, no_inputs): # create feature columns feature_cols = [] for i in range(no_inputs): feature_cols.append( tf.feature_column.categorical_column_with_vocabulary_list( str(i + 1), vocabulary_list=['S', 'O', 'E'])) hidden_layer_units = [27, 27, 27] # instantiate estimator self.estimator = tf.estimator.DNNRegressor( feature_columns=feature_cols, model_dir='train', hidden_units=hidden_layer_units, optimizer=lambda: tf.AdamOptimizer( learning_rate=tf.exponential_decay(learning_rate=0.1, global_step=tf. get_global_step(), decay_steps=10000, decay_rate=0.96)))
def createGraph(a,b,lr,optimiser ): tf.reset_default_graph() w1 = tf.get_variable(dtype=tf.float32, shape=(), name="w1", initializer=tf.random_normal_initializer(0.0,1.0)) w2 = tf.get_variable(dtype=tf.float32, shape=(), name="w2", initializer=tf.random_normal_initializer(0.0,1.0)) a = tf.constant(a , name="a") b = tf.constant(b, name="b") costFunc = tf.add( tf.square( tf.subtract( a, w1, name='term1'), name='term1_sq'),\ tf.multiply( b,\ tf.square( tf.subtract( \ w2,\ tf.square( w1,\ name='weight1_sq'),\ name='term2'),\ name='term2_sq'),\ name='term2_Sq_Mul_b'),\ name='costFunc') print(a,b) print(lr) print(optimiser) if 'gd'==optimiser: train_step =tf.train.GradientDescentOptimizer(learning_rate=lr, name='GradientDescent').minimize(costFunc, name= 'train_step') if 'gdm'==optimiser: train_step =tf.MomentumOptimizer(learning_rate=lr,momentum=0.9, name='Momentum').minimize(costFunc, name= 'train_step') if 'adam'==optimiser: train_step =tf.AdamOptimizer(learning_rate=lr,name='Adam').minimize(costFunc, name= 'train_step') init = tf.global_variables_initializer() #once you build the graph, write to file file_writer= tf.summary.FileWriter("./datasets/myTensorboardLogs/HW1_Oct11/", tf.get_default_graph()) sess = tf.Session() sess.run(fetches=[init]) for x in range(10): acc= sess.run(fetches=[train_step,w1,w2]) print('Accuracy at step %s: %s' % (x, acc))
def build_model(features, lables, mode, params): output_size = params["output_size"] num_layers = params["num_layers"] batch_size = params["batch_size"] num_step = params["num_step"] input_size = params["input_size"] hidden_size = params["hidden_size"] x = tf.placeholder(tf.float32, shape=[batch_size, num_step, input_size]) y = tf.placeholder(tf.float32, shape=[batch_size, num_step, output_size]) lstm_cell = tf.contrib.rnn.BasicLSTMCell(hidden_size) lstm_cell = tf.contrib.rnn.DropoutWrapper(lstm_cell, output_keep_prob=0.5) cells = tf.contrib.rnn.MultiRNNCell([lstm_cell for _ in range(num_layers)]) init_state = lstm_cell.zero_state(batch_size, tf.float32) lstm_output, state = tf.nn.dynamic_rnn(cells, x, initial_state=init_state) lstm_output_reshape = (lstm_output, [batch_size, -1]) dence1 = tf.layers.dense(inputs=lstm_output_reshape, units=num_step * 128, activation=tf.nn.relu) Batch_out = tf.layers.batch_normaliztion(inputs=dence1, axis=1) output = tf.layers.dence(inputs=Batch_out, units=num_step * output_size, actication=tf.nn.relu) y_reshaped = tf.reshape(y, shape=[batch_size, -1]) if mode == tf.estimator.Modekeys.PREDICT: prediction = {'pred': output} return tf.estimator.Estimatorspec(mode, prediction=prediction) reward = y_reshaped - output r_sum = tf.reduce_sum(reward) train_op = tf.AdamOptimizer(lr).minimize( -r_sum, global_step=tf.train.got_global_step()) return tf.estimator.Estimatorspec(mode, loss=r_sum, train_op=train_op)
def train_neural_network(x): prediction = neural_network_model(x) cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(prediction, y)) optimizer = tf.AdamOptimizer().minimize(cost) hm_epochs = 10 with tf.Session() as sess: sess.run(tf.initialize_all_variables()) for epoch in range(hm_epochs): epoch_loss = 0 for _ in range(int(mnist.train.num_examples / batch_size)): x, y = mnist.train.next_batch(batch_size) _, c = sess.run([optimizer, cost], feed_dict={x: x, y: y}) epoch_loss += c print('Epoch', epoch, 'completed out of', hm_epochs, 'loss:', epoch_loss) correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct, 'float')) print('Accuracy:', accuracy.eval({ x: mnist.test.images, y: mnist.test.labels }))
results_dir = dir + '/data/st' input_shape = [None, args.image_size, args.image_size, 3] fst = fast_style_transfer(input_shape, ratio=args.ratio, nb_res_layer=args.nb_res_layer) tf.image_summary('input_img', fst['input'], 2) tf.image_summary('output_img', fst['output'], 2) vgg = vgg19.Vgg19() style_loss = 1 content_loss = 1 tv_loss = 1 tf.scalar_summary('style_loss', style_loss) tf.scalar_summary('content_loss', content_loss) tf.scalar_summary('tv_loss', tv_loss) adam = tf.AdamOptimizer(1e-3) train_op = adam.minimize(total_loss) for i in range(10): style_coef = np.random.random_integers(50,150) content_coef = np.random.random_integers(5,10) tv_coef = np.random.random_integers(5,10) total_loss = style_coef * style_loss + content_coef * content_loss + tv_coef * tv_loss tf.scalar_summary('total_loss', total_loss) summaries_op = tf.merge_all_summaries()
layer_1 = new_fc_layer(x_flat, img_size_flat, layer_1_size) layer_2 = new_fc_layer(layer_1, layer_1_size, layer_2_size) layer_3 = new_fc_layer(layer_2, layer_2_size, layer_3_size) layer_output = new_fc_layer(layer_3, layer_3_size, layer_output_size) y_pred = tf.nn.softmax(layer_output) y_pred_cls = tf.argmax(y_pred, axis=1) cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=layer_output, labels=y_true) cost = tf.reduce_mean(cross_entropy) optimizer = tf.AdamOptimizer(learning_rate=1e-4).minimize(cost) correct_pred_vec = tf.equal(y_pred_cls, y_true_cls) accuracy = tf.reduce_mean(tf.cast(correct_pred_vec, tf.float32)) session = tf.Session() session.run(tf.global_variable_initializer()) saver = tf.train.saver() # Training Routine (optimize) epochs = 10
# regularization. estimator = DNNRegressor( feature_columns=[categorical_feature_a_emb, categorical_feature_b_emb], hidden_units=[1024, 512, 256], optimizer=tf.train.ProximalAdagradOptimizer( learning_rate=0.1, l1_regularization_strength=0.001 )) # Or estimator using an optimizer with a learning rate decay. estimator = DNNRegressor( feature_columns=[categorical_feature_a_emb, categorical_feature_b_emb], hidden_units=[1024, 512, 256], optimizer=lambda: tf.AdamOptimizer( learning_rate=tf.exponential_decay( learning_rate=0.1, global_step=tf.get_global_step(), decay_steps=10000, decay_rate=0.96)) # Or estimator with warm-starting from a previous checkpoint. estimator = DNNRegressor( feature_columns=[categorical_feature_a_emb, categorical_feature_b_emb], hidden_units=[1024, 512, 256], warm_start_from="/path/to/checkpoint/dir") # Input builders def input_fn_train: # returns x, y pass estimator.train(input_fn=input_fn_train, steps=100) def input_fn_eval: # returns x, y
h_pool2 = max_pool_2x2(h_conv2) """全连接层""" W_fc1 = weight_variable([7 * 7 * 64, 1024]) b_fc1 = bias_variable([1024]) h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2, W_fc1) + b_fc1) """DropOut层""" h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) """全连接层""" W_fc2 = weight_variable([1024, 10]) b_fc2 = bias_variable([10]) y = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) """定义损失和优化器""" cross_entropy = tf.reduce_mean( -tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) train_step = tf.AdamOptimizer(learning_rate).minimize(cross_entropy) predictions = tf.eqaul(tf.argmax(y_, 1), tf.argmax(y, 1)) accuarcy = tf.reduce_mean(tf.cast(predictions, tf.float32)) """训练过程""" tf.global_variables_initializer().run() for i in range(training_nums): x_batch, y_batch = tf.train.next_batch(batch_size) if i % 100 == 0: print('step: %d, accuarcy: %g' % (i, accuarcy.eval(feed_dict={ x: x_batch, y_: y_batch, keep_prob: 1.0 }))) train_step.run(feed_dict={x: x_batch, y_: y_batch, keep_prob: 0.5})
} # Setting up layers of network layer_1 = tf.add(tf.matmul(X, weights['w1']), biases['b1']) layer_2 = tf.add(tf.matmul(layer_1, weights['w2']), biases['b2']) layer_3 = tf.add(tf.matmul(layer_2, weights['w3']), biases['b3']) layer_drop = tf.nn.dropout(layer_3, keep_prob) output_layer = tf.matmul(layer_3, weights['out'] + biases['out']) # Defining loss function cross_entropy = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits( labels=Y, logits=output_layer ) ) train_step = tf.AdamOptimizer(1e-4).minimize(cross_entropy) # Evaluating loss and accuracy at runtime correct_pred = tf.equal(tf.argmax(output_layer, 1), tf.argmax(Y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) # Creating a session init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) # train on mini batches for i in range(n_iterations): batch_x, batch_y = mnist.train.next_batch(batch_size) sess.run(train_step, feed_dict={ X: batch_x, Y: batch_y, keep_prob: dropout
def build_computation_graph(self): self.loss = 0.0 # TODO: build full computation graph, I think this should depend on using a MLP to sample actions not sure self.meta_optimizer = tf.AdamOptimizer(self.beta).minimize(self.loss)
svconfdict, mvconfdict, output_number = 3 + 3, sv_sqfeatnum = 16, mv_featnum = 256, use_rgb = True, # Obsoleted, always True master_driver = None, grads_applier = None worker_thread_index = -1, continuous_policy_loss = False): self.init_state = init_state self.worker_thread_index = worker_thread_index if grads_applier is not None: self.grads_applier = grads_applier else: self.grads_applier = tf.AdamOptimizer() self.action_size = output_number self.continuous_policy_loss = continuous_policy_loss self.renderer = pyosr.Renderer() r = self.renderer r.pbufferWidth = config.DEFAULT_RES r.pbufferHeight = config.DEFAULT_RES if master_driver is None: r.setup() r.loadModelFromFile(models[0]) if len(models) > 1 and models[1] is not None: r.loadRobotFromFile(models[1]) r.state = np.array(init_state, dtype=np.float32) print('robot loaded') r.scaleToUnit() r.angleModel(0.0, 0.0)
import tensorflow as tf # Note tf.contrib.keras from mid-March 2017, TF v1.1 # and tf.keras by TF v1.2 # At the current stage, "AttributeError: 'module' object has no attribute 'keras'" video = tf.keras.layers.Input(shape=(None, 150, 150, 3)) cnn = tf.keras.application.InceptionV3(weights='imagenet', include_top=False, pool='avg') cnn.trainable = False encoded_frames = tf.keras.layers.TimeDistributed(cnn)(video) encoded_vid = tf.layers.LSTM(256)(encoded_frames) question = tf.keras.layers.Input(shape=(100), dtype='int32') x = tf.keras.layers.Embedding(10000, 256, mask_zero=True)(question) encoded_q = tf.keras.layers.LSTM(128)(x) x = tf.keras.layers.concat([encoded_vid, encoded_q]) x = tf.keras.layers.Dense(128, activation=tf.nn.relu)(x) outputs = tf.keras.layers.Dense(1000)(x) model = tf.keras.models.Model([video, question], outputs) model.compile(optimizer=tf.AdamOptimizer(), loss=tf.softmax_crossentropy_with_logits)
def optimizer(loss, var_list): initial_learning_rate = 0.005 decay = 0.95 num_decay_steps = 150 step = slim.get_or_create_global_step() lr = tf.train.exponential_decay(learning_rate=initial_learning_rate , global_step=step, decay_steps=num_decay_steps, decay_rate=decay, staircase=True, name='learning_rate') train_op = tf.AdamOptimizer(learning_rate).minimize(loss, global_step=step, var_list=var_list) return train_op class GAN(object): def __init__(self, params): with tf.variable_scope('G'): self.noise = tf.placeholder(tf.float32, shape=(params.batch_size, 1)) self.generator = generator(self.noise, params.hidden_size) # The discriminator tries to tell the difference between samples from # the true data distribution (self.x) and the generated samples # (self.noise). self.x = tf.placeholder(tf.float32, shape=(params.batch_size, 1))