text:
# Copyright (C) 2016-2018 Alibaba Group Holding Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

# -*- coding: utf-8 -*-
import mxnet as mx
import xdl
import numpy as np
import time
import datetime

import os
import ctypes
from xdl.python.utils.collections import READER_HOOKS, get_collection
from xdl.python.utils.metrics import add_metrics
from xdl.python.training.train_session import QpsMetricsHook, MetricsPrinterHook
from utils import Auc

'''
Usage:
python mtl.py --run_mode=local --ckpt_dir=./ckpt --task_index=0 --config=config.json
'''

batch_size = 5000
embed_size = 12
train_batch_num = 8000
test_batch_num = 8000

lr = 0.001
is_debug = 0

user_fn=['101', '109_14', '110_14', '127_14', '150_14', '121', '122', '124', '125', '126', '127', '128', '129']
ad_fn=['205', '206', '207', '210', '216', '508', '509', '702', '853', '301']

def reader(name, files, epochs, batch_size, label_dim, user_fn, ad_fn,
           file_type=xdl.parsers.pb, fs_type=xdl.fs.local, enable_state=False):
    data_io = xdl.DataIO(name, file_type=file_type, fs_type=fs_type,
                         enable_state=enable_state)
    data_io.epochs(epochs)
    data_io.threads(len(files))
    data_io.batch_size(batch_size)
    data_io.label_count(label_dim)

    for fn in user_fn:
        data_io.feature(name=fn, type=xdl.features.sparse, table=1)

    for fn in ad_fn:
        data_io.feature(name=fn, type=xdl.features.sparse)

    for path in files:
        data_io.add_path(path)

    data_io.startup()
    return data_io

def run(is_training, files):

    data_io = reader("mmoe", files, 2, batch_size, 2, user_fn, ad_fn)
    batch = data_io.read()

    user_embs = list()
    for fn in user_fn:
        emb = xdl.embedding('u_'+fn, batch[fn],
                            xdl.TruncatedNormal(stddev=0.001),
                            embed_size, 1000, 'sum', vtype='hash')
        user_embs.append(emb)

    ad_embs = list()
    for fn in ad_fn:
        emb = xdl.embedding('a_'+fn, batch[fn],
                            xdl.TruncatedNormal(stddev=0.001),
                            embed_size, 1000, 'sum', vtype='hash')
        ad_embs.append(emb)

    var_list = model(is_training)(ad_embs, user_embs,
                                  batch["indicators"][0],
                                  batch["label"])
    keys = ['loss', 'ctr_prop', 'ctcvr_prop', 'cvr_prop', 'ctr_label',
            'ctcvr_label', 'cvr_label']
    run_vars = dict(zip(keys, list(var_list)))

    hooks = []
    if is_training:
        train_op = xdl.Adam(lr).optimize()
        hooks = get_collection(READER_HOOKS)
        if hooks is None:
            hooks = []
        if xdl.get_task_index() == 0:
            ckpt_hook = xdl.CheckpointHook(1000, max_to_keep=100)
            hooks.append(ckpt_hook)

        run_vars.update({None: train_op})

    if is_debug > 1:
        print("=========gradients")
        grads = xdl.get_gradients()
        grads_keys = grads[''].keys()
        grads_keys.sort()
        for key in grads_keys:
            run_vars.update({"grads {}".format(key): grads[''][key]})

    hooks.append(QpsMetricsHook())
    log_format = "lstep[%(lstep)s] gstep[%(gstep)s] " \
                 "lqps[%(lqps)s] gqps[%(gqps)s]"
    hooks.append(MetricsPrinterHook(log_format, 100))

    ckpt = xdl.get_config("checkpoint", "ckpt")
    print('ckpt: ', ckpt)
    from xdl.python.training.saver import Saver
    saver = Saver()
    #saver.save('./esmm_init.ckp')
    if ckpt is not None and len(ckpt) > 0:
        if int(xdl.get_task_index()) == 0:
            #from xdl.python.training.saver import Saver
            #saver = Saver()
            print("restore from %s" % ckpt)
            saver.restore(ckpt)
        else:
            time.sleep(120)

    sess = xdl.TrainSession(hooks)

    if is_training:
        itr = 1
        ctr_auc = Auc('ctr')
        ctcvr_auc = Auc('ctcvr')
        cvr_auc = Auc('cvr')
        while not sess.should_stop():
            print('iter=', itr)
            values = sess.run(run_vars.values())
            if not values:
                continue
            value_map = dict(zip(run_vars.keys(), values))
            print('loss=', value_map['loss'])
            #print('ctcvr_prop: ', value_map['ctcvr_prop'])
            #print('cvr_prop: ', value_map['cvr_prop'])
            #print('ctcvr_label: ', value_map['ctcvr_label'])
            #print('cvr_label: ', value_map['cvr_label'])
            #print('ctcvr_label - cvr_label:', np.sum(value_map['ctcvr_label'] - value_map['cvr_label']))
            #print('cvr_prop - ctcvr_prop: ', np.sum(value_map['cvr_prop'] - value_map['ctcvr_prop']))
            ctr_auc.add(value_map['ctr_prop'], value_map['ctr_label'])
            ctcvr_auc.add(value_map['ctcvr_prop'], value_map['ctcvr_label'])
            #cvr_auc.add(value_map['cvr_prop'], value_map['cvr_label'])
            cvr_auc.add_with_filter(value_map['cvr_prop'],
                    value_map['cvr_label'],
                    np.where(value_map['ctr_label'] == 1))
            itr += 1
            if itr > train_batch_num:
                break
        ctr_auc.show()
        ctcvr_auc.show()
        cvr_auc.show()
    else:
        print('+++++++++++++++++++test')
        ctr_test_auc = Auc('ctr')
        ctcvr_test_auc = Auc('ctcvr')
        cvr_test_auc = Auc('cvr')
        for i in xrange(test_batch_num):
            print('iter=', i+1)
            values = sess.run(run_vars.values())
            value_map = dict(zip(run_vars.keys(), values))
            print('test_loss=', value_map['loss'])
            ctr_test_auc.add(value_map['ctr_prop'], value_map['ctr_label'])
            ctcvr_test_auc.add(value_map['ctcvr_prop'],
                    value_map['ctcvr_label'])
            cvr_test_auc.add_with_filter(value_map['cvr_prop'],
                    value_map['cvr_label'],
                    np.where(value_map['ctr_label'] == 1))
        ctr_test_auc.show()
        ctcvr_test_auc.show()
        cvr_test_auc.show()

class Identity(mx.init.Initializer):
    def __init__(self, init_value=None):
        super(Identity, self).__init__(init_value=init_value)

def batch_norm(tag, data, fix_gamma=False, eps=1e-3, momentum=0.9):
    gamma = mx.sym.var(name='%s_bn_gamma' % tag, init=mx.init.Constant(1.0))
    beta = mx.sym.var(name='%s_bn_beta' % tag, init=mx.init.Constant(0.))
    moving_mean = mx.sym.var(name='%s_bn_moving_mean' %
                             tag, init=mx.init.Constant(0.))
    moving_var = mx.sym.var(name='%s_bn_moving_var' %
                            tag, init=mx.init.Constant(0.))
    return mx.sym.BatchNorm(data=data, gamma=gamma, beta=beta,
            moving_mean=moving_mean, moving_var=moving_var,
            eps=eps, momentum=momentum, fix_gamma=fix_gamma,
            name='%s_bn' % tag)

def fc(tag, data, in_dim, out_dim, active='prelu', use_bn=False):
    init_stddev = 1.0
    init_mean = 0.0
    init_value = (init_stddev * np.random.randn(out_dim, in_dim).\
            astype(np.float32) + init_mean) / np.sqrt(in_dim)

    weight = mx.sym.var(name='%s_weight' % tag,
            init=Identity(init_value=init_value.tolist()))
    bias = mx.sym.var(name='%s_bias' % tag, init=mx.init.Constant(0.1))
    dout = mx.sym.FullyConnected(data=data, weight=weight,
            bias=bias, num_hidden=out_dim, name=tag)

    if use_bn:
        dout = batch_norm(tag, dout)

    if active == 'sigmoid':
        out = mx.symbol.Activation(
            data=dout, act_type="sigmoid", name=('%s_sigmoid' % tag))
    elif active == 'relu':
        out = mx.symbol.Activation(
            data=dout, act_type="relu", name=('%s_relu' % tag))
    elif active == 'prelu':
        gamma = mx.sym.var(name=('%s_prelu_gamma' % tag),
                           init=mx.init.Constant(-0.25))
        out = mx.symbol.LeakyReLU(data=dout, act_type='prelu',
                gamma=gamma, slope=0.25, name=('%s_prelu' % tag))
    elif active == 'dice':
        out = batch_norm(tag, dout, fix_gamma=True, eps=1e-4, momentum=0.99)
        prop = mx.symbol.Activation(data=out, act_type="sigmoid",
                name=('%s_dice_sigmoid' % tag))
        dice_gamma = mx.sym.var(name='%s_dice_gamma' % tag,
                shape=((1, out_dim)), init=mx.init.Constant(-0.25))
        out = mx.symbol.broadcast_mul(lhs=dice_gamma,
                rhs=((1.0 - prop) * dout)) + prop * dout
    elif active == '':  # no active function in the last layer
        out = dout
    else:
        print("Unknown activation type %s." % active)
        exit(1)

    return out

def model(is_training, device_type="cpu"):
    @xdl.mxnet_wrapper(is_training=is_training, device_type=device_type)
    def _model(ad_embs, user_embs, indicator, label):
        indicator = mx.sym.BlockGrad(indicator)
        din_ad = mx.sym.concat(*ad_embs)
        din_user = mx.sym.concat(*user_embs)

        din_user = mx.sym.take(din_user, indicator)
        din = mx.sym.concat(din_user, din_ad)

        feature_size = len(ad_embs)+len(user_embs)
        act = 'prelu'

        ############## expert
        expert_num = 2
        expert_out = list()
        #hidden_size = [256, 128, 64]
        for i in range(expert_num):
            expert_fc1 = fc('expert_{}_fc1'.format(i), din, feature_size*embed_size, 256, act, use_bn=False)
            expert_fc2 = fc('expert_{}_fc2'.format(i), expert_fc1, 256, 128, act, use_bn=False)
            expert_fc3 = fc('expert_{}_fc3'.format(i), expert_fc2, 128, 64, act, use_bn=False)
            expert_out.append(expert_fc3)

        ############## ctr
        # gate
        ctr_gate = fc('ctr_gate', din, feature_size*embed_size, expert_num, '')
        ctr_gate = mx.symbol.softmax(data=ctr_gate, axis=1)
        ctr_gate_out = list()
        for i, eo in enumerate(expert_out):
            w = mx.symbol.slice_axis(data=ctr_gate, axis=1, begin=i, end=i+1)
            out = mx.symbol.broadcast_mul(lhs=eo, rhs=w)
            ctr_gate_out.append(out)
        ctr_gate_out = mx.symbol.concat(*ctr_gate_out, dim=1)

        ctr_fc1 = fc('ctr_fc1', ctr_gate_out, 64 * len(expert_out), 80, act)
        ctr_out = fc('ctr_out', ctr_fc1, 80, 1, '')

        ############## cvr
        # gate
        cvr_gate = fc('cvr_gate', din, feature_size*embed_size, expert_num, '')
        cvr_gate = mx.symbol.softmax(data=cvr_gate, axis=1)
        cvr_gate_out = list()
        for i, eo in enumerate(expert_out):
            w = mx.symbol.slice_axis(data=cvr_gate, axis=1, begin=i, end=i+1)
            out = mx.symbol.broadcast_mul(lhs=eo, rhs=w)
            cvr_gate_out.append(out)
        cvr_gate_out = mx.symbol.concat(*cvr_gate_out, dim=1)

        cvr_fc1 = fc('cvr_fc1', cvr_gate_out, 64 * len(expert_out), 80, act)
        cvr_out = fc('cvr_out', cvr_fc1, 80, 1, '')

        ctr_clk = mx.symbol.slice_axis(data=label, axis=1, begin=0, end=1)
        ctr_label = mx.symbol.concat(*[1 - ctr_clk, ctr_clk], dim=1)

        ctcvr_buy = mx.symbol.slice_axis(data=label, axis=1, begin=1, end=2)
        ctcvr_label = mx.symbol.concat(*[1 - ctcvr_buy, ctcvr_buy], dim=1)

        ctr_prop_one = mx.symbol.sigmoid(data=ctr_out, axis=1)
        ctr_prop = mx.symbol.concat(*[1 - ctr_prop_one, ctr_prop_one], dim=1)

        cvr_prop_one = mx.symbol.sigmoid(data=cvr_out, axis=1)
        cvr_prop = mx.symbol.concat(*[1 - cvr_prop_one, cvr_prop_one], dim=1)

        #ctr_prop_one =mx.symbol.slice_axis(data=ctr_prop, axis=1,
        #                                   begin=1, end=2)
        #cvr_prop_one =mx.symbol.slice_axis(data=cvr_prop, axis=1,
        #                                   begin=1, end=2)

        ############## new version
        #ctcvr_prop_one = ctr_prop_one * cvr_prop_one
        ctcvr_prop_one = cvr_prop_one
        ctcvr_prop = mx.symbol.concat(*[1 - ctcvr_prop_one, \
            ctcvr_prop_one], dim=1)

        loss_r = mx.symbol.MakeLoss(-mx.symbol.sum_axis( \
            #mx.symbol.broadcast_mul( \
            #        lhs=mx.symbol.log(cvr_prop) * ctcvr_label, \
            #        rhs=ctr_clk) +  \
            mx.symbol.log(ctr_prop) * ctr_label + \
            mx.symbol.log(ctcvr_prop) * ctcvr_label, \
            axis=1, keepdims=True), \
            normalization="null", \
            grad_scale = 1.0 / batch_size, \
            name="esmm_loss")
        ctr_loss = - mx.symbol.sum(mx.symbol.log(ctr_prop) * ctr_label ) \
                / batch_size
        ctcvr_loss = - mx.symbol.sum(mx.symbol.log(ctcvr_prop) * ctcvr_label) \
                / batch_size

        cnt_cvr_sample = mx.symbol.sum_axis(ctr_clk)
        cnt_ctcvr_sample = mx.symbol.sum_axis(ctcvr_buy)

        cvr_loss = - mx.symbol.sum(mx.symbol.sum_axis( \
            mx.symbol.log(cvr_prop) * ctcvr_label, \
            axis=1, keepdims=True) * ctr_clk) / cnt_cvr_sample

        return loss_r, mx.sym.BlockGrad(ctr_prop_one), \
               mx.sym.BlockGrad(ctcvr_prop_one), \
               mx.sym.BlockGrad(cvr_prop_one), mx.sym.BlockGrad(ctr_clk), \
               mx.sym.BlockGrad(ctcvr_buy), mx.sym.BlockGrad(ctcvr_buy)
    return _model

