text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jan  6 15:01:30 2020

@author: grat05
"""

import json
from glob import iglob
import pandas as pd
import numpy as np
from collections.abc import Mapping

from .parse_cmd_args import args

class DataLoader(Mapping):
    def __init__(self, data_root):
        self.data_root = data_root
        files = [glb.replace('\\','/') for glb in iglob(data_root+'/data/*/*')]
        self.files = {}
        for file in files:
            filename = file.split('/')[-1]
            pmid,ext = filename.split('.')
            self.files[pmid] = dict(filepath = file,
                                    filename = filename,
                                    ext = ext)

    def __getitem__(self, key):
        item = self.files[key]
        if not 'data' in item:
            try:
                if item['ext'] == 'json':
                    with open(item['filepath'],'r') as file:
                        data = json.load(file)
                else:
                    data = pd.read_csv(file)
                item['data'] = data
            except Exception as e:
                print("Could not read file: ", item['filepath'])
                raise e
        return item['data']

    def __iter__(self):
        return iter(self.files)

    def __len__(self):
        return len(self.files)

    def _keytransform(self, key):
        return key

# def load_all_data(data_root = args.data_root):
#     files = [glb.replace('\\','/') for glb in iglob(data_root+'/data/*/*')]
#     data = {}
#     for file in files:
#         try:
#             filename = file.split('/')[-1]
#             pmid,ext = filename.split('.')
#             if ext == 'json':
#                 js = json.load(open(file,'r'))
#                 data[pmid] = js
#             else:
#                 df = pd.read_csv(file)
#                 data[pmid] = df
#         except:
#             print("Could not read file:")
#             print(file)
#     return data

try: all_data
except NameError: all_data = DataLoader(args.data_root)

def converter(data_root = args.data_root):
    folder2pmid = {}
    for folder in (glb.replace('\\','/') for glb in iglob(data_root+'/data/*')):
        files = [glb.replace('\\','/') for glb in iglob(folder+'/*')]
        pmids = set((file.split('/')[-1].split('_')[0] for file in files))
        folder_name = folder.split('/')[-1]
        folder2pmid[folder_name] = pmids

    pmid2folder = []
    for folder,pmids in folder2pmid.items():
        for pmid in pmids:
            pmid2folder.append((pmid,folder))
    pmid2folder = sorted(pmid2folder)
    return folder2pmid, pmid2folder

def load_data_parameters(filename, sheet_name, data = all_data, default_duration=100):
    data_parameters = pd.read_excel(all_data.data_root+'/'+filename,sheet_name=sheet_name,index_col=[0,1])

    #convert to kelvin
    if 'temp ( C )' in data_parameters:
        data_parameters['temp ( C )'] += 273.15
        data_parameters.rename(columns={'temp ( C )':'temp ( K )'},inplace=True)

    #set unset duration
    if 'duration (ms)' in data_parameters:
        data_parameters.loc[data_parameters['duration (ms)'].isnull(),'duration (ms)'] = default_duration

    sub_data = extract_sub_data(data_parameters.index, data=data)

    return data_parameters, sub_data

def extract_figures(fig_names, data=all_data):
    sub_data = {}
    for idx in fig_names:
        datasets = data[idx]['datasetColl']
        sub_fig = {}
        for dataset in datasets:
            fig_data = [entry['value'] for entry in dataset['data']]
            temp_data = np.array(fig_data)
            order = np.argsort(temp_data[:,0])
            sub_fig[dataset['name']] = temp_data[order,:]
        sub_data[idx] = sub_fig
    return sub_data

def extract_sub_data(data_keys, data=all_data):
    #extract sub_data
    sub_data = {}
    for idx in data_keys:
        datasets = data[idx[0]]['datasetColl']
        for dataset in datasets:
            if dataset['name'] == idx[1]:
                fig_data = [entry['value'] for entry in dataset['data']]
                temp_data = np.array(fig_data)
                order = np.argsort(temp_data[:,0])
                sub_data[idx] = temp_data[order,:]
                break
    return sub_data


text:
"""Brachistochrone problem.

Example 4.10 from Betts, J. T. (2010). Practical methods for optimal control
and estimation using nonlinear programming - 2nd Edition. Society for
Industrial and Applied Mathematics, p215 - 216.

