text:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import time, random
import numpy as np
import tensorflow as tf
from tensorflow.python.layers import core as layers_core
import argparse
from tensorflow.python.client import device_lib
import os
from utils import *

class Option(object):
    def __init__(self, d):
        self.__dict__ = d
    def save(self):
        with open(os.path.join(self.this_expsdir, "option.txt"), "w") as f:
            for key, value in sorted(self.__dict__.items(), key=lambda x: x[0]):
                f.write("%s, %s\n" % (key, str(value)))

logging = tf.logging

def data_type():
  return  tf.float32

class PTBModel(object):
  #The language model.

  def __init__(self, is_training, is_test_LM=False):
    self._is_training = is_training
    self.batch_size = config.batch_size
    self.num_steps = config.num_steps
    size = config.hidden_size
    vocab_size = config.vocab_size
    self._input=tf.placeholder(shape=[None, config.num_steps], dtype=tf.int32)
    self._target=tf.placeholder(shape=[None, config.num_steps], dtype=tf.int32)
    self._sequence_length=tf.placeholder(shape=[None], dtype=tf.int32)
    with tf.device("/cpu:0"):
      embedding = tf.get_variable("embedding", [vocab_size, size], dtype=data_type())
      inputs = tf.nn.embedding_lookup(embedding, self._input)
    softmax_w = tf.get_variable(
          "softmax_w", [size, vocab_size], dtype=data_type())
    softmax_b = tf.get_variable("softmax_b", [vocab_size], dtype=data_type())
    if is_training and config.keep_prob < 1:
      inputs = tf.nn.dropout(inputs, config.keep_prob)
    output = self._build_rnn_graph(inputs, self._sequence_length, is_training)

    output=tf.reshape(output, [-1, config.hidden_size])
    logits = tf.nn.xw_plus_b(output, softmax_w, softmax_b)
      # Reshape logits to be a 3-D tensor for sequence loss
    logits = tf.reshape(logits, [-1, self.num_steps, vocab_size])
    self._output_prob=tf.nn.softmax(logits)
      # Use the contrib sequence loss and average over the batches
    mask=tf.sequence_mask(lengths=self._sequence_length, maxlen=self.num_steps, dtype=data_type())
    loss = tf.contrib.seq2seq.sequence_loss(
      logits,
      self._target,
      mask,
      average_across_timesteps=True,
      average_across_batch=True)

    # Update the cost
    self._cost = loss

    #self._lr = tf.Variable(0.0, trainable=False)
    tvars = tf.trainable_variables()
    grads, _ = tf.clip_by_global_norm(tf.gradients(self._cost, tvars),
                                      config.max_grad_norm)
    optimizer = tf.train.AdamOptimizer()
    self._train_op = optimizer.apply_gradients(
        zip(grads, tvars),
        global_step=tf.train.get_or_create_global_step())

  def _build_rnn_graph(self, inputs, sequence_length, is_training):
    return self._build_rnn_graph_lstm(inputs, sequence_length, is_training)

  def _get_lstm_cell(self, is_training):
    return tf.contrib.rnn.BasicLSTMCell(
          config.hidden_size, forget_bias=0.0, state_is_tuple=True,
          reuse=not is_training)

  def _build_rnn_graph_lstm(self, inputs, sequence_length, is_training):
    """Build the inference graph using canonical LSTM cells."""
    # Slightly better results can be obtained with forget gate biases
    # initialized to 1 but the hyperparameters of the model would need to be
    # different than reported in the paper.
    def make_cell():
      cell = self._get_lstm_cell( is_training)
      if is_training and config.keep_prob < 1:
        cell = tf.contrib.rnn.DropoutWrapper(
            cell, output_keep_prob=config.keep_prob)
      return cell

    cell = tf.contrib.rnn.MultiRNNCell(
        [make_cell() for _ in range(config.num_layers)], state_is_tuple=True)
    outputs, states=tf.nn.dynamic_rnn(cell=cell, inputs=inputs, sequence_length=sequence_length, dtype=data_type())

    return outputs

def run_epoch(sess, model, input, sequence_length, target=None, mode='train'):
  #Runs the model on the given data.
  if mode=='train':
    #train language model
    _,cost = sess.run([model._train_op, model._cost], feed_dict={model._input: input, model._target:target, model._sequence_length:sequence_length})
    return cost
  elif mode=='test':
    #test language model
    cost = sess.run(model._cost, feed_dict={model._input: input, model._target:target, model._sequence_length:sequence_length})
    return cost
  else:
    #use the language model to calculate sentence probability
    output_prob = sess.run(model._output_prob, feed_dict={model._input: input, model._sequence_length:sequence_length})
    return output_prob

def main(config):

  if config.mode=='forward' or config.mode=='use':
    with tf.name_scope("forward_train"):
      with tf.variable_scope("forward", reuse=None):
        m_forward = PTBModel(is_training=True)
    with tf.name_scope("forward_test"):
      with tf.variable_scope("forward", reuse=True):
        mtest_forward = PTBModel(is_training=False)
    var=tf.trainable_variables()
    var_forward=[x for x in var if x.name.startswith('forward')]
    saver_forward=tf.train.Saver(var_forward, max_to_keep=1)
  if config.mode=='backward' or config.mode=='use':
    with tf.name_scope("backward_train"):
      with tf.variable_scope("backward", reuse=None):
        m_backward = PTBModel(is_training=True)
    with tf.name_scope("backward_test"):
      with tf.variable_scope("backward", reuse=True):
        mtest_backward = PTBModel(is_training=False)
    var=tf.trainable_variables()
    var_backward=[x for x in var if x.name.startswith('backward')]
    saver_backward=tf.train.Saver(var_backward, max_to_keep=1)

  init = tf.global_variables_initializer()

  with tf.Session() as session:
    session.run(init)
    input = [[3,4,5,6,6,7,8,9,4,5,6,7,8,9,2]]
    sequence_length = [10]
    prob_old=run_epoch(session, mtest_forward, input, sequence_length, mode='use')
    print(prob_old)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Experiment setup")
    # misc
    parser.add_argument('--seed', default=33, type=int)
    parser.add_argument('--gpu', default="3", type=str)
    parser.add_argument('--no_train', default=False, action="store_true")
    parser.add_argument('--no_preds', default=False, action="store_true")
    parser.add_argument('--exps_dir', default=None, type=str)
    parser.add_argument('--exp_name', default=None, type=str)
    parser.add_argument('--load', default=None, type=str)

    # data property
    parser.add_argument('--data_path', default='data/quora/quora.txt', type=str)
    parser.add_argument('--dict_path', default='data/quora/dict.pkl', type=str)
    parser.add_argument('--dict_size', default=30000, type=int)
    parser.add_argument('--vocab_size', default=30003, type=int)
    parser.add_argument('--backward', default=False, action="store_true")
    parser.add_argument('--keyword_pos', default=True, action="store_false")
    # model architecture
    parser.add_argument('--num_steps', default=15, type=int)
    parser.add_argument('--num_layers', default=2, type=int)
    parser.add_argument('--emb_size', default=256, type=int)
    parser.add_argument('--hidden_size', default=300, type=int)
    parser.add_argument('--dropout', default=0.0, type=float)
    parser.add_argument('--model', default=0, type=int)
    # optimization
    parser.add_argument('--batch_size', default=128, type=int)
    parser.add_argument('--epochs', default=200, type=int)
    parser.add_argument('--learning_rate', default=0.001, type=float)
    parser.add_argument('--weight_decay', default=0.00, type=float)
    parser.add_argument('--clip_norm', default=0.00, type=float)
    parser.add_argument('--no_cuda', default=False, action="store_true")
    parser.add_argument('--local', default=False, action="store_true")
    parser.add_argument('--threshold', default=0.1, type=float)

    # evaluation
    parser.add_argument('--sim', default='word_max', type=str)
    parser.add_argument('--mode', default='sa', type=str)
    parser.add_argument('--accuracy', default=False, action="store_true")
    parser.add_argument('--top_k', default=10, type=int)
    parser.add_argument('--accumulate_step', default=1, type=int)
    parser.add_argument('--backward_path', default=None, type=str)
    parser.add_argument('--forward_path', default=None, type=str)

    # sampling
    parser.add_argument('--use_data_path', default='data/input/input.txt', type=str)
    parser.add_argument('--reference_path', default=None, type=str)
    parser.add_argument('--pos_path', default='POS/english-models', type=str)
    parser.add_argument('--emb_path', default='data/quora/emb.pkl', type=str)
    parser.add_argument('--max_key', default=3, type=float)
    parser.add_argument('--max_key_rate', default=0.5, type=float)
    parser.add_argument('--rare_since', default=30000, type=int)
    parser.add_argument('--sample_time', default=100, type=int)
    parser.add_argument('--search_size', default=100, type=int)
    parser.add_argument('--action_prob', default=[0.3,0.3,0.3,0.3], type=list)
    parser.add_argument('--just_acc_rate', default=0.0, type=float)
    parser.add_argument('--sim_mode', default='keyword', type=str)
    parser.add_argument('--save_path', default='temp.txt', type=str)
    parser.add_argument('--forward_save_path', default='data/tfmodel/forward.ckpt', type=str)
    parser.add_argument('--backward_save_path', default='data/tfmodel/backward.ckpt', type=str)
    parser.add_argument('--max_grad_norm', default=5, type=float)

    parser.add_argument('--keep_prob', default=1, type=float)

    d = vars(parser.parse_args())
    option = Option(d)

    random.seed(option.seed)
    np.random.seed(option.seed)
    os.environ["CUDA_VISIBLE_DEVICES"] = option.gpu
    config = option
    main(option)

max_stars_repo_name: aolney/upsa-docker

max_issues_repo_name: aolney/upsa-docker

max_forks_repo_name: aolney/upsa-docker


text:
import unittest

import numpy as np

from .. import ChargingNetwork
from ..base import ErrorAllWrapper