if __name__ == '__main__':
    is_training = xdl.get_config("is_training")
    files = xdl.get_config("files")
    run(is_training, files)

max_stars_repo_name: tencent-dlen/dlen

max_issues_repo_name: tencent-dlen/dlen

max_forks_repo_name: tencent-dlen/dlen


text:
import numpy as np

# To install the DBN package: pip install git+git://github.com/albertbup/deep-belief-network.git
# Further information: https://github.com/albertbup/deep-belief-network
from dbn import SupervisedDBNClassification

from sklearn.datasets import load_wine
from sklearn.metrics.classification import classification_report
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

# Set random seed for reproducibility
np.random.seed(1000)

if __name__ == '__main__':
    # Load and normalize the dataset
    wine = load_wine()

    ss = StandardScaler()
    X = ss.fit_transform(wine['data'])
    Y = wine['target']

    # Create train and test sets
    X_train, X_test, Y_train, Y_test = \
        train_test_split(X, Y,
                         test_size=0.25,
                         random_state=1000)

    # Train the model
    classifier = SupervisedDBNClassification(
        hidden_layers_structure=[16, 8],
        learning_rate_rbm=0.001,
        learning_rate=0.01,
        n_epochs_rbm=20,
        n_iter_backprop=100,
        batch_size=16,
        activation_function='relu',
        dropout_p=0.1)

    classifier.fit(X_train, Y_train)

    Y_pred = classifier.predict(X_test)
    print(classification_report(Y_test, Y_pred))

ext: py

reason: import numpy


text:
# coding=utf8
"""
python=3.5
TensorFlow=1.2.1
"""

import pandas as pd
from scipy import sparse
import collections
import random
import time
import numpy as np
import tensorflow as tf
import multi_view_data_input

flags = tf.app.flags
FLAGS = flags.FLAGS

flags.DEFINE_string('summaries_dir', 'Summaries', 'Summaries directory')
flags.DEFINE_float('learning_rate', 0.05, 'Initial learning rate.')
flags.DEFINE_integer('max_steps', 800000, 'Number of steps to run trainer.')
flags.DEFINE_integer('epoch_steps', 200, "Number of steps in one epoch.")
flags.DEFINE_integer('test_pack_size', 3185, "Number of steps in one epoch.")
flags.DEFINE_bool('gpu', 0, "Enable GPU or not")

start = time.time()
# user feature维度
user_dimension = 17309
# 负样本个数
NEG = 4
# positive batch size
user_BS = 100
# batch size
# BS = user_BS * (NEG + 1)
# 第1层网络的单元数目
L1_N = 400
# 第1层网络的单元数目
L2_N = 120

# 读取数据
# train_size, test_size = 1000000, 100000
# data_sets = multi_view_data_input.load_data()
data_sets = multi_view_data_input.get_data()
user_dimension = data_sets.TRIGRAM_D
# view1维度
view1_dimension = data_sets.app_number
view2_dimension = data_sets.music_number
view3_dimension = data_sets.novel_number
# view1 训练集大小
view1_size = data_sets.app_his.shape[0]
view2_size = data_sets.music_his.shape[0]
view3_size = data_sets.novel_his.shape[0]
total_size = view1_size + view2_size + view3_size
# view1 测试集大小
view1_size_test = data_sets.app_his_test.shape[0]
view2_size_test = data_sets.music_his_test.shape[0]
view3_size_test = data_sets.novel_his_test.shape[0]
# 测试集package size
flags.test_pack_size = int((view1_size_test + view2_size_test + view3_size_test) / user_BS)

def batch_normalization(x, phase_train, out_size):
    """
    Batch normalization on convolutional maps.
    Ref.: http://stackoverflow.com/questions/33949786/how-could-i-use-batch-normalization-in-tensorflow
    Args:
        x:           Tensor, 4D BHWD input maps
        out_size:       integer, depth of input maps
        phase_train: boolean tf.Varialbe, true indicates training phase
        scope:       string, variable scope
    Return:
        normed:      batch-normalized maps
    """
    with tf.variable_scope('bn'):
        beta = tf.Variable(tf.constant(0.0, shape=[out_size]),
                           name='beta', trainable=True)
        gamma = tf.Variable(tf.constant(1.0, shape=[out_size]),
                            name='gamma', trainable=True)
        batch_mean, batch_var = tf.nn.moments(x, [0], name='moments')
        ema = tf.train.ExponentialMovingAverage(decay=0.5)

        def mean_var_with_update():
            ema_apply_op = ema.apply([batch_mean, batch_var])
            with tf.control_dependencies([ema_apply_op]):
                return tf.identity(batch_mean), tf.identity(batch_var)

        mean, var = tf.cond(phase_train,
                            mean_var_with_update,
                            lambda: (ema.average(batch_mean), ema.average(batch_var)))
        normed = tf.nn.batch_normalization(x, mean, var, beta, gamma, 1e-3)
    return normed

def variable_summaries(var, name):
    """Attach a lot of summaries to a Tensor."""
    with tf.name_scope('summaries'):
        mean = tf.reduce_mean(var)
        tf.summary.scalar('mean/' + name, mean)
        with tf.name_scope('stddev'):
            stddev = tf.sqrt(tf.reduce_sum(tf.square(var - mean)))
        tf.summary.scalar('sttdev/' + name, stddev)
        tf.summary.scalar('max/' + name, tf.reduce_max(var))
        tf.summary.scalar('min/' + name, tf.reduce_min(var))
        tf.summary.histogram(name, var)

with tf.name_scope('input'):
    user_batch = tf.sparse_placeholder(tf.float32, shape=[None, user_dimension], name='user_batch')
    view1_batch = tf.sparse_placeholder(tf.float32, shape=[None, view1_dimension], name='view1_batch')
    view2_batch = tf.sparse_placeholder(tf.float32, shape=[None, view2_dimension], name='view2_batch')
    view3_batch = tf.sparse_placeholder(tf.float32, shape=[None, view3_dimension], name='view3_batch')
    active_view = tf.placeholder(tf.int32, name='active_view_number')
    on_train = tf.placeholder(tf.bool)

with tf.name_scope('User_View'):
    with tf.name_scope('User_FC1'):
        user_fc1_par_range = np.sqrt(6.0 / (user_dimension + L1_N))
        user_weight1 = tf.Variable(tf.random_uniform([user_dimension, L1_N], -user_fc1_par_range, user_fc1_par_range))
        user_bias1 = tf.Variable(tf.random_uniform([L1_N], -user_fc1_par_range, user_fc1_par_range))
        # variable_summaries(user_weight1, 'L1_weights')
        # variable_summaries(user_bias1, 'L1_biases')

        user_l1 = tf.sparse_tensor_dense_matmul(user_batch, user_weight1) + user_bias1
        user_l1_out = tf.nn.relu(user_l1)

    with tf.name_scope('User_FC2'):
        user_fc2_par_range = np.sqrt(6.0 / (L1_N + L2_N))
        user_weight2 = tf.Variable(tf.random_uniform([L1_N, L2_N], -user_fc2_par_range, user_fc2_par_range))
        user_bias2 = tf.Variable(tf.random_uniform([L2_N], -user_fc2_par_range, user_fc2_par_range))
        # variable_summaries(user_weight2, 'L2_weights')
        # variable_summaries(user_bias2, 'L2_biases')

        user_l2 = tf.matmul(user_l1_out, user_weight2) + user_bias2
        user_y = tf.nn.relu(user_l2)

with tf.name_scope('Item_view1'):
    with tf.name_scope('Item_FC1'):
        view1_fc1_par_range = np.sqrt(6.0 / (view1_dimension + L1_N))
        view1_weight1 = tf.Variable(tf.random_uniform([view1_dimension, L1_N], -view1_fc1_par_range, view1_fc1_par_range))
        view1_bias1 = tf.Variable(tf.random_uniform([L1_N], -view1_fc1_par_range, view1_fc1_par_range))
        # variable_summaries(item_weight1, 'L1_weights')
        # variable_summaries(item_bias1, 'L1_biases')
        view1_positive_l1 = tf.sparse_tensor_dense_matmul(view1_batch, view1_weight1) + view1_bias1
        view1_positive_l1_out = tf.nn.relu(view1_positive_l1)

    with tf.name_scope('Item_FC2'):
        view1_fc2_par_range = np.sqrt(6.0 / (L1_N + L2_N))
        view1_weight2 = tf.Variable(tf.random_uniform([L1_N, L2_N], -view1_fc2_par_range, view1_fc2_par_range))
        view1_bias2 = tf.Variable(tf.random_uniform([L2_N], -view1_fc2_par_range, view1_fc2_par_range))
        # variable_summaries(item_weight2, 'L2_weights')
        # variable_summaries(item_bias2, 'L2_biases')

        view1_positive_l2 = tf.matmul(view1_positive_l1_out, view1_weight2) + view1_bias2
        view1_positive_y = tf.nn.relu(view1_positive_l2)

with tf.name_scope('Item_view2'):
    with tf.name_scope('Item_FC1'):
        view2_fc1_par_range = np.sqrt(6.0 / (view2_dimension + L1_N))
        view2_weight1 = tf.Variable(tf.random_uniform([view2_dimension, L1_N], -view2_fc1_par_range, view2_fc1_par_range))
        view2_bias1 = tf.Variable(tf.random_uniform([L1_N], -view2_fc1_par_range, view2_fc1_par_range))
        # variable_summaries(item_weight1, 'L1_weights')
        # variable_summaries(item_bias1, 'L1_biases')
        view2_positive_l1 = tf.sparse_tensor_dense_matmul(view2_batch, view2_weight1) + view2_bias1
        view2_positive_l1_out = tf.nn.relu(view2_positive_l1)

    with tf.name_scope('Item_FC2'):
        view2_fc2_par_range = np.sqrt(6.0 / (L1_N + L2_N))
        view2_weight2 = tf.Variable(tf.random_uniform([L1_N, L2_N], -view2_fc2_par_range, view2_fc2_par_range))
        view2_bias2 = tf.Variable(tf.random_uniform([L2_N], -view2_fc2_par_range, view2_fc2_par_range))
        # variable_summaries(item_weight2, 'L2_weights')
        # variable_summaries(item_bias2, 'L2_biases')

        view2_positive_l2 = tf.matmul(view2_positive_l1_out, view2_weight2) + view2_bias2
        view2_positive_y = tf.nn.relu(view2_positive_l2)