"""

import numpy as np
import sympy as sym

import pycollo

x, y, v, u = sym.symbols('x y v u')

problem = pycollo.OptimalControlProblem(name="Brachistochrone")
phase = problem.new_phase(name="A")
phase.state_variables = [x, y, v]
phase.control_variables = u

g = 9.81
t0 = 0
tfmin = 0
tfmax = 10
x0 = 0
y0 = 0
v0 = 0
xf = 2
yf = 2
xmin = 0
xmax = 10
ymin = 0
ymax = 10
vmin = -50
vmax = 50
umin = -np.pi / 2
umax = np.pi / 2

phase.state_equations = [v * sym.sin(u), v * sym.cos(u), g * sym.cos(u)]

phase.auxiliary_data = {}

problem.objective_function = phase.final_time_variable

phase.bounds.initial_time = 0.0
phase.bounds.final_time = [tfmin, tfmax]
phase.bounds.state_variables = [[xmin, xmax], [ymin, ymax], [vmin, vmax]]
phase.bounds.control_variables = [[umin, umax]]
phase.bounds.initial_state_constraints = {x: x0, y: y0, v: v0}
phase.bounds.final_state_constraints = {x: xf, y: yf}

# Guess
phase.guess.time = np.array([t0, tfmax])
phase.guess.state_variables = np.array([[x0, xf], [y0, yf], [v0, v0]])
phase.guess.control_variables = np.array([[0, umax]])

problem.settings.display_mesh_result_graph = True

problem.initialise()
problem.solve()

ext: py

max_stars_repo_name: NoNotCar/pycollo

max_issues_repo_name: NoNotCar/pycollo

max_forks_repo_name: NoNotCar/pycollo

reason: import numpy,import sympy


text:
import numpy as np

import torch
import torch.nn as nn

from rl.agent.core import Actor, Critic, BaseAgent
from rl.agent.normalizer import Normalizer
from rl.agent.latent_planner import ValueFunction, MlpAutoEncoder, Cluster

from rl.utils import mpi_utils, net_utils

class DistCritic(nn.Module):
    def __init__(self, env_params, args):
        super().__init__()
        self._dist_func = Critic(env_params, args)
        self.args = args
        self.gamma = args.gamma

    def forward(self, pi_inputs, actions):
        dist = self._dist_func(pi_inputs, actions)
        log_gamma = np.log(self.gamma)
        return - (1 - torch.exp(dist * log_gamma)) / (1 - self.gamma)

    def get_dist(self, pi_inputs, actions):
        dist = self._dist_func(pi_inputs, actions)
        if self.args.q_offset:
            dist += 1.0
        return dist

class DistReverseCritic(nn.Module):
    def __init__(self, env_params, args):
        super().__init__()
        self._q_func = Critic(env_params, args)
        self.args = args
        self.gamma = args.gamma

    def forward(self, pi_inputs, actions):
        q_value = self._q_func(pi_inputs, actions)
        return q_value

    def get_dist(self, pi_inputs, actions):
        q_value = self._q_func(pi_inputs, actions)
        log_gamma = np.log(self.gamma)
        dist = torch.log(1. + q_value * (1. - self.gamma)) / log_gamma
        if self.args.q_offset:
            dist += 1.0
        return dist

class Agent(BaseAgent):
    def __init__(self, env_params, args, name='agent'):
        super().__init__(env_params, args, name=name)

        self.actor = Actor(env_params, args)
        critic_func = DistCritic
        if args.use_reverse_dist_func:
            critic_func = DistReverseCritic
        self.critic = critic_func(env_params, args)

        if mpi_utils.use_mpi():
            mpi_utils.sync_networks(self.actor)
            mpi_utils.sync_networks(self.critic)

        self.actor_targ = Actor(env_params, args)
        self.critic_targ = critic_func(env_params, args)

        self.actor_targ.load_state_dict(self.actor.state_dict())
        self.critic_targ.load_state_dict(self.critic.state_dict())

        net_utils.set_requires_grad(self.actor_targ, allow_grad=False)
        net_utils.set_requires_grad(self.critic_targ, allow_grad=False)

        self.vf = ValueFunction(env_params, args)
        self.ae = MlpAutoEncoder(env_params, args)
        self.cluster = Cluster(env_params, args)

        if self.args.cuda:
            self.cuda()

        self.o_normalizer = Normalizer(size=env_params['obs'], default_clip_range=self.args.clip_range)
        self.g_normalizer = Normalizer(size=env_params['goal'], default_clip_range=self.args.clip_range)

    def cuda(self):
        self.actor.cuda()
        self.critic.cuda()
        self.actor_targ.cuda()
        self.critic_targ.cuda()
        self.vf.cuda()
        self.ae.cuda()
        self.cluster.cuda()

    def _clip_inputs(self, x):
        if type(x) == np.ndarray:
            return np.clip(x, -self.args.clip_obs, self.args.clip_obs)
        else:
            return torch.clamp(x, -self.args.clip_obs, self.args.clip_obs)

    @staticmethod
    def _concat(x, y):
        assert type(x) == type(y)
        if type(x) == np.ndarray:
            return np.concatenate([x, y], axis=-1)
        else:
            return torch.cat([x, y], dim=-1)

    def _preprocess_inputs(self, obs, goal):
        obs = self.to_2d(obs)
        goal = self.to_2d(goal)
        if self.args.clip_inputs:
            obs = self._clip_inputs(obs)
            goal = self._clip_inputs(goal)
        return obs, goal

    def _process_inputs(self, obs, goal):
        if self.args.normalize_inputs:
            obs = self.o_normalizer.normalize(obs)
            goal = self.g_normalizer.normalize(goal)
        return self.to_tensor(self._concat(obs, goal))

    def get_actions(self, obs, goal):
        obs, goal = self._preprocess_inputs(obs, goal)
        inputs = self._process_inputs(obs, goal)
        with torch.no_grad():
            actions = self.actor(inputs).cpu().numpy().squeeze()
        return actions

    def get_pis(self, obs, goal):
        obs, goal = self._preprocess_inputs(obs, goal)
        inputs = self._process_inputs(obs, goal)
        pis = self.actor(inputs)
        return pis

    def get_qs(self, obs, goal, actions):
        obs, goal = self._preprocess_inputs(obs, goal)
        inputs = self._process_inputs(obs, goal)
        actions = self.to_tensor(actions)
        qs = self.critic(inputs, actions)
        return qs

    def forward(self, obs, goal, q_target=False, pi_target=False):
        obs, goal = self._preprocess_inputs(obs, goal)
        inputs = self._process_inputs(obs, goal)
        q_net = self.critic_targ if q_target else self.critic
        a_net = self.actor_targ if pi_target else self.actor
        pis = a_net(inputs)
        qs = q_net(inputs, pis)
        return qs, pis

    def target_update(self):
        net_utils.target_soft_update(source=self.actor, target=self.actor_targ, polyak=self.args.polyak)
        net_utils.target_soft_update(source=self.critic, target=self.critic_targ, polyak=self.args.polyak)

    def normalizer_update(self, obs, goal):
        obs, goal = self._preprocess_inputs(obs, goal)
        self.o_normalizer.update(obs)
        self.g_normalizer.update(goal)
        self.o_normalizer.recompute_stats()
        self.g_normalizer.recompute_stats()

    def pairwise_value(self, obs, goal):
        obs, goal = self._preprocess_inputs(obs, goal)
        inputs = self._process_inputs(obs, goal)
        pis = self.actor(inputs)
        dist = self.critic.get_dist(inputs, pis)  # this is positive
        return -dist

    def get_dists(self, obs, goal, actions):
        obs, goal = self._preprocess_inputs(obs, goal)
        inputs = self._process_inputs(obs, goal)
        actions = self.to_tensor(actions)
        dist = self.critic.get_dist(inputs, actions)  # this is positive
        return dist

    def _process_vf_inputs(self, ag, bg):
        ag = self.to_2d(ag)
        bg = self.to_2d(bg)
        if self.args.clip_inputs:
            ag = self._clip_inputs(ag)
            bg = self._clip_inputs(bg)
        if self.args.normalize_inputs:
            ag = self.g_normalizer.normalize(ag)
            bg = self.g_normalizer.normalize(bg)
        return self.to_tensor(self._concat(ag, bg))

    def get_vf_value(self, ag, bg):
        inputs = self._process_vf_inputs(ag, bg)
        vf_value = self.vf(inputs)
        return vf_value

    def state_dict(self):
        return {'actor': self.actor.state_dict(), 'actor_targ': self.actor_targ.state_dict(),
                'critic': self.critic.state_dict(), 'critic_targ': self.critic_targ.state_dict(),
                'vf': self.vf.state_dict(), 'ae': self.ae.state_dict(), 'cluster': self.cluster.state_dict(),
                'o_normalizer': self.o_normalizer.state_dict(), 'g_normalizer': self.g_normalizer.state_dict()}

    def load_state_dict(self, state_dict):
        self.actor.load_state_dict(state_dict['actor'])
        self.actor_targ.load_state_dict(state_dict['actor_targ'])
        self.critic.load_state_dict(state_dict['critic'])
        self.critic_targ.load_state_dict(state_dict['critic_targ'])
        self.vf.load_state_dict(state_dict['vf'])
        self.ae.load_state_dict(state_dict['ae'])
        self.cluster.load_state_dict(state_dict['cluster'])
        self.o_normalizer.load_state_dict(state_dict['o_normalizer'])
        self.g_normalizer.load_state_dict(state_dict['g_normalizer'])

max_stars_repo_name: Koen-AI/world-model-as-a-graph

max_issues_repo_name: Koen-AI/world-model-as-a-graph

max_forks_repo_name: Koen-AI/world-model-as-a-graph

reason: import numpy


text:
import numpy
import theano
from keras.callbacks import EarlyStopping
from keras.callbacks import ModelCheckpoint
from keras.layers import LSTM
from keras.layers import Dense
from keras.layers import Dropout
from keras.models import load_model
from keras.models import Sequential

def configure_single_layer(input_data_dim, output_data_dim, neuron_count):
    batch_size = 1
    timestep = 1
    model = Sequential([
        LSTM(
            neuron_count,
            batch_input_shape=(batch_size, timestep, input_data_dim),
            return_sequences=False, unit_forget_bias=True,
            kernel_initializer='glorot_uniform'),
        Dropout(0.05),
        Dense(output_data_dim, activation='sigmoid'),
    ])
    model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
    return model

def configure_double_layer(input_data_dim, output_data_dim, neuron_count):
    batch_size = 1
    timestep = 1
    model = Sequential([
        LSTM(
            neuron_count,
            batch_input_shape=(batch_size, timestep, input_data_dim),
            return_sequences=True, unit_forget_bias=True,
            kernel_initializer='glorot_uniform'),
        Dropout(0.90),
        LSTM(
            neuron_count, return_sequences=False,
            unit_forget_bias=True, kernel_initializer='glorot_uniform'),
        Dropout(0.60),
        Dense(output_data_dim, activation='sigmoid'),
    ])

    model.compile(loss='mean_squared_error', optimizer='adam',
                  metrics=['accuracy'])
    return model

def select_features(impressions, features, window_size, forecast_length):
    x_data, y_data = [], []
    for i in range(len(impressions)-window_size-forecast_length-1):
        x_data.append(numpy.append(impressions[i:(i+window_size), 0],
                                   features[(i+window_size)]))
        y_data.append(
            impressions[(i+window_size):(i+window_size+forecast_length), 0])

    # reshapes dataX to be a 3D array.  Necessary for the input of the LSTM
    x_data = numpy.array(x_data)
    x_data = numpy.reshape(x_data, (x_data.shape[0], 1, x_data.shape[1]))
    y_data = numpy.array(y_data)

    return x_data, y_data

def train_model(x_data, y_data, model, model_name, epoch_number):

    checkpoint = ModelCheckpoint(model_name, monitor='val_acc', verbose=1,
                                 save_best_only=True, mode='max')
    callback_list = [checkpoint]
    # EarlyStopping(monitor='val_acc', patience=30),

    model.fit(x_data, y_data, epochs=epoch_number, callbacks=callback_list, batch_size=1, verbose=2,
              shuffle=False, validation_split=0.33)

    # model2 = load_model(model_name)
    return model

def main():
    print("LSTM Neural Network module")

if __name__ == '__main__':
    main()

max_stars_repo_name: PricelessAntonio/Foresite

max_issues_repo_name: PricelessAntonio/Foresite

max_forks_repo_name: PricelessAntonio/Foresite

reason: import numpy,import theano


text:
"""
    strides = [1, 3, 3]
    mask = tf.layers.conv3d_transpose(attention, 1,
                                      (1,3,3),
                                      strides,
                                      activation=tf.nn.relu,
                                      use_bias=False,
                                      padding="SAME",
                                      name='mask_level1',
                                      data_format="channels_last")
    strides = [1, 2, 2]
    mask = tf.layers.conv3d_transpose(mask, 1,
                                      (1,3,3),
                                      strides,
                                      activation=tf.nn.relu,
                                      use_bias=False,
                                      padding="SAME",
                                      name='mask_level2',
                                      data_format="channels_last")
    mask = tf.transpose(mask, (0,4,2,3,1),name='pre_crop')
    mask = tf.squeeze(mask, axis=1,name='mask_pre' )

    shape = mask.shape.as_list()
    crop_size = tf.constant([112,112],dtype=tf.int32)
    h_0 = (shape[1]-112)/(2.0*shape[1])
    w_0 = (shape[2]-112)/(2.0*shape[2])
    h_1 = ((shape[1]-112)/2.0 + 112)/shape[1]
    w_1 = ((shape[2]-112)/2.0 + 112)/shape[2]
    boxes = tf.constant([[h_0,w_0,h_1,w_1]]*shape[0],dtype=tf.float32, name='fixed_scale')
    box_ind = tf.constant([i for i in range(shape[0])],dtype=tf.int32)

    final_mask = tf.image.crop_and_resize(mask,
                                          boxes,
                                          box_ind,
                                          crop_size,
                                          name='mask_per_images')

