def test_batch_and_unpad_2d_tensors_of_different_sizes_in_1st_dimension( self): with self.test_session() as sess: batch_size = 3 num_batches = 2 examples = tf.Variable(tf.constant(2, dtype=tf.int32)) counter = examples.count_up_to(num_batches * batch_size + 2) boxes = tf.tile(tf.reshape(tf.range(4), [1, 4]), tf.stack([counter, tf.constant(1)])) batch_queue = batcher.BatchQueue(tensor_dict={'boxes': boxes}, batch_size=batch_size, batch_queue_capacity=100, num_batch_queue_threads=1, prefetch_queue_capacity=100) batch = batch_queue.dequeue() for tensor_dict in batch: for tensor in tensor_dict.values(): self.assertAllEqual([None, 4], tensor.get_shape().as_list()) tf.initialize_all_variables().run() with slim.queues.QueueRunners(sess): i = 2 for _ in range(num_batches): batch_np = sess.run(batch) for tensor_dict in batch_np: for tensor in tensor_dict.values(): self.assertAllEqual(tensor, np.tile(np.arange(4), (i, 1))) i += 1 with self.assertRaises(tf.errors.OutOfRangeError): sess.run(batch)
def test_batch_and_unpad_2d_tensors_of_same_size_in_all_dimensions(self): with self.test_session() as sess: batch_size = 3 num_batches = 2 examples = tf.Variable(tf.constant(1, dtype=tf.int32)) counter = examples.count_up_to(num_batches * batch_size + 1) image = tf.reshape(tf.range(1, 13), [4, 3]) * counter batch_queue = batcher.BatchQueue(tensor_dict={'image': image}, batch_size=batch_size, batch_queue_capacity=100, num_batch_queue_threads=1, prefetch_queue_capacity=100) batch = batch_queue.dequeue() for tensor_dict in batch: for tensor in tensor_dict.values(): self.assertAllEqual([4, 3], tensor.get_shape().as_list()) tf.initialize_all_variables().run() with slim.queues.QueueRunners(sess): i = 1 for _ in range(num_batches): batch_np = sess.run(batch) for tensor_dict in batch_np: for tensor in tensor_dict.values(): self.assertAllEqual( tensor, np.arange(1, 13).reshape((4, 3)) * i) i += 1 with self.assertRaises(tf.errors.OutOfRangeError): sess.run(batch)
def soft_load(self): self.logger.info("loading model parameter") tf.initialize_all_variables().run(session=self.sess) saver = tf.train.Saver() saver.restore(self.sess, tf.train.latest_checkpoint(self.checkpoints_dir)) self.logger.info("loading model successfully")
def test_batcher_when_batch_size_is_one(self): with self.test_session() as sess: batch_size = 1 num_batches = 2 examples = tf.Variable(tf.constant(2, dtype=tf.int32)) counter = examples.count_up_to(num_batches * batch_size + 2) image = tf.reshape(tf.range(counter * counter), tf.stack([counter, counter])) batch_queue = batcher.BatchQueue(tensor_dict={'image': image}, batch_size=batch_size, batch_queue_capacity=100, num_batch_queue_threads=1, prefetch_queue_capacity=100) batch = batch_queue.dequeue() for tensor_dict in batch: for tensor in tensor_dict.values(): self.assertAllEqual([None, None], tensor.get_shape().as_list()) tf.initialize_all_variables().run() with slim.queues.QueueRunners(sess): i = 2 for _ in range(num_batches): batch_np = sess.run(batch) for tensor_dict in batch_np: for tensor in tensor_dict.values(): self.assertAllEqual( tensor, np.arange(i * i).reshape((i, i))) i += 1 with self.assertRaises(tf.errors.OutOfRangeError): sess.run(batch)
def create_svm(m, optimize_model): batch_size = 1 model_name = f"svm{m}" with tf.Session() as sess: x = tf.placeholder(tf.float32, shape=(m,), name='x') y = tf.placeholder(tf.float32, shape=(1,), name='y') w = tf.placeholder(tf.float32, shape=(m,), name='W') input_shapes = {"x:0": x.shape, "W:0": w.shape, "y:0": y.shape} mu = tf.constant(1, dtype=tf.float32, name="mu") h = tf.reduce_sum(w * x) c = y * h ny = 0 - y p = tf.cast((c > y), dtype=ny.dtype) * ny g = p * x w = tf.subtract(w, mu * g, name="W_out") sess.run(tf.initialize_all_variables()) input_names = ['x:0', 'y:0'] output_names = ['W_out:0'] onnx_graph = tf2onnx.tfonnx.process_tf_graph(sess.graph, input_names=input_names, output_names=output_names) model_proto = onnx_graph.make_model(model_name) model_proto = optimizer.optimize(model_proto, ['eliminate_identity']) if optimize_model: model_proto, check = simplify(model_proto, input_shapes=input_shapes) assert check with open(f"./{model_name}.onnx", "wb") as f: f.write(model_proto.SerializeToString())
def sample(self, n_batches): self.session.run(tf.initialize_all_variables()) self.load() all_voxels = [] all_imgs = [] all_zs = [] for i in xrange(n_batches): batch_z = np.random.uniform(-1, 1, [self.batch_size, self.z_size]) all_zs.append(batch_z) voxels = self.voxels.eval(session=self.session, feed_dict={ self.z: batch_z, self.train_flag: False }) imgs = self.final_imgs.eval(session=self.session, feed_dict={ self.z: batch_z, self.train_flag: False }) all_voxels.append(np.array(voxels)) all_imgs.append(np.array(imgs)) all_voxels = np.concatenate(all_voxels, axis=0) all_imgs = np.concatenate(all_imgs, axis=0) all_zs = np.vstack(all_zs) print all_voxels.shape np.save("results/PrGAN{}".format(self.dataset_name), all_zs) ops.save_voxels(all_voxels, "results/PrGAN{}".format(self.dataset_name)) ops.save_separate_images(all_imgs, "results/PrGAN{}".format(self.dataset_name))
def create_dummy_saved_model(path): """Creates minimal saved model for testing purposes.""" class ImageModel(tf.train.Checkpoint): """Dummy image model.""" def __init__(self): super(ImageModel, self).__init__() self.v = tf.Variable(1., use_resource=True) @tf.function(input_signature=[ tf.TensorSpec(name="input", shape=[32, 224, 224, 3], dtype=tf.float32), tf.TensorSpec(name="training", shape=None, dtype=tf.bool), ]) def __call__(self, x, training): return tf.reduce_mean(x, axis=[1, 2]) + self.v with tf.Session() as sess: model = ImageModel() # Explicitly reference save_counter so that is initialized before saving # the model. model.save_counter # pylint: disable=pointless-statement model.trainable_variables = [model.v] init = tf.initialize_all_variables() sess.run(init) tf.compat.v2.saved_model.save(model, path)
def __call__(self, batcher, placeholders, loss, session=None): self.loss = loss minimization_op = self.optimizer.minimize(loss) if session is None: session = tf.Session() init = tf.initialize_all_variables() session.run(init) epoch = 1 while epoch < self.max_epochs: iteration = 1 total_loss = 0 total = 0 for values in batcher: iteration += 1 total += len(values[-1]) feed_dict = {} for i in range(0, len(placeholders)): feed_dict[placeholders[i]] = values[i] _, current_loss = session.run([minimization_op, loss], feed_dict=feed_dict) total_loss += current_loss print("Epoch ", str(epoch), "\tLoss ", total_loss) epoch += 1 return session
def test_neuron_update_fwd(self): tf.reset_default_graph() tf.disable_v2_behavior() n_in, n_out = 10, 5 inp, out, w = random_dense(n_in, n_out) env = blur_env.tf_env pre, post, synapse, genome, network_spec = get_blur_state(env, inp, out, w) pre_fwd, post_fwd = blur.dense_neuron_update( pre, post, synapse, inp_act=None, out_act=sigmoid_with_grad, neuron_genome=genome.neuron, update_type=synapse_util.UpdateType.FORWARD, global_spec=network_spec, env=env) inp = tf.constant(inp.astype(blur_env.NP_FLOATING_TYPE)) ww = w.astype(blur_env.NP_FLOATING_TYPE) inp_with_bias = tf.concat([inp[Ellipsis, 0], [[[[1]]]]], axis=-1) exp_results = inp_with_bias @ ww with tf.Session() as s: s.run(tf.initialize_all_variables()) self.assertAllClose(pre_fwd, pre) self.assertAllClose(post_fwd[Ellipsis, 0], tf.math.sigmoid(exp_results)) self.assertAllClose(post_fwd[Ellipsis, 1], d_sigmoid(out[Ellipsis, 0] + exp_results))
def __init__(self, sess, state_dim, action_dim, lr, tau_in): # Dimensions and Hyperparams # self.env_dim = inp_dim # self.act_dim = out_dim self.time_step = 0 self.tau_in = tau_in self.lr = lr self.sess = sess self.state_dim = state_dim self.action_dim = action_dim # Build models and target models self.state_input, self.action_input, \ self.q_value_output, self.net = self.create_q_network(state_dim, action_dim) self.action_net = [self.net[0], self.net[1], self.net[2], self.net[3]] self.state_net = [self.net[4], self.net[5]] self.combined_net = [ self.net[6], self.net[7], self.net[8], self.net[9], self.net[10], self.net[11], self.net[12] ] self.target_state_input, self.target_action_input, \ self.target_q_value_output, self.target_update, \ self.target_net = self.create_target_q_network(state_dim, action_dim, self.net) self.create_training_method() self.sess.run(tf.initialize_all_variables()) self.first_target_update = 0 self.update_target() self.first_target_update = 1
def build_networks(self): print("Building YOLO_tiny graph...") self.x = tf.placeholder('float32', [None, 448, 448, 3]) self.conv_1 = self.conv_layer(1, self.x, 16, 3, 1) self.pool_2 = self.pooling_layer(2, self.conv_1, 2, 2) self.conv_3 = self.conv_layer(3, self.pool_2, 32, 3, 1) self.pool_4 = self.pooling_layer(4, self.conv_3, 2, 2) self.conv_5 = self.conv_layer(5, self.pool_4, 64, 3, 1) self.pool_6 = self.pooling_layer(6, self.conv_5, 2, 2) self.conv_7 = self.conv_layer(7, self.pool_6, 128, 3, 1) self.pool_8 = self.pooling_layer(8, self.conv_7, 2, 2) self.conv_9 = self.conv_layer(9, self.pool_8, 256, 3, 1) self.pool_10 = self.pooling_layer(10, self.conv_9, 2, 2) self.conv_11 = self.conv_layer(11, self.pool_10, 512, 3, 1) self.pool_12 = self.pooling_layer(12, self.conv_11, 2, 2) self.conv_13 = self.conv_layer(13, self.pool_12, 1024, 3, 1) self.conv_14 = self.conv_layer(14, self.conv_13, 1024, 3, 1) self.conv_15 = self.conv_layer(15, self.conv_14, 1024, 3, 1) self.fc_16 = self.fc_layer(16, self.conv_15, 256, flat=True, linear=False) self.fc_17 = self.fc_layer(17, self.fc_16, 4096, flat=False, linear=False) # skip dropout_18 self.fc_19 = self.fc_layer(19, self.fc_17, 1470, flat=False, linear=True) self.sess = tf.Session() self.sess.run(tf.initialize_all_variables()) self.saver = tf.train.Saver() self.saver.restore(self.sess, self.weights_file) print("Loading complete!" + '\n')
def main(): tf.compat.v1.disable_eager_execution() # data = tf.keras.datasets.mnist # (x_train, y_train), (x_test, y_test) = data.load_data() mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # plt.imshow(x_train[1], cmap="binary") # plt.show() sess = tfc.InteractiveSession() x = tfc.placeholder("float", shape=[None, 784]) y_ = tfc.placeholder("float", shape=[None, 10]) W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) sess.run(tfc.initialize_all_variables()) y = tf.nn.softmax(tf.matmul(x, W) + b) cross_entropy = -tf.reduce_sum(y_ * tfc.log(y)) train_step = tfc.train.GradientDescentOptimizer(0.01).minimize( cross_entropy) for i in range(1000): batch = mnist.train.next_batch(50) train_step.run(feed_dict={x: batch[0], y_: batch[1]}) correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) print( "测试集的正确率为", accuracy.eval(feed_dict={ x: mnist.test.images, y_: mnist.test.labels }))
def cnn_3layer(input_kspace,w1,b1,w2,b2,w3,b3,w4,w5,Rx,Ry,Rt,sess): input_phdr = tf.placeholder(tf.float32, input_kspace.shape) w1_phdr = tf.placeholder(tf.float32, w1.shape) w2_phdr = tf.placeholder(tf.float32, w2.shape) w3_phdr = tf.placeholder(tf.float32, w3.shape) w4_phdr = tf.placeholder(tf.float32, w4.shape) w5_phdr = tf.placeholder(tf.float32, w5.shape) #h_conv1 = tf.nn.relu(conv3d_dilate(input_kspace, w1,Rx,Ry)) h_conv1 = conv3d_dilate(input_phdr, w1_phdr, strides=[Rx,Ry,Rt]) h_conv2 = conv3d_dilate(h_conv1, w2_phdr) #h_conv2 = tf.nn.relu(conv3d_dilate(h_conv1, w2_phdr)) h_conv3 = tf.nn.relu(conv3d_dilate(h_conv2, w3_phdr)) h_conv4 = conv3d_dilate(h_conv3, w4_phdr) h_conv5 = conv3d_dilate(h_conv4, w5_phdr) if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1: init = tf.initialize_all_variables() else: init = tf.global_variables_initializer() sess.run(init) return sess.run(h_conv5, feed_dict={input_phdr: input_kspace, w1_phdr: w1, w2_phdr: w2, w3_phdr: w3, w4_phdr: w4, w5_phdr: w5})
def restore_best_model(): """Load bestmodel file from eval directory, add variables for adagrad, and save to train directory""" tf.logging.info("Restoring bestmodel for training...") # Initialize all vars in the model sess = tf.Session(config=util.get_config()) print("Initializing all variables...") sess.run(tf.initialize_all_variables()) # Restore the best model from eval dir saver = tf.train.Saver( [v for v in tf.all_variables() if "Adagrad" not in v.name]) print("Restoring all non-adagrad variables from best model in eval dir...") curr_ckpt = util.load_ckpt(saver, sess, "eval") print("Restored %s." % curr_ckpt) # Save this model to train dir and quit new_model_name = curr_ckpt.split("/")[-1].replace("bestmodel", "model") new_fname = os.path.join(FLAGS.log_root, "train", new_model_name) print("Saving model to %s..." % (new_fname)) new_saver = tf.train.Saver( ) # this saver saves all variables that now exist, including Adagrad variables new_saver.save(sess, new_fname) print("Saved.") exit()
def trainNetwork(bird, path): # 保存于加载网络 saver = tf.train.Saver() bird.sess.run(tf.initialize_all_variables()) saver.restore(bird.sess, path) tf.reset_default_graph() return bird
def test_discriminator(self, discriminator_f): images = np.ones(shape=(32, 10), dtype=np.float32) input_tensor = tf.placeholder(tf.float32, shape=(None, 10)) logits, probs = discriminator_f(input_tensor) with self.test_session() as sess: sess.run(tf.initialize_all_variables()) sess.run([logits, probs], feed_dict={input_tensor: images})
def test_get_synaptic_update_backward(self): tf.reset_default_graph() tf.disable_v2_behavior() n_in, n_out = 10, 5 inp, out, w = random_dense(n_in, n_out) env = blur_env.tf_env pre, post, synapse, genome, _ = get_blur_state(env, inp, out, w) update_bwd, _ = blur.get_synaptic_update( pre, post, synapse, input_transform_gn=genome.neuron.transform, update_type=synapse_util.UpdateType.BACKWARD, env=env) out = tf.constant(out.astype(blur_env.NP_FLOATING_TYPE)) ww = w.astype(blur_env.NP_FLOATING_TYPE) exp_results = out[Ellipsis, 0] @ tf.transpose(ww, (0, 1, 3, 2)) with tf.Session() as s: s.run(tf.initialize_all_variables()) self.assertAllClose(update_bwd[Ellipsis, 0], tf.zeros((1, 1, 1, n_in))) self.assertAllClose(update_bwd[Ellipsis, 1], exp_results[Ellipsis, :-1])
def test_decoder(self, decoder_f): latent_variable = np.ones(shape=(10, 15), dtype=np.float32) input_tensor = tf.placeholder(tf.float32, shape=(None, 15)) images = decoder_f(input_tensor, [64, 64, 1]) with self.test_session() as sess: sess.run(tf.initialize_all_variables()) sess.run(images, feed_dict={input_tensor: latent_variable})
def main(unused_argv=None): obj = create_hub_module_object() with tf.Session() as sess: sess.run(tf.initialize_all_variables()) tf.train.Saver(obj.variables).restore(sess, FLAGS.checkpoint) tf.saved_model.save(obj, FLAGS.export_path, signatures=obj.__call__) tf.logging.info('Saved hub module in: %s', FLAGS.export_path)
def test_variable_replace_getter(self): with self.test_session() as sess: context = variable_replace.VariableReplaceGetter() mod = snt.Linear(1, custom_getter=context) inp_data = tf.ones([10, 1]) with context.use_variables(): y1 = mod(inp_data) sess.run(tf.initialize_all_variables()) np_y1 = sess.run(y1) values = context.get_variable_dict() new_values = {k: v + 1 for k, v in values.items()} with context.use_value_dict(new_values): np_y2 = mod(inp_data).eval() self.assertNear((np_y2 - np_y1)[0], 2, 1e-8) for v in values.values(): v.assign(v + 1).eval() with context.use_variables(): np_y3 = mod(inp_data).eval() self.assertNear((np_y3 - np_y2)[0], 0, 1e-8)
def __init__(self, actions): # init replay memory self.replayMemory = deque() # init some parameters self.timeStep = 0 self.epsilon = INITIAL_EPSILON self.actions = actions # init Q network self.stateInput, self.QValue, self.W_conv1, self.b_conv1, self.W_conv2, self.b_conv2, self.W_conv3, self.b_conv3, self.W_fc1, self.b_fc1, self.W_fc2, self.b_fc2 = self.createQNetwork() # init Target Q Network self.stateInputT, self.QValueT, self.W_conv1T, self.b_conv1T, self.W_conv2T, self.b_conv2T, self.W_conv3T, self.b_conv3T, self.W_fc1T, self.b_fc1T, self.W_fc2T, self.b_fc2T = self.createQNetwork() self.copyTargetQNetworkOperation = [self.W_conv1T.assign(self.W_conv1), self.b_conv1T.assign(self.b_conv1), self.W_conv2T.assign(self.W_conv2), self.b_conv2T.assign(self.b_conv2), self.W_conv3T.assign(self.W_conv3), self.b_conv3T.assign(self.b_conv3), self.W_fc1T.assign(self.W_fc1), self.b_fc1T.assign(self.b_fc1), self.W_fc2T.assign(self.W_fc2), self.b_fc2T.assign(self.b_fc2)] self.createTrainingMethod() # config config = tf.ConfigProto() # saving and loading networks self.saver = tf.train.Saver(max_to_keep=10000) self.session = tf.InteractiveSession(config=config) self.merge_summary = tf.summary.merge_all() self.session.run(tf.initialize_all_variables()) checkpoint = tf.train.get_checkpoint_state("saved_networks") if checkpoint and checkpoint.model_checkpoint_path: self.saver.restore(self.session, checkpoint.model_checkpoint_path) print("Successfully loaded:", checkpoint.model_checkpoint_path) else: print("Could not find old network weights")
def train_neural_network(x): prediction = neural_network_model(x) cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(prediction, y)) optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost) with tf.Session() as sess: sess.run(tf.initialize_all_variables()) for epoch in range(hm_epochs): epoch_loss = 0 i=0 while i < len(train_x): start = i end = i+batch_size batch_x = np.array(train_x[start:end]) batch_y = np.array(train_y[start:end]) _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y}) epoch_loss += c i+=batch_size print('Epoch', epoch+1, '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:test_x, y:test_y}))
def testMakeLogJointFnTemplate(self): """Test `make_log_joint_fn` on program returned by tf1.make_template.""" def variational(): loc = tf1.get_variable("loc", []) qz = ed.Normal(loc=loc, scale=0.5, name="qz") return qz def true_log_joint(loc, qz): log_prob = tf.reduce_sum( tfd.Normal(loc=loc, scale=0.5).log_prob(qz)) return log_prob qz_value = 1.23 variational_template = tf1.make_template("variational", variational) log_joint = ed.make_log_joint_fn(variational_template) expected_log_prob = log_joint(qz=qz_value) loc = tf1.trainable_variables("variational")[0] actual_log_prob = true_log_joint(loc, qz_value) with self.cached_session() as sess: sess.run(tf1.initialize_all_variables()) actual_log_prob_, expected_log_prob_ = sess.run( [actual_log_prob, expected_log_prob]) self.assertEqual(actual_log_prob_, expected_log_prob_)
def fit(self, X, Y): self.__train_writer = tf.train.SummaryWriter('tensorboard/train', self.session.graph) # init all variables. self.session.run(tf.initialize_all_variables()) # simple train. tm = time.time() data_size = len(X) iteration_count = (self.__epoch * data_size) // self.__batch_size print >> sys.stderr, 'Iteration=%d (batchsize=%d, epoch=%d, datasize=%d)' % ( iteration_count, self.__batch_size, self.__epoch, data_size) offset = 0 self.__current_iteration = 0 for it in xrange(iteration_count): self.__current_iteration = it sub_X = X[offset:offset + self.__batch_size, ...] sub_Y = Y[offset:offset + self.__batch_size, ...] offset = (offset + self.__batch_size) % data_size cost, summary_info = self.fit_one_batch(sub_X, sub_Y) self.__train_writer.add_summary(summary_info, self.__current_iteration) if (it + 1) % 10 == 0: diff_tm = time.time() - tm print >> sys.stderr, 'iter=%d/%d, cost=%.5f, tm=%.3f' % ( it + 1, iteration_count, cost, diff_tm) tm = time.time()
def valid_nin(): print( bcolors.G + "Task : val\nvalidate the pre-trained nin model, should be same as caffe result" + bcolors.END) pool3 = nin() #pool3 = lenet() gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1.0) sess = tf.InteractiveSession(config=tf.ConfigProto( gpu_options=gpu_options)) init = tf.initialize_all_variables() sess.run(init) mean = cal_mean() data, label = read_cifar10('test') total = 0 correct = 0 begin = time.time() for j in range(len(data) // batch_size): batch_x = data[j * batch_size:(j + 1) * batch_size] - mean prob = sess.run([pool3], feed_dict={ x: batch_x, y: np.ones((batch_size)), keep_prob: 1.0 }) if np.argmax(prob[0]) == label[j]: correct += 1 total += 1 end = time.time() print("acc = %f . %d/%d. Computing time = %f seconds" % (float(correct) / total, correct, total, end - begin))
def test_net(): print(bcolors.G + "Task : test\n" + bcolors.END) student = lenet() pred = tf.nn.softmax(student) gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1.0) sess = tf.InteractiveSession(config=tf.ConfigProto( gpu_options=gpu_options)) init = tf.initialize_all_variables() sess.run(init) with tf.device('/gpu:0'): saver = tf.train.Saver() model_file = tf.train.latest_checkpoint(model) saver.restore(sess, model_file) mean = cal_mean() data, label = read_cifar10('test') total = 0 correct = 0 begin = time.time() for j in range(len(data) // batch_size): batch_x = data[j * batch_size:(j + 1) * batch_size] - mean prob = sess.run([pred], feed_dict={ x: batch_x, y: np.ones((batch_size)), keep_prob: 1.0 }) if np.argmax(prob[0]) == label[j]: correct += 1 total += 1 #print(("acc = %f . %d/%d"%(float(correct)/total, correct, total)) end = time.time() print("acc = %f . %d/%d. Computing time = %f seconds" % (float(correct) / total, correct, total, end - begin))
def build(self, env): """ This method creates the tf placeholders and operations that form the policy network. As well as its optimization """ tf.compat.v1.disable_eager_execution() # Learning rate placeholder self.lr = tf.placeholder(tf.float32, shape=None, name='learning_rate') # Input placeholders self.states = tf.placeholder(tf.float32, shape=[None, env.state_space], name='state') self.actions = tf.placeholder(tf.int32, shape=(None,), name='actions') self.returns = tf.placeholder(tf.float32, shape=(None,), name='returns') self.adv = tf.placeholder(tf.float32, shape=(None,), name='advantages') # Network architecture. Takes as input states, outputs logits self.pg = self.dense_nn(self.states, self.param.layer_shapes + [env.action_space], name='PG_network') # Pick an action given the output logits from the network self.output_action = tf.squeeze(tf.random.categorical(self.pg, 1)) # Calculate loss, gradients and update parameters with tf.variable_scope('pg_optimize'): self.loss_pg = tf.reduce_mean( tf.stop_gradient(self.adv) * tf.nn.sparse_softmax_cross_entropy_with_logits( logits=self.pg, labels=self.actions), name='loss_pg') self.opt_pg = tf.train.AdamOptimizer(self.lr).minimize(self.loss_pg, name='adam_optim_pg') init_op = tf.initialize_all_variables() self.sess.run(init_op)
def test_encoder(self, encoder_f): minibatch = np.ones(shape=(10, 64, 64, 1), dtype=np.float32) input_tensor = tf.placeholder(tf.float32, shape=(None, 64, 64, 1)) latent_mean, latent_logvar = encoder_f(input_tensor, 10) with self.test_session() as sess: sess.run(tf.initialize_all_variables()) sess.run( [latent_mean, latent_logvar], feed_dict={input_tensor: minibatch})
def __init__(self, num_action, input_shape=(120, 160, 3), batch_size=64, training=True, model_path=None, *args, **kwargs): super(DDPG, self).__init__(*args, **kwargs) self.actor_img, self.actor_speed, self.actor = default_model( num_action, input_shape, actor_critic='actor') _, _, self.actor_target = default_model(num_action, input_shape, actor_critic='actor') self.critic_img, self.critic_speed, self.critic_action, self.critic = default_model( num_action, input_shape, actor_critic='critic') _, _, _, self.critic_target = default_model(num_action, input_shape, actor_critic='critic') self.actor_critic_grad = tf.placeholder( tf.float32, [None, 2]) # where we will feed de/dC (from critic) actor_model_weights = self.actor.trainable_weights self.actor_grads = tf.gradients( self.actor.output, actor_model_weights, -self.actor_critic_grad) # dC/dA (from actor) grads = zip(self.actor_grads, actor_model_weights) self.lr = 0.002 self.optimize = tf.train.AdamOptimizer(self.lr).apply_gradients(grads) self.critic_grads = tf.gradients( self.critic.output, self.critic_action) # where we calcaulte de/dC for feeding above self.epsilon = 1 self.epsilon_decay = -5000 self.sess = tf.Session() self.ou = OU() self.n = 0 self.model_path = model_path self.batch_size = batch_size self.train_step = 0 self.r_sum = 0 self.last_state = None self.last_actions = None self.tau = 1e-3 self.gamma = 0.99 K.set_session(self.sess) # Initialize for later gradient calculations self.sess.run(tf.initialize_all_variables()) self.memory = Memory(50000) self.actor.summary() self.critic.summary() if training: self.compile()
def test_prefetch_tensors_with_partially_defined_shapes(self): with self.test_session() as sess: batch_size = 10 image_size = 32 num_batches = 5 examples = tf.Variable(tf.constant(0, dtype=tf.int64)) counter = examples.count_up_to(num_batches) image = tf.random_normal([ batch_size, tf.Variable(image_size), tf.Variable(image_size), 3 ], dtype=tf.float32, name='image') image.set_shape([batch_size, None, None, 3]) label = tf.random_uniform([batch_size, tf.Variable(1)], 0, 10, dtype=tf.int32, name='label') label.set_shape([batch_size, None]) prefetch_queue = prefetcher.prefetch(tensor_dict={ 'counter': counter, 'image': image, 'label': label }, capacity=100) tensor_dict = prefetch_queue.dequeue() self.assertAllEqual(tensor_dict['image'].get_shape().as_list(), [batch_size, None, None, 3]) self.assertAllEqual(tensor_dict['label'].get_shape().as_list(), [batch_size, None]) tf.initialize_all_variables().run() with slim.queues.QueueRunners(sess): for _ in range(num_batches): results = sess.run(tensor_dict) self.assertEquals(results['image'].shape, (batch_size, image_size, image_size, 3)) self.assertEquals(results['label'].shape, (batch_size, 1)) with self.assertRaises(tf.errors.OutOfRangeError): sess.run(tensor_dict)