with tf.name_scope('Item_view3'):
    with tf.name_scope('Item_FC1'):
        view3_fc1_par_range = np.sqrt(6.0 / (view3_dimension + L1_N))
        view3_weight1 = tf.Variable(tf.random_uniform([view3_dimension, L1_N], -view3_fc1_par_range, view3_fc1_par_range))
        view3_bias1 = tf.Variable(tf.random_uniform([L1_N], -view3_fc1_par_range, view3_fc1_par_range))
        # variable_summaries(item_weight1, 'L1_weights')
        # variable_summaries(item_bias1, 'L1_biases')
        view3_positive_l1 = tf.sparse_tensor_dense_matmul(view3_batch, view3_weight1) + view3_bias1
        view3_positive_l1_out = tf.nn.relu(view3_positive_l1)

    with tf.name_scope('Item_FC2'):
        view3_fc2_par_range = np.sqrt(6.0 / (L1_N + L2_N))
        view3_weight2 = tf.Variable(tf.random_uniform([L1_N, L2_N], -view3_fc2_par_range, view3_fc2_par_range))
        view3_bias2 = tf.Variable(tf.random_uniform([L2_N], -view3_fc2_par_range, view3_fc2_par_range))
        # variable_summaries(item_weight2, 'L2_weights')
        # variable_summaries(item_bias2, 'L2_biases')

        view3_positive_l2 = tf.matmul(view3_positive_l1_out, view3_weight2) + view3_bias2
        view3_positive_y = tf.nn.relu(view3_positive_l2)

with tf.name_scope('Make_Negative_Item'):
    # 合并负样本，tile可选择是否扩展负样本。
    # 判断激活哪一个view。
    if active_view == 1:
        item_y = tf.tile(view1_positive_y, [1, 1])
    elif active_view == 2:
        item_y = tf.tile(view2_positive_y, [1, 1])
    else:
        item_y = tf.tile(view3_positive_y, [1, 1])

    item_y_temp = tf.tile(item_y, [1, 1])
    # batch内随机负采样。
    for i in range(NEG):
        rand = int((random.random() + i) * user_BS / NEG)
        item_y = tf.concat([item_y,
                            tf.slice(item_y_temp, [rand, 0], [user_BS - rand, -1]),
                            tf.slice(item_y_temp, [0, 0], [rand, -1])], 0)

with tf.name_scope('Cosine_Similarity'):
    # Cosine similarity
    # query_norm = sqrt(sum(each x^2))
    query_norm = tf.tile(tf.sqrt(tf.reduce_sum(tf.square(user_y), 1, True)), [NEG + 1, 1])
    # doc_norm = sqrt(sum(each x^2))
    doc_norm = tf.sqrt(tf.reduce_sum(tf.square(item_y), 1, True))
    # query * doc
    prod = tf.reduce_sum(tf.multiply(tf.tile(user_y, [NEG + 1, 1]), item_y), 1, True)
    # ||query|| * ||doc||
    norm_prod = tf.multiply(query_norm, doc_norm)
    # cos_sim_raw = query * doc / (||query|| * ||doc||)
    cos_sim_raw = tf.truediv(prod, norm_prod)
    # gamma = 20
    # shape = [user_BS, NEG + 1]，第一列是正样本cos相似度。
    cos_sim = tf.transpose(tf.reshape(tf.transpose(cos_sim_raw), [NEG + 1, user_BS])) * 20

with tf.name_scope('Loss'):
    # Train Loss
    # 转化为softmax概率矩阵。
    prob = tf.nn.softmax(cos_sim)
    # 只取第一列，即正样本列概率。
    hit_prob = tf.slice(prob, [0, 0], [-1, 1])
    loss = -tf.reduce_sum(tf.log(hit_prob))
    tf.summary.scalar('loss', loss)

with tf.name_scope('Training'):
    # Optimizer
    train_step = tf.train.AdamOptimizer(FLAGS.learning_rate).minimize(loss)

# with tf.name_scope('Accuracy'):
#     correct_prediction = tf.equal(tf.argmax(prob, 1), 0)
#     accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
#     tf.summary.scalar('accuracy', accuracy)

merged = tf.summary.merge_all()

with tf.name_scope('Test'):
    average_loss = tf.placeholder(tf.float32)
    loss_summary = tf.summary.scalar('average_loss', average_loss)

with tf.name_scope('Train'):
    train_average_loss = tf.placeholder(tf.float32)
    train_loss_summary = tf.summary.scalar('train_average_loss', train_average_loss)

def convert_to_sparse_tensor(data_in):
    data_in = data_in.tocoo()
    data_in = tf.SparseTensorValue(
        np.transpose([np.array(data_in.row, dtype=np.int64), np.array(data_in.col, dtype=np.int64)]),
        np.array(data_in.data, dtype=np.float),
        np.array(data_in.shape, dtype=np.int64))
    return data_in

def pull_batch(user_data, item_positive, batch_id):
    batch_id = int(batch_id)
    user_in = user_data[batch_id * user_BS:(batch_id + 1) * user_BS, :]
    item_positive_in = item_positive[batch_id * user_BS:(batch_id + 1) * user_BS, :]
    user_in, item_positive_in = convert_to_sparse_tensor(user_in), convert_to_sparse_tensor(item_positive_in)
    return user_in, item_positive_in

def feed_dict(on_verify, Train, batch_id):
    view1_batch_in = convert_to_sparse_tensor(sparse.csr_matrix(([], ([], [])), shape=(user_BS, view1_dimension)))
    view2_batch_in = convert_to_sparse_tensor(sparse.csr_matrix(([], ([], [])), shape=(user_BS, view2_dimension)))
    view3_batch_in = convert_to_sparse_tensor(sparse.csr_matrix(([], ([], [])), shape=(user_BS, view3_dimension)))
    active_view_in = 1
    if Train:
        if batch_id <= view1_size / user_BS:
            batch_id = batch_id if batch_id < view1_size / user_BS - 1 else batch_id - 1
            active_view_in = 1
            user_batch_in, view1_batch_in = pull_batch(data_sets.app_search, data_sets.app_his, batch_id)
        elif view1_size / user_BS < batch_id <= (view1_size + view2_size) / user_BS:
            batch_id -= view1_size / user_BS
            batch_id = batch_id if batch_id < view2_size / user_BS - 1 else batch_id - 1
            active_view_in = 2
            user_batch_in, view2_batch_in = pull_batch(data_sets.music_search, data_sets.music_his, batch_id)
        else:
            batch_id -= view1_size / user_BS + view2_size / user_BS
            batch_id = batch_id if batch_id < view3_size / user_BS - 1 else batch_id - 1
            active_view_in = 3
            user_batch_in, view3_batch_in = pull_batch(data_sets.novel_search, data_sets.novel_his, batch_id)

    else:
        if batch_id <= view1_size_test / user_BS:
            batch_id = batch_id if batch_id < view1_size_test / user_BS - 1 else batch_id - 1
            active_view_in = 1
            user_batch_in, view1_batch_in = pull_batch(data_sets.app_search_test, data_sets.app_his_test, batch_id)
        elif view1_size_test / user_BS < batch_id <= (view1_size_test + view2_size_test) / user_BS:
            batch_id -= view1_size_test / user_BS
            batch_id = batch_id if batch_id < view2_size_test / user_BS - 1 else batch_id - 1
            active_view_in = 2
            user_batch_in, view2_batch_in = pull_batch(data_sets.music_search_test, data_sets.music_his_test, batch_id)
        else:
            batch_id -= view1_size_test / user_BS + view2_size_test / user_BS
            batch_id = batch_id if batch_id < view3_size_test / user_BS - 1 else batch_id - 1
            active_view_in = 3
            user_batch_in, view3_batch_in = pull_batch(data_sets.novel_search_test, data_sets.novel_his_test, batch_id)

    return {user_batch: user_batch_in,
            view1_batch: view1_batch_in,
            view2_batch: view2_batch_in,
            view3_batch: view3_batch_in,
            active_view: active_view_in,
            on_train: on_verify}

# config = tf.ConfigProto()  # log_device_placement=True)
# config.gpu_options.allow_growth = True
# if not FLAGS.gpu:
# config = tf.ConfigProto(device_count= {'GPU' : 0})

# 创建一个Saver对象，选择性保存变量或者模型。
saver = tf.train.Saver()
# with tf.Session(config=config) as sess:
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    train_writer = tf.summary.FileWriter(FLAGS.summaries_dir + '/train', sess.graph)
    # test_writer = tf.summary.FileWriter(FLAGS.summaries_dir + '/test', sess.graph)

    start = time.time()
    for step in range(FLAGS.max_steps):
        batch_id = int(random.random() * (total_size / user_BS - 1))
        # print(batch_id)
        sess.run(train_step, feed_dict=feed_dict(True, True, batch_id))

        if step % FLAGS.epoch_steps == 0:
            # train loss
            loss_v = sess.run(loss, feed_dict=feed_dict(False, True, batch_id))

            loss_v /= user_BS
            train_loss = sess.run(train_loss_summary, feed_dict={train_average_loss: loss_v})
            train_writer.add_summary(train_loss, step + 1)
            end = time.time()
            print("\nEpoch #%-5d | Train Loss: %-4.3f | PureTrainTime: %-3.3fs" %
                  (step / FLAGS.epoch_steps, loss_v, end - start))

            # test loss
            epoch_loss = 0
            for i in range(FLAGS.test_pack_size):
                loss_v = sess.run(loss, feed_dict=feed_dict(False, False, i))
                epoch_loss += loss_v
            epoch_loss /= (FLAGS.test_pack_size * user_BS)
            test_loss = sess.run(loss_summary, feed_dict={average_loss: epoch_loss})
            train_writer.add_summary(test_loss, step + 1)
            start = time.time()
            print("Epoch #%-5d | Test  Loss: %-4.3f | Calc_LossTime: %-3.3fs" %
                  (step / FLAGS.epoch_steps, epoch_loss, start - end))

    # 保存模型
    save_path = saver.save(sess, "model/model_1.ckpt")
    print("Model saved in file: ", save_path)

reason: import numpy,from scipy


text:
import ujson as json, pprint, csv, numpy as np, rasterio, rasterio.features, fiona, matplotlib.pyplot as plt, os, seaborn as sns
from affine import Affine
from scipy.stats.mstats import mquantiles
from shapely.geometry import shape
from shapely.ops import unary_union
from tqdm import *

pp = pprint.PrettyPrinter(indent=2)

def clip_and_pull(vname, rdir, dumpCSV=False, dumpJSON=False, view=False):
    county, video, name, nation = {}, [], [], []
    rfiles = [ x for x in os.listdir(rdir) if 'tif' == x[-3:] ]
    rdates = [ x[10:16] for x in os.listdir(rdir) if 'tif' == x[-3:] ]
    for i, rname in enumerate(tqdm(rfiles)):
        with open(vname, 'r') as vector, rasterio.open(rdir + rname, 'r') as raster:
            for feature in tqdm(json.load(vector)['features']):
                # pp.pprint(feature['properties'])
                # create a shapely geometry
                # this is done for the convenience for the .bounds property only
                geometry = shape(feature['geometry'])
                nation.append(geometry)
                try:
                    cntyid = feature['properties'][u'name:en']
                except KeyError:
                    cntyid = feature['properties'][u'name']
                try:
                    county[cntyid][rdates[i]] = { 'name': cntyid }
                except KeyError:
                    county[cntyid] = { rdates[i]: { 'name': cntyid } }

                # get pixel coordinates of the geometry's bounding box
                ul = raster.index(*geometry.bounds[0:2])
                lr = raster.index(*geometry.bounds[2:4])

                # read the subset of the data into a numpy array
                window = ((lr[0], ul[0]+1), (ul[1], lr[1]+1))
                data = raster.read(1, window=window)

                # create an affine transform for the subset data
                t = raster.affine
                shifted_affine = Affine(t.a, t.b, t.c+ul[1]*t.a, t.d, t.e, t.f+lr[0]*t.e)

                # rasterize the geometry
                mask = rasterio.features.rasterize(
                    [(geometry, 0)],
                    out_shape=data.shape,
                    transform=shifted_affine,
                    fill=1,
                    all_touched=True,
                    dtype=np.uint8)

                # create a masked numpy array
                masked_data = np.ma.array(data=data, mask=mask.astype(bool))

                # plot area to review
                if view:
                    name.append((cntyid, rdates[i]))
                    video.append(masked_data)

                # calculate statistics at county level
                county[cntyid][rdates[i]]['sum'] = float(np.ma.sum(masked_data))
                county[cntyid][rdates[i]]['mean'] = float(np.ma.mean(masked_data))
                county[cntyid][rdates[i]]['std'] = float(np.ma.std(masked_data))
                county[cntyid][rdates[i]]['quantiles'] = [ float(x) for x in mquantiles(masked_data, prob=[0,0.05,.1, .2, 0.3,0.4,.5, 0.6,0.7, 0.8, 0.9,0.95, 1], alphap=1, betap=1).tolist() ]

    # pp.pprint(county)
    # save a copy
    if dumpJSON:
        with open('%s.json'%vname, 'wb') as f:
            json.dump(county, f)

    if dumpCSV:
        with open('%s.csv'%rname, 'wb') as f:
            writeme = csv.writer(f)
            writeme.writerow(['Name', 'ID', 'Radiance'])
            for muni in county.keys():
                writeme.writerow([county[muni]['name'], muni, county[muni]['sum']])

    if view:
        plt.ion()
        for i in range(len(video)):
            plt.imsave('data/image/%s-%s.jpg'%(name[i][0], name[i][1]), video[i], vmin=0, vmax=1, cmap='afmhot')
            # plt.figure()
            # plt.imshow(video[i], vmin=0, vmax=1, cmap='afmhot')
            # plt.show()
            # plt.pause(0.001)
            # plt.close()

    return county