"""
import tensorflow as tf
from dataset import Dataset, Dataloader
from tensorboardX import SummaryWriter
import numpy as np

# [9, 1, 16, 112, 112]
def models(inputs,class_num=101,scope='try_model'):
    with tf.variable_scope(scope):
        mid = tf.squeeze(inputs, axis=-1)

        in_channels = mid.shape.as_list()[1]
        w = tf.get_variable(name='c0_/kernel', shape=(5,5,in_channels,1))
        mid = tf.nn.depthwise_conv2d(mid,w,
                                     strides=(1,1,2,2),
                                     padding='SAME',
                                     rate=(3, 3),
                                     name='depthwise0_group',
                                     data_format='NCHW')
        mid = tf.nn.relu(mid,name='mid_1')
        multiplier = 16
        w1 = tf.get_variable(name='c1_/kernel', shape=(3,3,in_channels,multiplier))
        mid = tf.nn.depthwise_conv2d(mid, w1, strides=(1, 1, 2, 2), padding='VALID',
                                     rate=(1, 1),
                                     name='depthwise1_group',
                                     data_format='NCHW')

        mid = tf.split(mid,multiplier,axis=1,name='splits_for_channel_1')
        mid0 = tf.stack(mid[0:7],axis=4,name='stack_1')  # a new axis for channels
        mid1 = tf.stack(mid[8:15],axis=4,name='stack_2')
        shape = mid0.shape.as_list()
        mid0 = tf.reshape(mid0,(shape[0],shape[1],shape[2]*shape[3],shape[4]),name='prepare_attention_0')
        mid1 = tf.reshape(mid1,(shape[0],shape[1],shape[4],shape[2]*shape[3]),name='prepare_attention_1')
        mid = tf.matmul(mid0,mid1,name='attention_fusion')
        attention = tf.expand_dims(mid,axis=4,name='attention_dims')
        fet = tf.layers.conv3d(attention,16,(3,3,3),
                               strides=(2,2,2),
                               use_bias=False,
                               activation=tf.nn.sigmoid,
                               padding='VALID',
                               data_format='channels_last',
                               name='conv3d_0')
        fet = tf.layers.batch_normalization(fet,axis=4,momentum=0.98,epsilon=1e-5,name='batchnorm_0')
        fet = tf.layers.max_pooling3d(fet,pool_size=(1,3,3),strides=(1,2,2),name='max_pool_0')
        fet = tf.layers.conv3d(fet, 16, (3, 3, 3),
                               strides=(2, 2, 2),
                               use_bias=False,
                               activation=tf.nn.sigmoid,
                               padding='VALID',
                               data_format='channels_last',
                               name='conv3d_1')
        fet = tf.layers.batch_normalization(fet, axis=4, momentum=0.98, epsilon=1e-5, name='batchnorm_1')
        fet = tf.layers.max_pooling3d(fet,pool_size=(1,3,3),strides=(1,2,2),name='max_pool_1')
        fet = tf.layers.conv3d(fet, 32, (3, 3, 3),
                               strides=(2, 2, 2),
                               use_bias=False,
                               activation=tf.nn.sigmoid,
                               padding='VALID',
                               data_format='channels_last',
                               name='conv3d_2')
        fet = tf.layers.batch_normalization(fet, axis=4, momentum=0.98, epsilon=1e-5, name='batchnorm_2')
        fet = tf.layers.max_pooling3d(fet, pool_size=(1, 3, 3), strides=(1, 2, 2), name='max_pool_2')
        fet = tf.layers.conv3d(fet, 64, (1, 2, 2),
                               strides=(1, 1, 1),
                               use_bias=False,
                               activation=tf.nn.sigmoid,
                               padding='VALID',
                               data_format='channels_last',
                               name='conv3d_3')
        fet = tf.layers.batch_normalization(fet, axis=4, momentum=0.98, epsilon=1e-5, name='batchnorm_3')

        fet = tf.reshape(fet,(shape[0],-1),name='flatten')
        fet = tf.layers.dense(fet,class_num,activation=tf.nn.sigmoid,use_bias=True,name='L%d' % class_num)
    return fet

FLAGS = tf.flags.FLAGS
tf.flags.DEFINE_integer('batch_size', 9, 'batch_size to specify the input_placeholder')
tf.flags.DEFINE_boolean('checkpoint', False, "is there snapshot to load to continue training")
tf.flags.DEFINE_boolean('pretrained', False, 'use pretrained models for hot starting')
if __name__ == '__main__':
    tf.logging.set_verbosity(tf.logging.INFO)
    _SAMPLE_VIDEO_FRAMES = 16
    _IMAGE_SIZE = 112
    NUM_CLASS = 101
    _CHECKPOINT_PATHS = {
        'pretrained_model': 'pretrained-models/r2plus1-18/Caffe2TfR2.5d.ckpt',
        'snapshots': 'saved_models/model.ckpt'
    }
    is_checkpoint = FLAGS.checkpoint
    batch_size = FLAGS.batch_size
    use_pretrained = FLAGS.pretrained

    train_set = Dataset.DataSet(clip_length=_SAMPLE_VIDEO_FRAMES,
                                sample_step=2,
                                data_root='/home/pr606/Pictures/UCF101DATASET/ucf101',
                                annotation_path='/home/pr606/Pictures/dataset_annotations/ucf101_json_file/ucf101_01.json',
                                spatial_transform=None,
                                mode='train')
    validate_set = Dataset.DataSet(clip_length=_SAMPLE_VIDEO_FRAMES,
                                   sample_step=2,
                                   data_root='/home/pr606/Pictures/UCF101DATASET/ucf101',
                                   annotation_path='/home/pr606/Pictures/dataset_annotations/ucf101_json_file/ucf101_01.json',
                                   spatial_transform=None,
                                   mode='validation')
    train_generator = Dataloader.DataGenerator(train_set, batch_size=batch_size)
    validate_generator = Dataloader.DataGenerator(validate_set, batch_size=batch_size)
    num_train = train_generator.__len__()
    num_validate = validate_generator.__len__()

    print("training data num is %d" % num_train)  # 733
    print("validation data num is %d" % num_validate)  # 291

    graph = tf.get_default_graph()
    with graph.as_default():
        data = tf.placeholder(shape=(batch_size, _SAMPLE_VIDEO_FRAMES, _IMAGE_SIZE, _IMAGE_SIZE, 1), dtype=tf.float32)
        label = tf.placeholder(shape=(batch_size, NUM_CLASS), dtype=tf.int32)
        result = models(data,
                        class_num=101,
                        scope='at_model')
        global_step = tf.Variable(name='global_step', initial_value=0, trainable=False)
        # global_step = tf.get_variable("global_step", shape=(),trainable=False,initializer=tf.constant_initializer(0), dtype=tf.int64)

        variable_map = {}
        regular_map = {}
        extra_map = {}
        # Note: key of variable_map should be specific enough to match the unique variable
        for var in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES):
            print(var.name, var.shape)
            if 'L101' in var.name:
                extra_map[var.name.replace(":0", '')] = var
                continue
            elif 'kernel:0' in var.name:
                regular_map[var.name.replace(":0", '')] = var
                continue
            else:
                variable_map[var.name.replace(":0", '')] = var

        # rgb_loader = tf.train.Saver(var_list=dict(variable_map,**regular_map), reshape=True)
        rgb_resaver = tf.train.Saver(var_list=dict(variable_map, **regular_map, **extra_map), reshape=True)
        """
        with tf.Session() as sess:
            global_init = tf.global_variables_initializer()
            sess.run(global_init)
            rgb_loader.restore(sess, _CHECKPOINT_PATHS['pretrained_model'])
            rgb_resaver.save(sess, _CHECKPOINT_PATHS['snapshots'])
        """

        top1_prediction = tf.math.in_top_k(result, tf.argmax(label, 1), 1)
        top5_prediction = tf.math.in_top_k(result, tf.argmax(label, 1), 5)
        acc_top1 = tf.reduce_mean(tf.cast(top1_prediction, tf.float32))
        acc_top5 = tf.reduce_mean(tf.cast(top5_prediction, tf.float32))
        weight_deacy_loss = tf.reduce_mean([0.005 * tf.nn.l2_loss(var) for var in list(regular_map.values())[-6:]])
        # weight_deacy_loss = 0.0
        # entropy_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=result, labels=label), axis=0)
        entropy_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=result, labels=label), axis=0)
        learning_rate = tf.train.exponential_decay(learning_rate=0.05, global_step=global_step, decay_steps=1200,
                                                   decay_rate=0.98)
        loss = entropy_loss + weight_deacy_loss
        opt = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
        # opt = tf.train.AdamOptimizer(learning_rate=1e-3)
        # train_step = opt.minimize(loss, global_step=global_step)

        grads_var = opt.compute_gradients(loss)
        train_step = opt.apply_gradients(grads_var, global_step=global_step)
        with tf.name_scope("train_step"):
            tf.summary.scalar("accuracy_top1", acc_top1)
            tf.summary.scalar("accuracy_top5", acc_top5)
            tf.summary.scalar("entropy_loss", loss)
        update_ops = tf.group(*tf.get_collection(tf.GraphKeys.UPDATE_OPS))
        with tf.control_dependencies([train_step, update_ops]):
            train_op = tf.no_op(name='train')

        merge1 = tf.summary.merge(tf.get_collection(tf.GraphKeys.SUMMARIES, scope="train_step"))
        train_writer = tf.summary.FileWriter("./logs/train", graph)
        validate_writer = SummaryWriter("./logs/validate", graph)  # tensorboardX

        global_init = tf.global_variables_initializer()
        config = tf.ConfigProto(allow_soft_placement=True)
        # config.gpu_options.allow_growth=True
        config.gpu_options.per_process_gpu_memory_fraction = 0.85
        with tf.Session(config=config) as sess:
            sess.run(global_init)
            feed_dict = {}
            if is_checkpoint:
                rgb_resaver.restore(sess, _CHECKPOINT_PATHS['snapshots'])
                tf.logging.info('RGB checkpoint restored')
            '''according to the value of global_step of saved graph if necessary:sess.run(tf.assign(global_step, 500))'''
            #sess.run(tf.assign(global_step, 13767))
            epoch = 0
            while epoch <= 6:
                for i, (datas, labells) in enumerate(train_generator):
                    if i > num_train - 1:
                        break
                    feed_dict[data] = datas[:, :, :, :, np.newaxis]
                    feed_dict[label] = labells
                    _, Entropyloss, top1, top5, summaries = sess.run([train_op, loss, acc_top1, acc_top5, merge1],
                                                                     feed_dict=feed_dict)
                    step = global_step.eval()
                    train_writer.add_summary(summaries, step)
                    print('steps/epochs:{}/{}===> train_loss: {:.6f}, acc_top1: {:.4f}, acc_top5: {:.4f}'
                          .format(step, epoch, Entropyloss, top1, top5))
                top1 = 0.0
                top5 = 0.0
                Entropyloss = 0.0
                k = 0
                for i, (datas, labells) in enumerate(validate_generator):

                    feed_dict[data] = datas[:, :, :, :, np.newaxis]
                    feed_dict[label] = labells
                    _Entropyloss, _top1, _top5 = sess.run([loss, acc_top1, acc_top5],
                                                          feed_dict=feed_dict)
                    Entropyloss += _Entropyloss
                    top1 += _top1
                    top5 += _top5
                    k += 1
                    print("steps/epochs:{}/{}===>validate_loss:{:.6f},acc_top1:{:.4f},acc_top5: {:.4f}"
                          .format(k, epoch, _Entropyloss, _top1, _top5))
                    if i >= num_validate - 1:
                        break
                    if k >= 300:
                        break
                top1 = float(top1) / k
                top5 = float(top5) / k
                Entropyloss = float(Entropyloss) / k
                validate_writer.add_scalar("validate/mean_loss", Entropyloss, epoch)
                validate_writer.add_scalar("validate/mean_accuracy_top1", top1, epoch)
                validate_writer.add_scalar("validate/mean_accuracy_top5", top5, epoch)
                epoch += 1

                rgb_resaver.save(sess, _CHECKPOINT_PATHS['snapshots'])
            train_writer.close()
            validate_writer.close()

max_stars_repo_name: shenqiang-Yuan/GPA

max_issues_repo_name: shenqiang-Yuan/GPA

max_forks_repo_name: shenqiang-Yuan/GPA


text:
"""Courtesy of https://github.com/openai/random-network-distillation"""
import time
from copy import copy