class TestErrorAllWrapper(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.builtin_data = 2
        cls.builtin_wrapper = ErrorAllWrapper(cls.builtin_data)
        cls.network_data = ChargingNetwork()
        cls.network_wrapper = ErrorAllWrapper(cls.network_data)

    def test_correct_on_init(self):
        self.assertEqual(self.builtin_data, self.builtin_wrapper.data)
        self.assertEqual(self.network_data, self.network_wrapper.data)

    def test_builtin_success(self):
        self.assertEqual(self.builtin_wrapper.__dict__, {'_data': 2})

    def test_builtin_error(self):
        with self.assertRaises(TypeError):
            _ = self.builtin_wrapper == self.network_wrapper

    def test_network_attribute_error(self):
        with self.assertRaises(TypeError):
            _ = self.network_wrapper.constraint_matrix

    def test_network_function_error(self):
        with self.assertRaises(TypeError):
            _ = self.network_wrapper.is_feasible(np.array([[0]]))

if __name__ == '__main__':
    unittest.main()

ext: py

max_stars_repo_name: gazzayeah/acnportal

max_issues_repo_name: gazzayeah/acnportal

max_forks_repo_name: gazzayeah/acnportal

reason: import numpy


text:
#!/usr/bin/env python

import cv2
import numpy as np
import rospy
import tf # not tensorflow - local library
import yaml
from cv_bridge import CvBridge
from geometry_msgs.msg import PointStamped, Pose, PoseStamped
from image_geometry import PinholeCameraModel
from sensor_msgs.msg import CameraInfo, Image
from std_msgs.msg import Int32
from tf.transformations import euler_from_quaternion

from light_classification.tl_classifier import TLClassifier
from styx_msgs.msg import TrafficLight, TrafficLightArray
from styx_msgs.msg import Lane

import os

STATE_COUNT_THRESHOLD = 3

def distance2(a, b):
    r'''Return the squared distance between `a` and `b`.

        If `a` is a `n X d` matrix and `b` a `d`-dimensional array, return a
        1-D array of distances between each `a[i]` and `b`.
    '''
    d = (a - b) ** 2.0
    if len(d.shape) > 1:
        return np.sum(d, axis=1).flat
    else:
        return np.sum(d)

def distance(a, b):
    r'''Return the Euclidean distance between `a` and `b`.

        If `a` is a `n X d` matrix and `b` a `d`-dimensional array, return a
        1-D array of distances between each `a[i]` and `b`.
    '''
    return np.sqrt(distance2(a, b))

def closest(P, p):
    r'''Given a `n X d` matrix `P` and a d-dimensional point `p`, return
        the index `i` such that `P[i]` is the point closest to `p` in `P`.
    '''
    return np.argmin(distance2(P, p))

class TLDetector(object):
    def __init__(self):
        rospy.init_node('tl_detector')

        self.camera = None
        self.camera_image = None
        self.pose = None
        self.stop_indexes = None
        self.traffic_lights = None
        self.waypoints = None

        self.bridge = CvBridge()
        self.light_classifier = TLClassifier()
        self.listener = tf.TransformListener()

        self.last_state = TrafficLight.UNKNOWN
        self.state = TrafficLight.UNKNOWN
        self.state_count = 0
        self.last_wp = -1

        config = yaml.load(rospy.get_param("/traffic_light_config"))
        self.stop_lines = np.array(config['stop_line_positions'])

        self.subscribers = [
            rospy.Subscriber('/camera_info', CameraInfo, self.camera_info_cb, queue_size=1),
            rospy.Subscriber('/current_pose', PoseStamped, self.pose_cb, queue_size=1),
            rospy.Subscriber('/base_waypoints', Lane, self.waypoints_cb, queue_size=1),
            rospy.Subscriber('/image_color', Image, self.image_cb, queue_size=1),
            rospy.Subscriber('/vehicle/traffic_lights', TrafficLightArray, self.traffic_cb, queue_size=1)
        ]

        self.upcoming_red_light_pub = rospy.Publisher('/traffic_waypoint', Int32, queue_size=1)
        self.image_zoomed = rospy.Publisher('/image_zoomed', Image, queue_size=1)

        # keeps python from exiting until node is stopped
        rospy.spin()

    def camera_info_cb(self, camera_info):
        if self.camera is not None:
            return

        self.camera = PinholeCameraModel()
        self.camera.fromCameraInfo(camera_info)

    def pose_cb(self, msg):
        self.pose = msg

    def waypoints_cb(self, waypoints):
        if self.waypoints is not None:
            return

        self.waypoints = np.array([[w.pose.pose.position.x, w.pose.pose.position.y] for w in waypoints.waypoints])
        self.stop_indexes = np.array([closest(self.waypoints, light) for light in self.stop_lines])

    def image_cb(self, msg):
        r'''Identifies red lights in the incoming camera image and publishes the index
            of the waypoint closest to the red light to /traffic_waypoint

        Args:
            msg (Image): image from car-mounted camera

        '''
        self.camera_image = msg
        light_wp, state = self.process_traffic_lights()

        '''
        Publish upcoming red lights at camera frequency.
        Each predicted state has to occur `STATE_COUNT_THRESHOLD` number
        of times till we start using it. Otherwise the previous stable state is
        used.
        '''
        if self.state != state:
            self.state_count = 0
            self.state = state
        elif self.state_count >= STATE_COUNT_THRESHOLD:
            self.last_state = self.state
            if (
                state == TrafficLight.GREEN
                or (
                    state == TrafficLight.YELLOW
                    and self.state_count < 2 * STATE_COUNT_THRESHOLD
                )
            ):
                light_wp = -1

            self.last_wp = light_wp
            self.upcoming_red_light_pub.publish(Int32(light_wp))
        else:
            self.upcoming_red_light_pub.publish(Int32(self.last_wp))

        self.state_count += 1

    def traffic_cb(self, msg):
        if self.traffic_lights is not None:
            return

        def pose(light):
            position = light.pose.pose.position
            return [position.x, position.y, position.z]

        self.traffic_lights = np.array([pose(light) for light in msg.lights])

    def get_closest_waypoint(self, pose):
        r'''Identifies the closest path waypoint to the given position
            https://en.wikipedia.org/wiki/Closest_pair_of_points_problem
        Args:
            pose (Pose): position to match a waypoint to

        Returns:
            int: index of the closest waypoint in self.waypoints

        '''
        p = np.array([pose.position.x, pose.position.y])
        return closest(self.waypoints, p)

    def project_to_image_plane(self, point3d):
        r'''Project point from 3D world coordinates to 2D camera image location

        Args:
            point3d (Point): 3D location of a point in the world

        Returns:
            x (int): x coordinate of target point in image
            y (int): y coordinate of target point in image
        '''
        stamp = self.camera_image.header.stamp

        p_world = PointStamped()
        p_world.header.seq = self.camera_image.header.seq
        p_world.header.stamp = stamp
        p_world.header.frame_id = '/world'
        p_world.point.x = point3d[0]
        p_world.point.y = point3d[1]
        p_world.point.z = point3d[2]

        # Transform point from world to camera frame.
        self.listener.waitForTransform('/base_link', '/world', stamp, rospy.Duration(1.0))
        p_camera = self.listener.transformPoint('/base_link', p_world)

        # The navigation frame has X pointing forward, Y left and Z up, whereas the
        # vision frame has X pointing right, Y down and Z forward; hence the need to
        # reassign axes here.
        x = -p_camera.point.y
        y = -p_camera.point.z
        z = p_camera.point.x

        return self.camera.project3dToPixel((x, y, z))

    def get_light_state(self, light):
        r'''Determines the current color of the traffic light

        Args:
            light (TrafficLight): light to classify

        Returns:
            int: ID of traffic light color (specified in styx_msgs/TrafficLight)
        '''
        if self.camera_image is None:
            self.prev_light_loc = None
            return False

        (x_tl, y_tl) = self.project_to_image_plane(light + np.array([-0.7, -0.7, 1.0]))
        (x_br, y_br) = self.project_to_image_plane(light + np.array([0.5, 0.5, -1.0]))

        cv_image = self.bridge.imgmsg_to_cv2(self.camera_image, "bgr8")
        shape = cv_image.shape

        a = int(max(0, y_tl)+5)
        b = int(min(y_br, shape[0]))
        c = int(max(0, min(x_br, x_tl)))
        d = int(min(max(x_br, x_tl)-20, shape[1]))

        if (b - a) < 10 or (d - c) < 10:
            return TrafficLight.UNKNOWN

        # original 100, 140
        zoomed = cv2.resize(cv_image[a:b, c:d], (40, 90))

        state = self.light_classifier.get_classification(zoomed)
        self.image_zoomed.publish(self.bridge.cv2_to_imgmsg(zoomed, 'bgr8'))
        return state

    def process_traffic_lights(self):
        r'''Finds closest visible traffic light, if one exists, and determines its
            location and color

        Returns:
            int: index of waypoint closes to the upcoming stop line for a traffic light (-1 if none exists)
            int: ID of traffic light color (specified in styx_msgs/TrafficLight)
        '''

        # Entire function has changed? what exacty is going on here?
        # is it failing to classify all topics, or is it not listening at all?

        # Don't process if there are no waypoints or current position.
        if any(v is None for v in [self.waypoints, self.stop_indexes, self.pose]):
            return (-1, TrafficLight.UNKNOWN)

        # Get car's position.
        i_car = self.get_closest_waypoint(self.pose.pose)
        p_car = self.waypoints[i_car]

        # Get the closest stop line's index.
        j_stop = closest(self.stop_lines, p_car)
        i_stop = self.stop_indexes[j_stop]

        # If the car is ahead of the closest stop line, get the next one.
        if i_car > i_stop:
            j_stop = (j_stop + 1) % len(self.stop_indexes)
            i_stop = self.stop_indexes[j_stop]

        # Don't process if the closest stop line is too far.
        if distance(p_car, self.stop_lines[j_stop]) > 50.0:
            return (-1, TrafficLight.UNKNOWN)

        # Return the index and state of the traffic light.
        state = self.get_light_state(self.traffic_lights[j_stop])
        return (i_stop, state)

if __name__ == '__main__':
    try:
        TLDetector()
    except rospy.ROSInterruptException:
        rospy.logerr('Could not start traffic node.')


text:
#!/usr/bin/env python36
# -*- coding: utf-8 -*-
"""
Created on 13/03/2018 3:30 PM

@author: Tangrizzly
"""

from __future__ import print_function
from collections import OrderedDict
import time
import datetime
import cPickle
import numpy as np
import os
import random
from public.BPR import OboBpr
from public.GRU import OboGru
from public.CA_RNN import OboCARNN
from public.GRU_Spatial import OboSpatialGru
from public.Global_Best import GlobalBest
from public.Load_Data_by_length import load_data, fun_data_buys_masks
from public.Load_Data_by_length import fun_random_neg_masks_tra, fun_random_neg_masks_tes, fun_compute_dist_neg
from public.Load_Data_by_length import fun_compute_distance, fun_acquire_prob
from public.Valuate import fun_predict_auc_recall_map_ndcg, fun_save_best_and_losses
__docformat__ = 'restructedtext en'

WHOLE = './poidata/'
PATH_f = os.path.join(WHOLE, 'Foursquare/sequence')
PATH_g = os.path.join(WHOLE, 'Gowalla/sequence')
PATH = PATH_f

def exe_time(func):
    def new_func(*args, **args2):
        name = func.__name__
        start = datetime.datetime.now()
        print("-- {%s} start: @ %ss" % (name, start))
        back = func(*args, **args2)
        end = datetime.datetime.now()
        print("-- {%s} start: @ %ss" % (name, start))
        print("-- {%s} end:   @ %ss" % (name, end))
        total = (end - start).total_seconds()
        print("-- {%s} total: @ %.3fs = %.3fh" % (name, total, total / 3600.0))
        return back
    return new_func

class Params(object):
    def __init__(self, p=None):
        if not p:
            t = 't'
            assert 't' == t or 'v' == t or 's' == t
            p = OrderedDict(
                [
                    ('dataset',             'Foursquare.txt'),
                    # ('dataset',             'Gowalla.txt'),
                    ('mode',                'test' if 't' == t else 'valid' if 'v' == t else 's'),
                    ('load_epoch',          0),
                    ('save_per_epoch',      100),
                    ('split',               -2 if 'v' == t else -1),
                    ('at_nums',             [5, 10, 15, 20]),
                    ('epochs',              101),

                    ('latent_size',         20),
                    ('alpha',               0.01),
                    ('lambda',              0.001),
                    ('loss_weight',         [0.5, 0.5]),

                    ('dd',                  200),
                    ('UD',                  40),

                    ('mini_batch',          0),
                    ('gru',                 0),

                    ('batch_size_train',    1),
                    ('batch_size_test',     32),
                ])
            for i in p.items():
                print(i)
        dist_num = int(p['UD'] * 1000 / p['dd'])    # 1520 = 38*1000/25。idx=[0, 1519+1]

        [(user_num, item_num), pois_cordis, (tra_buys, tes_buys), (tra_dist, tes_dist)] = \
            load_data(os.path.join(PATH, p['dataset']), p['mode'], p['split'], p['dd'], dist_num)
        tra_buys_masks, tra_dist_masks, tra_masks = fun_data_buys_masks(tra_buys, tra_dist, [item_num], [dist_num])
        tes_buys_masks, tes_dist_masks, tes_masks = fun_data_buys_masks(tes_buys, tes_dist, [item_num], [dist_num])
        tra_buys_neg_masks = fun_random_neg_masks_tra(item_num, tra_buys_masks)
        tes_buys_neg_masks = fun_random_neg_masks_tes(item_num, tra_buys_masks, tes_buys_masks)
        tra_dist_neg_masks = fun_compute_dist_neg(tra_buys_masks, tra_masks, tra_buys_neg_masks, pois_cordis, p['dd'], dist_num)
        usrs_last_poi_to_all_intervals = fun_compute_distance(tra_buys_masks, tra_masks, pois_cordis, p['dd'], dist_num)

        self.p = p
        self.user_num, self.item_num, self.dist_num = user_num, item_num, dist_num
        self.pois_cordis = pois_cordis
        self.tra_buys_masks, self.tra_masks, self.tra_buys_neg_masks = tra_buys_masks, tra_masks, tra_buys_neg_masks
        self.tes_buys_masks, self.tes_masks, self.tes_buys_neg_masks = tes_buys_masks, tes_masks, tes_buys_neg_masks
        self.tra_dist_masks = tra_dist_masks
        self.tes_dist_masks = tes_dist_masks
        self.tra_dist_neg_masks = tra_dist_neg_masks
        self.ulptai = usrs_last_poi_to_all_intervals

    def build_model_one_by_one(self, flag=0):
        """
        建立模型对象
        :param flag: 参数变量、数据
        :return:
        """
        print('Building the model one_by_one ...')
        p = self.p
        size = p['latent_size']
        if 0 == flag:
            model = OboBpr(
                train=[self.tra_buys_masks, self.tra_masks, self.tra_buys_neg_masks],
                test= [self.tes_buys_masks, self.tes_masks, self.tes_buys_neg_masks],
                alpha_lambda=[p['alpha'], p['lambda']],
                n_user=self.user_num,
                n_item=self.item_num,
                n_in=size,
                n_hidden=size)
        elif 1 == flag:
            model = OboGru(
                train=[self.tra_buys_masks, self.tra_masks, self.tra_buys_neg_masks],
                test= [self.tes_buys_masks, self.tes_masks, self.tes_buys_neg_masks],
                alpha_lambda=[p['alpha'], p['lambda']],
                n_user=self.user_num,
                n_item=self.item_num,
                n_in=size,
                n_hidden=size)
        elif 2 == flag:
            model = OboSpatialGru(
                train=[self.tra_buys_masks, self.tra_masks, self.tra_buys_neg_masks],
                test= [self.tes_buys_masks, self.tes_masks, self.tes_buys_neg_masks],
                dist= [self.tra_dist_masks, self.tes_dist_masks, self.tra_dist_neg_masks],
                alpha_lambda=[p['alpha'], p['lambda']],
                n_user=self.user_num,
                n_item=self.item_num,
                n_dists=[self.dist_num, 1.0*p['dd']/1000],
                n_in=size,              # 总的input是2 * size。
                n_hidden=size)
        else:
            model = OboCARNN(
                train=[self.tra_buys_masks, self.tra_masks, self.tra_buys_neg_masks],
                test= [self.tes_buys_masks, self.tes_masks, self.tes_buys_neg_masks],
                dist= [self.tra_dist_masks, self.tes_dist_masks, self.tra_dist_neg_masks],
                alpha_lambda=[p['alpha'], p['lambda']],
                n_user=self.user_num,
                n_item=self.item_num,
                n_dists=[self.dist_num, 1.0*p['dd']/1000],
                n_in=size,
                n_hidden=size,
                ulptai=self.ulptai)
        model_name = model.__class__.__name__
        print('\t the current Class name is: {val}'.format(val=model_name))
        return model, model_name

    def compute_start_end(self, flag):
        """
        获取mini-batch的各个start_end(np.array类型，一组连续的数值)
        :param flag: 'train', 'test'
        :return: 各个start_end组成的list
        """
        assert flag in ['train', 'test', 'test_auc']
        if 'train' == flag:
            size = self.p['batch_size_train']
        elif 'test' == flag:
            size = self.p['batch_size_test']        # test: top-k and acquire user vector
        else:
            size = self.p['batch_size_test'] * 10   # test: auc
        user_num = self.user_num
        rest = (user_num % size) > 0
        n_batches = np.minimum(user_num // size + rest, user_num)
        batch_idxs = np.arange(n_batches, dtype=np.int32)
        starts_ends = []
        for bidx in batch_idxs:
            start = bidx * size
            end = np.minimum(start + size, user_num)
            start_end = np.arange(start, end, dtype=np.int32)
            starts_ends.append(start_end)
        return batch_idxs, starts_ends

def train_valid_or_test(pas):
    """
    主程序
    :return:
    """
    # 建立参数、数据、模型、模型最佳值
    p = pas.p
    model, model_name = pas.build_model_one_by_one(flag=p['gru'])
    best = GlobalBest(at_nums=p['at_nums'])   # 存放最优数据
    _, starts_ends_tes = pas.compute_start_end(flag='test')
    _, starts_ends_auc = pas.compute_start_end(flag='test_auc')

    # 直接取出来部分变量，后边就不用加'pas.'了。
    user_num, item_num, dist_num = pas.user_num, pas.item_num, pas.dist_num
    tra_buys_masks, tra_masks, tra_buys_neg_masks = pas.tra_buys_masks, pas.tra_masks, pas.tra_buys_neg_masks
    tes_buys_masks, tes_masks, tes_buys_neg_masks = pas.tes_buys_masks, pas.tes_masks, pas.tes_buys_neg_masks
    dd = p['dd']
    pois_cordis = pas.pois_cordis
    ulptai = pas.ulptai
    del pas

    ini_epoch = 0
    if 2 == p['gru']:
        ini_epoch = p['load_epoch']
        if p['load_epoch'] != 0:
            print('Loading model ...')
            m_path = './model/' + p['dataset'] + '/' + model_name + '_size' + \
                     str(p['latent_size']) + '_UD' + str(p['UD']) + '_dd' + str(p['dd']) + '_epoch' + str(
                p['load_epoch'])
            with open(m_path, 'rb') as f:
                loaded_objects = cPickle.load(f)
            model.load_params(loaded_objects)
            ini_epoch = p['load_epoch'] + 1

    # 主循环
    losses = []
    times0, times1, times2, times3 = [], [], [], []
    for epoch in np.arange(ini_epoch, p['epochs']):
        print("Epoch {val} ==================================".format(val=epoch))
        if epoch > 0:
            tra_buys_neg_masks = fun_random_neg_masks_tra(item_num, tra_buys_masks)
            tes_buys_neg_masks = fun_random_neg_masks_tes(item_num, tra_buys_masks, tes_buys_masks)
            if p['gru'] in [0, 1]:
                model.update_neg_masks(tra_buys_neg_masks, tes_buys_neg_masks)
            else:
                tra_dist_neg_masks = fun_compute_dist_neg(tra_buys_masks, tra_masks, tra_buys_neg_masks, pois_cordis, dd, dist_num)
                model.s_update_neg_masks(tra_buys_neg_masks, tes_buys_neg_masks, tra_dist_neg_masks)

        # ----------------------------------------------------------------------------------------------------------
        print("\tTraining ...")
        t0 = time.time()
        loss = 0.
        ls = [0, 0]
        total_ls = []
        random.seed(str(123 + epoch))
        user_idxs_tra = np.arange(user_num, dtype=np.int32)
        random.shuffle(user_idxs_tra)
        if 0 == p['gru']:
            for uidx in user_idxs_tra:
                tra = tra_buys_masks[uidx]
                neg = tra_buys_neg_masks[uidx]
                for i in np.arange(sum(tra_masks[uidx])):
                    loss += model.train(uidx, [tra[i], neg[i]])
        elif 1 == p['gru'] or 3 == p['gru']:
            for uidx in user_idxs_tra:
                loss += model.train(uidx)
        else:
            for uidx in user_idxs_tra:
                los, a, b, ls = model.train(uidx)
                loss += los
                a_b = [a, b]
                a_b.extend(ls)
                total_ls.append(a_b)
        rnn_l2_sqr = model.l2.eval()

        def cut2(x):
            return '%0.2f' % x

        print('\t\tsum_loss = {val} = {v1} + {v2}'.format(val=loss + rnn_l2_sqr, v1=loss, v2=rnn_l2_sqr))
        losses.append('{v1}'.format(v1=int(loss + rnn_l2_sqr)))
        # ls = model.loss_weight
        print('\t\tloss_weight = {v1}, {v2}'.format(v1=ls[0], v2=ls[1]))
        t1 = time.time()
        times0.append(t1 - t0)

        # ----------------------------------------------------------------------------------------------------------
        print("\tPredicting ...")
        if 0 == p['gru']:
            model.update_trained_items()
            model.update_trained_users()
        elif 1 == p['gru']:
            model.update_trained_items()
            all_hus = []
            for start_end in starts_ends_tes:
                sub_all_hus = model.predict(start_end)
                all_hus.extend(sub_all_hus)
            model.update_trained_users(all_hus)
        elif 2 == p['gru']:
            model.update_trained_items()
            model.update_trained_dists()
            all_hus = []
            all_sus = []
            for start_end in starts_ends_tes:
                [sub_all_hus, sub_all_sus] = model.predict(start_end)
                all_hus.extend(sub_all_hus)
                all_sus.extend(sub_all_sus)
            probs = fun_acquire_prob(all_sus, ulptai, dist_num)
            model.update_trained_users(all_hus)
            model.update_prob(probs)
        else:
            model.update_trained_items()
            model.update_trained_dists()
            all_hus = []
            for start_end in starts_ends_tes:
                sub_all_hus = model.predict(start_end)
                all_hus.extend(sub_all_hus)
            model.update_trained_users(all_hus)
        t2 = time.time()
        times1.append(t2 - t1)

        fun_predict_auc_recall_map_ndcg(
            p, model, best, epoch, starts_ends_auc, starts_ends_tes, tes_buys_masks, tes_masks)
        best.fun_print_best(epoch)
        t3 = time.time()
        times2.append(t3-t2)
        print('\tavg. time (train, user, test): %0.0fs,' % np.average(times0),
              '%0.0fs,' % np.average(times1), '%0.0fs' % np.average(times2),
              '| alpha, lam: {v1}'.format(v1=', '.join([str(lam) for lam in [p['alpha'], p['lambda']]])),
              '| model: {v1}'.format(v1=model_name))

        # ----------------------------------------------------------------------------------------------------------
        if epoch == p['epochs'] - 1:
            print("\tBest and losses saving ...")
            path = os.path.join(os.path.split(__file__)[0], '..', 'Results_best_and_losses', PATH.split('/')[-2])
            fun_save_best_and_losses(path, model_name, epoch, p, best, losses)
            if 2 == p['gru']:
                size = p['latent_size']
                fil_name = 'size' + str(size) + 'UD' + str(p['UD']) + 'dd' + str(p['dd']) + 'loss.txt'
                fil = os.path.join(path, fil_name)
                np.savetxt(fil, total_ls)

        if 2 == p['gru'] and epoch % p['save_per_epoch'] == 0 and epoch != 0:
            m_path = './model/' + p['dataset'] + '/' + model_name + '_size' + \
                     str(p['latent_size']) + '_UD' + str(p['UD']) + '_dd' + str(p['dd']) + '_epoch' + str(epoch)
            with open(m_path, 'wb') as file:
                save_model = [model.loss_weight.get_value(), model.wd.get_value(), model.lt.get_value(), model.di.get_value(),
                              model.ui.get_value(), model.wh.get_value(), model.bi.get_value(), model.vs.get_value(),
                              model.bs.get_value()]
                cPickle.dump(save_model, file, protocol=cPickle.HIGHEST_PROTOCOL)

    for i in p.items():
        print(i)
    print('\t the current Class name is: {val}'.format(val=model_name))

def cal_s(pas):
    p = pas.p
    model, model_name = pas.build_model_one_by_one(flag=p['gru'])
    _, starts_ends_tes = pas.compute_start_end(flag='test')
    user_num, dist_num = pas.user_num, pas.dist_num
    ulptai = pas.ulptai

    del pas

    m_path = './model/' + p['dataset'] + '/' + model_name + '_size' +\
             str(p['latent_size']) + '_UD' + str(p['UD']) + '_dd' + str(p['dd']) + '_epoch' + str(p['load_epoch'])
    print('Loading model ...')
    with open(m_path, 'rb') as f:
        loaded_objects = cPickle.load(f)
    model.load_params(loaded_objects)

    print("\tPredicting ...")
    model.update_trained_items()
    model.update_trained_dists()
    all_sus = []
    for start_end in starts_ends_tes:
        [_, sub_all_sus] = model.predict(start_end)
        all_sus.extend(sub_all_sus)

    np.save('./Lmdd/' + p['dataset'] + '_size' + str(p['latent_size']) + '_UD' + str(p['UD']) + '_dd' + str(p['dd']) + '_epoch' + str(p['load_epoch']) + 'last1',
            all_sus)

@exe_time
def main():
    pas = Params()
    if pas.p['mode'] == 's':
        cal_s(pas)
    else:
        train_valid_or_test(pas)

if '__main__' == __name__:
    main()


text:
import numpy

import theano
import theano.tensor as T

from logistic_sgd import LogisticRegression
from mlp import HiddenLayer
import json
from cnn import LeNetConvPoolLayer

with open("cnn.json") as f:
    network = json.loads(f.read())

batch_size = 1
rng = numpy.random.RandomState(23455)
x = T.matrix('x')
y = T.ivector('y')

layer0_input = x.reshape((batch_size, 1, 28, 28))

input = layer0_input
layers = []
network['layers'][0]['image_shape'] = (1, 1, 28, 28)
network['layers'][1]['image_shape'] = (1, 20, 12, 12)
for layer in network['layers']:
    if layer['type'] == 'convolutional':
        layers.append(LeNetConvPoolLayer(
            rng,
            input=input,
            image_shape=layer['image_shape'],
            filter_shape=layer['filter_shape'],
            W=theano.shared(numpy.asarray(layer['W'], dtype=theano.config.floatX), borrow=True),
            b=theano.shared(numpy.asarray(layer['b'], dtype=theano.config.floatX), borrow=True),
            poolsize=layer['poolsize'])
        )
        input = layers[-1].output
    elif layer['type'] == 'hidden':
        W = numpy.asarray(layer['W'], dtype=theano.config.floatX)
        b = numpy.asarray(layer['b'], dtype=theano.config.floatX)
        layers.append(HiddenLayer(
                rng,
                input=input.flatten(2),
                n_in=W.shape[0],
                n_out=W.shape[1],
                W=theano.shared(W, borrow=True),
                b=theano.shared(b, borrow=True),
                activation=T.tanh)
        )
        input = layers[-1].output
    elif layer['type'] == 'softmax':
        W = numpy.asarray(layer['W'], dtype=theano.config.floatX)
        b = numpy.asarray(layer['b'], dtype=theano.config.floatX)
        layers.append(LogisticRegression(
            input=input,
            n_in=W.shape[0],
            n_out=W.shape[1],
            W=theano.shared(W, borrow=True),
            b=theano.shared(b, borrow=True))
        )

index = T.lscalar()  # index to a [mini]batch

def classify(example):
    train_set_x = theano.shared(numpy.asarray([example.flatten()], dtype=theano.config.floatX), borrow=True)
    fun = theano.function([index], [layers[0].output, layers[1].output, layers[2].output, layers[3].p_y_given_x, layers[3].y_pred],
            givens={
                x: train_set_x[index * batch_size: (index + 1) * batch_size],
            }, on_unused_input='ignore')

    return fun(0)[-1][0]

max_stars_repo_name: piotrdubiel/scribeserver

max_issues_repo_name: piotrdubiel/scribeserver

max_forks_repo_name: piotrdubiel/scribeserver

reason: import numpy,import theano


text:
import os
import gym
import numpy as np
import torch
import logging
import agent
import utils
import constants
import nussl
import audio_processing
import agent
from datasets import BufferData, RLDataset
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import math

class RnnAgent(agent.AgentBase):
    def __init__(self, env_config, dataset_config, rnn_config=None, stft_config=None,
                 verbose=False, autoclip_percentile=10, learning_rate=.001, pretrained=False):
        """
        Args:
            env_config (dict): Dictionary containing the audio environment config
            dataset_config (dict): Dictionary consisting of dataset related parameters. List of parameters
            'batch_size' : Denotes the batch size of the samples
            'num_updates': Amount of iterations we run the training for in each pass.
             Ex - If num_updates = 5 and batch_size = 25, then we run the update process 5 times where in each run we
             sample 25 data points.
             'sampler': The sampler to use. Ex - Weighted Sampler, Batch sampler etc

            rnn_config (dict):  Dictionary containing the parameters for the model
            stft_config (dict):  Dictionary containing the parameters for STFT
        """

        # Select device
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        print('DEVICE:', self.device)
        # Use default config if configs are not provided by user
        if rnn_config is None:
            self.rnn_config = nussl.ml.networks.builders.build_recurrent_end_to_end(
                bidirectional=True, dropout=0.3, filter_length=256, hidden_size=300,
                hop_length=64, mask_activation=['sigmoid'], mask_complex=False, mix_key='mix_audio',
                normalization_class='BatchNorm', num_audio_channels=1, num_filters=256,
                num_layers=2, num_sources=2, rnn_type='lstm', trainable=False, window_type='sqrt_hann'
            )
        else:
            self.rnn_config = nussl.ml.networks.builders.build_recurrent_end_to_end(**rnn_config)

        if stft_config is None:
            self.stft_diff = nussl.ml.networks.modules.STFT(
                hop_length=128, filter_length=512,
                direction='transform', num_filters=512
            )
        else:
            self.stft_diff = nussl.ml.networks.modules.STFT(**stft_config)

        # Initialize the Agent Base class
        super().__init__(**env_config)

        # Uncomment this to find backprop errors
        # torch.autograd.set_detect_anomaly(True)

        # Initialize the rnn model
        self.rnn_model = RnnSeparator(self.rnn_config).to(self.device)
        self.rnn_model_stable = RnnSeparator(self.rnn_config).to(self.device)

        # Load pretrained model
        if pretrained:
            model_dict = torch.load(constants.PRETRAIN_PATH)
            self.rnn_model.rnn_model.load_state_dict(model_dict)

        # Initialize dataset related parameters
        self.bs = dataset_config['batch_size']
        self.num_updates = dataset_config['num_updates']
        self.dynamic_dataset = RLDataset(buffer=self.dataset, sample_size=self.bs)
        self.dataloader = torch.utils.data.DataLoader(self.dynamic_dataset, batch_size=self.bs)

        # Initialize network layers for DQN network
        filter_length = (stft_config['num_filters'] // 2 + 1) * 2 if stft_config is not None else 514
        total_actions = self.env.action_space.n
        network_params = {'filter_length': filter_length, 'total_actions': total_actions, 'stft_diff': self.stft_diff}
        self.q_net = DQN(network_params).to(self.device)
        self.q_net_stable = DQN(network_params).to(self.device)  # Fixed Q net

        # Tell optimizer which parameters to learn
        if pretrained:
            # Freezing the rnn model weights, only optimizing Q-net
            params = self.q_net.parameters()
        else:
            params = list(self.rnn_model.parameters()) + list(self.q_net.parameters())
        self.optimizer = optim.Adam(params, lr=learning_rate)

        # Folder path where the model will be saved
        self.SAVE_PATH = dataset_config['save_path']
        self.grad_norms = None
        self.percentile = autoclip_percentile

    def update(self):
        """
        Runs the main training pipeline. Sends mix to RNN separator, then to the DQN.
        Calculates the q-values and the expected q-values, comparing them to get the loss and then
        computes the gradient w.r.t to the entire differentiable pipeline.
        """
        # Run the update only if samples >= batch_size
        if len(self.dataset.items) < self.bs:
            return

        for index, data in enumerate(self.dataloader):
            if index > self.num_updates:
                break

            # Get the total number of time steps
            total_time_steps = data['mix_audio_prev_state'].shape[-1]

            # Reshape the mixture to pass through the separation model (Convert dual channels into one)
            # Also, rename the state to work on to mix_audio so that it can pass through remaining nussl architecture
            # Move to GPU
            data['mix_audio'] = data['mix_audio_prev_state'].float().view(
                -1, 1, total_time_steps).to(self.device)
            data['action'] = data['action'].to(self.device)
            data['reward'] = data['reward'].to(self.device)
            agent_info = data['agent_info'].to(self.device)

            # Get the separated sources by running through RNN separation model
            output = self.rnn_model(data)
            output['mix_audio'] = data['mix_audio']

            # Pass then through the DQN model to get q-values
            q_values = self.q_net(output, agent_info, total_time_steps)
            q_values = q_values.gather(1, data['action'])

            with torch.no_grad():
                # Now, get q-values for the next-state
                # Get the total number of time steps
                total_time_steps = data['mix_audio_new_state'].shape[-1]

                # Reshape the mixture to pass through the separation model (Convert dual channels into one)
                data['mix_audio'] = data['mix_audio_new_state'].float().view(
                    -1, 1, total_time_steps).to(self.device)
                stable_output = self.rnn_model_stable(data)
                stable_output['mix_audio'] = data['mix_audio']
                q_values_next = self.q_net_stable(stable_output, agent_info, total_time_steps).max(1)[0].unsqueeze(-1)

            expected_q_values = data['reward'] + self.gamma * q_values_next

            # Calculate loss
            loss = F.l1_loss(q_values, expected_q_values)
            self.losses.append(loss)
            self.writer.add_scalar('Loss/train', loss, len(self.losses))

            # Optimize the model with backprop
            self.optimizer.zero_grad()
            loss.backward()

            # Applying AutoClip
            self.grad_norms = utils.autoclip(self.rnn_model, self.percentile, self.grad_norms)

            # Stepping optimizer
            self.optimizer.step()

    def choose_action(self):
        """
        Runs a forward pass though the RNN separator and then the Q-network. An action is choosen
        by taking the argmax of the output vector of the network, where the output is a
        probability distribution over the action space (via softmax).

        Returns:
            action (int): the argmax of the q-values vector
        """
        with torch.no_grad():
            # Get the latest state from the buffer
            data = self.dataset[self.dataset.last_ptr]

            # Perform the forward pass (RNN separator => DQN)
            total_time_steps = data['mix_audio_new_state'].shape[-1]
            data['mix_audio'] = data['mix_audio_new_state'].float().view(
                -1, 1, total_time_steps).to(self.device)
            output = self.rnn_model(data)
            output['mix_audio'] = data['mix_audio']
            agent_info = data['agent_info'].to(self.device)

            # action = argmax(q-values)
            q_values = self.q_net(output, agent_info, total_time_steps)
            action = q_values.max(1)[1].unsqueeze(-1)
            action = action[0].item()

            action = int(action)

            return action

    def update_stable_networks(self):
        self.rnn_model_stable.load_state_dict(self.rnn_model.state_dict())
        self.q_net_stable.load_state_dict(self.q_net.state_dict())

    def save_model(self, name):
        """
        Args:
            name (str): Name contains the episode information (To give saved models unique names)
        """
        # Save the parameters for rnn model and q net separately
        metadata = {
            'sample_rate': 8000
        }
        self.rnn_model.rnn_model.save(os.path.join(self.SAVE_PATH, 'sp_' + name), metadata)
        torch.save(self.rnn_model.state_dict(), os.path.join(self.SAVE_PATH, 'rnn_' + name))
        torch.save(self.q_net.state_dict(), os.path.join(self.SAVE_PATH, 'qnet_' + name))

class RnnSeparator(nn.Module):
    def __init__(self, rnn_config, verbose=False):
        super(RnnSeparator, self).__init__()
        self.rnn_model = nussl.ml.SeparationModel(rnn_config, verbose=verbose)

    def forward(self, x):
        return self.rnn_model(x)

class DQN(nn.Module):
    def __init__(self, network_params):
        """
        The main DQN class, which takes the output of the RNN separator and input and
        returns q-values (prob dist of the action space)

        Args:
            network_params (dict): Dict of network parameters

        Returns:
            q_values (torch.Tensor): q-values
        """
        super(DQN, self).__init__()

        self.stft_diff = network_params['stft_diff']
        self.fc = nn.Linear(9, network_params['total_actions'])
        self.prelu = nn.PReLU()

    def forward(self, output, agent_info, total_time_steps):
        # Reshape the output again to get dual channels
        # Perform short time fourier transform of this output
        _, _, nt = output['mix_audio'].shape
        output['mix_audio'] = output['mix_audio'].reshape(-1, 2, nt)
        stft_data = self.stft_diff(output['mix_audio'], direction='transform')

        # Get the IPD and ILD features from the stft data
        _, nt, nf, _, ns = output['mask'].shape
        output['mask'] = output['mask'].view(-1, 2, nt, nf, ns)
        output['mask'] = output['mask'].max(dim=1)[0]
        ipd, ild, vol = audio_processing.ipd_ild_features(stft_data)

        ipd = ipd.unsqueeze(-1)
        ild = ild.unsqueeze(-1)
        vol = vol.unsqueeze(-1)

        ipd_means = (output['mask'] * ipd).mean(dim=[1, 2]).unsqueeze(1)
        ild_means = (output['mask'] * ild).mean(dim=[1, 2]).unsqueeze(1)
        vol_means = (output['mask'] * vol).mean(dim=[1, 2]).unsqueeze(1)
        X = torch.cat([ipd_means, ild_means, vol_means], dim=1)
        X = X.reshape(X.shape[0], -1)
        agent_info = agent_info.view(-1, 3)
        X = torch.cat((X, agent_info), dim=1)
        X = self.prelu(self.fc(X))
        q_values = F.softmax(X, dim=1)

        return q_values

    def flatten_features(self, x):
        # Flatten all dimensions except the batch
        size = x.size()[1:]
        num_features = 1
        for dimension in size:
            num_features *= dimension

        return num_features

max_stars_repo_name: pseeth/otoworld

max_issues_repo_name: pseeth/otoworld

max_forks_repo_name: pseeth/otoworld


text:
import copy
import pickle

import numpy as np
import pytest

from garage.envs import GarageEnv
from meta_agents.policies import GaussianMLPPolicy

from tests.fixtures.envs.dummy import DummyBoxEnv

@pytest.mark.parametrize('policy_cls', [GaussianMLPPolicy])
def test_policy_serialization(policy_cls):
    env = GarageEnv(DummyBoxEnv())
    policy = policy_cls(env_spec=env.spec, hidden_sizes=(8, 8))

    round_trip = pickle.loads(pickle.dumps(policy))
    round_trip2 = copy.deepcopy(policy)
    ori_params = policy.parameters()
    round_trip_params = round_trip.parameters()
    round_trip_params2 = round_trip2.parameters()

    for p, p1, p2 in zip(ori_params, round_trip_params, round_trip_params2):
        assert np.array_equal(
            p1.detach().numpy(), p.detach().numpy())
        assert np.array_equal(
            p2.detach().numpy(), p.detach().numpy())


text:
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

from collections import namedtuple
import logging

from iree.jax2.staging_api import *
from iree.jax2.builtins import *

import jax
import jax.numpy as jnp

logging.basicConfig(level=logging.DEBUG)
jax.config.update("jax_enable_mlir", True)

a = jnp.zeros((3, 4), jnp.float32)
b = jnp.zeros((3, 4), jnp.float32)

Params = namedtuple("Params", "a,b")

params = Params(a, b)

class LineModule(StagedModule):

  # TODO: Add trivial initialization support.
  _params = export_global(params)

  @export_kernel
  def linear(_, m, x, b):
    return m * x + b

  @export_traced_proc(signature=[a])
  def run(mdl, multiplier):
    result = mdl.linear(multiplier, mdl._params.a, mdl._params.b)
    store_global(mdl._params.a, result)

  @export_traced_proc
  def get_params(mdl):
    return mdl._params

print(get_mlir_module(LineModule))

lm = LineModule()

max_stars_repo_name: powderluv/iree-samples

max_issues_repo_name: powderluv/iree-samples

max_forks_repo_name: powderluv/iree-samples

reason: import jax


text:
'''
Date: 2022-01-25 17:20:12
LastEditors: cvhadessun
LastEditTime: 2022-01-25 18:53:59
FilePath: /PG-engine/src/tools/image_transform.py
'''

import OpenEXR
import Imath
import array
import numpy as np
import cv2

FLOAT = Imath.PixelType(Imath.PixelType.FLOAT)
# depth
path = '/home/swh/workspace/PG-engine/output/debug/experimental/Camera.009/00000_normal/Image0001.exr'
exr_file = OpenEXR.InputFile(path)

def exr_2_png_depth(exr_path,save_path,resx=640,resy=480):
    exr_file = OpenEXR.InputFile(exr_path)
    r = array.array("f", exr_file.channel('R', FLOAT)).tolist()
    r_img = np.array(r).reshape(resy,resx)
    img = np.zeros([resy,resx,3]).astype(np.uint16)
    img = np.zeros([resy,resx,3]).astype(np.uint16)
    img[:,:,0] = r_img
    img[:,:,1] = r_img
    img[:,:,2] = r_img
    cv2.imwrite(save_path,img,[int(cv2.IMWRITE_PNG_COMPRESSION), 0])
    # ori_img = cv2.imread(save_path,-1)

def get_normal_from_exr(exr_path,resx=640,resy=480):
    exr_file = OpenEXR.InputFile(exr_path)
    r = array.array("f", exr_file.channel('R', FLOAT)).tolist()
    g = array.array("f", exr_file.channel('G', FLOAT)).tolist()
    b = array.array("f", exr_file.channel('B', FLOAT)).tolist()
    r = np.array(r).reshape(resy,resx)
    g = np.array(g).reshape(resy,resx)
    b = np.array(b).reshape(resy,resx)
    return [r,g,b]

max_stars_repo_name: CvHadesSun/PG-engine

max_issues_repo_name: CvHadesSun/PG-engine

max_forks_repo_name: CvHadesSun/PG-engine


text:
"""
Download, transform and simulate various binary datasets.
"""

# Author: Georgios Douzas <gdouzas@icloud.com>
#         Joao Fonseca <jpfonseca@novaims.unl.pt>
# License: MIT

from re import sub
from collections import Counter
from itertools import product
from urllib.parse import urljoin
from string import ascii_lowercase
from zipfile import ZipFile
from io import BytesIO, StringIO

import requests
import numpy as np
import pandas as pd
from sklearn.utils import check_X_y
from imblearn.datasets import make_imbalance

from .base import Datasets, FETCH_URLS, RANDOM_STATE

class ImbalancedBinaryDatasets(Datasets):
    """Class to download, transform and save binary class imbalanced
    datasets."""

    MULTIPLICATION_FACTORS = [2, 3]

    @staticmethod
    def _calculate_ratio(multiplication_factor, y):
        """Calculate ratio based on IRs multiplication factor."""
        ratio = Counter(y).copy()
        ratio[1] = int(ratio[1] / multiplication_factor)
        return ratio

    def _make_imbalance(self, data, multiplication_factor):
        """Undersample the minority class."""
        X_columns = [col for col in data.columns if col != "target"]
        X, y = check_X_y(data.loc[:, X_columns], data.target)
        if multiplication_factor > 1.0:
            sampling_strategy = self._calculate_ratio(multiplication_factor, y)
            X, y = make_imbalance(
                X, y, sampling_strategy=sampling_strategy, random_state=RANDOM_STATE
            )
        data = pd.DataFrame(np.column_stack((X, y)))
        data.iloc[:, -1] = data.iloc[:, -1].astype(int)
        return data

    def download(self):
        """Download the datasets and append undersampled versions of them."""
        super(ImbalancedBinaryDatasets, self).download()
        undersampled_datasets = []
        for (name, data), factor in list(
            product(self.content_, self.MULTIPLICATION_FACTORS)
        ):
            ratio = self._calculate_ratio(factor, data.target)
            if ratio[1] >= 15:
                data = self._make_imbalance(data, factor)
                undersampled_datasets.append((f"{name} ({factor})", data))
        self.content_ += undersampled_datasets
        return self

    def fetch_breast_tissue(self):
        """Download and transform the Breast Tissue Data Set.
        The minority class is identified as the `car` and `fad`
        labels and the majority class as the rest of the labels.

        http://archive.ics.uci.edu/ml/datasets/breast+tissue
        """
        data = pd.read_excel(FETCH_URLS["breast_tissue"], sheet_name="Data")
        data = data.drop(columns="Case #").rename(columns={"Class": "target"})
        data["target"] = data["target"].isin(["car", "fad"]).astype(int)
        return data

    def fetch_ecoli(self):
        """Download and transform the Ecoli Data Set.
        The minority class is identified as the `pp` label
        and the majority class as the rest of the labels.

        https://archive.ics.uci.edu/ml/datasets/ecoli
        """
        data = pd.read_csv(FETCH_URLS["ecoli"], header=None, delim_whitespace=True)
        data = data.drop(columns=0).rename(columns={8: "target"})
        data["target"] = data["target"].isin(["pp"]).astype(int)
        return data

    def fetch_eucalyptus(self):
        """Download and transform the Eucalyptus Data Set.
        The minority class is identified as the `best` label
        and the majority class as the rest of the labels.

        https://www.openml.org/d/188
        """
        data = pd.read_csv(FETCH_URLS["eucalyptus"])
        data = data.iloc[:, -9:].rename(columns={"Utility": "target"})
        data = data[data != "?"].dropna()
        data["target"] = data["target"].isin(["best"]).astype(int)
        return data

    def fetch_glass(self):
        """Download and transform the Glass Identification Data Set.
        The minority class is identified as the `1` label
        and the majority class as the rest of the labels.

        https://archive.ics.uci.edu/ml/datasets/glass+identification
        """
        data = pd.read_csv(FETCH_URLS["glass"], header=None)
        data = data.drop(columns=0).rename(columns={10: "target"})
        data["target"] = data["target"].isin([1]).astype(int)
        return data

    def fetch_haberman(self):
        """Download and transform the Haberman's Survival Data Set.
        The minority class is identified as the `1` label
        and the majority class as the `0` label.

        https://archive.ics.uci.edu/ml/datasets/Haberman's+Survival
        """
        data = pd.read_csv(FETCH_URLS["haberman"], header=None)
        data.rename(columns={3: "target"}, inplace=True)
        data["target"] = data["target"].isin([2]).astype(int)
        return data

    def fetch_heart(self):
        """Download and transform the Heart Data Set.
        The minority class is identified as the `2` label
        and the majority class as the `1` label.

        http://archive.ics.uci.edu/ml/datasets/statlog+(heart)
        """
        data = pd.read_csv(FETCH_URLS["heart"], header=None, delim_whitespace=True)
        data.rename(columns={13: "target"}, inplace=True)
        data["target"] = data["target"].isin([2]).astype(int)
        return data

    def fetch_iris(self):
        """Download and transform the Iris Data Set.
        The minority class is identified as the `1` label
        and the majority class as the rest of the labels.

        https://archive.ics.uci.edu/ml/datasets/iris
        """
        data = pd.read_csv(FETCH_URLS["iris"], header=None)
        data.rename(columns={4: "target"}, inplace=True)
        data["target"] = data["target"].isin(["Iris-setosa"]).astype(int)
        return data

    def fetch_libras(self):
        """Download and transform the Libras Movement Data Set.
        The minority class is identified as the `1` label
        and the majority class as the rest of the labels.

        https://archive.ics.uci.edu/ml/datasets/Libras+Movement
        """
        data = pd.read_csv(FETCH_URLS["libras"], header=None)
        data.rename(columns={90: "target"}, inplace=True)
        data["target"] = data["target"].isin([1]).astype(int)
        return data

    def fetch_liver(self):
        """Download and transform the Liver Disorders Data Set.
        The minority class is identified as the `1` label
        and the majority class as the '2' label.

        https://archive.ics.uci.edu/ml/datasets/liver+disorders
        """
        data = pd.read_csv(FETCH_URLS["liver"], header=None)
        data.rename(columns={6: "target"}, inplace=True)
        data["target"] = data["target"].isin([1]).astype(int)
        return data

    def fetch_pima(self):
        """Download and transform the Pima Indians Diabetes Data Set.
        The minority class is identified as the `1` label
        and the majority class as the '0' label.

        https://www.kaggle.com/uciml/pima-indians-diabetes-database
        """
        data = pd.read_csv(FETCH_URLS["pima"], header=None, skiprows=9)
        data.rename(columns={8: "target"}, inplace=True)
        return data

    def fetch_vehicle(self):
        """Download and transform the Vehicle Silhouettes Data Set.
        The minority class is identified as the `1` label
        and the majority class as the rest of the labels.

        https://archive.ics.uci.edu/ml/datasets/Statlog+(Vehicle+Silhouettes)
        """
        data = pd.DataFrame()
        for letter in ascii_lowercase[0:9]:
            partial_data = pd.read_csv(
                urljoin(FETCH_URLS["vehicle"], "xa%s.dat" % letter),
                header=None,
                delim_whitespace=True,
            )
            partial_data = partial_data.rename(columns={18: "target"})
            partial_data["target"] = partial_data["target"].isin(["van"]).astype(int)
            data = data.append(partial_data)
        return data

    def fetch_wine(self):
        """Download and transform the Wine Data Set.
        The minority class is identified as the `2` label
        and the majority class as the rest of the labels.

        https://archive.ics.uci.edu/ml/datasets/wine
        """
        data = pd.read_csv(FETCH_URLS["wine"], header=None)
        data.rename(columns={0: "target"}, inplace=True)
        data["target"] = data["target"].isin([2]).astype(int)
        return data

    def fetch_new_thyroid_1(self):
        """Download and transform the Thyroid Disease Data Set.
        The minority class is identified as the `positive`
        label and the majority class as the `negative` label.

        .. note:: The positive class was originally label 2.

        https://archive.ics.uci.edu/ml/datasets/Thyroid+Disease
        """
        data = pd.read_csv(
            FETCH_URLS["new_thyroid"],
            header=None,
        )
        data.rename(columns={0: "target"}, inplace=True)
        data["target"] = (data["target"] == 2).astype(int)
        return data

    def fetch_new_thyroid_2(self):
        """Download and transform the Thyroid Disease Data Set.
        The minority class is identified as the `positive`
        label and the majority class as the `negative` label.

        .. note:: The positive class was originally label 3.

        https://archive.ics.uci.edu/ml/datasets/Thyroid+Disease
        """
        data = pd.read_csv(
            FETCH_URLS["new_thyroid"],
            header=None,
        )
        data.rename(columns={0: "target"}, inplace=True)
        data["target"] = (data["target"] == 3).astype(int)
        return data

    def fetch_cleveland(self):
        """Download and transform the Heart Disease Cleveland Data Set.
        The minority class is identified as the `positive` label and
        the majority class as the `negative` label.

        https://archive.ics.uci.edu/ml/datasets/heart+disease
        """
        data = pd.read_csv(FETCH_URLS["cleveland"], header=None)
        data.rename(columns={13: "target"}, inplace=True)
        data["target"] = (data["target"] == 1).astype(int)
        return data

    def fetch_dermatology(self):
        """Download and transform the Dermatology Data Set.
        The minority class is identified as the `positive` label and
        the majority class as the `negative` label.

        https://archive.ics.uci.edu/ml/datasets/Dermatology
        """
        data = pd.read_csv(FETCH_URLS["dermatology"], header=None)
        data.rename(columns={34: "target"}, inplace=True)
        data.drop(columns=33, inplace=True)
        data["target"] = (data.target == 6).astype(int)
        return data

    def fetch_led(self):
        """Download and transform the LED Display Domain Data Set.
        The minority class is identified as the `positive` label and
        the majority class as the `negative` label.

        https://www.openml.org/d/40496
        """
        data = pd.read_csv(FETCH_URLS["led"])
        data.rename(columns={"Class": "target"}, inplace=True)
        data["target"] = (data.target == 1).astype(int)
        return data

    def fetch_page_blocks(self):
        """Download and transform the Page Blocks Data Set.
        The minority class is identified as the `positive` label and
        the majority class as the `negative` label.

        https://www.openml.org/d/1021
        """
        data = pd.read_csv(FETCH_URLS["page_blocks"])
        data.rename(columns={"class": "target"}, inplace=True)
        data["target"] = (data.target != 1).astype(int)
        return data

    def fetch_vowel(self):
        """Download and transform the Vowel Recognition Data Set.
        The minority class is identified as the `positive` label and
        the majority class as the `negative` label.

        https://www.openml.org/d/375
        """

        data = pd.read_csv(FETCH_URLS["vowels"])
        data.rename(columns={"speaker": "target"}, inplace=True)
        data.drop(columns=["utterance", "frame"], inplace=True)
        data["target"] = (data["target"] == 1).astype(int)
        return data

    def fetch_yeast(self):
        """Download and transform the Yeast Data Set.
        The minority class is identified as the `positive` label and
        the majority class as the `negative` label.

        https://archive.ics.uci.edu/ml/datasets/Yeast
        """
        data = pd.read_csv(FETCH_URLS["yeast"], header=None)
        data = pd.DataFrame(
            [
                [val for val in row.split(" ") if len(val) != 0]
                for row in data[0].tolist()
            ]
        )
        data.drop(columns=0, inplace=True)
        data.rename(columns={9: "target"}, inplace=True)
        data["target"] = (data["target"] == "MIT").astype(int)
        return data

class BinaryDatasets(Datasets):
    """Class to download, transform and save binary class datasets."""

    def fetch_banknote_authentication(self):
        """Download and transform the Banknote Authentication Data Set.

        https://archive.ics.uci.edu/ml/datasets/banknote+authentication
        """
        data = pd.read_csv(FETCH_URLS["banknote_authentication"], header=None)
        data.rename(columns={4: "target"}, inplace=True)
        return data

    def fetch_arcene(self):
        """Download and transform the Arcene Data Set.

        https://archive.ics.uci.edu/ml/datasets/Arcene
        """
        url = FETCH_URLS["arcene"]
        data, labels = [], []
        for data_type in ("train", "valid"):
            data.append(
                pd.read_csv(
                    urljoin(url, f"ARCENE/arcene_{data_type}.data"),
                    header=None,
                    sep=" ",
                ).drop(columns=list(range(1998, 10001)))
            )
            labels.append(
                pd.read_csv(
                    urljoin(
                        url,
                        ("ARCENE/" if data_type == "train" else "")
                        + f"arcene_{data_type}.labels",
                    ),
                    header=None,
                ).rename(columns={0: "target"})
            )
        data = pd.concat(data, ignore_index=True)
        labels = pd.concat(labels, ignore_index=True)
        data = pd.concat([data, labels], axis=1)
        data["target"] = data["target"].isin([1]).astype(int)
        return data

    def fetch_audit(self):
        """Download and transform the Audit Data Set.

        https://archive.ics.uci.edu/ml/datasets/Audit+Data
        """
        zipped_data = requests.get(FETCH_URLS["audit"]).content
        unzipped_data = (
            ZipFile(BytesIO(zipped_data))
            .read("audit_data/audit_risk.csv")
            .decode("utf-8")
        )
        data = pd.read_csv(StringIO(sub(r"@.+\n+", "", unzipped_data)), engine="python")
        data = (
            data.drop(columns=["LOCATION_ID"])
            .rename(columns={"Risk": "target"})
            .dropna()
        )
        return data

    def fetch_spambase(self):
        """Download and transform the Spambase Data Set.

        https://archive.ics.uci.edu/ml/datasets/Spambase
        """
        data = pd.read_csv(FETCH_URLS["spambase"], header=None)
        data.rename(columns={57: "target"}, inplace=True)
        return data

    def fetch_parkinsons(self):
        """Download and transform the Parkinsons Data Set.

        https://archive.ics.uci.edu/ml/datasets/parkinsons
        """
        data = pd.read_csv(FETCH_URLS["parkinsons"])
        data = pd.concat(
            [
                data.drop(columns=["name", "status"]),
                data[["status"]].rename(columns={"status": "target"}),
            ],
            axis=1,
        )
        data["target"] = data["target"].isin([0]).astype(int)
        return data

    def fetch_ionosphere(self):
        """Download and transform the Ionosphere Data Set.

        https://archive.ics.uci.edu/ml/datasets/ionosphere
        """
        data = pd.read_csv(FETCH_URLS["ionosphere"], header=None)
        data = data.drop(columns=[0, 1]).rename(columns={34: "target"})
        data["target"] = data["target"].isin(["b"]).astype(int)
        return data

    def fetch_breast_cancer(self):
        """Download and transform the Breast Cancer Wisconsin Data Set.

        https://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Wisconsin+(Diagnostic)
        """
        data = pd.read_csv(FETCH_URLS["breast_cancer"], header=None)
        data = pd.concat(
            [data.drop(columns=[0, 1]), data[[1]].rename(columns={1: "target"})], axis=1
        )
        data["target"] = data["target"].isin(["M"]).astype(int)
        return data

max_stars_repo_name: joaopfonseca/ml-research

max_issues_repo_name: joaopfonseca/ml-research

max_forks_repo_name: joaopfonseca/ml-research


text:
from collections import OrderedDict

import numpy as np
import torch
import torch.nn as nn
import torch.distributions

# Categorical
FixedCategorical = torch.distributions.Categorical

old_sample = FixedCategorical.sample
FixedCategorical.sample = lambda self: old_sample(self).unsqueeze(-1)

log_prob_cat = FixedCategorical.log_prob
#FixedCategorical.log_probs = lambda self, actions: log_prob_cat(self, actions.squeeze(-1)).view(actions.size(0), -1).sum(-1).unsqueeze(-1)
FixedCategorical.log_probs = lambda self, actions: log_prob_cat(self, actions.squeeze(-1)).unsqueeze(-1)

categorical_entropy = FixedCategorical.entropy
FixedCategorical.entropy = lambda self: categorical_entropy(self) * 10.0 # scaling

FixedCategorical.mode = lambda self: self.probs.argmax(dim=-1, keepdim=True)

# Normal
FixedNormal = torch.distributions.Normal

normal_init = FixedNormal.__init__
FixedNormal.__init__ = lambda self, mean, std: normal_init(self, mean.double(), std.double())

log_prob_normal = FixedNormal.log_prob
FixedNormal.log_probs = lambda self, actions: log_prob_normal(self, actions.double()).sum(-1, keepdim=True).float()

normal_entropy = FixedNormal.entropy
FixedNormal.entropy = lambda self: normal_entropy(self).sum(-1).float()

FixedNormal.mode = lambda self: self.mean.float()

normal_sample = FixedNormal.sample
FixedNormal.sample = lambda self: normal_sample(self).float()

normal_rsample = FixedNormal.rsample
FixedNormal.rsample = lambda self: normal_rsample(self).float()

def init(module, weight_init, bias_init, gain=1):
    weight_init(module.weight.data, gain=gain)
    bias_init(module.bias.data)
    return module

class AddBias(nn.Module):
    def __init__(self, bias):
        super().__init__()
        self._bias = nn.Parameter(bias.unsqueeze(1))

    def forward(self, x):
        if x.dim() == 2:
            bias = self._bias.t().view(1, -1)
        else:
            bias = self._bias.t().view(1, -1, 1, 1)
        return x + bias

class Categorical(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, x):
        return FixedCategorical(logits=x)

class DiagGaussian(nn.Module):
    def __init__(self, config):
        super().__init__()
        self.logstd = AddBias(torch.zeros(config.action_size))
        self.config = config

    def forward(self, x):
        zeros = torch.zeros(x.size()).to(self.config.device)
        logstd = self.logstd(zeros)
        return FixedNormal(x, logstd.exp())

class MixedDistribution(nn.Module):
    def __init__(self, distributions):
        super().__init__()
        assert isinstance(distributions, OrderedDict)
        self.distributions = distributions

    def mode(self):
        return OrderedDict(
            [(k, dist.mode()) for k, dist in self.distributions.items()]
        )

    def sample(self):
        return OrderedDict(
            [(k, dist.sample()) for k, dist in self.distributions.items()]
        )

    def rsample(self):
        return OrderedDict(
            [(k, dist.rsample()) for k, dist in self.distributions.items()]
        )

    def log_probs(self, x):
        assert isinstance(x, dict)
        return OrderedDict(
            [(k, dist.log_probs(x[k])) for k, dist in self.distributions.items()]
        )

    def entropy(self):
        return sum([dist.entropy() for dist in self.distributions.values()])

max_stars_repo_name: snasiriany/furniture

max_issues_repo_name: snasiriany/furniture

max_forks_repo_name: snasiriany/furniture


text:
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import numpy as np
from numpy import random as rd
import pandas as pd

def init_ppl(Tot_nbr_ppl,Init_prob_sick=0.995):
    """
    Initializes the df of ppl in the simulation.
    if 'enfermo' == 1 -> means true -> it's a sick person

    #Args:: Tot_nbr_ppl, Init_prob_sick (default = 0.995 == 99,5% not sick)

    #Returns:: DataFrame of people
    """
    ppl = [rd.random(size=4) for j in range(Tot_nbr_ppl)]
    ppl = pd.DataFrame(ppl,columns = ['x','y','enfermo','prob_contagio'])
    ppl.loc[:,'enfermo'] = ppl.loc[:,'enfermo'].apply(
            lambda x:1 if x > Init_prob_sick  else 0
            )

    return ppl

def update_vel(Tot_nbr_ppl):
    """
    Compute the velocity of each person in the simulation at each time and returns a generator
    Note: velocity df has to have the SAME dimensions as ppl df!

    #Args::Tot_nbr_ppl

    #Returns:: a velocity generator object.
    """
    vel = [[rd.random(), rd.random(), 0, 0] for t in range(Tot_nbr_ppl)]
    vel = pd.DataFrame(vel, columns=['x', 'y', 'enfermo','prob_contagio'])
    vel.loc[:,['x','y']] = vel.loc[:,['x','y']] - 0.5

    yield vel

def walk(Ppl,Tot_nbr_ppl,Step_vel):
    """
    Function to update the position of each person acordingly to a step velocity in a Random Walk

    #Args::
        Ppl: the DataFrame that holds positions
        Tot_nbr_ppl: total amount of people in simulation
        Step_vel: step to update the velocity of each person

    #Returns:: Ppl generator object
    """
    Vel = next(update_vel(Tot_nbr_ppl))
    # --Update people position
    Ppl = Ppl + Step_vel*Vel
    # --Set periodic boundary conditions
    Ppl[["x", 'y']] = Ppl[["x", 'y']] % 1

    yield Ppl

reason: import numpy,from numpy


text:
###############################################################################
# For more info, see https://hoseinkh.github.io/
###############################################################################
import gym
import os
import sys
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from gym import wrappers
from datetime import datetime
from sklearn.pipeline import FeatureUnion
from sklearn.preprocessing import StandardScaler
from sklearn.kernel_approximation import RBFSampler
from sklearn.linear_model import SGDRegressor
# code we already wrote
# import q_learning
###############################################################################
# from q_learning import plot_cost_to_go, FeatureTransformer, Model, plot_running_avg
# in this simulations we first generate some samples, learns an scaler on them and then ...
# ... perform RL on the new samples as they arrived. Hence the states (observations).
class FeatureTransformer:
  def __init__(self, env, n_components=500):
    # generate states (observations)
    observation_examples = np.array([env.observation_space.sample() for x in range(10000)])
    # define scaler and scale the states (observations) --> mean 0 and variance 1
    scaler = StandardScaler()
    scaler.fit(observation_examples)
    #
    # Now we basically use RBF to for feature generation
    # Each RBFSampler takes each (original) (feature representation) of ...
    # ... a state and converts it to "n_components" new featuers.
    # Hence, after concatenating the new features, we convert each state to ...
    # ... {(# RBF samplers) * n_components} new features.
    #
    # We use RBF kernels with different variances to cover different parts ...
    # ... of the space.
    #
    featurizer = FeatureUnion([
            ("rbf1", RBFSampler(gamma=5.0, n_components=n_components)),
            ("rbf2", RBFSampler(gamma=2.0, n_components=n_components)),
            ("rbf3", RBFSampler(gamma=1.0, n_components=n_components)),
            ("rbf4", RBFSampler(gamma=0.5, n_components=n_components))
            ])
    # For all the generated samples, transform original state representaions ...
    # ... to a new state representation using "featurizer"
    example_features = featurizer.fit_transform(scaler.transform(observation_examples))
    #
    self.dimensions = example_features.shape[1]
    self.scaler = scaler
    self.featurizer = featurizer
  ######################################
  def transform(self, observations):
    #
    scaled_original_state_representation = self.scaler.transform(observations)
    #
    scaled_higher_dimensions_state_representation = self.featurizer.transform(scaled_original_state_representation)
    return scaled_higher_dimensions_state_representation
###############################################################################
# Holds one SGDRegressor for each action
class Model:
  def __init__(self, env, feature_transformer, learning_rate):
    self.env = env
    self.SGDRegressors_approximate_Q_values_for_different_actions = []
    self.feature_transformer = feature_transformer
    for curr_action in range(env.action_space.n):
      # for each action, we fit a model to learn the corresponding Q-values for that action.
      curr_SGDRegressor_approximate_Q_values_for_curr_action = SGDRegressor(learning_rate=learning_rate)
      # we choose initial values high comparing to the typical rewards. This allows us ...
      # ... to use other exploration ideas, such as optimistic initial values, ...
      # ... , besides the epsiolon greedy algorithm!
      initial_optimistic_target_value = 0
      curr_SGDRegressor_approximate_Q_values_for_curr_action.partial_fit(feature_transformer.transform( [env.reset()] ), [initial_optimistic_target_value])
      self.SGDRegressors_approximate_Q_values_for_different_actions.append(curr_SGDRegressor_approximate_Q_values_for_curr_action)
  ######################################
  def predict(self, s):
    X_higher_dimension_representation_of_state_s = self.feature_transformer.transform([s])
    list_Q_values_for_state_s_and_different_actions = []
    for curr_action_index in range(len(self.SGDRegressors_approximate_Q_values_for_different_actions)):
      # for each action find the current estimate of the Q-value for each action ...
      # ... using the SGDRegressors, which are linear functions; however, we use ...
      # ... the extended feature representation instead of the original representation!
      curr_SGDRegressor_approximate_Q_values_for_curr_action = self.SGDRegressors_approximate_Q_values_for_different_actions[curr_action_index]
      curr_reward_for_curr_action_at_state_s = curr_SGDRegressor_approximate_Q_values_for_curr_action.predict(X_higher_dimension_representation_of_state_s)[0]
      list_Q_values_for_state_s_and_different_actions.append(curr_reward_for_curr_action_at_state_s)
    #
    return list_Q_values_for_state_s_and_different_actions
  ######################################
  def update(self, s, curr_action_index, G):
    X_higher_dimension_representation_of_state_s = self.feature_transformer.transform([s])
    # now we are going to update the SGDRegressor approximator for the Q-values for the action "curr_action_index"
    self.SGDRegressors_approximate_Q_values_for_different_actions[curr_action_index].partial_fit(X_higher_dimension_representation_of_state_s, [G])
  ######################################
  def epsilon_greedy_action_selection(self, s, eps):
    # Here we do epsilon greedy policy
    # Important: we can set eps = 0 so that it becomes purely greedy, ...
    # ... and we still achieve exploration! The reason is that ...
    # ... we use "optimistic initial values", and hence exploration ...
    # automatically happens even without doing explicit exploration ...
    # ... in the policy selection!
    #
    if np.random.random() < eps:
      return self.env.action_space.sample()
    else:
      return np.argmax(self.predict(s))
###############################################################################
def plot_running_avg(totalrewards):
  N = len(totalrewards)
  running_avg = np.empty(N)
  for t in range(N):
    running_avg[t] = totalrewards[max(0, t-100):(t+1)].mean()
  plt.plot(running_avg)
  plt.ylabel("Running Average Total Reward")
  plt.xlabel("Episode")
  # plt.show()
  plt.savefig("./figs/Running_Average_Total_Reward.png")
  plt.close()
###############################################################################
# here we plot the negative of the optimal state value functions (i,e, -V*(s))!
# Note that the optimal action values are equal to the negative of the average optimal time ...
# ... that it takes to reach the mountain.
# Hence this plot shows the average optimal time to reach the top of the mountain at each state.
def plot_avg_num_remaining_steps(env, estimator, num_tiles=20):
  x = np.linspace(env.observation_space.low[0], env.observation_space.high[0], num=num_tiles)
  y = np.linspace(env.observation_space.low[1], env.observation_space.high[1], num=num_tiles)
  X, Y = np.meshgrid(x, y)
  # both X and Y will be of shape (num_tiles, num_tiles)
  Z = np.apply_along_axis(lambda _: -1*np.max(estimator.predict(_)), 2, np.dstack([X, Y]))
  # Z will also be of shape (num_tiles, num_tiles)
  #
  fig = plt.figure(figsize=(10, 5))
  ax = fig.add_subplot(111, projection='3d')
  surf = ax.plot_surface(X, Y, Z,
    rstride=1, cstride=1, cmap=matplotlib.cm.coolwarm, vmin=-1.0, vmax=1.0)
  ax.set_xlabel('Position')
  ax.set_ylabel('Velocity')
  ax.set_zlabel('Num steps to reach mountain == -V(s)')
  ax.set_title("Num steps to Reach Mountain Function")
  fig.colorbar(surf)
  fig.savefig("./figs/Num_steps_to_Reach_Mountain.png")
  # plt.show()
  plt.close()
###############################################################################
# class SGDRegressor:
#   def __init__(self, **kwargs):
#     self.w = None
#     self.lr = 1e-2
#
#   def partial_fit(self, X, Y):
#     if self.w is None:
#       D = X.shape[1]
#       self.w = np.random.randn(D) / np.sqrt(D)
#     self.w += self.lr*(Y - X.dot(self.w)).dot(X)
#
#   def predict(self, X):
#     return X.dot(self.w)
##
# # replace SKLearn Regressor
# q_learning.SGDRegressor = SGDRegressor
###############################################################################
def play_one(model, eps, discount_rate, num_steps_for_TD_n=5):
  print("H1")
  observation = env.reset()
  print("observation = {}".format(observation))
  done = False
  totalreward = 0
  rewards = []
  states = []
  actions = []
  iters = 0
  # list of discount_rates for the next hypothetical steps: [discount_rate^0, discount_rate^1, ..., discount_rate^(n-1)]
  discount_rate_multipliers = []
  for i in range(num_steps_for_TD_n):
    discount_rate_multipliers.append(discount_rate**i)
  discount_rate_multipliers = np.array(discount_rate_multipliers)
  #
  # while the episode is not finished or we have limited number of iterations:
  while not done and iters < 10000:
    # randomly pick an actio
    action = model.epsilon_greedy_action_selection(observation, eps)
    # record the observation and the corrresponding action
    states.append(observation)
    actions.append(action)
    #
    prev_observation = observation
    observation, reward, done, info = env.step(action)
    # we record the rewards as we move forward because we need to have ...
    # ... a list of rewards (for the next steps) in order to calculate ...
    # ... the rewards.
    rewards.append(reward)
    #
    # # update the model
    # if we have at least n rewards, then keep updating the states!
    if len(rewards) >= num_steps_for_TD_n:
      #
      # total reward: G  = sum of discounted rewards for the past num_steps_for_TD_n steps (i.e. return_up_to_prediction) ...
      # ... plus the discounted Q-value afterwards i.e. ((discount_rate**num_steps_for_TD_n)*np.max(model.predict(observation)[0]))
      return_up_to_prediction = discount_rate_multipliers.dot(rewards[-num_steps_for_TD_n:])
      G = return_up_to_prediction + (discount_rate**num_steps_for_TD_n)*np.max(model.predict(observation)[0])
      # update the state-action value functions (if tabular) or update Q-value function approximator
      model.update(states[-num_steps_for_TD_n], actions[-num_steps_for_TD_n], G)
    #
    totalreward += reward
    iters += 1
  #
  # the episode is finished; hence empty the cache
  if num_steps_for_TD_n == 1: # i.e. TD(0)
    rewards = []
    states = []
    actions = []
  else: #i.e. keep the last (num_steps_for_TD_n - 1) rewards, states, and actions.
    rewards = rewards[-num_steps_for_TD_n + 1:]
    states = states[-num_steps_for_TD_n + 1:]
    actions = actions[-num_steps_for_TD_n + 1:]
  #
  # check to see if we successfully reached the mountain
  if observation[0] >= 0.5:
    # we reached the mountain
    while len(rewards) > 0:
      # update the Q-values for ALL the (state, action) pairs ...
      # ... in this finished episode
      G = discount_rate_multipliers[:len(rewards)].dot(rewards)
      model.update(states[0], actions[0], G)
      rewards.pop(0)
      states.pop(0)
      actions.pop(0)
  else:
    # we did not reach the goal and the gym stopped the episode.
    while len(rewards) > 0:
      # update the Q-values for ALL the (state, action) pairs ...
      # ... in this terminated episode
      # Because this episode is terminated, we guess the remaining steps would have ...
      # ... been unsuccessfull, and hence would have get -1 reward
      guess_rewards = rewards + [-1 for j in range(num_steps_for_TD_n - len(rewards))]
      G = discount_rate_multipliers.dot(guess_rewards)
      model.update(states[0], actions[0], G)
      rewards.pop(0)
      states.pop(0)
      actions.pop(0)
  #
  return totalreward
###############################################################################
if __name__ == '__main__':
  env = gym.make('MountainCar-v0').env
  ft = FeatureTransformer(env)
  model = Model(env, ft, "constant")
  discount_rate = 0.99
  num_steps_for_TD_n = 5
  #
  if True:
    monitor_dir = os.getcwd() + "/videos/" + str(datetime.now())
    env = wrappers.Monitor(env, monitor_dir)
  #
  num_of_episodes = 300
  totalrewards = np.empty(num_of_episodes)
  costs = np.empty(num_of_episodes)
  for i in range(num_of_episodes):
    # curr_eps = 0
    curr_eps = 0.1*(0.97**i)
    totalreward = play_one(model, curr_eps, discount_rate, num_steps_for_TD_n)
    totalrewards[i] = totalreward
    print("episode:", i, "total reward:", totalreward)
  print("avg reward for last 100 episodes:", totalrewards[-100:].mean())
  print("total steps:", -totalrewards.sum())
  #
  plt.plot(totalrewards)
  plt.xlabel("Episode")
  plt.ylabel("Rewards")
  plt.savefig("./figs/Average_Total_Reward.png")
  plt.close()
  #
  plot_running_avg(totalrewards)
  #
  # plot the optimal state-value function
  plot_avg_num_remaining_steps(env, model)
  #