if __name__=='__main__':
    levels = [ 'admin_level_4.geojson', 'regions.geojson' ]
    nations = [ 'china', 'north-korea', 'south-korea', 'bangladesh', 'india', 'japan', 'republic-of-china', 'thailand', 'vietnam' ]
    # final_csv = []
    # for nation in nations:
    #     # print 'Processing %s\n'%nation
    #     # for level in levels:
    #     #     clip_and_pull('data/vector/' + nation + '/' + level, 'data/raster/', view=True, dumpJSON=True)
    #     with open('data/vector/' + nation + '/regions.geojson.json', 'rb') as f:
    #         national = json.load(f)
    #         if nation == 'china':
    #             date = [ x[:4] + '-' + x[4:] for x in national[nation].keys() ]
    #             final_csv.append(date)
    #         avgrad = [ national[nation][x[:4]+x[5:]]['mean'] for x in date ]
    #         # avgrad = [ x / max(avgrad) for x in avgrad ]
    #         final_csv.append(avgrad)
    # with open('data/time_series.csv', 'wb') as f:
    #     writeme = csv.writer(f)
    #     writeme.writerow(['date'] + nations)
    #     for row in zip(*final_csv):
    #         writeme.writerow(row)
    # final_csv = []
    # with open('data/vector/north-korea/admin_level_4.geojson.json', 'rb') as f:
    #     nk = json.load(f)
    #     provinces = nk.keys()
    #     for province in nk.keys():
    #         if province == u'North Hamgyong':
    #             date = [ x[:4] + '-' + x[4:] for x in nk[province].keys() ]
    #             final_csv.append(date)
    #         final_csv.append([ nk[province][x]['mean'] for x in nk[province].keys() ])
    # with open('data/vector/south-korea/admin_level_4.geojson.json', 'rb') as f:
    #     sk = json.load(f)
    #     provinces.append(sk.keys())
    #     for province in sk.keys():
    #         final_csv.append([ sk[province][x]['mean'] for x in sk[province].keys() ])
    # with open('data/time_series_kor.csv', 'wb') as f:
    #     writeme = csv.writer(f)
    #     writeme.writerow(['date'] + provinces)
    #     for row in zip(*final_csv):
    #         writeme.writerow(row)
    parity = {}
    with open('data/MDG611.csv', 'rb') as f:
        reader = csv.reader(f)
        for row in reader:
            parity[unicode(row[1])] = row[24:28]
    with open('data/vector/countries.geo.json', 'rb') as f:
        planet = json.load(f)
        for country in planet['features']:
            try:
                country['properties']['parity1'] = parity[country['properties']['name']][0]
                country['properties']['parity2'] = parity[country['properties']['name']][1]
                country['properties']['parity3'] = parity[country['properties']['name']][2]
                country['properties']['parity4'] = parity[country['properties']['name']][3]
            except KeyError:
                country['properties']['parity1'] = None
                country['properties']['parity2'] = None
                country['properties']['parity3'] = None
                country['properties']['parity4'] = None
    with open('data/vector/countries_parity.geo.json', 'wb') as f:
        json.dump(planet, f)

max_stars_repo_name: StarCYing/jsm-deck

max_issues_repo_name: StarCYing/jsm-deck

max_forks_repo_name: StarCYing/jsm-deck

reason: from scipy


text:
#!/usr/bin/env python
# coding: utf-8

# # Homework 1 Solution Question 2

# In[1]:

#importing libraries
import csv
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

# In[2]:

#opening the data files for data 1
with open('data_1.csv') as csvfile:
    x_1=[]
    y_1=[]
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        if row[0].isdigit() == True: #removing x and y
            x_1.append(float(row[0]))
            y_1.append(float(row[1]))

# In[3]:

#opening the data files for data 2
with open('data_2.csv') as csvfile:
    x_2=[]
    y_2=[]
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        if row[0].isdigit() == True: #removing x and y
            x_2.append(float(row[0]))
            y_2.append(float(row[1]))

# In[4]:

#plotting graph 1
fig = plt.figure(figsize=(15,4))
plt.subplot(121)
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.scatter(x_1,y_1,c='red')
plt.title('Data 1')
#plotting graph 2
plt.subplot(122)
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Data 2')
plt.scatter(x_2,y_2,c='pink')

# In[5]:

#Using total least square error

# In[6]:

#error calculation
#all the tables that are necessary are written here
x_1_p2 = []
xy = []
x_p2_y = []
x_1_p3 = []
x_1_p4 = []

# In[7]:

#adding all elements to the list
for i in range(len(x_1)):
    x_1_p2.append(float(x_1[i]**2))
    xy.append(float(x_1[i]*y_1[i]))
    x_p2_y.append(float((x_1[i]**2)*y_1[i]))
    x_1_p3.append(float(x_1[i]**3))
    x_1_p4.append(float(x_1[i]**4))

# In[8]:

#initializing all sums to zero
sum_n = 0.0
sum_x_1=0.0
sum_y_1=0.0
sum_x_1_p2 =0.0
sum_xy =0.0
sum_x_p2_y =0.0
sum_x_1_p3 = 0.0
sum_x_1_p4 =0.0

#finding all sums in the table
for i in range(len(x_1)):
    sum_n = sum_n + i
    sum_x_1=sum_x_1+x_1[i]
    sum_y_1=sum_y_1+y_1[i]
    sum_x_1_p2 =sum_x_1_p2 + x_1_p2[i]
    sum_xy =sum_xy+xy[i]
    sum_x_p2_y =sum_x_p2_y+x_p2_y[i]
    sum_x_1_p3 = sum_x_1_p3+x_1_p3[i]
    sum_x_1_p4 = sum_x_1_p4 + x_1_p4[i]

# In[9]:

#three equations to find a b and c are >>>

# sum_y_1 = a*sum_x_1_p2 + b*sum_x_1 + c*sum_n
# sum_xy = a*sum_x_1_p3 + b*sum_x_1_p2 + c*sum_x_1
# sum_x_p2_y = a*sum_x_1_p4 + b*sum_x_1_p3 + c*sum_x_1_p2

#just for showcasing the theory

# In[10]:

#finding the solution for the above set of linear equations
a= np.array([[sum_x_1_p2,sum_x_1,sum_n],[sum_x_1_p3,sum_x_1_p2,sum_x_1],[sum_x_1_p4,sum_x_1_p3,sum_x_1_p2]])
b=np.array([sum_y_1,sum_xy,sum_x_p2_y])

# In[11]:

from numpy import linalg as LA
def Compute_Svd(A):                              #function definition for svd calculation
    AT=A.T                                       #transpose
    AAT=A.dot(AT)
    eigenvalue_U,eigenvector_U=LA.eig(AAT)
    sort_val1 = eigenvalue_U.argsort()[::-1]
    new_eigenvalue_U = eigenvalue_U[sort_val1]
    new_eigenvector_U = eigenvector_U[:,sort_val1]
    #temp = np.diag((np.sqrt(new_eigenvalue_U)))  #compute sigma matrix as a diagonal matrix with elements as square root of eigen values of U
    #sigma = np.zeros_like(A).astype(np.float64)
    #sigma[:temp.shape[0],:temp.shape[1]]=temp
    temp_1 = np.diag((np.sqrt((1/new_eigenvalue_U))))  #compute sigma matrix as a diagonal matrix with elements as square root of eigen values of U
    sigma_1 = np.zeros_like(A).astype(np.float64)
    sigma_1[:temp_1.shape[0],:temp_1.shape[1]]=temp_1
    VT_inter = sigma_1.dot(new_eigenvector_U.T)
    VT = VT_inter.dot(A)

    return new_eigenvector_U,sigma_1,VT

# In[12]:

U,S_inv,VT = Compute_Svd(a)

# In[13]:

sol_1 = VT.T.dot(S_inv.dot(U.T.dot(b)))
print("Co-efficients=",sol_1)
a = sol_1[0]
b = sol_1[1]
c = sol_1[2]

# In[14]:

y_new=[]#empty list

for i in range(0,len(x_1)):
    y = (a*(x_1[i]**2))+(b*x_1[i]) + c
    y_new.append(y)

fig = plt.figure(figsize=(10,4))
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Data 1')
plt.scatter(x_1,y_1,c='red')
plt.text(400,100,'TLS Algorithm')
plt.plot(x_1,y_new,'k')
plt.show()

# In[15]:

#the end

# In[ ]:

reason: import numpy,from numpy


text:
"""
This script loads up a trained model and collects trajectories using the model.
We choose how much data is generated by the model and random uniform policy.

The model path consists of the state dict of the model.

The config path consists of all the settings to load the environment
and preprocessing.

Example usage:
python collect_data.py --seed=0 --model_path=./state_dict.pt \
    --config_path=./experiment_setting.pkl --num_episodes=5 \
        --num_samples=1000 --save_path=./data.pkl
"""

import _pickle as pickle
import argparse
import gzip
import numpy as np
import os
import torch

from pprint import pprint
from tqdm import tqdm

import rl_sandbox.constants as c

from rl_sandbox.examples.evaluation_tools.utils import load_model
from rl_sandbox.learning_utils import evaluate_policy
from rl_sandbox.utils import set_seed

def collect_data(args):
    set_seed(args.seed)
    assert args.num_episodes > 0
    assert args.num_samples > 0
    assert 0 <= args.mixture_ratio <= 1

    dir_exists = os.path.isdir(args.save_path)
    assert dir_exists or not os.path.exists(args.save_path)

    if not dir_exists:
        os.makedirs(args.save_path, exist_ok=True)

    config, env, buffer_preprocess, agent = load_model(args.seed,
                                                       args.config_path,
                                                       args.model_path,
                                                       args.device,
                                                       args.intention)

    init_observations = []
    observations = []
    actions = []
    rewards = []
    dones = []

    episodes_pbar = tqdm(total=args.num_episodes)
    samples_pbar = tqdm(total=args.num_samples)

    sample_i = 0
    eval_returns = []
    for episode_i in range(args.num_episodes):
        eval_returns.append(0)
        obs = env.reset()

        init_observations.append(obs)

        buffer_preprocess.reset()
        obs = buffer_preprocess(obs)
        h_state = agent.reset()
        done = False

        while not done:
            if hasattr(env, c.RENDER) and args.render:
                env.render()

            if args.deterministic:
                action, h_state, act_info = agent.deterministic_action(
                    obs=obs, hidden_state=h_state)
            else:
                action, h_state, act_info = agent.compute_action(
                    obs=obs, hidden_state=h_state)

            if np.random.uniform() < args.mixture_ratio:
                action = np.random.uniform(config[c.MIN_ACTION], config[c.MAX_ACTION], config[c.ACTION_DIM])

            actions.append(action)

            if config[c.CLIP_ACTION]:
                action = np.clip(action, a_min=config[c.MIN_ACTION], a_max=config[c.MAX_ACTION])

            obs, reward, done, _ = env.step(action)

            observations.append(obs)
            rewards.append(reward)
            dones.append(done)
            obs = buffer_preprocess(obs)

            eval_returns[-1] += reward
            sample_i += 1
            samples_pbar.update(1)
            if sample_i >= args.num_samples:
                break
        else:
            episodes_pbar.update(1)
            continue
        break

    ret_mean = np.mean(eval_returns)
    ret_std = np.std(eval_returns)
    ret_max = np.max(eval_returns)
    ret_min = np.min(eval_returns)

    print("=" * 100)
    print("Interacted with {} complete episodes ({} timesteps)".format(episode_i, sample_i))
    print("Average Return: {} - Std: {}".format(ret_mean, ret_std))
    print("Max Return: {} - Min Return: {}".format(ret_max, ret_min))

    for (filename, data) in zip(("init_obss", "obss", "acts", "rews", "dones"),
                                (init_observations, observations, actions, rewards, dones)):
        with gzip.open(f"{args.save_path}/{filename}.pkl", "wb") as f:
            pickle.dump(data, f)

    with gzip.open(f"{args.save_path}/metadata.pkl", "wb") as f:
            pickle.dump({
                "returns": eval_returns,
                "min": ret_min,
                "max": ret_max,
                "avg": ret_mean,
                "std": ret_std,
                **args.__dict__,
            }, f)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--render", action="store_true", help="Render the environment")

    parser.add_argument("--seed", type=int, default=0, help="The random seed")
    parser.add_argument("--save_path", type=str, required=True, help="The directory to save the trajectories")
    parser.add_argument("--mixture_ratio", required=True, type=float, help="Amount of data sampled using random uniform policy")
    parser.add_argument("--deterministic", action="store_true", help="Whether or not to use deterministic action (the action mean) from the agent")
    parser.add_argument("--num_episodes", required=True, type=int, help="The maximum number of episodes")
    parser.add_argument("--num_samples", required=True, type=int, help="The maximum number of samples")

    parser.add_argument("--model_path", required=True, type=str, help="The path to load the model")
    parser.add_argument("--config_path", required=True, type=str, help="The path to load the config that trained the model")
    parser.add_argument("--intention", type=int, default=0, help="The intention to use for SAC-X")
    parser.add_argument("--device", type=str, help="Device to load models on")
    args = parser.parse_args()

    pprint(args)

    collect_data(args)