import cv2
import gym
import numpy as np

from algorithms.utils.env_wrappers import unwrap_env

class StickyActionWrapper(gym.Wrapper):
    def __init__(self, env, p=0.25):
        super(StickyActionWrapper, self).__init__(env)
        self.p = p
        self.last_action = 0

    def reset(self):
        self.last_action = 0
        return self.env.reset()

    def step(self, action):
        if self.unwrapped.np_random.uniform() < self.p:
            action = self.last_action
        self.last_action = action
        obs, reward, done, info = self.env.step(action)
        return obs, reward, done, info

class MaxAndSkipWrapper(gym.Wrapper):
    def __init__(self, env, skip=4):
        """Return only every `skip`-th frame"""
        gym.Wrapper.__init__(self, env)
        # most recent raw observations (for max pooling across time steps)
        self._obs_to_maxpool = 2
        self._obs_buffer = np.zeros((self._obs_to_maxpool,) + env.observation_space.shape, dtype=np.uint8)
        self._skip = skip

    def reset(self, **kwargs):
        return self.env.reset(**kwargs)

    def step(self, action):
        """Repeat action, sum reward, and max over last observations."""
        total_reward, num_frames = 0.0, 0
        done = None
        for i in range(self._skip):
            obs, reward, done, info = self.env.step(action)
            num_frames += 1

            if i >= self._skip - self._obs_to_maxpool:
                self._obs_buffer[self._skip - i - 1] = obs

            total_reward += reward
            if done:
                break

        info['num_frames'] = num_frames

        # Note that the observation on the done=True frame doesn't matter
        max_frame = self._obs_buffer.max(axis=0)
        return max_frame, total_reward, done, info

