def linear_regression(): x_train = np.asarray([1, 2, 3, 4, 5, 6, 7, 8, 9, 11]) y_train = np.asarray([0.1, 0.2, 0.32, 0.43, 0.54, 0.65, 0.77, 0.88, 0.94, 1]) n_sample = x_train.shape[0] x_ = tf.placeholder(tf.float32, name="x") y_ = tf.placeholder(tf.float32, name="y") w = tf.get_variable("weights", initializer=tf.constant(0.0)) b = tf.get_variable("bias", initializer=tf.constant(0.0)) y_predict = w * x_ + b loss = tf.square(y_ - y_predict, name='loss') optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss) writer = tf.summary.FileWriter("./graphs", tf.get_default_graph()) writer.close() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(100): total_loss = 0 for x, y in zip(x_train, y_train): _, _loss = sess.run([optimizer, loss], feed_dict={x_: x, y_: y}) total_loss += _loss print(f"Epoch {i}: {total_loss / n_sample}") w_out, b_out = sess.run([w, b]) y_predict = x_train * w_out + b_out for i, j in zip(y_predict, y_train): print(f"{i} : {j}") plt.plot(x_train, y_predict, "r-", label="predict") plt.plot(x_train, y_train, "go", label="data") plt.title("ABC") plt.xlabel("x") plt.ylabel("y") plt.show()
def __init__(self, checkpoint_filename, input_name="images", output_name="features"): self.session = tf.Session() with tf.gfile.GFile(checkpoint_filename, "rb") as file_handle: graph_def = tf.GraphDef() graph_def.ParseFromString(file_handle.read()) tf.import_graph_def(graph_def, name="net") self.input_var = tf.get_default_graph().get_tensor_by_name( "%s:0" % input_name) self.output_var = tf.get_default_graph().get_tensor_by_name( "%s:0" % output_name) assert len(self.output_var.get_shape()) == 2 assert len(self.input_var.get_shape()) == 4 self.feature_dim = self.output_var.get_shape().as_list()[-1] self.image_shape = self.input_var.get_shape().as_list()[1:]
def capture_ops(): """Decorator to capture ops created in the block. with capture_ops() as ops: # create some ops print(ops) # => prints ops created. """ micros = int(time.time() * 10 ** 6) scope_name = str(micros) op_list = [] with tf.name_scope(scope_name): yield op_list g = tf.get_default_graph() op_list.extend(ge.select_ops(scope_name + "/.*", graph=g))
prediction = tf.concat([Q_output, action_output], 1, name = "concat_node") prediction_identity = tf.identity(prediction, name = "prediction_node") Q_loss = tf.keras.losses.mean_squared_error(y_true = Q_target, y_pred = Q_output_raw) policy_loss = tf.keras.losses.categorical_crossentropy(y_true = action_target, y_pred = action_output_raw) total_loss = Q_loss + policy_loss train_op = tf.train.AdamOptimizer(learning_rate = learning_rate, name = "Optimizer").minimize(total_loss, name = 'optimize_node') init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) train_writer = tf.summary.FileWriter(path_to_store + "/summary", sess.graph) train_writer.close() with open(os.path.join(path_to_store, model_name + '.pb'), 'wb') as f: f.write(tf.get_default_graph().as_graph_def().SerializeToString()) # builder = tf.saved_model.builder.SavedModelBuilder("C:/Users/Snurka/init_model") # builder.add_meta_graph_and_variables( # sess, # [tf.saved_model.tag_constants.SERVING] # ) # builder.save()
def get(name): return tf.get_default_graph().get_tensor_by_name('import/' + name + ':0')
def agent(self, obs, config): """Central function for an agent. Relevant properties of arguments: obs: halite: a one-dimensional list of the amount of halite in each board space player: integer, player id, generally 0 or 1 players: a list of players, where each is: [halite, { 'shipyard_uid': position }, { 'ship_uid': [position, halite] }] step: which turn we are on (counting up) Should return a dictionary where the key is the unique identifier string of a ship/shipyard and action is one of "CONVERT", "SPAWN", "NORTH", "SOUTH", "EAST", "WEST" ("SPAWN" being only applicable to shipyards and the others only to ships). """ this_turn = self # this_turn.print_board() current_player = this_turn.board.current_player size = self.size opponents = this_turn.board.opponents halite_map = np.zeros((size, size)) ship_map = np.zeros((size, size)) cargo_map = np.zeros((size, size)) shipyard_map = np.zeros((size, size)) myship_map = np.zeros((size, size)) # Load halite for i in range(size): for j in range(size): halite_map[i][j] = obs.halite[i * size + j] # print(halite_map) # Load current player for ship in current_player.ships: position = self.convert_kaggle_2D_to_coordinate_2D( this_turn.size, list(ship.position)) ship_map[position[0]][position[1]] = 2 cargo_map[position[0]][position[1]] = ship.halite myship_map[position[0]][position[1]] = 1 for shipyard in current_player.shipyards: position = self.convert_kaggle_2D_to_coordinate_2D( this_turn.size, list(shipyard.position)) shipyard_map[position[0]][position[1]] = 2 for opponent in opponents: for ship in opponent.ships: position = self.convert_kaggle_2D_to_coordinate_2D( this_turn.size, list(ship.position)) ship_map[position[0]][position[1]] = 1 cargo_map[position[0]][position[1]] = ship.halite for shipyard in opponent.shipyards: position = self.convert_kaggle_2D_to_coordinate_2D( this_turn.size, list(shipyard.position)) shipyard_map[position[0]][position[1]] = 1 actions = {} if not self.sess or not self.saver: print("Error, no model found") return {} sess, saver = self.sess, self.saver # print([n.name for n in tf.get_default_graph().as_graph_def().node]) frame_node = tf.get_default_graph().get_collection('frames')[0] loss_node = tf.get_default_graph().get_collection('loss')[0] my_ships_node = tf.get_collection('my_ships')[0] turns_left_node = tf.get_default_graph().get_collection( 'turns_left')[0] train_x = np.zeros((32, 32, 4)) ship_info = np.zeros((32, 32)) # add padding halite_map = np.array(halite_map) ship_map = np.array(ship_map) cargo_map = np.array(cargo_map) pad_offset = (32 - size) // 2 print(halite_map.shape) moves_node = tf.get_default_graph().get_collection('m_logits')[0] spawn_node = tf.get_default_graph().get_collection('s_logits')[0] train_features = np.stack( (halite_map, ship_map, cargo_map, shipyard_map), axis=-1) train_x[pad_offset:pad_offset + size, pad_offset:pad_offset + size, :] = train_features X = [train_x] X = np.array(X) ship_info[pad_offset:pad_offset + size, pad_offset:pad_offset + size] = myship_map my_ships = [ship_info] my_ships = np.expand_dims(np.array(my_ships), -1) turns_left = np.array(config.episodeSteps - obs.step - 1).reshape(1, 1) feed_dict = { frame_node: X, turns_left_node: turns_left, my_ships_node: my_ships } print("Training data dimension:", X.shape) padded_moves, spawn_or_not = sess.run([moves_node, spawn_node], feed_dict) print('padded_moves is', padded_moves.shape) padded_moves = padded_moves[0] ship_moves = padded_moves[pad_offset:pad_offset + size, pad_offset:pad_offset + size, :] valid_move = ["STAY", "EAST", "WEST", "SOUTH", "NORTH", "CONVERT"] print("spawn or not is", spawn_or_not) for ship in current_player.ships: position = self.convert_kaggle_2D_to_coordinate_2D( this_turn.size, list(ship.position)) print(ship_moves[position[0]][position[1]]) this_action = valid_move[np.argmax( ship_moves[position[0]][position[1]])] if this_action == "STAY": continue #actions[ship.id] = "NORTH" else: actions[ship.id] = this_action return actions