text:
# Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache 2.0 License.
# You may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

"""
This script calculates the entropy for a given set of MDNS annotations.
The mean and median entropy values are printed to the console.
"""

import pandas as pd
import numpy as np
import json
import random
import itertools
import sys
import os
import scipy
import numpy as np
import argparse

from scipy.stats import beta

def calculate_entropy(annotator_ratings):
    """Calculates entropy of the passed `pd.Series`
    """
    frequencies = pd.Series(annotator_ratings).value_counts()           # counts occurrence of each value
    entropy = scipy.stats.entropy(frequencies)  # get entropy from counts
    return entropy

def main(annotation_file):
    # Load raw annotator ratings to Dataframe
    df = pd.read_json(annotation_file, orient='index')
    e_names = df.columns

    print('\nCalculating entropy per expression dimension for annotation file: ' + annotation_file)
    print('Num. expression dimensions (D): ' + str(len(e_names)))
    print('Num. images (N): '+ str(df.shape[0]) + '\n')
    print('Printing statistics below:')

    entropies = {}
    col=0
    for index, row in df.iterrows():
        entropies[row.name] = {}
        for e in e_names:
            entropy = calculate_entropy(df.at[row.name,e])
            entropies[row.name][e] = entropy

    entropy_df = pd.DataFrame.from_dict(entropies, orient='index')
    print('\nMean Etropy per expression dimension:')
    for e in e_names:
        print(e + ': '+ str(round(entropy_df[e].mean(),2)) + ' STD: '+ str(round(entropy_df[e].std(),2)))

    print()
    print('\nMedian Etropy per expression dimension:')
    for e in e_names:
        print(e + ': '+ str(round(entropy_df[e].median(),2)))

    print('\nDone.')

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-p",
                        "--annotations_file_path",
                        type = str,
                        help = "Path to JSON file containing raw annotation data.",
                        required = True)

    args = parser.parse_args()
    main(args.annotations_file_path)

max_stars_repo_name: deairab/MDNS

max_issues_repo_name: deairab/MDNS

max_forks_repo_name: deairab/MDNS

reason: import numpy,import scipy,from scipy


text:
from math import sqrt
import numpy as np

from .object3d import Object3D

class Tetrahedron(Object3D):

    vertices = np.array([
        [1, 1, 1],
        [-1, -1, 1],
        [1, -1, -1],
        [-1, 1, -1],
    ], dtype=np.float64)

    edges = (
        (0, 1),
        (1, 2),
        (2, 3),
        (1, 3),
        (0, 2),
        (0, 3),
    )

    polys = (
        (0, 1, 2),
        (0, 2, 3),
        (0, 3, 1),
        (3, 2, 1)
    )

class Cube(Object3D):

    vertices = np.array([
        [1, 1, -1],
        [-1, 1, -1],
        [-1, -1, -1],
        [1, -1, -1],

        [1, 1, 1],
        [-1, 1, 1],
        [-1, -1, 1],
        [1, -1, 1]
    ], dtype=np.float64)

    edges = (
        (0, 1),
        (1, 2),
        (2, 3),
        (3, 0),

        (4, 5),
        (5, 6),
        (6, 7),
        (7, 4),

        (0, 4),
        (1, 5),
        (2, 6),
        (3, 7),
    )

    polys = (
        (0, 1, 2),
        (2, 3, 0),
        (4, 7, 6),
        (6, 5, 4),
        (1, 5, 6),
        (6, 2, 1),
        (0, 3, 7),
        (7, 4, 0),
        (3, 2, 6),
        (6, 7, 3),
        (5, 1, 0),
        (0, 4, 5),
    )

class Octahedron(Object3D):

    vertices = np.array([
        [1, 0, 0],
        [-1, 0, 0],
        [0, 1, 0],
        [0, -1, 0],
        [0, 0, 1],
        [0, 0, -1],
    ], dtype=np.float64)

    edges = (
        (0, 2),
        (0, 3),
        (0, 4),
        (0, 5),

        (1, 2),
        (1, 3),
        (1, 4),
        (1, 5),

        (2, 4),
        (2, 5),
        (3, 4),
        (3, 5),
    )

    polys = (
        (2, 4, 0),
        (2, 0, 5),
        (2, 5, 1),
        (2, 1, 4),

        (3, 0, 4),
        (3, 5, 0),
        (3, 1, 5),
        (3, 4, 1),
    )

class Dodecahedron(Object3D):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        # lay out as cube + 3 rects as on
        # https://en.wikipedia.org/wiki/Regular_dodecahedron?oldformat=true#Cartesian_coordinates
        phi = (1 + sqrt(5)) / 2

        vertices = np.array([
            # cube
            [1, 1, 1],
            [1, -1, 1],
            [-1, -1, 1],
            [-1, 1, 1],

            [1, 1, -1],
            [1, -1, -1],
            [-1, -1, -1],
            [-1, 1, -1],

            [phi, 1/phi, 0],
            [phi, -1/phi, 0],
            [-phi, -1/phi, 0],
            [-phi, 1/phi, 0],

            [0, phi, 1/phi],
            [0, phi, -1/phi],
            [0, -phi, -1/phi],
            [0, -phi, 1/phi],

            [1/phi, 0, phi],
            [1/phi, 0, -phi],
            [-1/phi, 0, -phi],
            [-1/phi, 0, phi]

        ], dtype=np.float64)

        self.edges = (
            # one r/g/b vertex for each cube corner vertex
            (0, 8),
            (0, 12),
            (0, 16),

            (1, 9),
            (1, 15),
            (1, 16),

            (2, 10),
            (2, 15),
            (2, 19),

            (3, 11),
            (3, 12),
            (3, 19),

            (4, 8),
            (4, 13),
            (4, 17),

            (5, 9),
            (5, 14),
            (5, 17),

            (6, 10),
            (6, 14),
            (6, 18),

            (7, 11),
            (7, 13),
            (7, 18),

            # lace up the rects exterior edges
            # r
            (8, 9),
            (10, 11),
            # g
            (12, 13),
            (14, 15),
            # b
            (17, 18),
            (19, 16)
        )
        self.vertices = self.to_homogenous_coords(vertices / (2*phi))

max_stars_repo_name: jsheedy/punyty

max_issues_repo_name: jsheedy/punyty

max_forks_repo_name: jsheedy/punyty


text:
import os
import os.path as osp
import time
import gym
from gym.wrappers import Monitor
import numpy as np
import tensorflow as tf
import atari_py
from spinup.algos.dqn.gym_wrappers import MainGymWrapper
from spinup.utils.logx import EpochLogger, Logger
from spinup.algos.dqn.replay_buffer import ReplayBuffer
from spinup.algos.dqn import core
from spinup.algos.dqn.core import DQN

FRAMES_IN_OBSERVATION = 4
FRAME_SIZE = 84
INPUT_SHAPE = (FRAMES_IN_OBSERVATION, FRAME_SIZE, FRAME_SIZE)

def make_train_and_test_envs(env_name, max_ep_len,
                             output_dir, record_video_every=100, save_video=True):
    # Add env Monitor wrapper to record video
    monitor_path_train = os.path.join(output_dir, 'videos_train')
    monitor_path_test = os.path.join(output_dir, 'videos_test')
    if not os.path.exists(monitor_path_train):
        os.makedirs(monitor_path_train)
    if not os.path.exists(monitor_path_test):
        os.makedirs(monitor_path_test)

    env_train = gym.make(env_name)
    env_train._max_episode_steps = max_ep_len
    env_test = gym.make(env_name)
    env_test._max_episode_steps = max_ep_len
    if save_video:
        env_train = Monitor(env_train, directory=monitor_path_train,
                            video_callable=lambda count: count % record_video_every == 0,
                            resume=True)
        env_test = Monitor(env_test, directory=monitor_path_test,
                           video_callable=lambda count: count % record_video_every == 0,
                           resume=True)
    # MainGymWrapper preprocesses state
    env_train, env_test = MainGymWrapper.wrap(env_train), MainGymWrapper.wrap(env_test)
    return env_train, env_test

def dqn(seed, game_name, render,
        gamma=0.99, q_lr=0.00025,
        start_steps=50000, epochs=1000, steps_per_epoch=5000, max_ep_len=5000,
        replay_size=int(1e6), batch_size=32, target_net_update_frequency = 10000,
        epsilon_max = 1, epsilon_min = 0.1, epsilon_decay_steps = 1000000, epsilon_test=0.02,
        record_video_every=50,
        logger_kwargs=dict()):
    """

    :param seed:
    :param game_name:
    :param render:
    :param logger_kwargs:
    :param max_ep_len: the maximum episode length set to 5 minutes of real-time play (18,000 frames)
    :return:
    """
    logger = EpochLogger(**logger_kwargs)
    logger.save_config(locals())

    tf.set_random_seed(seed)
    np.random.seed(seed)

    env_name = game_name + "Deterministic-v4"  # Handles frame skipping (4) at every iteration
    env, test_env = make_train_and_test_envs(env_name, max_ep_len,
                                             logger_kwargs['output_dir'], record_video_every)
    obs_dim = FRAMES_IN_OBSERVATION*FRAME_SIZE*FRAME_SIZE
    act_dim = env.action_space.n

    a_ph, r_ph, d_ph = core.placeholders(None, None, None)
    x_ph = tf.placeholder(dtype=tf.float32, shape=(None, FRAMES_IN_OBSERVATION, FRAME_SIZE, FRAME_SIZE))
    x2_ph = tf.placeholder(dtype=tf.float32, shape=(None, FRAMES_IN_OBSERVATION, FRAME_SIZE, FRAME_SIZE))

    # Define main DQN
    with tf.variable_scope('main'):
        dqn_model = DQN(act_dim)
        q = dqn_model(x_ph)
        pi = tf.argmax(q, axis=1)

    # Define target DQN
    with tf.variable_scope('target'):
        dqn_model_targ = DQN(act_dim)
        q_targ = dqn_model_targ(x2_ph)

    # Experience buffer
    replay_buffer = ReplayBuffer(obs_dim=obs_dim, act_dim=act_dim, size=replay_size,
                                 logger_fname='experiences_log.txt', **logger_kwargs)

    # Bellman backup for Q functions
    backup = tf.stop_gradient(r_ph + gamma*(1-d_ph)*tf.reduce_max(q_targ, axis=1))

    # Train operation
    q_loss = tf.reduce_mean((q_targ - q)**2)
    q_optimizer = tf.train.RMSPropOptimizer(learning_rate=q_lr)
    train_q_op = q_optimizer.minimize(q_loss, var_list=dqn_model.variables)

    # Update target q-network
    target_update = tf.group([tf.assign(v_targ, v_main)
                              for v_main, v_targ in zip(dqn_model.variables, dqn_model_targ.variables)])

    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    sess.run(target_update)

    # Get action
    def get_action(o, epsilon):
        if np.random.rand() < epsilon:
            return np.random.randint(0, act_dim, size=1)
        return sess.run(pi, feed_dict={x_ph: np.expand_dims(np.asarray(o), axis=0)})[0]

    def test_agent(epsilon_test, n=10):
        for j in range(n):
            o, r, d, ep_ret, ep_len = test_env.reset(), 0, False, 0, 0
            while not(d or (ep_len == max_ep_len)):
                # Take epsilon greedy action
                o, r, d, _ = test_env.step(get_action(o, epsilon_test))
                ep_ret += r
                ep_len += 1
            logger.store(TestEpRet=ep_ret, TestEpLen=ep_len)

    start_time = time.time()
    o, r, d, ep_ret, ep_len = env.reset(), 0, False, 0, 0
    total_steps = steps_per_epoch * epochs

    # Initialize epsilon
    epsilon_decay = (epsilon_max-epsilon_min)/epsilon_decay_steps
    epsilon = epsilon_max

    # Main loop: collect experience in env and update/log each epoch
    for t in range(total_steps):
        if t%1000==0:
            print('Step: {}'.format(t))
        if render:
            env.render()

        if t > start_steps:
            a = get_action(o, epsilon)
            epsilon = max(epsilon_min, epsilon-epsilon_decay)
        else:
            a = env.action_space.sample()

        o2, r, d, _ = env.step(a)
        ep_ret += r
        ep_len += 1
        # Ignore the "done" signal if it comes from hitting the time
        # horizon (that is, when it's an artificial terminal signal
        # that isn't based on the agent's state)
        d = False if ep_len == max_ep_len else d

        # Store experience to replay buffer
        replay_buffer.store(o, a, r, o2, d, t, steps_per_epoch, start_time)

        # Super critical, easy to overlook step: make sure to update
        # most recent observation!
        o = o2

        if t > batch_size:
            # Train
            batch = replay_buffer.sample_batch(batch_size=batch_size)
            feed_dict = {x_ph: batch['obs1'],
                         x2_ph: batch['obs2'],
                         a_ph: batch['acts'],
                         r_ph: batch['rews'],
                         d_ph: batch['done']}
            q_step_ops = [q_loss, q, train_q_op]
            outs = sess.run(q_step_ops, feed_dict=feed_dict)
            logger.store(LossQ=outs[0], QVals=outs[1])

        # Update target q-net

        if t % target_net_update_frequency == 0:
            sess.run(target_update)

        if d or (ep_len == max_ep_len):
            # Log episode data
            logger.store(EpRet=ep_ret, EpLen=ep_len)
            o, r, d, ep_ret, ep_len = env.reset(), 0, False, 0, 0

        # End of epoch wrap-up
        if t > 0 and t % steps_per_epoch == 0:
            epoch = t // steps_per_epoch

            # # Save model
            # if (epoch % save_freq == 0) or (epoch == epochs - 1):
            #     logger.save_state({'env': env}, None)

            # Test the performance of the deterministic version of the agent.
            test_agent(epsilon_test, n= 4)

            # Log info about epoch
            logger.log_tabular('Epoch', epoch)
            logger.log_tabular('EpRet', with_min_and_max=True)
            logger.log_tabular('TestEpRet', with_min_and_max=True)
            logger.log_tabular('EpLen', average_only=True)
            logger.log_tabular('TestEpLen', average_only=True)
            logger.log_tabular('TotalEnvInteracts', t)
            logger.log_tabular('QVals', with_min_and_max=True)
            logger.log_tabular('LossQ', average_only=True)
            logger.log_tabular('Time', time.time() - start_time)
            logger.dump_tabular()

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    available_games = list((''.join(x.capitalize() or '_' for x in word.split('_')) for word in atari_py.list_games()))
    parser.add_argument("-g", "--game",
                        help="Choose from available games: " + str(available_games) + ". Default is 'breakout'.",
                        default="SpaceInvaders")
    parser.add_argument("-m", "--mode",
                        help="Choose from available modes: ddqn_train, ddqn_test, ge_train, ge_test. Default is 'ddqn_training'.",
                        default="ddqn_training")
    parser.add_argument("-r", "--render", help="Choose if the game should be rendered. Default is 'False'.",
                        default=False, type=bool)
    parser.add_argument("-tsl", "--total_step_limit",
                        help="Choose how many total steps (frames visible by agent) should be performed. Default is '5000000'.",
                        default=5000000, type=int)
    parser.add_argument("-trl", "--total_run_limit",
                        help="Choose after how many runs we should stop. Default is None (no limit).", default=None,
                        type=int)
    parser.add_argument("-c", "--clip", help="Choose whether we should clip rewards to (0, 1) range. Default is 'True'",
                        default=True, type=bool)
    parser.add_argument('-s', '--seed', type=int, default=0)
    parser.add_argument('--exp_name', type=str, default='dqn_atari')
    args = parser.parse_args()

    print('Selected game: {}'.format(args.game))
    print('Selected mode: {}'.format(args.mode))
    print('Should render: {}'.format(args.render))
    print('Should clip: {}'.format(args.clip))
    print('Total step limit: {}'.format(args.total_step_limit))
    print('Total run limit: {}'.format(args.total_run_limit))

    # Set log data saving directory
    from spinup.utils.run_utils import setup_logger_kwargs

    data_dir = osp.join(osp.dirname(osp.dirname(osp.dirname(osp.dirname(osp.dirname(osp.abspath(__file__)))))),
                        'spinup_data')
    logger_kwargs = setup_logger_kwargs(args.exp_name, args.seed, data_dir, datestamp=True)
    dqn(seed=args.seed, game_name=args.game, render=args.render, logger_kwargs=logger_kwargs)