class AtariVisitedRoomsInfoWrapper(gym.Wrapper):
    """This is good for summaries, to monitor training progress."""

    def __init__(self, env):
        super(AtariVisitedRoomsInfoWrapper, self).__init__(env)
        env_id = env.unwrapped.spec.id
        self.room_address = 3 if 'Montezuma' in env_id else 1
        self.visited_rooms = set()

    def get_current_room(self):
        ram = unwrap_env(self.env).ale.getRAM()
        assert len(ram) == 128
        return int(ram[self.room_address])

    def reset(self):
        return self.env.reset()

    def step(self, action):
        obs, rew, done, info = self.env.step(action)
        self.visited_rooms.add(self.get_current_room())
        if done:
            if 'episode' not in info:
                info['episode'] = {}
            info['episode'].update(visited_rooms=copy(self.visited_rooms))
            self.visited_rooms.clear()
        return obs, rew, done, info

class RenderWrapper(gym.Wrapper):
    def __init__(self, env, render_w=420, render_h=420, fps=15):
        super(RenderWrapper, self).__init__(env)
        self.w = render_w
        self.h = render_h
        self.last_frame = time.time()
        self.fps = fps

    def reset(self, **kwargs):
        return self.env.reset(**kwargs)

    def step(self, action):
        return self.env.step(action)

    def render(self, mode='human', **kwargs):
        atari_img = self.env.render(mode='rgb_array')

        if mode == 'rgb_array':
            return atari_img
        elif mode != 'human':
            raise Exception(f'Rendering mode {mode} not supported')

        atari_img = cv2.cvtColor(atari_img, cv2.COLOR_BGR2RGB)
        atari_img_big = cv2.resize(atari_img, (self.w, self.h), interpolation=cv2.INTER_NEAREST)
        cv2.imshow('atari', atari_img_big)

        since_last_frame = time.time() - self.last_frame
        wait_time_sec = max(1.0 / self.fps - since_last_frame, 0.001)
        wait_time_ms = max(int(1000 * wait_time_sec), 1)
        cv2.waitKey(wait_time_ms)
        self.last_frame = time.time()

