def create_placeholders(raw_graphs): """Creates placeholders for the model training and evaluation. Args: rand: A random seed (np.RandomState instance). batch_size: Total number of graphs per batch. min_max_nodes: A 2-tuple with the [lower, upper) number of nodes per graph. The number of nodes for a graph is uniformly sampled within this range. geo_density: A `float` threshold parameters for the geographic threshold graph's threshold. Default= the number of nodes. Returns: source_ph: The source graph's placeholders, as a graph namedtuple. target_ph: The target graph's placeholders, as a graph namedtuple. """ # Create some example data for inspecting the vector sizes. source_graphs = [source_from_raw(raw) for raw in raw_graphs] source_ph = utils_tf.placeholders_from_networkxs( source_graphs, force_dynamic_num_graphs=True) target_graphs = [target_from_raw(raw) for raw in raw_graphs] target_ph = utils_tf.placeholders_from_networkxs( target_graphs, force_dynamic_num_graphs=True) return source_ph, target_ph
def create_placeholders(rand, batch_size, num_nodes_min_max, theta): """Creates placeholders for the model training and evaluation. Args: rand: A random seed (np.RandomState instance). batch_size: Total number of graphs per batch. num_nodes_min_max: A 2-tuple with the [lower, upper) number of nodes per graph. The number of nodes for a graph is uniformly sampled within this range. theta: A `float` threshold parameters for the geographic threshold graph's threshold. Default= the number of nodes. Returns: input_ph: The input graph's placeholders, as a graph namedtuple. target_ph: The target graph's placeholders, as a graph namedtuple. """ #Create some example data for inspecting the vector sizes. input_graphs, target_graphs, _ = generate_networkx_graphs( rand, batch_size, num_nodes_min_max, theta) # input_graphs, target_graphs, _ = generate_surface_graphs( # rand, # batch_size, # num_nodes_min_max, # theta # ) input_ph = utils_tf.placeholders_from_networkxs(input_graphs) target_ph = utils_tf.placeholders_from_networkxs(target_graphs) return input_ph, target_ph
def _create_placeholders(self, loader): ''' Creates input and output placeholders for model. Loader is used to get the shapes of the data :param loader: GraphTypleDataLoader instance. Used only to get the shapes of the graphs. ''' # Create some example data for inspecting the vector sizes. input_graphs, target_graphs = loader.next(raw_graphs=True) self.input_ph = utils_tf.placeholders_from_networkxs(input_graphs) self.target_ph = utils_tf.placeholders_from_networkxs(target_graphs)
def test_graph_network(tf_session): logdir = os.path.join(TEST_FOLDER, 'test_logdir/with_trace') os.makedirs(logdir, exist_ok=True) starcluster_graphs_nx, gaia_graphs_nx = make_example_graphs( 2, num_stars=10, sc_edge_size=2, sc_node_size=12, sc_global_size=5, g_edge_size=3, g_node_size=13, g_global_size=6) with tf_session.graph.as_default(): sc_pl = placeholders_from_networkxs(starcluster_graphs_nx, force_dynamic_num_graphs=False) g_pl = placeholders_from_networkxs(gaia_graphs_nx, force_dynamic_num_graphs=False) sc_graphtuple = networkxs_to_graphs_tuple(starcluster_graphs_nx) g_graphtuple = networkxs_to_graphs_tuple(gaia_graphs_nx) encoded_size = 7 t_network = StarClusterTNetwork(encoded_size, sc_encoder_latent_size=16, sc_encoder_num_layers=2, sc_decoder_latent_size=16, sc_decoder_num_layers=2, g_encoder_latent_size=16, g_encoder_num_layers=2) t_network_output = t_network(g_pl, sc_pl, num_samples=2, num_processing_steps=1) summary = tf.summary.merge_all() writer = tf.compat.v1.summary.FileWriter(logdir, tf_session.graph, session=tf_session) tf_session.run(tf.global_variables_initializer()) run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() t_network_output_res, summary_eval = tf_session.run( [t_network_output, summary], feed_dict={ sc_pl: sc_graphtuple, g_pl: g_graphtuple }, options=run_options, run_metadata=run_metadata) writer.add_run_metadata(run_metadata, 'step%d' % 0) writer.add_summary(summary_eval, 0)
def create_placeholders(rand, batch_size, raw_input_graphs, raw_target_graphs, edge_permutations): """Creates placeholders for the model training and evaluation.""" # Create some example data for inspecting the vector sizes. input_graphs, target_graphs = generate_networkx_graphs( rand, batch_size, raw_input_graphs, raw_target_graphs, edge_permutations, np.arange(2), True) input_ph = utils_tf.placeholders_from_networkxs(input_graphs) target_ph = utils_tf.placeholders_from_networkxs(target_graphs) weight_ph = tf.zeros_like(target_ph.edges[:, :1]) return input_ph, target_ph, weight_ph
def create_placeholders(input_graphs, target_graphs): """ Creates placeholders for the model training and evaluation. Returns: input_ph: The input graph's placeholders, as a graph namedtuple. target_ph: The target graph's placeholders, as a graph namedtuple. """ input_ph = utils_tf.placeholders_from_networkxs( input_graphs, name="input_placeholders_from_networksx") target_ph = utils_tf.placeholders_from_networkxs( target_graphs, name="target_placeholders_from_networkxs") return input_ph, target_ph
def create_placeholders(graphcache,rand,batch_size,all_db): """Creates placeholders for the model training and evaluation. Args: rand: A random seed (np.RandomState instance). batch_size: Total number of graphs per batch. Returns: input_ph: The input graph's placeholders, as a graph namedtuple. target_ph: The target graph's placeholders, as a graph namedtuple. """ # Create some example data for inspecting the vector sizes. input_graphs, target_graphs, _, _ = generate_networkx_graphs(graphcache,all_db, rand, batch_size,'train') input_ph = utils_tf.placeholders_from_networkxs(input_graphs) target_ph = utils_tf.placeholders_from_networkxs(target_graphs) return input_ph, target_ph
def test_feed_data(self): networkx = [_generate_graph(batch_index) for batch_index in range(16)] placeholders = utils_tf.placeholders_from_networkxs( networkx, force_dynamic_num_graphs=True) # Does not need to be the same size networkxs = [_generate_graph(batch_index) for batch_index in range(2)] with self.test_session() as sess: output = sess.run( placeholders, utils_tf.get_feed_dict(placeholders, utils_np.networkxs_to_graphs_tuple(networkxs))) self.assertAllEqual( np.array([[0, 0], [1, 0], [2, 0], [3, 0], [0, 1], [1, 1], [2, 1], [3, 1]]), output.nodes) self.assertEqual(np.float32, output.nodes.dtype) self.assertAllEqual(np.array([[0], [1]]), output.globals) self.assertEqual(np.float32, output.globals.dtype) sorted_edges_content = sorted( [(x, y, z) for x, y, z in zip(output.receivers, output.senders, output.edges)]) self.assertAllEqual([0, 0, 1, 4, 4, 5], [x[0] for x in sorted_edges_content]) self.assertAllEqual([1, 2, 3, 5, 6, 7], [x[1] for x in sorted_edges_content]) self.assertEqual(np.float64, output.edges.dtype) self.assertAllEqual( np.array([[0, 1, 0], [1, 2, 0], [2, 3, 0], [0, 1, 1], [1, 2, 1], [2, 3, 1]]), [x[2] for x in sorted_edges_content])
def create_placeholders(rand, batch_size, min_max_nodes, geo_density): # Create some example data for inspecting the vector sizes. raw_graphs = generate_raw_graphs(rand, batch_size, min_max_nodes, geo_density) source_graphs = [source_from_raw(raw) for raw in raw_graphs] source_ph = utils_tf.placeholders_from_networkxs( source_graphs, force_dynamic_num_graphs=True ) target_graphs = [target_from_raw(raw) for raw in raw_graphs] # print_graphs(target_graphs) target_ph = utils_tf.placeholders_from_networkxs( target_graphs, force_dynamic_num_graphs=True ) return source_ph, target_ph
def test_placeholders_from_networkxs(self): num_graphs = 16 networkxs = [ _generate_graph(batch_index) for batch_index in range(num_graphs) ] placeholders = utils_tf.placeholders_from_networkxs( networkxs, force_dynamic_num_graphs=False) self._assert_expected_shapes(placeholders, num_graphs=num_graphs) self.assertEqual(tf.float32, placeholders.nodes.dtype) self.assertEqual(tf.float64, placeholders.edges.dtype)
def __init__(self,gtemp): tf.reset_default_graph() with tf.variable_scope("defscope"): self.SEED = 3 self.GAMMA = 0.99 self.BATCH = 5 random.seed(a=self.SEED) self.random = np.random.RandomState(seed=self.SEED) np.random.seed(self.SEED) tf.set_random_seed(self.SEED) """ replay memory """ self.D = deque() """ initialize and setup the network """ self.inputPh = utils_tf.placeholders_from_networkxs( [self._gtmp2intmp(gtemp)]) self.targetPh = utils_tf.placeholders_from_networkxs( [self._gtmp2ttmp(gtemp)]) self.numProcessingSteps = 10 self.model = models.EncodeProcessDecode(edge_output_size=1, node_output_size=0) self.output_ops_tr = self.model(self.inputPh, self.numProcessingSteps) self.loss_ops_tr = self._create_loss_ops(self.targetPh, self.output_ops_tr) self.loss_op_tr = sum(self.loss_ops_tr) / self.numProcessingSteps self.learning_rate = 1e-3 self.optimizer = tf.train.AdamOptimizer(self.learning_rate) self.step_op = self.optimizer.minimize(self.loss_op_tr) self.inputPh, self.targetPh = self._make_all_runnable_in_session(self.inputPh, self.targetPh) self.sess = tf.Session() self.sess.run(tf.global_variables_initializer()) self.epsilon = 0.01 self.outg = None
def test_get_feed_dict_raises(self, none_fields): networkxs = [_generate_graph(batch_index) for batch_index in range(16)] placeholders = utils_tf.placeholders_from_networkxs(networkxs) feed_values = utils_np.networkxs_to_graphs_tuple(networkxs) with self.assertRaisesRegexp(ValueError, ""): utils_tf.get_feed_dict( placeholders.map(lambda _: None, none_fields), feed_values) with self.assertRaisesRegexp(ValueError, ""): utils_tf.get_feed_dict(placeholders, feed_values.map(lambda _: None, none_fields))
def test_placeholders_from_networkxs_missing_edges(self): num_graphs = 16 networkxs = [ _generate_graph(batch_index, add_edges=False) for batch_index in range(num_graphs) ] placeholders = utils_tf.placeholders_from_networkxs( networkxs, force_dynamic_num_graphs=False) self.assertEqual(None, placeholders.edges) self._assert_expected_shapes( placeholders, but_for=["edges"], num_graphs=num_graphs)
def create_placeholders(config, batch_data, batch_processing=True): """ if gripper_as_global = False, this function will still return 3 values (input_ph, target_ph, input_ctrl_ph) but the last will (input_ctrl_ph) will be None, caller needs to check this """ input_graphs, target_graphs, _ = create_singulation_graphs( config, batch_data, initial_pos_vel_known=config.initial_pos_vel_known, batch_processing=batch_processing) if batch_processing: """ in this case we get a list of chunk lists (each representing a full batch)""" input_graphs = input_graphs[0] target_graphs = target_graphs[0] else: input_graphs = [input_graphs[0]] target_graphs = [target_graphs[0]] input_ph = utils_tf.placeholders_from_networkxs( input_graphs, force_dynamic_num_graphs=True) target_ph = utils_tf.placeholders_from_networkxs( target_graphs, force_dynamic_num_graphs=True) return input_ph, target_ph
def __init__(self, name, gtemp): self.SEED = 3 self.gtemp = gtemp random.seed(a=self.SEED) tf.set_random_seed(self.SEED) self.name = name self.inputPh = utils_tf.placeholders_from_networkxs( [self._gtmp2intmp(gtemp)]) # the scenrio: 1 def 1 att 1 uav: N_edges + N_edges + 2(uav action dim) # def act dim: N_edges N_edges = len(gtemp.edges) # question: split actions or concat all together? # answer: need to split actions # self.allActPh = tf.placeholder(tf.float64, shape=[1,N_edges*2+2]) self.defActPh = tf.placeholder(tf.float64, shape=[1, N_edges]) self.attActPh = tf.placeholder(tf.float64, shape=[1, N_edges]) self.uav2ActPh = tf.placeholder(tf.float64, shape=[1, 2]) """ setting up the actor network """ self.actor_out_graphs, self.actor_out_edge = self.actor_network( gtemp, name + "_actor") """ setting up the critic network """ self.critic_out_ph = self.critic_network( gtemp, name + "_critic", tf.concat([self.defActPh, self.attActPh, self.uav2ActPh], axis=1)) """ structure to compute loss and optimize actor network """ self.actor_loss_op = -tf.reduce_mean( self.critic_network( gtemp, name + "_critic", tf.concat([self.actor_out_edge, self.attActPh, self.uav2ActPh], axis=1), reuse=True)) self.actor_optimizer = tf.train.AdamOptimizer(1e-3) # step operation to be placed in feed_dict self.actor_step_op = self.actor_optimizer.minimize(self.actor_loss_op) """ structure to compute loss and optimize critic network """ self.targetQ_ph = tf.placeholder(shape=[1, 1], dtype=tf.float64) self.critic_loss_op = tf.reduce_mean( tf.square(self.targetQ_ph - self.critic_out_ph)) self.critic_optimizer = tf.train.AdamOptimizer(1e-3) # step operation to be placed in feed_dict self.critic_step_op = self.critic_optimizer.minimize( self.critic_loss_op)
def test_placeholders_from_networkxs_hints(self): num_graphs = 16 networkxs = [ _generate_graph(batch_index, n_nodes=0, add_edges=False) for batch_index in range(num_graphs) ] placeholders = utils_tf.placeholders_from_networkxs( networkxs, node_shape_hint=[14], edge_shape_hint=[17], data_type_hint=tf.float64, force_dynamic_num_graphs=False) self.assertAllEqual([None, 14], placeholders.nodes.shape.as_list()) self.assertAllEqual([None, 17], placeholders.edges.shape.as_list()) self._assert_expected_shapes( placeholders, but_for=["nodes", "edges"], num_graphs=num_graphs) self.assertEqual(tf.float64, placeholders.nodes.dtype) self.assertEqual(tf.float64, placeholders.edges.dtype)
def test_feed_data_no_nodes(self): networkx = [ _generate_graph(batch_index, n_nodes=0, add_edges=False) for batch_index in range(16) ] placeholders = utils_tf.placeholders_from_networkxs( networkx, force_dynamic_num_graphs=True) # Does not need to be the same size networkxs = [ _generate_graph(batch_index, n_nodes=0, add_edges=False) for batch_index in range(2) ] self.assertEqual(None, placeholders.nodes) self.assertEqual(None, placeholders.edges) with self.test_session() as sess: output = sess.run( placeholders.replace(nodes=tf.no_op(), edges=tf.no_op()), utils_tf.get_feed_dict(placeholders, utils_np.networkxs_to_graphs_tuple(networkxs))) self.assertAllEqual(np.array([[0], [1]]), output.globals) self.assertEqual(np.float32, output.globals.dtype)
def create_placeholders(input_graph, target_graph): input_ph = utils_tf.placeholders_from_networkxs([input_graph]) target_ph = utils_tf.placeholders_from_networkxs([target_graph]) return input_ph, target_ph
# add ops to save and restore all the variables output_dir = os.path.join(config['output_dir'], prod_name) print("trained model will be save at:", output_dir) if not os.path.exists(output_dir): os.makedirs(output_dir) ## start to build tensorflow sessions tf.reset_default_graph() #from nx_graph.model_mlp import SegmentClassifier #model = SegmentClassifier() from nx_graph import get_model model = get_model(config['model']['name']) input_graphs, target_graphs = generate_input_target(n_graphs) input_ph = utils_tf.placeholders_from_networkxs( input_graphs, force_dynamic_num_graphs=True) target_ph = utils_tf.placeholders_from_networkxs( target_graphs, force_dynamic_num_graphs=True) output_ops_tr = model(input_ph, num_processing_steps_tr) # Training loss. loss_ops_tr = create_loss_ops(target_ph, output_ops_tr) # Loss across processing steps. loss_op_tr = sum(loss_ops_tr) / num_processing_steps_tr # Optimizer learning_rate = config_tr['learning_rate'] optimizer = tf.train.AdamOptimizer(learning_rate) step_op = optimizer.minimize(loss_op_tr)
def create_placeholders(self, input_graphs, batchsize=20): #input_ph = utils_tf.placeholders_from_networkxs(list(input_graphs[0:batchsize]), force_dynamic_num_graphs=True) input_ph = utils_tf.placeholders_from_networkxs( [input_graphs[0]], force_dynamic_num_graphs=True) output_ph = tf.placeholder(tf.float64, shape=(None, 1)) return input_ph, output_ph
ttmp.graph["features"] = [0.0] return ttmp def make_all_runnable_in_session(*args): """Lets an iterable of TF graphs be output from a session as NP graphs.""" return [utils_tf.make_runnable_in_session(a) for a in args] gtemp, _ = getDefaultGraph5x5() g0 = _gtmp2intmp(gtemp) t0 = _gtmp2ttmp(gtemp) inputPh = utils_tf.placeholders_from_networkxs([_gtmp2intmp(gtemp)]) targetPh = utils_tf.placeholders_from_networkxs([_gtmp2ttmp(gtemp)]) # input state input_graphs = utils_np.networkxs_to_graphs_tuple([g0]) # target target_graphs = utils_np.networkxs_to_graphs_tuple([t0]) feed_dict = {inputPh: input_graphs, targetPh: target_graphs} # for critic network criticModel = models.EncodeProcessDecode(edge_output_size=1, node_output_size=0)
def create_trained_model(config_name, input_ckpt=None): """ @config: configuration for train_nx_graph """ # load configuration file config = load_config(config_name) config_tr = config['train'] log_every_seconds = config_tr['time_lapse'] batch_size = n_graphs = config_tr['batch_size'] # need optimization num_processing_steps_tr = config_tr['n_iters'] ## level of message-passing prod_name = config['prod_name'] if input_ckpt is None: input_ckpt = os.path.join(config['output_dir'], prod_name) # generate inputs base_dir = os.path.join(config['data']['input_dir'], 'event00000{}_g{:03d}.npz') generate_input_target = inputs_generator(base_dir) # build TF graph tf.reset_default_graph() model = get_model(config['model']['name']) input_graphs, target_graphs = generate_input_target(n_graphs) input_ph = utils_tf.placeholders_from_networkxs( input_graphs, force_dynamic_num_graphs=True) target_ph = utils_tf.placeholders_from_networkxs( target_graphs, force_dynamic_num_graphs=True) output_ops_tr = model(input_ph, num_processing_steps_tr) def evaluator(iteration, n_test_graphs=10): try: sess.close() except NameError: pass sess = tf.Session() saver = tf.train.Saver() saver.restore(sess, os.path.join(input_ckpt, ckpt_name.format(iteration))) odds = [] tdds = [] for _ in range(n_test_graphs): feed_dict = create_feed_dict(generate_input_target, batch_size, input_ph, target_ph, is_trained=False) predictions = sess.run( { "outputs": output_ops_tr, 'target': target_ph }, feed_dict=feed_dict) output = predictions['outputs'][-1] target = predictions['target'] odd, tdd = eval_output(target, output) odds.append(odd) tdds.append(tdd) return np.concatenate(odds), np.concatenate(tdds) return evaluator
datapath = u'../../mac_yr_local/v280/pursueAsk_graph_v280_190723/tmp_sym/pkls' gene = data_generator(datapath) gllx, glly = [], [] for x_y in gene: gllx = [p['x'] for p in x_y] #glly=[p['y'] for p in x_y] glly = [p['yid'] for p in x_y] break tf.reset_default_graph() ###### #### placeholder input_ph = utils_tf.placeholders_from_networkxs( gllx, force_dynamic_num_graphs=True) # target_ph = utils_tf.placeholders_from_networkxs( # glly, force_dynamic_num_graphs=True) target_ph = tf.placeholder(dtype=tf.int32, shape=[None, None, 1, 1]) # [batch step 1 1] ## print('') seed = 1 rand = np.random.RandomState(seed=seed) # Model parameters. # Number of processing (message-passing) steps. #num_processing_steps_tr = 1 if debug_flag==True else 12 #num_processing_steps_ge = 16
t1.add_edge(1,0,features=[1.0]) t1.add_edge(1,2,features=[1.0]) t1.add_edge(2,1,features=[1.0]) t1.add_edge(2,3,features=[1.0]) t1.add_edge(3,2,features=[1.0]) t1.add_edge(0,3,features=[0.0]) t1.add_edge(3,0,features=[0.0]) t1.add_edge(0,2,features=[0.0]) t1.add_edge(2,0,features=[0.0]) t1.add_edge(0,0,features=[0.0]) t1.add_edge(1,1,features=[0.0]) t1.add_edge(2,2,features=[0.0]) t1.add_edge(3,3,features=[0.0]) input_ph = utils_tf.placeholders_from_networkxs([g0],force_dynamic_num_graphs=False,node_shape_hint=[4],edge_shape_hint=[9]) target_ph = utils_tf.placeholders_from_networkxs([t0],force_dynamic_num_graphs=False,node_shape_hint=[4],edge_shape_hint=[9]) input_graphs = utils_np.networkxs_to_graphs_tuple([g0]) target_graphs = utils_np.networkxs_to_graphs_tuple([t0]) feed_dict = {input_ph: input_graphs, target_ph: target_graphs} num_processing_steps_tr = 1 model = models.EncodeProcessDecode(edge_output_size=1, node_output_size=0) output_ops_tr = model(input_ph, num_processing_steps_tr)
t1.add_edge(1,0,features=[1.0]) t1.add_edge(1,2,features=[1.0]) t1.add_edge(2,1,features=[1.0]) t1.add_edge(2,3,features=[1.0]) t1.add_edge(3,2,features=[1.0]) t1.add_edge(0,3,features=[0.0]) t1.add_edge(3,0,features=[0.0]) t1.add_edge(0,2,features=[0.0]) t1.add_edge(2,0,features=[0.0]) t1.add_edge(0,0,features=[0.0]) t1.add_edge(1,1,features=[0.0]) t1.add_edge(2,2,features=[0.0]) t1.add_edge(3,3,features=[0.0]) input_ph = utils_tf.placeholders_from_networkxs([g0]) target_ph = utils_tf.placeholders_from_networkxs([t0]) input_graphs = utils_np.networkxs_to_graphs_tuple([g0]) target_graphs = utils_np.networkxs_to_graphs_tuple([t0]) feed_dict = {input_ph: input_graphs, target_ph: target_graphs} num_processing_steps_tr = 10 num_processing_steps_ge = 10 model = models.EncodeProcessDecode(edge_output_size=1, node_output_size=0)