max_stars_repo_name: LinghengMeng/spinningup

max_issues_repo_name: LinghengMeng/spinningup

max_forks_repo_name: LinghengMeng/spinningup


text:
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import numpy as np
from tqdm import tqdm

train_batch_size = 64
test_batch_size = 1000
learning_rate = 0.0001

train_dataset = torchvision.datasets.FashionMNIST(root='./data', train=True, download=True, transform=transforms.ToTensor())

test_dataset = torchvision.datasets.FashionMNIST(root='./data', train=False, download=True, transform=transforms.ToTensor())

train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=train_batch_size, shuffle=True)

test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=test_batch_size, shuffle=False)

print(len(train_dataset))

class ConvNet(nn.Module):
    def __init__(self):
        super(ConvNet, self).__init__()
        self.pool = nn.MaxPool2d(2, 2)
        self.conv1 = nn.Conv2d(1, 32, 3)
        self.conv2 = nn.Conv2d(32, 64, 2)
        self.fc1 = nn.Linear(2304, 128)
        self.fc2 = nn.Linear(128, 10)

    def forward(self, x):
        # -> n, 1, 28, 28
        x = self.conv1(x)
        x = F.relu(x)
        x = self.pool(x)
        x = self.conv2(x)
        x = F.relu(x)
        x = self.pool(x) # 64 x 6 x 6
        x = torch.flatten(x, 1)
        x = self.fc1(x)
        x = F.relu(x)
        x = self.fc2(x)
        output = F.softmax(x)
        return output

def train(model, device, train_loader, optimizer, criterion, epochs, interval=500):
    for epoch in range(epochs):
        print(f'Starting #{epoch}')
        for batch_idx, (images, labels) in tqdm(enumerate(train_loader)):
            images = images.to(device)
            labels = labels.to(device)
            outputs = model(images)
            loss = criterion(outputs, labels)

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            if batch_idx % interval == 0 or batch_idx * len(images) ==  len(train_loader.dataset):
                print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                    epoch, batch_idx * len(images), len(train_loader.dataset),
                    100. * batch_idx / len(train_loader), loss.item()))
        print(f'Finishing Epoch #{epoch}')

def test(model, device, test_loader):
    with torch.no_grad():
        correct_imgs = 0
        all_imgs = 0
        matrix = [[0] * 10] * 10 # TODO
        for images, labels in test_loader:
            images = images.to(device)
            labels = labels.to(device)
            outputs = model(images)
            predictions = outputs.argmax()
            for pred, label in list(zip(predictions, labels)):

                matrix[pred][label] += 1
                all_imgs += 1
                if label == pred:
                    correct_imgs += 1

        return correct_imgs / all_imgs, matrix

model = ConvNet().to(device)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adagrad(model.parameters(), lr=learning_rate)

max_stars_repo_name: pavponn/machine-learning

max_issues_repo_name: pavponn/machine-learning

max_forks_repo_name: pavponn/machine-learning


text:
import numpy as np
import qutip

def compute_Q_ptrace(ket, N):
    """Computes Meyer-Wallach measure using alternative interpretation, i.e. as
    an average over the entanglements of each qubit with the rest of the system
    (see https://arxiv.org/pdf/quant-ph/0305094.pdf).

    Args:
    =====
    ket : numpy.ndarray or list
        Vector of amplitudes in 2**N dimensions
    N : int
        Number of qubits

    Returns:
    ========
    Q : float
        Q value for input ket
    """
    ket = qutip.Qobj(ket, dims=[[2]*(N), [1]*(N)]).unit()
    print('KET=  ', ket)
    entanglement_sum = 0
    for k in range(N):
        print('value of n', k, 'PTrace: ',ket.ptrace([k])**2 )
        rho_k_sq = ket.ptrace([k])**2
        entanglement_sum += rho_k_sq.tr()

    Q = 2*(1 - (1/N)*entanglement_sum)
    return Q

if __name__ == "__main__":

    # Test #1: bell state (Q should be 1)
    n_qubits = 2
    test_state = np.zeros(2**n_qubits)
    test_state[0] = 1
    test_state[-1] = 1
    test_state /= np.linalg.norm(test_state)
    print('test state:',test_state.shape)

    print('Test #1 (Q=1):')
    Q_value = compute_Q_ptrace(ket=test_state, N=n_qubits)
    print('Q = {}\n'.format(Q_value))

    # Test #2: product state (Q should be 0)
    n_qubits = 4
    test_state = np.zeros(2**n_qubits)
    test_state[0] = 1
    test_state[1] = 1
    test_state /= np.linalg.norm(test_state)

    print('Test #2 (Q=0):')
    Q_value = compute_Q_ptrace(ket=test_state, N=n_qubits)
    print('Q = {}\n'.format(Q_value))


text:
from NeuralNetwork import NeuralNetwork
import numpy

# training_file = "mnist_dataset/mnist_train_100.csv"
# test_file = "mnist_dataset/mnist_test_10.csv"

training_file = "/Users/amirmukeri/Downloads/mnist_train.csv"
test_file = "/Users/amirmukeri/Downloads/mnist_test.csv"

input_nodes = 784
hidden_nodes = 200
output_nodes = 10
learning_rate = 0.1

n = NeuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)

training_data_file = open(training_file, 'r')
training_data_list = training_data_file.readlines()
training_data_file.close()

epochs = 5

for e in range(epochs):
    for record in training_data_list:
        all_values = record.split(',')
        inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
        inputs
        targets = numpy.zeros(output_nodes) + 0.01
        targets[int(all_values[0])] = 0.99
        n.train(inputs, targets)
        pass
    pass

test_data_file = open(test_file, 'r')
test_data_list = test_data_file.readlines()
test_data_file.close()

scorecard = []

for record in test_data_list:
    all_values = record.split(',')
    correct_label = int(all_values[0])
    inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
    outputs = n.query(inputs)
    label = numpy.argmax(outputs)
    if(label == correct_label):
        scorecard.append(1)
    else:
        scorecard.append(0)
        pass
    pass

scorecard_array = numpy.asarray(scorecard)
print("Performance:", scorecard_array.sum()/scorecard_array.size)


text:
import numpy as np
from hyperactive import Hyperactive

def ackley_function(para):
    x, y = para["x"], para["y"]

    loss = (
        -20 * np.exp(-0.2 * np.sqrt(0.5 * (x * x + y * y)))
        - np.exp(0.5 * (np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y)))
        + np.exp(1)
        + 20
    )

    return -loss

search_space = {
    "x": list(np.arange(-10, 10, 0.01)),
    "y": list(np.arange(-10, 10, 0.01)),
}

hyper = Hyperactive()
hyper.add_search(ackley_function, search_space, n_iter=100000)
hyper.run()

max_stars_repo_name: PartiallyTyped/Hyperactive

max_issues_repo_name: PartiallyTyped/Hyperactive

max_forks_repo_name: PartiallyTyped/Hyperactive


text:
import unittest
import numpy as np
import netomaton as ntm