class OneLifeWrapper(gym.Wrapper):
    def __init__(self, env):
        super(OneLifeWrapper, self).__init__(env)
        self.lives = 0
        self.die = False

    def reset(self, **kwargs):
        self.lives = 0
        self.die = False
        return self.env.reset(**kwargs)

    def step(self, action):
        obs, reward, done, info = self.env.step(action)
        game_lives = info['ale.lives']
        if game_lives < self.lives:
            self.die = True
        self.lives = game_lives

        if self.die:
            done = True
            reward = min(reward, -1.0)

        reward = max(reward, -1.0)
        return obs, reward, done, info

max_stars_repo_name: alex-petrenko/landmark-exploration

max_issues_repo_name: pelillian/landmark-exploration

max_forks_repo_name: pelillian/landmark-exploration


text:
from numpy import *

def randInitializeWeights(layers):

    num_of_layers = len(layers)
    epsilon = 0.12

    Theta = []
    for i in range(num_of_layers-1):
        # W = zeros((layers[i+1], layers[i] + 1),dtype = 'float64')
        # Instructions: Initialize W randomly so that we break the symmetry while
        #               training the neural network.
        W = (random.rand(layers[i + 1], layers[i] + 1) - 0.5) * (2 * epsilon)
        Theta.append(W)

    return Theta

max_stars_repo_path: Assignment 1 - Neural networks/randInitializeWeights.py

max_stars_repo_name: marioyc/Hand-written-Digit-Recognition

max_issues_repo_name: marioyc/Hand-written-Digit-Recognition

max_forks_repo_name: marioyc/Hand-written-Digit-Recognition

reason: from numpy


text:
import gym
from gym import error, spaces, utils
from gym.utils import seeding
from collections import Counter

import quandl
import numpy as np
from numpy import random
import pandas as pd
import logging
import pdb
from sklearn import preprocessing
import tempfile

log = logging.getLogger(__name__)
log.info('%s logger started.',__name__)

def _sharpe(Returns, freq=252) :
  """Given a set of returns, calculates naive (rfr=0) sharpe """
  return (np.sqrt(freq) * np.mean(Returns))/np.std(Returns)

def _prices2returns(prices):
  px = pd.DataFrame(prices)
  nl = px.shift().fillna(0)
  R = ((px - nl)/nl).fillna(0).replace([np.inf, -np.inf], np.nan).dropna()
  R = np.append( R[0].values, 0)
  return R

