def train(): # Download dataset. download() # Create folders. if not os.path.isdir(SAVE_DIR): os.makedirs(SAVE_DIR) # Load and check whether UNK, SOS, EOS appears in the vocabulary. source_vocab, source_vocab_length = load_and_check_vocab(SOURCE_VOCAB_FILE) target_vocab, target_vocab_length = load_and_check_vocab(TARGET_VOCAB_FILE) # Get UNK, SOS, EOS id. source_unk_id = source_vocab.index(UNK) source_eos_id = source_vocab.index(EOS) target_unk_id = target_vocab.index(UNK) target_sos_id = target_vocab.index(SOS) target_eos_id = target_vocab.index(EOS) # Compute the length of effective training data. source_bool_mask = check_dataset(SOURCE_TRAINING_FILE) target_bool_mask = check_dataset(TARGET_TRAINING_FILE) bool_mask = [x and y for x, y in zip(source_bool_mask, target_bool_mask)] dataset_length = sum(bool_mask) # Create source and target vocabulary tables. source_vocab_table = tf.contrib.lookup.index_table_from_file(DATA_DIR + SOURCE_VOCAB_FILE, default_value = source_unk_id) target_vocab_table = tf.contrib.lookup.index_table_from_file(DATA_DIR + TARGET_VOCAB_FILE, default_value = target_unk_id) # Get dataset iterator tuple(initializer, source_input, target_input, target_output, source_length, target_length). iterator = get_dataset_iterator(dataset_length, SOURCE_TRAINING_FILE, TARGET_TRAINING_FILE, source_vocab_table, target_vocab_table, source_eos_id, target_sos_id, target_eos_id) # Load model. model = Seq2Seq_Model() model.build_training_model(source_vocab_length, target_vocab_length, iterator.source_input, iterator.target_input, iterator.target_output, iterator.source_length, iterator.target_length) iterator_initializer = iterator.initializer table_initializer = tf.tables_initializer() variable_initializer = tf.global_variables_initializer() with tf.Session() as sess: # Initialization. sess.run([iterator_initializer, table_initializer, variable_initializer]) avg_loss_buffer = Buffer(2000) for step in range(TRAINING_STEP): _, loss, avg_loss = sess.run([model.train_op, model.loss, model.avg_loss]) avg_loss_buffer.append(avg_loss) buffer_size = avg_loss_buffer.get_size() avg_loss_ = np.mean(avg_loss_buffer.get_buffer()) print("Current progress: ", step+1, "/", TRAINING_STEP, ". Average loss over the latest ", buffer_size, " training steps: ", format(avg_loss_, ".8f"), sep = "", end = "\r", flush = True) # Save parameters. saver_embedding = tf.train.Saver(model.embedding) saver_network = tf.train.Saver(model.network_params) saver_embedding.save(sess, SAVE_DIR + "embedding") saver_network.save(sess, SAVE_DIR + "network_params")
def _loop(mailbox, channels, archive_name): """Main server loop. Intended to be a Process target (and private to this module). Accepts messages through its mailbox queue, and takes the appropriate action based on the command and parameters contained within the message. Parameters ---------- mailbox : Queue Used for inter-process communication. channels : list of str list of channel names in the underlying data table. Any records written to the buffer are expected to have an entry for each channel. archive_name : str sqlite database name """ buf = Buffer(channels=channels, archive_name=archive_name) while True: # Messages should be tuples with the structure: # (sender, (command, params)) # where sender is either None or a Queue. msg = mailbox.get() sender, body = msg command, params = body if command == MSG_EXIT: delete_archive = params buf.cleanup(delete_archive=delete_archive) sender.put(('exit', 'ok')) break elif command == MSG_PUT: record = params buf.append(record) elif command == MSG_GET_ALL: sender.put(buf.all()) elif command == MSG_COUNT: sender.put(len(buf)) elif command == MSG_QUERY_SLICE: start, end, field = params logging.debug("Sending query: {}".format((start, end, field))) sender.put(buf.query(start, end, field)) elif command == MSG_QUERY: # Generic query filters, ordering, max_results = params sender.put(buf.query_data(filters, ordering, max_results)) elif command == MSG_STARTED: sender.put(('started', 'ok')) else: logging.debug("Error; message not understood: {}".format(msg))
class WebSocket: GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" handshake_ans = \ 'HTTP/1.1 101 Switching Protocols\n' \ 'Upgrade: Websocket\n' \ 'Connection: Upgrade\n' \ 'Sec-WebSocket-Accept: {0}\r\n\r\n' conn = None hands_shook = False time_at_last_activity = 0 time_rec_next_pong_to_live = 0 is_alive = True server_requested_close = False buffer = None ping_interval_seconds = 20 ping_pong_keep_alive_interval = 3 def __init__(self, conn, ping_interval_seconds, ping_pong_keep_alive_interval, debug=False): self.conn = conn self.conn.setblocking(0) self.ping_interval_seconds = ping_interval_seconds self.ping_pong_keep_alive_interval = ping_pong_keep_alive_interval self.debug = debug def keep_alive(self): """ Does checks to see if connection is still alive and sends a ping if no activity. """ now = time.time() if now - self.time_at_last_activity > self.ping_interval_seconds: self.send_ping() self.time_at_last_activity = now if self.time_rec_next_pong_to_live > self.ping_pong_keep_alive_interval: self.time_rec_next_pong_to_live = now + self.ping_pong_keep_alive_interval self.send_close('Connection closed due to inactivity') self.close() def close(self, ): """ Closes the connection and sets a value that lets the server-threads know that it is closed. :return: """ self.is_alive = False self.conn.close() def do_handshake(self, headers): """ Parses the incoming headers and responds with a handshake if the incoming headers are correct :param headers: Dictionary of headers received from the client. Keys in lower case :return: True if the handshake-request was valid, False otherwise """ sec_web_key = 'Sec-WebSocket-Key'.lower() if sec_web_key in headers.keys(): value = headers[sec_web_key].strip() h = value + self.GUID s = sha1(h.encode()).digest() r_key = b64encode(s).decode().strip() ws_answer = self.handshake_ans.format(r_key) self.send_msg(ws_answer.encode('utf-8')) self.hands_shook = True return True def send_msg(self, msg): """ Sends the given frame to the client :param msg: A frame """ self.conn.send(msg) def request_close(self): """ Sends a close-request with the code '1000' to the client """ self.server_requested_close = True self.send_close('1000') def recv(self): """ Receives and parses a message from the client. :return: The decoded message payload if one was received and it was not a control-frame. None if not. """ if self.hands_shook: self.keep_alive() # raises error and returns if no data received. Error is handled byte the server bytes_rec = self.conn.recv(65536) self.time_at_last_activity = time.time() if not bytes_rec: return if self.hands_shook: try: if self.buffer: msg = frame_handler.unmask(bytes_rec, self.buffer.datatype) else: msg = frame_handler.unmask(bytes_rec) except ValueError as er: self.send_close(er) self.close() return payload = msg.return_data if msg.is_pong: return self.handle_pong() if msg.is_ping: return self.handle_ping(payload) if msg.is_close: return self.handle_close(payload) if msg.is_close_fin: if msg.is_continuation: self.buffer.append(payload) return_data = self.buffer.get_data() self.buffer = None return return_data else: return payload else: if msg.is_continuation: self.buffer.append(payload) else: self.buffer = Buffer(msg.is_text) self.buffer.append(payload) else: # if handshake has not been done headers = self.parse_as_headers(bytes_rec) if not self.do_handshake(headers): # not a valid handshake as first request # close connection if self.debug: print('Not a valid handshake request received as first message. Closing connection...') self.close() else: if self.debug: print('Valid handshake completed') def handle_pong(self): """ Handles a pong-request. Nothing has to be done here. """ if self.debug: print('Received pong') pass def send_ping(self): """ Sends a ping-request to the client """ if self.debug: print('Sending ping') self.send_msg(frame_handler.build_frame('phony', opcode=frame_handler.OPCODE_PING)) def send_close(self, msg): """ Sends a close-request to the client """ if self.debug: print('Sending close') self.send_msg(frame_handler.build_frame(msg, frame_handler.OPCODE_CLOSE)) def handle_ping(self, message): """ Handles a ping-request from the client """ pong_msg = frame_handler.build_frame(message, frame_handler.OPCODE_PONG) self.send_msg(pong_msg) def handle_close(self, msg): """ Sends a close-request to the client """ if not self.server_requested_close: self.send_close(msg) self.close() @staticmethod def parse_as_headers(data): """ Parses data as a list of headers and returns the headers as a dictionary. :param data: data received from client :return: Headers as a dictionary with keys in lower-case """ data = data.strip() def parse_val(val): return val.decode('utf-8').strip() headers = {} for l in data.split(b'\r\n')[1:]: key, value = l.split(b': ') headers[parse_val(key).lower()] = parse_val(value) return headers
class TcpConnection(object): def __init__(self, io_loop, iostream, peer_addr): """although iostream has read buffer, but provide a buffer object is more helpful for upper logic :param peer_addr (host, prot) """ self.io_loop = io_loop self._iostream = iostream self._read_buffer = Buffer() self._peer_addr = peer_addr self._connection_cb = None self._write_complete_cb = None self._message_cb = None self._iostream.read_until_close(streaming_callback=self._on_message) self._is_connected = True self._context = None def set_connection_callback(self, callback): self._connection_cb = callback def set_message_callback(self, callback): """callback(connection, buffer)""" self._message_cb = callback def _on_message(self, data): self._read_buffer.append(data) if self._message_cb: self._message_cb(self, self._read_buffer) def set_write_complete_callback(self, callback): self._write_complete_cb = callback def set_close_callback(self, callback): self._iostream.set_close_callback(lambda: callback(self)) def connect_established(self): print "connect_established", self.name if self._connection_cb: self._connection_cb(self) def send(self, buffer): self._iostream.write(buffer.retrieve_all(), self._write_complete_cb) def shut_down(self): self._iostream.close() @property def name(self): addr = self._peer_addr name = (addr[0], ':', str(addr[1])) return ''.join(name) def connect_destroyed(self): """no need to call self._iostream.close()""" print "connect_destroyed", self.name self._is_connected = False if self._connection_cb: self._connection_cb(self) @property def is_connected(self): return self._is_connected @property def context(self): return self._context @context.setter def context(self, value): self._context = value
from buffer import Buffer import configs env = DroneEnv() ppo = PPO() buffer = Buffer() while (True): s = env.reset() while (True): a = ppo.choose_action(s) s_, r, done, _ = env.step(a) buffer.append(s, a, r) s = s_ if (env.t + 1) % configs.BATCH == 0: v = ppo.getValue(s_) buffer.discount(v) #print(f'{v} {np.linalg.norm(s[0:3]-s[3:6])} {s} ') f = buffer.format() ppo.update(f[0], f[1], f[2]) buffer.clear() if done: break
t_last_log = time.time() frames_processed = 0 filter = Filter(3) filter_bg = Filter(300) epoch = datetime.datetime.utcfromtimestamp(0) epoch = epoch.replace(tzinfo=pytz.utc) log.info("start processing...") with ZmqArrow(address=input_address) as sub: with ZmqArrow(address="tcp://*:{}".format(port), socket_type=zmq.PUB) as pub: while True: t_frame, tensor = sub.zmq_socket.recv_time_and_tensor(copy=False) raw = tensor.to_numpy() buffer.append(t_frame, raw) amp, phases = processor.process_fft(raw) img_act = filter(t_frame, amp) img_bg = filter_bg(t_frame, amp) img = ((img_act - img_bg) / (img_act + img_bg) + 0.2) pub.zmq_socket.send_time_and_tensor(t_frame, pa.Tensor.from_numpy(img), copy=False) mqtt.publish('{}/radar/activity/max'.format(device_name), payload=json.dumps({'timeMilliseconds': round((t_frame-epoch).total_seconds() * 1000), 'value': img.max()})) frames_processed += 1 t = time.time() if frames_processed == 1 or ((t - t_last_log) > log_every_n_second):
elif req.event == req.EVENT_FIRST_LINE: if req.is_request(): print 'first-line: ' + repr('%s %s %s'%(req.method, req.uri, req.version)) else: print 'first-line: ' + repr('%s %s %s'%(req.version, req.status_code, req.status_text)) """ for chunk_size in range(3, 6): buf = Buffer() req = HttpResponse() req.CHUNK_SIZE = chunk_size print '---chunk_size[%d]---'%chunk_size if 0: for ch in list(text): buf.append(ch) print buf if proc_recv(req, buf) == -1: exit(0) print 'error' break if req.event == req.EVENT_READY: print '------' req = HttpRequest() req.CHUNK_SIZE = chunk_size else: buf.append(text) while buf.len() > 0: if proc_recv(req, buf) == -1: print 'error' exit(0)