class TestFunctions(unittest.TestCase):

    def test_neighbourhood(self):
        network = ntm.topology.from_adjacency_matrix([
           [1.0, 0.9, 0.0, 0.0, 1.0],
           [1.0, 1.0, 1.0, 0.0, 0.0],
           [0.0, 1.0, 1.0, 1.0, 0.0],
           [0.0, 0.0, 1.0, 1.0, 1.0],
           [1.0, 0.0, 0.0, 1.0, 0.8]
        ])

        initial_conditions = [2., 3., 4., 5., 6.]

        def evaluate_neighbourhoods(ctx):
            c = ctx.node_label
            if c == 0:
                np.testing.assert_equal([2., 3., 6.], ctx.neighbourhood_activities)
                np.testing.assert_equal([0, 1, 4], ctx.neighbour_labels)
                np.testing.assert_equal({0: [{'weight': 1.0}], 1: [{'weight': 1.0}], 4: [{'weight': 1.0}]},
                                        ctx.connection_states)
            elif c == 1:
                np.testing.assert_equal([2., 3., 4.], ctx.neighbourhood_activities)
                np.testing.assert_equal([0, 1, 2], ctx.neighbour_labels)
                np.testing.assert_equal({0: [{'weight': .9}], 1: [{'weight': 1.0}], 2: [{'weight': 1.0}]},
                                        ctx.connection_states)
            elif c == 2:
                np.testing.assert_equal([3., 4., 5.], ctx.neighbourhood_activities)
                np.testing.assert_equal([1, 2, 3], ctx.neighbour_labels)
                np.testing.assert_equal({1: [{'weight': 1.0}], 2: [{'weight': 1.0}], 3: [{'weight': 1.0}]},
                                        ctx.connection_states)
            elif c == 3:
                np.testing.assert_equal([4., 5., 6.], ctx.neighbourhood_activities)
                np.testing.assert_equal([2, 3, 4], ctx.neighbour_labels)
                np.testing.assert_equal({2: [{'weight': 1.0}], 3: [{'weight': 1.0}], 4: [{'weight': 1.0}]},
                                        ctx.connection_states)
            elif c == 4:
                np.testing.assert_equal([2., 5., 6.], ctx.neighbourhood_activities)
                np.testing.assert_equal([0, 3, 4], ctx.neighbour_labels)
                np.testing.assert_equal({0: [{'weight': 1.0}], 3: [{'weight': 1.0}], 4: [{'weight': .8}]},
                                        ctx.connection_states)

        ntm.evolve(initial_conditions=initial_conditions, network=network, timesteps=2,
                   activity_rule=evaluate_neighbourhoods)

    def test_init_simple_1(self):
        arr = ntm.init_simple(1)
        self.assertEqual(len(arr), 1)
        self.assertEqual(arr[0], 1)

    def test_init_simple_1_val2(self):
        arr = ntm.init_simple(1, val=2)
        self.assertEqual(len(arr), 1)
        self.assertEqual(arr[0], 2)

    def test_init_simple_3(self):
        arr = ntm.init_simple(3)
        self.assertEqual(len(arr), 3)
        self.assertEqual(arr[0], 0)
        self.assertEqual(arr[1], 1)
        self.assertEqual(arr[2], 0)

    def test_init_random_3(self):
        arr = ntm.init_random(3, empty_value=9)
        self.assertEqual(len(arr), 3)
        self.assertTrue(0 <= arr[0] <= 1)
        self.assertTrue(0 <= arr[1] <= 1)
        self.assertTrue(0 <= arr[2] <= 1)

    def test_init_random_3_k3(self):
        arr = ntm.init_random(3, k=3, empty_value=9)
        self.assertEqual(len(arr), 3)
        self.assertTrue(0 <= arr[0] <= 2)
        self.assertTrue(0 <= arr[1] <= 2)
        self.assertTrue(0 <= arr[2] <= 2)

    def test_init_random_3_k3_n1(self):
        arr = ntm.init_random(3, k=3, n_randomized=1, empty_value=9)
        self.assertEqual(len(arr), 3)
        self.assertEqual(arr[0], 9)
        self.assertTrue(0 <= arr[1] <= 2)
        self.assertEqual(arr[2], 9)

    def test_init_random_3_k3_n0(self):
        arr = ntm.init_random(3, k=3, n_randomized=0, empty_value=9)
        self.assertEqual(len(arr), 3)
        self.assertEqual(arr[0], 9)
        self.assertEqual(arr[1], 9)
        self.assertEqual(arr[2], 9)

    def test_init_random_3_k3_n3(self):
        arr = ntm.init_random(3, k=3, n_randomized=3, empty_value=9)
        self.assertEqual(len(arr), 3)
        self.assertTrue(0 <= arr[0] <= 2)
        self.assertTrue(0 <= arr[1] <= 2)
        self.assertTrue(0 <= arr[2] <= 2)

    def test_init_random_3_k3_n2(self):
        arr = ntm.init_random(3, k=3, n_randomized=2, empty_value=9)
        self.assertEqual(len(arr), 3)
        self.assertTrue(0 <= arr[0] <= 2)
        self.assertTrue(0 <= arr[1] <= 2)
        self.assertEqual(arr[2], 9)

    def test_init_random_1_k3_n1(self):
        arr = ntm.init_random(1, k=3, n_randomized=1, empty_value=9)
        self.assertEqual(len(arr), 1)
        self.assertTrue(0 <= arr[0] <= 2)

    def test_init_random_1_k3_n0(self):
        arr = ntm.init_random(1, k=3, n_randomized=0, empty_value=9)
        self.assertEqual(len(arr), 1)
        self.assertEqual(arr[0], 9)

    def test_init_simple2d_1x1(self):
        arr = ntm.init_simple2d(rows=1, cols=1)
        self.assertEqual(len(arr), 1)
        self.assertEqual(arr[0], 1)

    def test_init_simple2d_1x1_val2(self):
        arr = ntm.init_simple2d(rows=1, cols=1, val=2)
        self.assertEqual(len(arr), 1)
        self.assertEqual(arr[0], 2)

    def test_init_simple2d_2x2(self):
        arr = ntm.init_simple2d(rows=2, cols=2)
        self.assertEqual(len(arr), 4)
        self.assertEqual(arr, [0, 0, 0, 1])

    def test_init_simple2d_3x3(self):
        arr = ntm.init_simple2d(rows=3, cols=3)
        self.assertEqual(len(arr), 9)
        self.assertEqual(arr, [0, 0, 0, 0, 1, 0, 0, 0, 0])

    def test_init_simple2d_2x3(self):
        arr = ntm.init_simple2d(rows=2, cols=3)
        self.assertEqual(len(arr), 6)
        self.assertEqual(arr, [0, 0, 0, 0, 1, 0])

    def test_past_activities_single_past(self):
        network = ntm.topology.from_adjacency_matrix([
            [1, 1],
            [1, 1]
        ])

        initial_conditions = [1, 1]

        past_conditions = [
            [0, 0]
        ]

        def activity_rule(ctx):
            p = ctx.past_activities
            t = ctx.timestep
            if t == 1:
                self.assertEqual(p, [{0: 0, 1: 0}])
            if t == 2:
                self.assertEqual(p, [{0: 1, 1: 1}])
            if t == 3:
                self.assertEqual(p, [{0: 2, 1: 2}])
            return ctx.current_activity + 1

        trajectory = ntm.evolve(initial_conditions=initial_conditions, network=network,
                                activity_rule=activity_rule, timesteps=4, past_conditions=past_conditions)

        activities = ntm.get_activities_over_time_as_list(trajectory)
        np.testing.assert_equal(activities, [
            [1, 1],
            [2, 2],
            [3, 3],
            [4, 4]
        ])

    def test_past_activities_multiple_past(self):
        network = ntm.topology.from_adjacency_matrix([
            [1, 1],
            [1, 1]
        ])

        initial_conditions = [1, 1]

        past_conditions = [
            [-1, -1],
            [0, 0]
        ]

        def activity_rule(ctx):
            p = ctx.past_activities
            t = ctx.timestep
            if t == 1:
                self.assertEqual(p, [
                    {0: -1, 1: -1},
                    {0: 0, 1: 0}
                ])
            if t == 2:
                self.assertEqual(p, [
                    {0: 0, 1: 0},
                    {0: 1, 1: 1}
                ])
            if t == 3:
                self.assertEqual(p, [
                    {0: 1, 1: 1},
                    {0: 2, 1: 2}
                ])
            return ctx.current_activity + 1

        trajectory = ntm.evolve(initial_conditions=initial_conditions, network=network,
                                activity_rule=activity_rule, timesteps=4, past_conditions=past_conditions)

        activities = ntm.get_activities_over_time_as_list(trajectory)
        np.testing.assert_equal(activities, [
            [1, 1],
            [2, 2],
            [3, 3],
            [4, 4]
        ])

    def test_persist_activities_true(self):
        network = ntm.topology.from_adjacency_matrix([[1]])
        initial_conditions = {0: 1}

        trajectory = ntm.evolve(network=network, initial_conditions=initial_conditions,
                                activity_rule=lambda ctx: 1, timesteps=3, persist_activities=True)

        activities = ntm.get_activities_over_time_as_list(trajectory)
        self.assertEqual([[1], [1], [1]], activities)

    def test_persist_activities_false(self):
        network = ntm.topology.from_adjacency_matrix([[1]])
        initial_conditions = {0: 1}

        trajectory = ntm.evolve(network=network, initial_conditions=initial_conditions,
                                activity_rule=lambda ctx: 1, timesteps=3, persist_activities=False)

        activities = ntm.get_activities_over_time_as_list(trajectory)
        self.assertEqual([[1]], activities)

    def test_persist_network_true(self):
        network = ntm.topology.from_adjacency_matrix([[1]])
        initial_conditions = {0: 1}

        trajectory = ntm.evolve(network=network, initial_conditions=initial_conditions,
                                topology_rule=lambda ctx: ctx.network,
                                timesteps=3, persist_network=True)

        topology = [state.network.to_adjacency_matrix() for state in trajectory if state.network]
        self.assertEqual([[[1]], [[1]], [[1]]], topology)

    def test_persist_network_false(self):
        network = ntm.topology.from_adjacency_matrix([[1]])
        initial_conditions = {0: 1}

        trajectory = ntm.evolve(network=network, initial_conditions=initial_conditions,
                                topology_rule=lambda ctx: ctx.network,
                                timesteps=3, persist_network=False)

        topology = [state.network.to_adjacency_matrix() for state in trajectory if state.network]
        self.assertEqual([[[1]]], topology)

max_stars_repo_name: lantunes/netomaton

max_issues_repo_name: lantunes/netomaton

max_forks_repo_name: lantunes/netomaton


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""