class QuandlEnvSrc(object):
  '''
  Quandl-based implementation of a TradingEnv's data source.

  Pulls data from Quandl, preps for use by TradingEnv and then
  acts as data provider for each new episode.
  '''
  q_api_key = "bB4wp5--7XrkpGZ7-gxJ"
  quandl.ApiConfig.api_key = q_api_key
  MinPercentileDays = 100
  QuandlAuthToken = ""  # not necessary, but can be used if desired
  Name = "TSE/9994" # https://www.quandl.com/search (use 'Free' filter)

  def __init__(self, days=252, name=Name, auth=QuandlAuthToken, scale=True ):
    self.name = name
    self.auth = auth
    self.days = days+1
    log.info('getting data for %s from quandl...',QuandlEnvSrc.Name)

    Stocks=['GE', 'AMD', 'F', 'AAPL', 'TWTR', 'CHK', 'MU', 'MSFT', 'CSCO', 'T', 'SNAP', 'INTC', 'WFC', 'VALE', 'PFE', 'SWN', 'NVDA', 'WFT', 'CMCSA', 'FCX', 'SIRI', 'KMI', 'XOM', 'PBR', 'RAD', 'JPM', 'VZ', 'NOK', 'C', 'ABEV', 'RIG', 'NWL', 'ORCL', 'QCOM', 'VIPS', 'KO', 'AMAT', 'TEVA', 'AKS', 'ESV', 'FEYE', 'ABX', 'SLB', 'GM', 'CTL', 'SBUX', 'GRPN', 'CX', 'DAL', 'CBL', 'PG', 'RF', 'S', 'ATVI', 'MRK', 'JD', 'MGM', 'HAL', 'MRO', 'V', 'EXPE', 'HBI', 'FOXA', 'CVS', 'HPE', 'KEY', 'NBR', 'ECA', 'EBAY', 'FDC', 'MS', 'GG', 'AIG', 'JNJ', 'CZR', 'AUY', 'DDR', 'SAN', 'PYPL', 'CLF', 'WMT', 'ITUB', 'AMZN', 'MDLZ', 'GILD', 'NKE', 'BRX', 'PBR', 'A', 'KGC', 'HPQ', 'X', 'DWDP', 'ON', 'VER', 'RRC', 'CY', 'TSLA', 'SCHW', 'PTEN']
    #if 10 stocks in stead of 100
    Stocks=['GE', 'AMD', 'F', 'AAPL', 'AIG', 'CHK', 'MU', 'MSFT', 'CSCO', 'T']

    #df = quandl.get_table('WIKI/PRICES', ticker=Stocks, qopts = { 'columns': ['ticker', 'volume','adj_close'] }, date = { 'gte': '2011-12-31', 'lte': '2016-12-31' }, paginate=True )

    #PATH_CSV="/afs/inf.ed.ac.uk/user/s17/s1793158/mlp3/10Stocks.csv"

    #GPU
    PATH_CSV=           "/Users/colinsmith/mlp3/10Stocks.csv"
    df=pd.read_csv(PATH_CSV, header=0, sep=',')

    self.NumberOfStocks=len(Stocks)

    df = df[ ~np.isnan(df.volume)][['ticker','volume', 'adj_close']]

    # we calculate returns and percentiles, then kill nans
    df = df[['ticker','adj_close','volume']]
    self.Dimension=len(list(df))

    df.volume.replace(0,1,inplace=True) # days shouldn't have zero volume..
    df['Return'] = (df.adj_close-df.adj_close.shift())/df.adj_close.shift()
    #df['Return2Day'] = (df.adj_close-df.adj_close.shift(periods=2))/df.adj_close.shift(periods=2)
    #df['Return5Day'] = (df.adj_close-df.adj_close.shift(periods=5))/df.adj_close.shift(periods=5)
    #df['Return10Day'] = (df.adj_close-df.adj_close.shift(periods=10))/df.adj_close.shift(periods=10)
    pctrank = lambda x: pd.Series(x).rank(pct=True).iloc[-1]
    names=["Stock"+str(i) for i in range(1,len(Stocks)+1)]

    for i ,j in enumerate(Stocks):
        if i==0:
            stock1=df[df['ticker'] == Stocks[i]].drop("ticker", axis=1 )
            stock1=  stock1.set_index(np.arange(0,len(stock1)))
            DF=stock1
        elif i==1:
            stock1=df[df['ticker'] == Stocks[i]].drop("ticker", axis=1 )
            stock1=  stock1.set_index(np.arange(0,len(stock1)))
            DF=DF.join(stock1, lsuffix='Stock1', rsuffix='Stock2')

        else:

            stock1=df[df['ticker'] == Stocks[i]].drop("ticker", axis=1 )
            stock1=  stock1.set_index(np.arange(0,len(stock1)))
            DF=DF.join(stock1, rsuffix=names[i])

    DF=DF.iloc[1:] # remove 1st 10
    colNames=list(DF)

    #removeRetCols = ["ReturnStock"+str(i) for i in range(1,3)]

    colNames = [i for j, i in enumerate(colNames) if j not in range(self.Dimension-1,self.NumberOfStocks*self.Dimension,self.Dimension)]

    DF[colNames] = DF[colNames].apply(lambda x: (x - x.mean()) / (x.var()))

    df=DF
    self.min_values = df.min(axis=0)
    self.max_values = df.max(axis=0)
    self.data = df
    self.step = 0

  def reset(self):
    # we want contiguous data

    self.idx = np.random.randint( low = 252, high=len(self.data.index)-self.days )
    self.step = 0

  def _step(self):
    obs = np.empty([0])
    for i in range(252):
        obs_i = self.data.iloc[(self.idx-252+i):(self.idx+i)].as_matrix()
        if i == 0:
            obs = obs_i
        else:
            obs = np.dstack((obs, obs_i))
    obs = np.moveaxis(obs, -1, 0)
    self.idx += 1
    self.step += 1
    done = self.step >= self.days

    retAllStocks=list(np.arange(self.Dimension-1,self.Dimension*self.NumberOfStocks,self.Dimension ))
    returns=self.data.iloc[:self.idx,retAllStocks] #past returns of stocks

    return obs,done,returns

                    #############################                 #########################################

                    #############################                 #########################################

class TradingSim(object) :
  """ Implements core trading simulator for single-instrument univ """

  def __init__(self, steps, trading_cost_bps = 1e-3, time_cost_bps = 1e-4,NumberOfStocks=2):

    # invariant for object life
    self.NumberOfStocks   =NumberOfStocks
    self.trading_cost_bps = trading_cost_bps
    self.time_cost_bps    = time_cost_bps
    self.steps            = steps
    # change every step
    self.step             = 0
    self.actions          = np.zeros((self.steps,self.NumberOfStocks))
    self.navs             = np.ones(self.steps)
    self.mkt_nav         = np.ones(self.steps)
    self.strat_retrns     = np.ones(self.steps)
    self.posns            = np.zeros(self.steps)
    self.costs            = np.zeros(self.steps)
    self.trades           = np.zeros(self.steps)
    self.mkt_retrns       = np.zeros((self.steps,1))
    self.total_returns    = 0
    self.negative_returns = [0]

  def reset(self):
    self.step = 0
    self.actions.fill(0)
    self.navs.fill(1)
    self.mkt_nav.fill(1)
    self.strat_retrns.fill(0)
    self.posns.fill(0)
    self.costs.fill(0)
    self.trades.fill(0)
    self.mkt_retrns.fill(0)
    self.total_returns    = 0
    self.negative_returns = [0]

  def _step(self, action, retrn ):
    """ Given an action and return for prior period, calculates costs, navs,
        etc and returns the reward and a  summary of the day's activity. """

    #bod_posn = 0.0 if self.step == 0 else self.posns[self.step-1]
    #bod_nav  = 1.0 if self.step == 0 else self.navs[self.step-1]
    #mkt_nav  = 1.0 if self.step == 0 else self.mkt_nav[self.step-1]

    self.actions = action
    #self.posns[self.step] = action - 1
    #self.trades[self.step] = self.posns[self.step] - bod_posn
    tradecosts = np.empty((10,1))
    tradecosts.fill(.0001)

    costs = np.dot(action, tradecosts)

    self.costs = costs
    reward= np.sum((retrn*action-costs), axis=1)

    newsort = 0
    sortchange = 0

    self.stdev_neg_returns = np.std(reward[reward < 0])
    if self.stdev_neg_returns == 0:
        self.stdev_neg_returns = .001

    def get_sortchange(x): return float(x)/float(self.stdev_neg_returns)
    f = np.vectorize(get_sortchange)
    sortchange = f(reward)
    newsort = sortchange.sum()
    nominal_reward = reward.sum()

    info = { 'reward': reward,  'costs':self.costs ,'nominal_reward':nominal_reward}

    self.step += 1
    return sortchange, newsort, info

  def to_df(self):
    """returns internal state in new dataframe """
    cols = ['action', 'bod_nav', 'mkt_nav','mkt_return','sim_return',
            'position','costs', 'trade' ]
    rets = _prices2returns(self.navs)
    #pdb.set_trace()
    df = pd.DataFrame( )
    """
        {'action':     self.actions, # today's action (from agent)
                          'bod_nav':    self.navs,    # BOD Net Asset Value (NAV)
                          'mkt_nav':  self.mkt_nav,
                          'mkt_return': self.mkt_retrns,
                          'sim_return': self.strat_retrns,
                          'position':   self.posns,   # EOD position
                          'costs':  self.costs,   # eod costs
                          'trade':  self.trades },# eod trade
                         columns=cols)
                             """
    return df

                    #############################                 #########################################

                    #############################                 #########################################

class TradingEnv(gym.Env):
  """This gym implements a simple trading environment for reinforcement learning.

  The gym provides daily observations based on real market data pulled
  from Quandl on, by default, the SPY etf. An episode is defined as 252
  contiguous days sampled from the overall dataset. Each day is one
  'step' within the gym and for each step, the algo has a choice:

  SHORT (0)
  FLAT (1)
  LONG (2)

  If you trade, you will be charged, by default, 10 BPS of the size of
  your trade. Thus, going from short to long costs twice as much as
  going from short to/from flat. Not trading also has a default cost of
  1 BPS per step. Nobody said it would be easy!

  At the beginning of your episode, you are allocated 1 unit of
  cash. This is your starting Net Asset Value (NAV). If your NAV drops
  to 0, your episode is over and you lose. If your NAV hits 2.0, then
  you win.

  The trading env will track a buy-and-hold strategy which will act as
  the benchmark for the game.

  """
  metadata = {'render.modes': ['human']}

  def __init__(self):
    self.days = 252
    self.src = QuandlEnvSrc(days=self.days)
    self.sim = TradingSim(steps=self.days, trading_cost_bps=1e-3,
                          time_cost_bps=1e-4,NumberOfStocks=self.src.NumberOfStocks)

    self.action_space =  spaces.Box(low=-1, high=1, shape=(self.src.NumberOfStocks,))

    self.observation_space= spaces.Box( self.src.min_values,
                                        self.src.max_values)
    self._reset()

  def _configure(self, display=None):
    self.display = display

  def _seed(self, seed=None):
    self.np_random, seed = seeding.np_random(seed)
    return [seed]

  def _step(self, action):
    #assert self.action_space.contains(action), "%r (%s) invalid"%(action, type(action))
    observation, done, Returns = self.src._step()
    retAllStocks=list(np.arange(self.src.Dimension-1,self.src.Dimension*self.src.NumberOfStocks,self.src.Dimension ))
    yret = observation[:,-1,retAllStocks]

    reward, sort, info = self.sim._step( action, yret )

    return observation, reward, done, sort, info, Returns

  def _reset(self):
    self.src.reset()
    self.sim.reset()
    out=self.src._step()#,self.src._step()[2]

    return out[0], out[2]#changes this form [0] to this

  def _render(self, mode='human', close=False):
    #... TODO
    pass

  # some convenience functions:

  def run_strat(self,  strategy, return_df=True):
    """run provided strategy, returns dataframe with all steps"""
    observation = self.reset()
    done = False
    while not done:
      action = strategy( observation, self ) # call strategy
      observation, reward, done, info = self.step(action)

    return self.sim.to_df() if return_df else None

  def run_strats( self, strategy, episodes=1, write_log=True, return_df=True):
    """ run provided strategy the specified # of times, possibly
        writing a log and possibly returning a dataframe summarizing activity.

        Note that writing the log is expensive and returning the df is moreso.
        For training purposes, you might not want to set both.
    """
    logfile = None
    if write_log:
      logfile = tempfile.NamedTemporaryFile(delete=False)
      log.info('writing log to %s',logfile.name)
      need_df = write_log or return_df

    alldf = None

    for i in range(episodes):
      df = self.run_strat(strategy, return_df=need_df)
      if write_log:
        #df.to_csv(logfile, mode='a')
        if return_df:
          alldf = df if alldf is None else pd.concat([alldf,df], axis=0)

    return alldf

                    #############################                 #########################################

                    #############################                 #########################################

reason: import numpy,from numpy


text:
import numba
import numpy as np
import argparse
import time

run_parallel = numba.config.NUMBA_NUM_THREADS > 1

@numba.njit(parallel=run_parallel, fastmath=True)
def logistic_regression(Y, X, w, iterations):
    for i in range(iterations):
        w -= np.dot(((1.0 / (1.0 + np.exp(-Y * np.dot(X, w))) - 1.0) * Y), X)
    return w

def main():
    parser = argparse.ArgumentParser(description='Logistic Regression.')
    parser.add_argument('--dimension', dest='dimension', type=int, default=10)
    parser.add_argument('--points', dest='points', type=int, default=20000000)
    parser.add_argument('--iterations', dest='iterations',
                        type=int, default=30)
    args = parser.parse_args()

    np.random.seed(0)
    D = 3
    N = 4
    iterations = 10
    points = np.ones((N, D))
    labels = np.ones(N)
    w = 2.0 * np.ones(D) - 1.3
    t1 = time.time()
    w = logistic_regression(labels, points, w, iterations)
    compiletime = time.time() - t1
    print("SELFPRIMED ", compiletime)
    print("checksum ", w)

    D = args.dimension
    N = args.points
    iterations = args.iterations
    print("D=", D, " N=", N, " iterations=", iterations)
    points = np.random.random((N, D))
    labels = np.random.random(N)
    w = 2.0 * np.ones(D) - 1.3
    t2 = time.time()
    w = logistic_regression(labels, points, w, iterations)
    selftimed = time.time() - t2
    print("SELFTIMED ", selftimed)
    print("checksum: ", np.sum(w))

if __name__ == '__main__':
    main()

max_stars_repo_name: uw-ipd/numba

max_issues_repo_name: uw-ipd/numba

max_forks_repo_name: uw-ipd/numba

reason: import numpy,import numba