@author: Maurizio Ferrari Dacrema
"""

import numpy as np
from Base.BaseRecommender import BaseRecommender
from Base.Recommender_utils import check_matrix

import scipy.sparse as sps

class PredefinedListRecommender(BaseRecommender):
    """PredefinedListRecommender recommender"""

    RECOMMENDER_NAME = "PredefinedListRecommenderRecommender"

    def __init__(self, URM_recommendations_items):
        super(PredefinedListRecommender, self).__init__()

        # convert to csc matrix for faster column-wise sum
        self.URM_recommendations = check_matrix(URM_recommendations_items, 'csr', dtype=np.int)

        self.URM_train = sps.csr_matrix((self.URM_recommendations.shape))

    def fit(self):
        pass

    def recommend(self, user_id, cutoff = None, remove_seen_flag=True, remove_top_pop_flag = False, remove_custom_items_flag = False):

        if cutoff is None:
            cutoff= self.URM_train.shape[1] - 1

        start_pos = self.URM_recommendations.indptr[user_id]
        end_pos = self.URM_recommendations.indptr[user_id+1]

        recommendation_list = self.URM_recommendations.data[start_pos:end_pos]

        if len(recommendation_list[:cutoff]) == 0:
            pass

        return recommendation_list[:cutoff]

    def __str__(self):
        return "PredefinedListRecommender"

max_stars_repo_name: SamanFekri/BookRecommendation

max_issues_repo_name: SamanFekri/BookRecommendation

max_forks_repo_name: SamanFekri/BookRecommendation

reason: import numpy,import scipy


text:
#!/usr/bin/python

'''
Purpose:
Initial test of EWS survivorship with filtered promoters OR DE genes
'''

import os
import glob
import lifelines
from statsmodels.stats.multitest import multipletests
from lifelines import CoxPHFitter
import numpy as np
import pandas as pd

#------------------------------------------------------------------------------
def loadAltProm(FileName):
	DF = pd.read_csv(FileName,header="infer",sep="\t")
	return DF.Transcript.values

#------------------------------------------------------------------------------
def loadClinical(FileName):
	# load clinical
	ClinicalDF = pd.read_csv(FileName,sep="\t", header="infer")
	ClinicalDF = ClinicalDF[["EWS ID", "TP53_mutation", "STAG2_mutation", "OS (months)", "Patient Status"]]
	ClinicalDF.set_index(["EWS ID"], inplace=True, drop=True)
	return ClinicalDF

#------------------------------------------------------------------------------
def loadPredData(FileName, Transcripts, ClinicalDF):
	# load and filter transcripts
	DF = pd.read_csv(FileName,header="infer",sep="\t")
	DF = DF[DF.Transcript.isin(Transcripts)]
	# index by promoter + gene name
	DF["Promoter"] = DF.GeneName.astype(str)+"_"+DF.Transcript.astype(str)
	DF.set_index("Promoter",inplace=True,drop=True)
	# keep only sample columns
	DF = DF[[c for c in list(DF) if "EWS_T" in c and "Median_Pred_H3K27ac" in c]]
	RenameCols = ["_".join(c.split("_")[:2]) for c in list(DF)]
	DF.columns= RenameCols
	# combine with Clinical data
	DF = DF.T
	DF.index.name = ClinicalDF.index.name
	CDF = pd.concat([ClinicalDF,DF],axis=1,join="inner")
	# replace text with numerical binary
	CDF = CDF.replace("no",0).replace("yes",1)
	for Term, Num in zip(["Alive","DOD","DOC","DCD"],[0,1,1,1]):
		CDF = CDF.replace(Term,Num)
	print(CDF)
	return CDF

#------------------------------------------------------------------------------
def testCoxModel(FitDF, Promoter):
	# build/fit model
	CPH = CoxPHFitter()
	CPH.fit(FitDF, duration_col="OS (months)", event_col="Patient Status")
	return CPH.summary

#------------------------------------------------------------------------------
def runExperimentCox(PredDF):
	StaticCols = ["TP53_mutation", "STAG2_mutation", "OS (months)", "Patient Status"]
	PromoterList = [c for c in list(PredDF) if c not in StaticCols]
	# first, iterate over each promoter
	ResultsList = []
	for Promoter in PromoterList:
		TotalCols = StaticCols+[Promoter]
		TempPredDF = PredDF[TotalCols]
		PartialResultsDF = testCoxModel(TempPredDF, Promoter)
		PartialResultsDF["Test"] = Promoter
		PartialResultsDF["Variables"] = ["TP53_mutation", "STAG2_mutation", Promoter]
		ResultsList.append(PartialResultsDF)
	ResultsDF = pd.concat(ResultsList,axis=0)
	FDR_Results = multipletests(ResultsDF["p"].values,alpha=.05,method="fdr_bh")
	ResultsDF["reject"] = FDR_Results[0]
	ResultsDF["p.adj"] = FDR_Results[1]
	ResultsDF.to_csv("CoxModel_All_Results_Statistics.txt",
						header=True,sep="\t",index=False)
	Parsed_Results = ResultsDF[(ResultsDF["reject"]==True)&(~ResultsDF.Variables.isin(["TP53_mutation","STAG2_mutation"]))]
	Parsed_Results["Variables"] = Parsed_Results["Variables"].str.split("_").str[1]
	Parsed_Results.to_csv("CoxModel_Parsed_Results_Statistics.txt",
						header=True,sep="\t",index=False)
	print(Parsed_Results)
	print("Finished Running Cox Model Experiment")

#------------------------------------------------------------------------------
def main():
	Transcripts = loadAltProm("FilteredList.txt")
	ClinicalDF = loadClinical("StJudeClinical_wMatchup.txt")
	PredDF = loadPredData("EWS_Ensemble_Combined_Predictions.txt",Transcripts, ClinicalDF)
	runExperimentCox(PredDF)

#------------------------------------------------------------------------------
if __name__ == '__main__':
	# Parse the arguments
	main()

reason: import numpy,from statsmodels


text:
# -*- coding:utf-8 -*-
from __future__ import print_function

import os
import numpy as np
import torch

from datasets.dataset import er_data_loader
from models.unet import UNet as u_net
from models.nested_unet import NestedUNet as u_net_plus
from models.deeplab_v3 import DeepLab as deeplab
from models.pe_net import PE_Net
from models.agnet import AG_Net as ag_net

import cv2
import torch.nn.functional as F

print("PyTorch Version: ",torch.__version__)

'''
evaluation
'''
def eval_model(opts):
    val_batch_size = opts["eval_batch_size"]
    dataset_type = opts['dataset_type']
    load_epoch = opts['load_epoch']
    gpus = opts["gpu_list"].split(',')
    gpu_list = []
    for str_id in gpus:
        id = int(str_id)
        gpu_list.append(id)
    os.environ['CUDA_VISIBLE_DEVICE'] = opts["gpu_list"]

    eval_data_dir = opts["eval_data_dir"]
    dataset_name = os.path.split(eval_data_dir)[-1].split('.')[0]

    train_dir = opts["train_dir"]
    model_type = opts['model_type']

    model_score_dir = os.path.join(str(os.path.split(train_dir)[0]), 'predict_score/' + dataset_name + '_' + str(load_epoch))
    if not os.path.exists(model_score_dir): os.makedirs(model_score_dir)

    viz_dir = train_dir.replace('checkpoints', 'viz')
    seg_save = os.path.join(viz_dir, 'seg')
    p_seg_save = os.path.join(viz_dir, 'p_seg')
    if not os.path.exists(seg_save): os.makedirs(seg_save)
    if not os.path.exists(p_seg_save): os.makedirs(p_seg_save)

    # dataloader
    print("==> Create dataloader")
    dataloader= er_data_loader(eval_data_dir, val_batch_size, dataset_type, is_train = False)

    # define network
    print("==> Create network")

    model = None

    if model_type == 'unet':
        model = u_net(1,1)
    elif model_type == 'unetPlus':
        model = u_net_plus(1,1)
    elif model_type == 'agnet':
        model = ag_net(2)
    elif model_type == 'deeplab':
        model = deeplab(backbone='resnet50', output_stride=16)
    elif model_type == 'penet':
        model = PE_Net()

    # load trained model
    pretrain_model = os.path.join(train_dir, "checkpoints_" + str(load_epoch) + ".pth")

    if os.path.isfile(pretrain_model):
        c_checkpoint = torch.load(pretrain_model)

        model.load_state_dict(c_checkpoint["model_state_dict"])

        print("==> Loaded pretrianed model checkpoint '{}'.".format(pretrain_model))

    else:
        print("==> No trained model.")
        return 0

    # set model to gpu mode
    print("==> Set to GPU mode")
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    model.to(device)
    model = torch.nn.DataParallel(model, device_ids=gpu_list)

    # enable evaluation mode
    with torch.no_grad():
        model.eval()

        total_img = 0

        for inputs in dataloader:
            images = inputs["image"].cuda()

            img_name = inputs['ID']

            total_img += len(images)

            p_seg = 0
            # unet
            if model_type == 'unet':
                p_seg = model(images)

            elif model_type == 'unetPlus':
                p_seg = model(images)
                p_seg = p_seg[-1]

            elif model_type == 'penet':
                p_seg, p_seg_down = model(images)

            # agnet
            elif model_type == 'agnet':
                out, side_5, side_6, side_7, side_8 = model(images)
                p_seg = F.softmax(side_8, dim=1)

            # deeplab
            elif model_type == 'deeplab':
                p_seg = model(images)

            for i in range(len(images)):

                print('predict image: {}'.format(img_name[i]))

                if model_type == 'agnet':
                    np.save(os.path.join(model_score_dir, img_name[i].split('.')[0] + '.npy'), p_seg[i][1].cpu().numpy().astype(np.float32))
                    cv2.imwrite(os.path.join(model_score_dir, img_name[i]), p_seg[i][1].cpu().numpy().astype(np.float32))
                else:
                    np.save(os.path.join(model_score_dir, img_name[i].split('.')[0] + '.npy'), p_seg[i][0].cpu().numpy().astype(np.float32))
                    cv2.imwrite(os.path.join(model_score_dir, img_name[i].split('.')[0] + '.tif'), p_seg[i][0].cpu().numpy().astype(np.float32))

        print("validation image number {}".format(total_img))

if __name__ == "__main__":
    opts = dict()
    opts['dataset_type'] = 'er'

    opts["eval_batch_size"] = 32
    opts["gpu_list"] = "0,1,2,3"
    opts["train_dir"] = "./train_log/er_train_aug_v1_20200727_bceloss/checkpoints"
    opts["eval_data_dir"] = "./datasets/test_er.txt"

    # model_type = [unet, unetPlus, agnet, deeplab, penet]
    opts['model_type'] = 'unetPlus'
    opts["load_epoch"] = 30

    eval_model(opts)


text:
import pygame
from pygame.math import Vector2
import numpy as np
import jupyterlab

# Pygame Properties
CAPTION = "Checkers"
WINDOW_SIZE = WINDOW_W, WINDOW_H = 600, 600
FPS = 60

# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
GRAVITY = 0.05

def draw_circle(pos: Vector2, radius: float = 1.0, width=5, color=WHITE, surface=None):
    surface = surface if surface else pygame.display.get_surface()
    pygame.draw.circle(surface, color, pos, radius, width)

def draw_rect(x, y, width, height, borderWidth=1, color=WHITE, surface=None):
    surface = surface if surface else pygame.display.get_surface()
    pygame.draw.rect(surface, color, pygame.Rect(x, y, width, height), borderWidth)

def draw_line(point1: Vector2, point2: Vector2, color=WHITE, width=1, surface=None):
    surface = surface if surface else pygame.display.get_surface()
    pygame.draw.line(surface, color, point1, point2, width)

def draw_lines(points, color=WHITE, width=1, connected=True, surface=None):
    surface = surface if surface else pygame.display.get_surface()
    pygame.draw.lines(surface, color, connected, points, width)

def draw_shape(points, width=0, color=WHITE, surface=None):
    surface = surface if surface else pygame.display.get_surface()
    pygame.draw.polygon(surface, color, points, width)


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jul  8 14:10:53 2020

@author: dberke

A script for comparing the sigma and sigma_sys distributions between fitting
runs using absolute magnitude and log(g).
"""

import argparse
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np
from tqdm import tqdm

import varconlib as vcl

parser = argparse.ArgumentParser()
era = parser.add_mutually_exclusive_group(required=True)
era.add_argument('--pre', action='store_true',
                 help="Plot the pre-fiber-change values.")
era.add_argument('--post', action='store_true',
                 help="Plot the pre-fiber-change values.")

args = parser.parse_args()

models = ('linear', 'quadratic', 'cross_term', 'quadratic_mag')

base_dir = vcl.output_dir

abs_mag_dir = base_dir / 'stellar_parameter_fits_abs_mag'
logg_dir = base_dir / 'stellar_parameter_fits'

fig = plt.figure(figsize=(8, 8), tight_layout=True)
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
ax4 = fig.add_subplot(2, 2, 4)

axes = (ax1, ax2, ax3, ax4)

bins = [x for x in range(0, 45, 4)]

for model, ax in tqdm(zip(models, axes)):

    if args.pre:
        ax.set_xlabel(r'$\sigma_\mathrm{sys}\mathrm{pre}$ (m/s)')
    elif args.post:
        ax.set_xlabel(r'$\sigma_\mathrm{sys}\mathrm{post}$ (m/s)')

    abs_mag_file = abs_mag_dir / f'{model}_corrected/{model}_fit_results.csv'
    logg_file = logg_dir / f'{model}_corrected/{model}_fit_results.csv'

    # sigma_pre, sigma_sys_pre, sigma_post, sigma_sys_post
    abs_mag_data = np.loadtxt(abs_mag_file, dtype=float, usecols=(2, 3, 5, 6),
                              delimiter=',')
    logg_data = np.loadtxt(logg_file, dtype=float, usecols=(2, 3, 5, 6),
                           delimiter=',')

    if args.pre:
        col = 1
    elif args.post:
        col = 3

    abs_mag_values = abs_mag_data[:, col]
    logg_values = logg_data[:, col]

    mag_median = np.median(abs_mag_values)
    logg_median = np.median(logg_values)

    ax.hist(abs_mag_values, bins=bins, color='Green', label='abs. mag.',
            histtype='step')
    ax.hist(logg_values, bins=bins, color='Blue', label='log(g)',
            linestyle='--',
            histtype='step')
    ax.axvline(x=mag_median, color='Green', label='abs. mag. median',
               linestyle='--', alpha=0.7)
    ax.axvline(x=logg_median, color='Blue', label='log(g) median',
               linestyle='-', alpha=0.7)
    ax.legend(title=model.capitalize())

plt.show()

max_stars_repo_name: DBerke/varconlib

max_issues_repo_name: DBerke/varconlib

max_forks_repo_name: DBerke/varconlib


text:
import unittest

from pyalink.alink import *

class TestYuhe(unittest.TestCase):

    def test_json_value_op(self):
        import numpy as np
        import pandas as pd
        data = np.array([
            ["{a:boy,b:{b1:1,b2:2}}"],
            ["{a:girl,b:{b1:1,b2:2}}"]])
        df = pd.DataFrame({"str": data[:, 0]})

        batchData = dataframeToOperator(df, schemaStr='str string', op_type='batch')

        JsonValueBatchOp().setJsonPath(["$.a", "$.b.b1"]).setSelectedCol("str").setOutputCols(["f0", "f1"]).linkFrom(
            batchData).print()


text:
"""
generate_data.py
created by Austin Poor

a file for generating fake data for the model to learn from,
based on the rules of the chrome-dino-game

used by dino_solver_deepQ.py

"""

import numpy as np

J_THRESHOLD = 160
D_THRESHOLD = 75

def get_action(x,y):
    if x < J_THRESHOLD and y > D_THRESHOLD:
        return 1.0
    else:
        return 0.0

def synthesize_rewards(x,y):
    if x < J_THRESHOLD:
        if y > D_THRESHOLD: # Jump over
            a = 1
            r = np.array([-100.0,50.0]) # Jump!
        else: # Fly over
            a = 0
            r = np.array([50.0,-100.0]) # Don't Jump!
    else: # Too far
        a = 0
        r = np.array([0.1,-0.1]) # Doesn't matter
    return r

def generate_point():
    """
    X: [tRexX,tRexY,speed,obsX,obsY]
    Y: [passReward,jumpReward]
    """
    X = np.array([
        np.random.normal(23,1),     # tRex X
        np.random.uniform(0,90),    # tRex Y
        np.random.uniform(6,12),    # speed
        np.random.uniform(-50,700), # obs1 X
        np.random.uniform(0,90)     # obs1 Y
    ])
    y = synthesize_rewards(X[3],X[4])
    return X, y

def generate_data(n_datapoints):
    X, y = zip(*(generate_point() for _ in range(n_datapoints)))
    X = np.array(X)
    y = np.array(y)
    return X, y

max_stars_repo_name: a-poor/chrome-dino-solver

max_issues_repo_name: a-poor/chrome-dino-solver

max_forks_repo_name: a-poor/chrome-dino-solver