text:
"""
Tools for analysis of location data.

Author: Jonas Busk (jonasbusk@gmail.com)
"""

from datetime import datetime
import warnings

from geopy.distance import geodesic
import numpy as np
import pandas as pd
from sklearn.cluster import DBSCAN

# preprocessing

def preprocess(df, min_samples_per_day=1, inplace=False):
    """
    Preprocess location data and remove outliers.

    :param df: dataframe of location points.
    :return: preprocessed dataframe of location points.
    """
    required_columns = ['user_id', 'timestamp', 'longitude', 'latitude']
    speed_of_sound = 343  # m/s

    if not inplace:
        df = df.copy()

    # validate input
    assert all(c in df.columns for c in required_columns)
    assert not df.empty

    # filter
    df = df[required_columns]
    df.dropna(inplace=True)

    # rename columns
    df.rename(columns={'latitude': 'lat', 'longitude': 'lon'}, inplace=True)

    # create time columns
    df['datetime'] = df['timestamp'].map(lambda t: datetime.fromtimestamp(t / 1000))
    df['datetime'] = df['datetime'].dt.round('S')  # round seconds
    df['date'] = df['datetime'].dt.date.astype('datetime64[ns]')
    df['hour'] = df['datetime'].dt.hour

    # drop dublicates
    # dropping on datetime drops observations within the same second
    df.drop_duplicates(subset=['user_id', 'datetime'], keep='first', inplace=True)

    # compute delta columns and remove outliers
    df = _compute_delta_columns(df)

    # drop observations where time has progressed but the location has not
    # changed: delta_seconds > 0, delta_meters==0
    # this prevents an issue where an old location is cached and used at a later
    # time, such as when using flight mode
    df = df[(df['delta_meters'] > 0) | (df['delta_seconds'] == 0)].copy()

    # recompute delta columns
    df = _compute_delta_columns(df)

    # drop speeds faster than the speed of sound
    df = df[(df.speed_in < speed_of_sound)]
    # repeat until no more rows are dropped
    while True:
        N = df.shape[0]
        df = _compute_delta_columns(df)
        df = df[(df.speed_in < speed_of_sound) & (df.speed_out < speed_of_sound)]
        if N == df.shape[0]:
            break

    # filter minimum number of samples per day
    df = df[df.groupby(['user_id', 'date']).lat.transform('count') >= min_samples_per_day]

    # rename columns
    df.rename(columns={'lat': 'latitude', 'lon': 'longitude'}, inplace=True)

    return df

def _compute_delta_columns(df):
    """Compute delta columns in place."""
    df.sort_values(['user_id', 'datetime'], inplace=True)

    # compute delta distance in meters (distance from previous observation)
    df['delta_meters'] = np.concatenate(df.groupby('user_id').apply(
        lambda x: haversine(x.lat.values, x.lon.values,
                            x.lat.shift().values, x.lon.shift().values)
    ).values)
    df['delta_meters'].fillna(0, inplace=True)

    # compute delta seconds (seconds since previous observation)
    datetime1 = df.groupby('user_id')['datetime'].shift()
    df['delta_seconds'] = (df['datetime'] - datetime1).dt.total_seconds().fillna(0)

    # compute speed in meters per second
    df['speed_in'] = (df['delta_meters'] / df['delta_seconds']).fillna(0)
    df['speed_out'] = df['speed_in'].shift(-1).fillna(0)
    df['delta_speed'] = df['speed_out'] - df['speed_in']

    return df

# utils

def haversine(lat1, lon1, lat2, lon2, earth_radius=6371000):
    """
    Calculate the great circle distance between two points on earth.

    This is not the most accurate method, but it is vectorized and fast.
    For accurate distances, use geopy.distance.geodesic

    :return: distance in meters.
    """
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        lat1, lon1, lat2, lon2 = np.radians([lat1, lon1, lat2, lon2])
        a = np.sin((lat2 - lat1) / 2.0) ** 2 + \
            np.cos(lat1) * np.cos(lat2) * np.sin((lon2 - lon1) / 2.0) ** 2
        return earth_radius * 2 * np.arcsin(np.sqrt(a))

# stops, places and moves

"""
Stops, places and moves location analysis.

Definitions:

- Location data is collected as a sequence of location samples with varying
sample frequency and accuracy.
- Places are locations of relevance to the user, such as home or workplace and
are described by their coordinates and an ID.
- Stops are specific visits to one of those places, described by their
coordinates along with arrival and departure time. A stop is always associated
with exactly one place while a place can be associated with many stops. Stops
are always non-overlapping in time.
- Moves are sequences of location points between stops and are described by
departure and arrival time, origin and destination place and the distance of
the move.
"""

def get_stops_places_and_moves(df, stop_duration=15, stop_dist=25,
                               place_dist=25, move_duration=5, move_dist=50,
                               merge=True, merge_dist=25, merge_time=5,
                               distf=lambda a, b: geodesic(a, b).meters):
    """
    Extract stops, places and moves for one user.

    1. Compute stops as spatio-temporal groups of location samples.
    2. Compute places by clustering stops based only on spatial location.
    3. Compute moves between stops.

    :param df: dataframe of location points with columns: user_id, datetime, latitude, longitude.
    :param stop_duration: minimum duration of a stop in specified in minutes.
    :param stop_dist: maximum distance between first point and all other points in a stop.
    :param place_dist: maximum distance between stops in a cluster specified in meters.
    :param move_duration: minimum duration of a move measured in minutes.
    :param move_dist: minimum distance of a move measured in meters.
    :param distf: distance function of the form: ((lat, lon),(lat, lon)) --> (distance in meters)
    :return: dataframe of labeled stops, dataframe of clusters and dataframe of moves.
    """
    REQUIRED_COLUMNS = ['user_id', 'datetime', 'longitude', 'latitude']
    # validate input
    assert all(c in df.columns for c in REQUIRED_COLUMNS)
    assert df.user_id.nunique() == 1
    # prepare data
    df = df.rename(columns={'latitude': 'lat', 'longitude': 'lon'})
    df = df.sort_values(by='datetime')
    # extract stops, places and moves
    stops = get_stops(df, stop_duration, stop_dist, distf)
    if merge and len(stops) > 1:
        stops = merge_stops(stops, merge_dist, merge_time, distf)
    stops, places = get_places(stops, place_dist, distf)
    moves = get_moves(df, stops, move_duration, move_dist, distf)
    # rename columns
    stops.rename(columns={'lat': 'latitude', 'lon': 'longitude'}, inplace=True)
    places.rename(columns={'lat': 'latitude', 'lon': 'longitude'}, inplace=True)
    moves.rename(columns={'from_lat': 'from_latitude', 'from_lon': 'from_longitude',
                 'to_lat': 'to_latitude', 'to_lon': 'to_longitude'}, inplace=True)
    return stops, places, moves

def get_stops_places_and_moves_daily(df, stop_duration=15, stop_dist=25,
                                     place_dist=25, move_duration=5, move_dist=50,
                                     merge=True, merge_dist=25, merge_time=5,
                                     distf=lambda a, b: geodesic(a, b).meters):
    """
    Extract stops, places and moves for one user.

    Assume multiple days of data and group by date when extracting stops and moves.

    1. Compute stops as spatio-temporal groups of location samples.
    2. Compute places by clustering stops based only on spatial location.
    3. Compute moves between stops.

    :param df: dataframe of location points with columns: user_id, datetime, latitude, longitude.
    :param stop_duration: minimum duration of a stop in specified in minutes.
    :param stop_dist: maximum distance between first point and all other points in a stop.
    :param place_dist: maximum distance between stops in a cluster specified in meters.
    :param move_duration: minimum duration of a move measured in minutes.
    :param move_dist: minimum distance of a move measured in meters.
    :param distf: distance function of the form: ((lat, lon),(lat, lon)) --> (distance in meters)
    :return: dataframe of labeled stops, dataframe of clusters and dataframe of moves.
    """
    REQUIRED_COLUMNS = ['user_id', 'datetime', 'date', 'longitude', 'latitude']
    # validate input
    assert all(c in df.columns for c in REQUIRED_COLUMNS)
    assert df.user_id.nunique() == 1
    # prepare data
    df = df.rename(columns={'latitude': 'lat', 'longitude': 'lon'})
    df = df.sort_values(by='datetime')
    # extract stops, places and moves
    stops = df.groupby('date') \
              .apply(lambda d: get_stops(d, stop_duration, stop_dist, distf)) \
              .reset_index(level=0).reset_index(drop=True)
    if merge and len(stops) > 1:
        stops = stops.groupby('date') \
                     .apply(lambda d: merge_stops(d, merge_dist, merge_time, distf)) \
                     .reset_index(drop=True)
    stops, places = get_places(stops, place_dist, distf)
    moves = df.groupby('date') \
              .apply(lambda d: get_moves(d, stops, move_duration, move_dist, distf)) \
              .reset_index(level=0).reset_index(drop=True)
    # ensure user_id is first column after groupby date
    if 'user_id' in stops.columns:
        stops = stops[['user_id'] + [c for c in stops.columns if not c == 'user_id']]
    if 'user_id' in moves.columns:
        moves = moves[['user_id'] + [c for c in moves.columns if not c == 'user_id']]
    # rename columns
    stops.rename(columns={'lat': 'latitude', 'lon': 'longitude'}, inplace=True)
    places.rename(columns={'lat': 'latitude', 'lon': 'longitude'}, inplace=True)
    moves.rename(columns={'from_lat': 'from_latitude', 'from_lon': 'from_longitude',
                 'to_lat': 'to_latitude', 'to_lon': 'to_longitude'}, inplace=True)
    return stops, places, moves

def get_stops(df, min_duration, dist, distf):
    """
    Compute stops for one user with distance grouping algorithm.

    Location points are grouped sequentially. If a point is too far from the
    current group medain, a new group is formed. Initially this strategy creates
    a lot of groups, which are then filtered by minimum duration. This leaves
    groups where movement stopped for some time.

    :param df: dataframe of location points sorted chronologically with columns:
               user_id, datetime, lat, lon.
    :param min_duration: minimum duration of a stop measured in minutes.
    :param dist: maximum distance between points and the median point in a stop.
    :param distf: distance function of the form: ((lat,lon),(lat,lon)) --> (meters)
    :return: dataframe of stops.
    """
    stops = []
    i, N = 0, len(df)
    while i < N:
        j = i + 1
        g = df.iloc[i:j]  # stop
        c = (g.lat.median(), g.lon.median())  # centroid
        while j < N and distf(c, (df.iloc[j].lat, df.iloc[j].lon)) <= dist:
            j += 1
            g = df.iloc[i:j]
            c = (g.lat.median(), g.lon.median())
        stops.append([c[0], c[1], g.shape[0], g.datetime.values[0], g.datetime.values[-1]])
        i = j
    stops = pd.DataFrame(stops, columns=['lat', 'lon', 'samples', 'arrival', 'departure'])
    stops.insert(0, 'user_id', df.user_id.values[0])
    stops['duration'] = (stops.departure - stops.arrival).dt.total_seconds() / 60
    stops = stops[stops.duration >= min_duration]
    stops.reset_index(drop=True, inplace=True)
    return stops

def merge_stops(stops, dist=50, time=5, distf=lambda a, b: geodesic(a, b).meters):
    """
    Merge stops that are close in time and space and have no stops between.

    :param stops: dataframe of stops with columns: [lat, lon].
    :param dist: minimum distance between stops in meters.
    :param time: minimum time between stops in minutes.
    :param distf: distance function of the form: ((lat, lon),(lat, lon)) --> (meters)
    :return: dataframe of merged stops.
    """
    if len(stops) < 2:
        return stops  # nothing to merge
    stops = stops.copy()
    # compute delta columns
    stops['lat1'], stops['lon1'] = stops.lat.shift().bfill(), stops.lon.shift().bfill()
    stops['delta_meters'] = stops.apply(lambda r: distf((r.lat, r.lon), (r.lat1, r.lon1)), axis=1)
    stops['delta_seconds'] = (stops['arrival'] - stops['departure'].shift()) \
        .dt.total_seconds().fillna(0)
    # create merge identifier
    stops['merge'] = stops.index.values
    stops.loc[(stops.delta_meters <= dist) & (stops.delta_seconds <= time * 60), 'merge'] = np.nan
    stops['merge'].values[0] = stops.index.values[0]
    stops['merge'] = stops['merge'].ffill()

    # merge group of stops to one stop
    def merge(g):
        r = g.iloc[0].copy()
        r.lat = g.lat.mean()
        r.lon = g.lon.mean()
        r.samples = g.samples.sum()
        r.departure = g.iloc[-1].departure
        r.duration = (r.departure - r.arrival).total_seconds() / 60
        return r

    stops = stops.groupby('merge').apply(merge).reset_index(drop=True)
    # cleanup and return
    stops.drop(columns=['lat1', 'lon1', 'delta_meters', 'delta_seconds', 'merge'], inplace=True)
    return stops

def get_places(stops, dist, distf):
    """
    Compute places for one user with DBSCAN algorithm.

    Compute places as clusters of stops and assign place labels to stops.

    :param stops: dataframe of stops with columns: [lat, lon].
    :param dist: maximum distance between stops in a cluster measured in meters.
    :param distf: distance function of the form: ((lat, lon),(lat, lon)) --> (meters)
    :return: dataframe of labeled stops and dataframe of places.
    """
    if stops.empty:
        stops['place'] = []
        places = pd.DataFrame(columns=['user_id', 'place', 'lat', 'lon', 'duration', 'stops'])
    else:
        points = stops[['lat', 'lon']].values
        dbs = DBSCAN(dist, min_samples=1, metric=distf).fit(points)
        stops['place'] = dbs.labels_
        places = stops.groupby('place').agg({
            'lat': np.median,
            'lon': np.median,
            'duration': np.sum,
            'samples': len,
        }).reset_index()
        places.rename(columns={'samples': 'stops'}, inplace=True)
        places.insert(0, 'user_id', stops.user_id.values[0])
    return stops, places

def get_moves(df, stops, min_duration, min_dist, distf):
    """
    Get moves defined as sequences of location points in between stops.

    :param df: dataframe of location points sorted chronologically including columns:
               [user_id, date, datetime, lat, lon].
    :param stops: dataframe of stops including columns: [place, arrival and departure].
    :param min_duration: minimum duration of a move measured in minutes.
    :param min_dist: minimum distance of a move measured in meters.
    :param distf: distance function of the form: ((lat, lon),(lat, lon)) --> (meters)
    :return: dataframe of moves.
    """
    moves = []
    if 'date'in stops.columns:
        stops = stops[stops.date == df.date.values[0]]
    departure = df.datetime.min()
    prev_place = np.nan
    for index, stop in stops.iterrows():
        g = df[(df.datetime >= departure) & (df.datetime <= stop.arrival)]
        if not g.empty:
            moves.append([g.lat.values[0], g.lon.values[0],
                          g.lat.values[-1], g.lon.values[-1], g.shape[0],
                          departure, stop.arrival, prev_place, stop.place,
                          _move_length(g, distf)])
        departure = stop.departure
        prev_place = stop.place
    else:
        g = df[df.datetime >= departure]
        if not g.empty:
            moves.append([g.lat.values[0], g.lon.values[0],
                          g.lat.values[-1], g.lon.values[-1], g.shape[0],
                          departure, g.datetime.max(), prev_place, np.nan,
                          _move_length(g, distf)])
    moves = pd.DataFrame(moves, columns=['from_lat', 'from_lon', 'to_lat', 'to_lon',
                                         'samples', 'departure', 'arrival',
                                         'from_place', 'to_place', 'distance'])
    moves.insert(0, 'user_id', df.user_id.values[0])
    moves['duration'] = (moves.arrival - moves.departure).dt.total_seconds() / 60
    moves['mean_speed'] = moves.distance / (moves.duration * 60)
    moves = moves[(moves.duration >= min_duration) & (moves.distance >= min_dist)]
    moves.reset_index(drop=True, inplace=True)
    return moves

def _move_length(move, distf):
    """
    Compute length of a move as the sum of distance between points.

    :param move: dataframe with columns: lat and lon.
    :param distf: distance function of the form: ((lat, lon),(lat, lon)) --> (meters)
    :return: length of the move in meters.
    """
    if len(move) <= 1:
        return 0
    g = move.copy()
    g['lat1'] = g.lat.shift().bfill()
    g['lon1'] = g.lon.shift().bfill()
    return g.apply(lambda r: distf((r.lat, r.lon), (r.lat1, r.lon1)), axis=1).sum()

# time spent at places

def get_time_spent_at_place_hours_of_day(stops, place_labels=None, days=range(7)):
    """
    Compute proportion of time spent at specified places for each hour of the day.

    :param place_labels: list of place labels to consider.
    :param stops: dataframe of labeled stops.
    :param days: list of weekdays (0-6) to consider in the analysis.
    :return: dataframe with labels as columns and a row for each hour of the day (0-23).
    """
    place_labels = place_labels if place_labels is not None else stops.place.unique()
    hours = pd.DataFrame(0, index=range(24), columns=list(place_labels))
    for p in place_labels:
        for index, stop in stops[stops.place == p].iterrows():
            h = pd.date_range(stop.arrival, stop.departure, freq='H')
            hours[p] += np.histogram(h[h.dayofweek.isin(days)].hour, bins=np.arange(25))[0]
    hours = hours.div(hours.sum(axis=1), axis=0)  # normalize
    return hours

def add_time_spent_column(places, hours, start, end):
    """
    Add 'time spent in interval' column to places dataframe.

    :param places: dataframe of places.
    :param hours: time spent at places for each hour of the day.
    :param start: start hour of interval (0-23).
    :param end: end hour of interval (0-23).
    :return: dataframe of places with time spent column.
    """
    column = 't_%dto%d' % (start, end)
    places[column] = np.nan
    for label in hours.columns:
        if start < end:
            time = hours[label][start:end].sum() / float(end - start)
        else:
            time = (hours[label][start:].sum() + hours[label][:end].sum()) \
                / float(24 - start + end)
        places.loc[places.place == label, column] = time
    return places

# routine index

def get_routine_indices(stops):
    """
    Compute routine indices per user per day.

    :param stops: dataframe of stops.
    :return: dataframe of routine indices.
    """
    res = stops.groupby(['user_id', 'date']).apply(
        lambda d: routine_index(stops[stops.user_id == d.user_id.unique()[0]], d.date.unique()[0]))
    res.name = 'routine_index'
    res = res.reset_index()
    return res

def routine_index(stops, date):
    """
    Compute routine index for a user on a given day.

    The routine index is a number between 0 and 1:
    - 0 means the distance to other days is small, i.e. the day is similar to other days.
    - 1 means the distance to other days is large, i.e. the day is different from other days.

    :param stops: dataframe of stops for a given user.
    :param date: the date for which to compute the routine index.
    :return: routine index as a number between 0 and 1.
    """
    assert stops.user_id.nunique() == 1
    assert date in stops.date.values
    if stops.date.nunique() == 1:
        return 0  # if there is only one date, we define the routine index to 0
    day = stops[stops.date == date]  # stops of the day for which to compute the routine index
    hist = stops[stops.date != date]  # stops of all other days
    return hist.groupby('date').apply(lambda d: routine_index_difference(d, day)).mean()

def routine_index_difference(day1, day2):
    """
    Hour by hour difference between two days.

    :param day1: dataframe of stops from one day.
    :param day2: dataframe of stops from another day.
    :return: difference between day1 and day2 as a number between 0 and 1.
    """
    # function for converting a stop row to a tuple: (place id, list of hours as integers)
    def stop_row_to_tuple(s):
        return (s.place,
                pd.date_range(s.arrival, s.departure, freq='H').to_series().dt.hour.values)
    d1 = day1.apply(stop_row_to_tuple, axis=1).values
    d2 = day2.apply(stop_row_to_tuple, axis=1).values
    hours = np.zeros(24, dtype=bool)  # 24 hour bins
    for place1, hours1 in d1:
        for place2, hours2 in d2:
            if place1 == place2:
                # if place is the same, set the overlapping hours to True
                hours[np.intersect1d(hours1, hours2)] = True
    return 1 - hours.mean()

# additional location features

def radius_of_gyration(stops, distf=lambda a, b: geodesic(a, b).meters):
    """
    Compute radius of gyration feature from stops.

    The deviation from the centroid of the stops.

    :param stops: dataframe of stops.
    :param distf: distance function of the form: ((lat, lon),(lat, lon)) --> (distance in meters)
    :return: radius of gyration in meters.
    """
    if stops.empty:
        return 0.0
    centroid = (stops.latitude.mean(), stops.longitude.mean())
    d = stops.apply(lambda r: distf((r.latitude, r.longitude), centroid), axis=1)
    return np.sqrt((stops.duration * d**2).sum() / stops.duration.sum())

def std_of_displacements(stops, distf=lambda a, b: geodesic(a, b).meters):
    """
    Compute standard deviation of displacements feature from stops.

    The standard deviation of distances between subsequent stops.

    :param stops: dataframe of stops.
    :param distf: distance function of the form: ((lat, lon),(lat, lon)) --> (distance in meters)
    :return: standard deviation of displacements in meters.
    """
    if len(stops) < 2:
        return 0.0
    dis = [distf((stops.iloc[i].latitude, stops.iloc[i].longitude),
                 (stops.iloc[i + 1].latitude, stops.iloc[i + 1].longitude))
           for i in range(len(stops) - 1)]
    return np.std(dis)

def log_variance(locations):
    """
    Compute location variance feature from location data.

    Logarithm of combined variance of latitude and longitude values.

    :param locations: dataframe of location data with columns latitude and longitude.
    :return: location variance.
    """
    if len(locations) < 2:
        return 0.0
    return np.log(locations.latitude.var() + locations.longitude.var() + 1)

def entropy(stops):
    """
    Compute entropy of time spent at different stops feature from stops.

    :param stops: dataframe of stops.
    :return: entropy of time spent at different stops.
    """
    if stops.empty:
        return 0.0
    ps = stops.groupby(stops.place).sum().duration / stops.duration.sum()
    return -ps.map(lambda p: p * np.log(p)).sum()

max_stars_repo_name: thomasnilsson/mobility-features-thesis

max_issues_repo_name: thomasnilsson/mobility-features-thesis

max_forks_repo_name: thomasnilsson/mobility-features-thesis


text:
'''
@date: 2021.03.10
@author: Ruixin Lee
用(*.fif)格式的样本文件来进行一系列的处理（例如：提取特征值并保存为(*.npy)文件格式）
从而计算特征矩阵的一些特点（例如，尺寸大小等）
为后续特征层融合做准备
'''

def myFunction():
    import numpy as np
    x = np.load('../EEG_1.npy')
    print(x.shape)

if __name__ == "__main__":
    # folder_path = '../../data/mahnob_example/EEG_files/'
    # original_filename = 'origin.fif'
    # label_filename = 'label.csv'
    myFunction()

ext: py

reason: import numpy


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Python version: 3.6

import os
import copy
import time
import pickle
import numpy as np
from tqdm import tqdm

import torch
from tensorboardX import SummaryWriter
#from torch.utils.tensorboard import SummaryWriter

from options import args_parser
from update import LocalUpdate, test_inference
from models import MLP, CNNMnist, CNNFashion_Mnist, CNNCifar
from utils import get_dataset, average_weights, exp_details, set_device, build_model
# os.environ['CUDA_VISIBLE_DEVICES'] ='0'

from sys import exit
#from torchsummary import summary

if __name__ == '__main__':
    start_time = time.time()

    # define paths
    path_project = os.path.abspath('..')
    logger = SummaryWriter('../logs')

    args = args_parser()
    exp_details(args)

    # Select CPU or GPU
    device = set_device(args)

    # load dataset and user groups
    train_dataset, test_dataset, user_groups = get_dataset(args)

    # BUILD MODEL
    global_model = build_model(args, train_dataset)

    # Set the model to train and send it to device.
    global_model.to(device)
    # Set model to use Floating Point 16
    global_model.to(dtype=torch.float16)  ##########
    global_model.train()
    print(global_model)

    # MODEL PARAM SUMMARY
    pytorch_total_params = sum(p.numel() for p in global_model.parameters())
    print("Model total number of parameters: ", pytorch_total_params)
    # print(global_model.parameters())

    # copy weights
    global_weights = global_model.state_dict()

    # Training
    train_loss, train_accuracy = [], []
    val_acc_list, net_list = [], []
    cv_loss, cv_acc = [], []
    print_every = 1
    val_loss_pre, counter = 0, 0
    testacc_check, epoch = 0, 0

    # for epoch in tqdm(range(args.epochs)):  # global training epochs
    for epoch in range(args.epochs):
    # while testacc_check < args.test_acc or epoch < args.epochs:
    # while testacc_check < args.test_acc:
        local_weights, local_losses = [], [] # init empty local weights and local losses
        print(f'\n | Global Training Round : {epoch+1} |\n') # starting with | Global Training Round : 1 |

        """
        model.train() tells your model that you are training the model. So effectively layers like dropout, batchnorm etc. which behave different on the train and test procedures know what is going on and hence can behave accordingly.

        More details: It sets the mode to train (see source code). You can call either model.eval() or model.train(mode=False) to tell that you are testing. It is somewhat intuitive to expect train function to train model but it does not do that. It just sets the mode.
        """
        # ============== TRAIN ==============
        global_model.train()
        m = max(int(args.frac * args.num_users), 1) # C = args.frac. Setting number of clients m for training
        idxs_users = np.random.choice(range(args.num_users), m, replace=False) # args.num_users=100 total clients. Choosing a random array of indices. Subset of clients.

        for idx in idxs_users: # For each client in the subset.
            local_model = LocalUpdate(args=args, dataset=train_dataset,
                                      idxs=user_groups[idx], logger=logger)
            w, loss = local_model.update_weights( # update_weights() contain multiple prints
                model=copy.deepcopy(global_model), global_round=epoch,dtype=torch.float16)
                # w = local model weights
            local_weights.append(copy.deepcopy(w))
            local_losses.append(copy.deepcopy(loss))

        # Averaging m local client weights
        global_weights = average_weights(local_weights)

        # update global weights
        global_model.load_state_dict(global_weights)

        loss_avg = sum(local_losses) / len(local_losses)
        train_loss.append(loss_avg) # Performance measure

        # ============== EVAL ==============
        # Calculate avg training accuracy over all users at every epoch
        list_acc, list_loss = [], []
        global_model.eval() # must set your model into evaluation mode when computing model output values if dropout or bach norm used for training.

        for c in range(args.num_users): # 0 to 99
            # local_model = LocalUpdate(args=args, dataset=train_dataset,
            #                           idxs=user_groups[idx], logger=logger)
            # Fix error idxs=user_groups[idx] to idxs=user_groups[c]
            local_model = LocalUpdate(args=args, dataset=train_dataset,
                                      idxs=user_groups[c], logger=logger)
            acc, loss = local_model.inference(model=global_model, dtype=torch.float16)
            list_acc.append(acc)
            list_loss.append(loss)
        train_accuracy.append(sum(list_acc)/len(list_acc)) # Performance measure

        # Add
        testacc_check = 100*train_accuracy[-1]
        epoch = epoch + 1

        # print global training loss after every 'i' rounds
        if (epoch+1) % print_every == 0: # If print_every=2, => print every 2 rounds
            print(f' \nAvg Training Stats after {epoch+1} global rounds:')
            print(f'Training Loss : {np.mean(np.array(train_loss))}')
            print('Train Accuracy: {:.2f}% \n'.format(100*train_accuracy[-1]))

    # Test inference after completion of training
    test_acc, test_loss = test_inference(args, global_model, test_dataset, dtype=torch.float16)

    print(f' \n Results after {epoch} global rounds of training:')
    print("|---- Avg Train Accuracy: {:.2f}%".format(100*train_accuracy[-1]))
    print("|---- Test Accuracy: {:.2f}%".format(100*test_acc))

    # Saving the objects train_loss and train_accuracy:
    file_name = '../save/objects_fp16/FL_{}_{}_{}_lr[{}]_C[{}]_iid[{}]_E[{}]_B[{}]_FP16.pkl'.\
        format(args.dataset, args.model, epoch, args.lr, args.frac, args.iid,
               args.local_ep, args.local_bs)

    with open(file_name, 'wb') as f:
        pickle.dump([train_loss, train_accuracy], f)

    print('\n Total Run Time: {0:0.4f}'.format(time.time()-start_time))

    # PLOTTING (optional)
    # import matplotlib
    # import matplotlib.pyplot as plt
    # matplotlib.use('Agg')

    # Plot Loss curve
    # plt.figure()
    # plt.title('Training Loss vs Communication rounds')
    # plt.plot(range(len(train_loss)), train_loss, color='r')
    # plt.ylabel('Training loss')
    # plt.xlabel('Communication Rounds')
    # plt.savefig('../save/fed_{}_{}_{}_C[{}]_iid[{}]_E[{}]_B[{}]_loss.png'.
    #             format(args.dataset, args.model, args.epochs, args.frac,
    #                    args.iid, args.local_ep, args.local_bs))
    #
    # # Plot Average Accuracy vs Communication rounds
    # plt.figure()
    # plt.title('Average Accuracy vs Communication rounds')
    # plt.plot(range(len(train_accuracy)), train_accuracy, color='k')
    # plt.ylabel('Average Accuracy')
    # plt.xlabel('Communication Rounds')
    # plt.savefig('../save/fed_{}_{}_{}_C[{}]_iid[{}]_E[{}]_B[{}]_acc.png'.
    #             format(args.dataset, args.model, args.epochs, args.frac,
    #                    args.iid, args.local_ep, args.local_bs))


text:
from math import sqrt
import numpy

def rmse(rating_list,predicted_matrix):
    return sqrt(mse(rating_list,predicted_matrix))

def mse(rating_list,predicted_matrix):
    squared_error = 0.
    for u_id, i_id ,rating in rating_list:
        squared_error += pow(rating - predicted_matrix[u_id][i_id],2)
    mean_squared_error = squared_error / len(rating_list)
    return mean_squared_error

def mae(rating_list,predicted_matrix):
    abs_error = 0.
    for u_id, i_id ,rating in rating_list:
        abs_error += abs(rating - predicted_matrix[u_id][i_id])
    mean_abs_error = abs_error / len(rating_list)
    return mean_abs_error

def mape(rating_list,predicted_matrix):
    abs_percentage_error = 0.
    for u_id, i_id ,rating in rating_list:
        abs_percentage_error += abs((rating - predicted_matrix[u_id][i_id]) / rating)
    mean_abs_percentage_error = abs_percentage_error / len(rating_list)
    return mean_abs_percentage_error * 100

def R2(rating_list, predicted_matrix):
    mean_rating =0.
    for u_id, i_id ,rating in rating_list:
        mean_rating+=rating
    mean_rating /= len(rating_list)

    sst = 0.
    ssr = 0.
    for u_id, i_id ,rating in rating_list:
        sst += pow(rating - mean_rating, 2)
        ssr += pow(rating -predicted_matrix[u_id][i_id], 2)
    return 1 - (ssr/sst)

def hit_rate_1(rating_list,predicted_matrix):
	return _hit_rate(rating_list,predicted_matrix,delta=0.1)

def hit_rate_2(rating_list,predicted_matrix):
	return _hit_rate(rating_list,predicted_matrix,delta=0.2)

def hit_rate_3(rating_list,predicted_matrix):
	return _hit_rate(rating_list,predicted_matrix,delta=0.3)

def _hit_rate(rating_list,predicted_matrix,delta=0.1):
    hits = 0
    for u_id, i_id ,rating in rating_list:
        abs_error = abs(rating - predicted_matrix[u_id][i_id])
        if abs_error <= delta:
            hits +=1
    mean_hits = float(hits) / len(rating_list)
    return mean_hits * 100

max_stars_repo_name: JalexChang/cross-media-attribution

max_issues_repo_name: JalexChang/cross-media-attribution

max_forks_repo_name: JalexChang/cross-media-attribution


text:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import six
import tensorflow as tf

from edward.models import RandomVariable
from edward.util import check_data, check_latent_vars, get_session

def ppc(T, data, latent_vars=None, n_samples=100):
  """Posterior predictive check
  [@rubin1984bayesianly; @meng1994posterior; @gelman1996posterior].

  PPC's form an empirical distribution for the predictive discrepancy,

  $p(T\mid x) = \int p(T(x^{\\text{rep}})\mid z) p(z\mid x) dz$

  by drawing replicated data sets $x^{\\text{rep}}$ and
  calculating $T(x^{\\text{rep}})$ for each data set. Then it
  compares it to $T(x)$.

  If `data` is inputted with the prior predictive distribution, then
  it is a prior predictive check [@box1980sampling].

  Args:
    T: function.
      Discrepancy function, which takes a dictionary of data and
      dictionary of latent variables as input and outputs a `tf.Tensor`.
    data: dict.
      Data to compare to. It binds observed variables (of type
      `RandomVariable` or `tf.Tensor`) to their realizations (of
      type `tf.Tensor`). It can also bind placeholders (of type
      `tf.Tensor`) used in the model to their realizations.
    latent_vars: dict.
      Collection of random variables (of type `RandomVariable` or
      `tf.Tensor`) binded to their inferred posterior. This argument
      is used when the discrepancy is a function of latent variables.
    n_samples: int.
      Number of replicated data sets.

  Returns:
    list of np.ndarray.
    List containing the reference distribution, which is a NumPy array
    with `n_samples` elements,

    $(T(x^{{\\text{rep}},1}, z^{1}), ...,
       T(x^{\\text{rep,nsamples}}, z^{\\text{nsamples}}))$

    and the realized discrepancy, which is a NumPy array with
    `n_samples` elements,

    $(T(x, z^{1}), ..., T(x, z^{\\text{nsamples}})).$

  #### Examples

  ```python
  # build posterior predictive after inference:
  # it is parameterized by a posterior sample
  x_post = ed.copy(x, {z: qz, beta: qbeta})

  # posterior predictive check
  # T is a user-defined function of data, T(data)
  T = lambda xs, zs: tf.reduce_mean(xs[x_post])
  ed.ppc(T, data={x_post: x_train})

  # in general T is a discrepancy function of the data (both response and
  # covariates) and latent variables, T(data, latent_vars)
  T = lambda xs, zs: tf.reduce_mean(zs[z])
  ed.ppc(T, data={y_post: y_train, x_ph: x_train},
         latent_vars={z: qz, beta: qbeta})

  # prior predictive check
  # run ppc on original x
  ed.ppc(T, data={x: x_train})
  ```
  """
  sess = get_session()
  if not callable(T):
    raise TypeError("T must be a callable function.")

  check_data(data)
  if latent_vars is None:
    latent_vars = {}

  check_latent_vars(latent_vars)
  if not isinstance(n_samples, int):
    raise TypeError("n_samples must have type int.")

  # Build replicated latent variables.
  zrep = {key: tf.convert_to_tensor(value)
          for key, value in six.iteritems(latent_vars)}

  # Build replicated data.
  xrep = {x: (x.value() if isinstance(x, RandomVariable) else obs)
          for x, obs in six.iteritems(data)}

  # Create feed_dict for data placeholders that the model conditions
  # on; it is necessary for all session runs.
  feed_dict = {key: value for key, value in six.iteritems(data)
               if isinstance(key, tf.Tensor) and "Placeholder" in key.op.type}

  # Calculate discrepancy over many replicated data sets and latent
  # variables.
  Trep = T(xrep, zrep)
  Tobs = T(data, zrep)
  Treps = []
  Ts = []
  for _ in range(n_samples):
    # Take a forward pass (session run) to get new samples for
    # each calculation of the discrepancy.
    # Alternatively, we could unroll the graph by registering this
    # operation `n_samples` times, each for different parent nodes
    # representing `xrep` and `zrep`. But it's expensive.
    Treps += [sess.run(Trep, feed_dict)]
    Ts += [sess.run(Tobs, feed_dict)]

  return [np.stack(Treps), np.stack(Ts)]

max_stars_repo_name: zhangyewu/edward

max_issues_repo_name: zhangyewu/edward

max_forks_repo_name: zhangyewu/edward


text:
import numpy as np
import math
portion = 1
Kp=Kd=Ki=1
prev_ang1=prev_ang2=0
def abc(dy,dx):
    global portion,Kp,Kd,Ki,prev_ang1,prev_ang2
    distance = (dx*dx + dy*dy )**0.5
    theta= np.degrees(math.atan2(dy,dx))

    if(theta < 0):
        theta = 360 + theta

    if(theta < 60):
        if(portion != 1 and portion != 0):
            temp = prev_ang1
            prev_ang1 = prev_ang2
            prev_ang2 = temp

        Angle_1 = (30*distance*math.sin((60-theta)*(math.pi/180)))/(7)
        Angle_2 = (30*distance*math.sin(theta*(math.pi/180)))/(7)

        Data_1 = max(-15,min(15,int(Kp*Angle_1+Kd*(prev_ang1-Angle_1)+Ki*(prev_ang1+Angle_1))))
        Data_2 = max(-15,min(15,int(Kp*Angle_2+Kd*(prev_ang2-Angle_2)+Ki*(prev_ang2+Angle_2))))

        Servo_1 = str(90-Data_2).zfill(3)
        Servo_2 = str(90+Data_2).zfill(3)
        Servo_3 = str(90).zfill(3)
        Servo_4 = str(90).zfill(3)
        Servo_5 = str(90-Data_1).zfill(3)
        Servo_6 = str(90+Data_1).zfill(3)

        portion = 1

    elif(theta < 120):
        theta = theta - 60
        if(portion!=2 and portion !=0):
            temp = prev_ang1
            prev_ang1 = prev_ang2
            prev_ang2 = temp

        Angle_1 = (30*distance*math.sin((60-theta)*(math.pi/180)))/(7)
        Angle_2 = (30*distance*math.sin(theta*(math.pi/180)))/(7)

        Data_1 = max(-15,min(15,int(Kp*Angle_1+Kd*(prev_ang1-Angle_1)+Ki*(prev_ang1+Angle_1))))
        Data_2 = max(-15,min(15,int(Kp*Angle_2+Kd*(prev_ang2-Angle_2)+Ki*(prev_ang2+Angle_2))))

        Servo_1 = str(90-Data_1).zfill(3)
        Servo_2 = str(90+Data_1).zfill(3)
        Servo_3 = str(90-Data_2).zfill(3)
        Servo_4 = str(90+Data_2).zfill(3)
        Servo_5 = str(90).zfill(3)
        Servo_6 = str(90).zfill(3)
        portion = 2

    elif(theta < 180):
        theta = theta - 120
        if(portion!=3 and portion !=0):
            temp = prev_ang1
            prev_ang1 = prev_ang2
            prev_ang2 = temp

        Angle_1 = (30*distance*math.sin((60-theta)*(math.pi/180)))/(7)
        Angle_2 = (30*distance*math.sin(theta*(math.pi/180)))/(7)

        Data_1 = max(-15,min(15,int(Kp*Angle_1+Kd*(prev_ang1-Angle_1)+Ki*(prev_ang1+Angle_1))))
        Data_2 = max(-15,min(15,int(Kp*Angle_2+Kd*(prev_ang2-Angle_2)+Ki*(prev_ang2+Angle_2))))

        Servo_1 = str(90).zfill(3)
        Servo_2 = str(90).zfill(3)
        Servo_3 = str(90-Data_1).zfill(3)
        Servo_4 = str(90+Data_1).zfill(3)
        Servo_5 = str(90+Data_2).zfill(3)
        Servo_6 = str(90-Data_2).zfill(3)

        portion = 3

    elif(theta < 240):
        theta = theta - 180
        if(portion!=4 and portion !=0):
            temp = prev_ang1
            prev_ang1 = prev_ang2
            prev_ang2 = temp

        Angle_1 = (30*distance*math.sin((60-theta)*(math.pi/180)))/(7)
        Angle_2 = (30*distance*math.sin(theta*(math.pi/180)))/(7)

        Data_1 = max(-15,min(15,int(Kp*Angle_1+Kd*(prev_ang1-Angle_1)+Ki*(prev_ang1+Angle_1))))
        Data_2 = max(-15,min(15,int(Kp*Angle_2+Kd*(prev_ang2-Angle_2)+Ki*(prev_ang2+Angle_2))))

        Servo_1 = str(90+Data_2).zfill(3)
        Servo_2 = str(90-Data_2).zfill(3)
        Servo_3 = str(90).zfill(3)
        Servo_4 = str(90).zfill(3)
        Servo_5 = str(90+Data_1).zfill(3)
        Servo_6 = str(90-Data_1).zfill(3)

        portion = 4

    elif(theta < 300):
        theta = theta - 240
        if(portion!=5 and portion !=0):
            temp = prev_ang1
            prev_ang1 = prev_ang2
            prev_ang2 = temp

        Angle_1 = (30*distance*math.sin((60-theta)*(math.pi/180)))/(7)
        Angle_2 = (30*distance*math.sin(theta*(math.pi/180)))/(7)

        Data_1 = max(-15,min(15,int(Kp*Angle_1+Kd*(prev_ang1-Angle_1)+Ki*(prev_ang1+Angle_1))))
        Data_2 = max(-15,min(15,int(Kp*Angle_2+Kd*(prev_ang2-Angle_2)+Ki*(prev_ang2+Angle_2))))

        Servo_1 = str(90+Data_1).zfill(3)
        Servo_2 = str(90-Data_1).zfill(3)
        Servo_3 = str(90+Data_2).zfill(3)
        Servo_4 = str(90-Data_2).zfill(3)
        Servo_5 = str(90).zfill(3)
        Servo_6 = str(90).zfill(3)

        portion = 5

    elif(theta < 360):
        theta = theta - 300
        if(portion!=6 and portion !=0):
            temp = prev_ang1
            prev_ang1 = prev_ang2
            prev_ang2 = temp

        Angle_1 = (30*distance*math.sin((60-theta)*(math.pi/180)))/(7)
        Angle_2 = (30*distance*math.sin(theta*(math.pi/180)))/(7)

        Data_1 = max(-15,min(15,int(Kp*Angle_1+Kd*(prev_ang1-Angle_1)+Ki*(prev_ang1+Angle_1))))
        Data_2 = max(-15,min(15,int(Kp*Angle_2+Kd*(prev_ang2-Angle_2)+Ki*(prev_ang2+Angle_2))))

        Servo_1 = str(90).zfill(3)
        Servo_2 = str(90).zfill(3)
        Servo_3 = str(90+Data_1).zfill(3)
        Servo_4 = str(90-Data_1).zfill(3)
        Servo_5 = str(90-Data_2).zfill(3)
        Servo_6 = str(90+Data_2).zfill(3)

        portion = 6

    arduino = Servo_1 + Servo_2 + Servo_3 + Servo_4 + Servo_5 + Servo_6 + "*"              #End of command character

    prev_ang1 = Angle_1
    prev_ang2 = Angle_2

    return arduino
print(abc(-3,10))

max_stars_repo_name: manishdsilva/BallBalanceTable

max_issues_repo_name: manishdsilva/BallBalanceTable

max_forks_repo_name: manishdsilva/BallBalanceTable


text:
import tensorflow as tf

def dot(left,right):
    return tf.matmul(tf.convert_to_tensor(left, dtype=tf.float32),tf.convert_to_tensor(right, dtype=tf.float32)).numpy()

def linalg_norm(matrix):
    return tf.norm(tf.convert_to_tensor(matrix, dtype=tf.float32)).numpy()

def mean(matrix):
    return tf.reduce_mean(tf.convert_to_tensor(matrix, dtype=tf.float32)).numpy()

def std(matrix):
    return tf.reduce_std(tf.convert_to_tensor(matrix, dtype=tf.float32)).numpy()

def tf_sum(matrix,axis=None):
    return tf.reduce_sum(tf.convert_to_tensor(matrix, dtype=tf.float32),axis=axis).numpy()

import numpy as np
np.linalg.norm = linalg_norm
np.mean = mean
np.std = std
np.sum = tf_sum

max_stars_repo_name: ynsgnr/GEM

max_issues_repo_name: ynsgnr/GEM

max_forks_repo_name: ynsgnr/GEM


text:
import csv
import numpy as np

NUM_FLUXES = 3197

def prepare_data(num_data, filename):

	#X_train = np.array()
	X_train = np.random.rand(num_data, NUM_FLUXES)
	Y_train = np.random.rand(num_data, 1)

	i = 0

	with open(filename) as file:

		csv_reader = csv.reader(file)

		for row in csv_reader:

			if(i != 0 and i <= num_data):

				X_train[i - 1] = row[1:]
				Y_train[i - 1] = 0 if row[0] == "1" else 1

			i += 1

	X_train = X_train.astype(np.float)
	Y_train = Y_train.astype(np.float)

	return X_train, Y_train

#Returns the positives and negatives predicted by the model
def get_positives_and_negatives(array, true_output):

	true_positives = 0
	false_positives = 0
	true_negatives = 0
	false_negatives = 0

	for i in range(len(array)):
		if(array[i] < 0.5):
			if(true_output[i] == 0):
				true_negatives += 1
			else:
				false_negatives += 1
		else:
			if(true_output[i] == 1):
				true_positives += 1
			else:
				false_positives += 1

	return true_positives, false_positives, true_negatives, false_negatives

def get_confusion_matrix(true_positives, false_positives, true_negatives, false_negatives):

	return ([[true_positives, false_positives], [false_negatives, true_negatives]])

def get_precision(true_positives, false_positives):

	return (true_positives / (true_positives + false_positives))

def get_recall(true_positives, false_negatives):

	return (true_positives / (true_positives + false_negatives))

def get_specificity(false_positives, true_negatives):

	return (true_negatives / (false_positives + true_negatives))

def get_F1(precision, recall):

	return (2 * ((precision * recall) / (precision + recall)))


text:
# -*- coding: utf-8 -*-

import numpy as np
import tensorflow as tf

slim = tf.contrib.slim

_BATCH_NORM_DECAY = 0.9
_BATCH_NORM_EPSILON = 1e-05
_LEAKY_RELU = 0.1

_ANCHORS = [(10, 13), (16, 30), (33, 23),
            (30, 61), (62, 45), (59, 119),
            (116, 90), (156, 198), (373, 326)]

@tf.contrib.framework.add_arg_scope
def _fixed_padding(inputs, kernel_size, *args, mode='CONSTANT', **kwargs):
    """
    Pads the input along the spatial dimensions independently of input size.

    Args:
      inputs: A tensor of size [batch, channels, height_in, width_in] or
        [batch, height_in, width_in, channels] depending on data_format.
      kernel_size: The kernel to be used in the conv2d or max_pool2d operation.
                   Should be a positive integer.
      data_format: The input format ('NHWC' or 'NCHW').
      mode: The mode for tf.pad.

    Returns:
      A tensor with the same format as the input with the data either intact
      (if kernel_size == 1) or padded (if kernel_size > 1).
    """
    pad_total = kernel_size - 1
    pad_beg = pad_total // 2
    pad_end = pad_total - pad_beg

    if kwargs['data_format'] == 'NCHW':
        padded_inputs = tf.pad(inputs, [[0, 0], [0, 0],
                                        [pad_beg, pad_end],
                                        [pad_beg, pad_end]],
                               mode=mode)
    else:
        padded_inputs = tf.pad(inputs, [[0, 0], [pad_beg, pad_end],
                                        [pad_beg, pad_end], [0, 0]], mode=mode)
    return padded_inputs

def _conv2d_fixed_padding(inputs, filters, kernel_size, strides=1):
    if strides > 1:
        inputs = _fixed_padding(inputs, kernel_size)
    inputs = slim.conv2d(inputs, filters, kernel_size, stride=strides,
                         padding=('SAME' if strides == 1 else 'VALID'))
    return inputs

def _get_size(shape, data_format):
    if len(shape) == 4:
        shape = shape[1:]
    return shape[1:3] if data_format == 'NCHW' else shape[0:2]

def _detection_layer(inputs, num_classes, anchors, img_size, data_format):
    num_anchors = len(anchors)
    predictions = slim.conv2d(inputs, num_anchors * (5 + num_classes), 1,
                              stride=1, normalizer_fn=None,
                              activation_fn=None,
                              biases_initializer=tf.zeros_initializer())

    shape = predictions.get_shape().as_list()
    grid_size = _get_size(shape, data_format)
    dim = grid_size[0] * grid_size[1]
    bbox_attrs = 5 + num_classes

    if data_format == 'NCHW':
        predictions = tf.reshape(
            predictions, [-1, num_anchors * bbox_attrs, dim])
        predictions = tf.transpose(predictions, [0, 2, 1])

    predictions = tf.reshape(predictions, [-1, num_anchors * dim, bbox_attrs])

    stride = (img_size[0] // grid_size[0], img_size[1] // grid_size[1])

    anchors = [(a[0] / stride[0], a[1] / stride[1]) for a in anchors]

    box_centers, box_sizes, confidence, classes = tf.split(
        predictions, [2, 2, 1, num_classes], axis=-1)

    box_centers = tf.nn.sigmoid(box_centers)
    confidence = tf.nn.sigmoid(confidence)

    grid_x = tf.range(grid_size[0], dtype=tf.float32)
    grid_y = tf.range(grid_size[1], dtype=tf.float32)
    a, b = tf.meshgrid(grid_x, grid_y)

    x_offset = tf.reshape(a, (-1, 1))
    y_offset = tf.reshape(b, (-1, 1))

    x_y_offset = tf.concat([x_offset, y_offset], axis=-1)
    x_y_offset = tf.reshape(tf.tile(x_y_offset, [1, num_anchors]), [1, -1, 2])

    box_centers = box_centers + x_y_offset
    box_centers = box_centers * stride

    anchors = tf.tile(anchors, [dim, 1])
    box_sizes = tf.exp(box_sizes) * anchors
    box_sizes = box_sizes * stride

    detections = tf.concat([box_centers, box_sizes, confidence], axis=-1)

    classes = tf.nn.sigmoid(classes)
    predictions = tf.concat([detections, classes], axis=-1)
    return predictions

def _upsample(inputs, out_shape, data_format='NCHW'):
    # tf.image.resize_nearest_neighbor accepts input in format NHWC
    if data_format == 'NCHW':
        inputs = tf.transpose(inputs, [0, 2, 3, 1])

    if data_format == 'NCHW':
        new_height = out_shape[2]
        new_width = out_shape[3]
    else:
        new_height = out_shape[1]
        new_width = out_shape[2]

    inputs = tf.image.resize_nearest_neighbor(inputs, (new_height, new_width))

    # back to NCHW if needed
    if data_format == 'NCHW':
        inputs = tf.transpose(inputs, [0, 3, 1, 2])

    inputs = tf.identity(inputs, name='upsampled')
    return inputs

def yolo_v4(inputs, num_classes, is_training=False, data_format='NCHW', reuse=False):
    """
    Creates YOLO v3 model.

    :param inputs: a 4-D tensor of size [batch_size, height, width, channels].
        Dimension batch_size may be undefined. The channel order is RGB.
    :param num_classes: number of predicted classes.
    :param is_training: whether is training or not.
    :param data_format: data format NCHW or NHWC.
    :param reuse: whether or not the network and its variables should be reused.
    :param with_spp: whether or not is using spp layer.
    :return:
    """

    # it will be needed later on
    img_size = inputs.get_shape().as_list()[1:3]

    # transpose the inputs to NCHW
    if data_format == 'NCHW':
        inputs = tf.transpose(inputs, [0, 3, 1, 2])

    # normalize values to range [0..1]
    inputs = inputs / 255

    # set batch norm params
    batch_norm_params = {
        'decay': _BATCH_NORM_DECAY,
        'epsilon': _BATCH_NORM_EPSILON,
        'scale': True,
        'is_training': is_training,
        'fused': None,  # Use fused batch norm if possible.
    }

    with slim.arg_scope([slim.conv2d, slim.batch_norm, _fixed_padding], data_format=data_format, reuse=reuse):
        with slim.arg_scope([slim.conv2d], normalizer_fn=slim.batch_norm,
                            normalizer_params=batch_norm_params,
                            biases_initializer=None,
                            activation_fn=lambda x:x* tf.math.tanh(tf.math.softplus(x))):
            inputs = _conv2d_fixed_padding(inputs, 32, 3, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 64, 3, strides=2)
            route1 = inputs
            inputs = _conv2d_fixed_padding(inputs, 64, 1, strides=1)
            route2 = inputs
            inputs = route1
            inputs = _conv2d_fixed_padding(inputs, 64, 1, strides=1)
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 32, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 64, 3, strides=1)
            inputs = inputs + shortcut
            inputs = _conv2d_fixed_padding(inputs, 64, 1, strides=1)
            route8 = inputs
            inputs = tf.concat([route8, route2], axis=1 if data_format == 'NCHW' else 3)
            inputs = _conv2d_fixed_padding(inputs, 64, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 128, 3, strides=2)
            route11 = inputs
            inputs = _conv2d_fixed_padding(inputs, 64, 1, strides=1)
            route12 = inputs
            inputs = route11
            inputs = _conv2d_fixed_padding(inputs, 64, 1, strides=1)
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 64, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 64, 3, strides=1)
            inputs = inputs + shortcut
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 64, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 64, 3, strides=1)
            inputs = inputs + shortcut
            inputs = _conv2d_fixed_padding(inputs, 64, 1, strides=1)
            route21 = inputs
            inputs = tf.concat([route21, route12], axis=1 if data_format == 'NCHW' else 3)
            inputs = _conv2d_fixed_padding(inputs, 128, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 256, 3, strides=2)
            route24 = inputs
            inputs = _conv2d_fixed_padding(inputs, 128, 1, strides=1)
            route25 = inputs
            inputs = route24
            inputs = _conv2d_fixed_padding(inputs, 128, 1, strides=1)
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 128, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 128, 3, strides=1)
            inputs = inputs + shortcut
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 128, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 128, 3, strides=1)
            inputs = inputs + shortcut
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 128, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 128, 3, strides=1)
            inputs = inputs + shortcut
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 128, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 128, 3, strides=1)
            inputs = inputs + shortcut
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 128, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 128, 3, strides=1)
            inputs = inputs + shortcut
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 128, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 128, 3, strides=1)
            inputs = inputs + shortcut
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 128, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 128, 3, strides=1)
            inputs = inputs + shortcut
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 128, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 128, 3, strides=1)
            inputs = inputs + shortcut
            inputs = _conv2d_fixed_padding(inputs, 128, 1, strides=1)
            route52 = inputs
            inputs = tf.concat([route52, route25], axis=1 if data_format == 'NCHW' else 3)
            inputs = _conv2d_fixed_padding(inputs, 256, 1, strides=1)
            route54 = inputs
            inputs = _conv2d_fixed_padding(inputs, 512, 3, strides=2)
            route55 = inputs
            inputs = _conv2d_fixed_padding(inputs, 256, 1, strides=1)
            route56 = inputs
            inputs = route55
            inputs = _conv2d_fixed_padding(inputs, 256, 1, strides=1)
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 256, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 256, 3, strides=1)
            inputs = inputs + shortcut
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 256, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 256, 3, strides=1)
            inputs = inputs + shortcut
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 256, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 256, 3, strides=1)
            inputs = inputs + shortcut
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 256, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 256, 3, strides=1)
            inputs = inputs + shortcut
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 256, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 256, 3, strides=1)
            inputs = inputs + shortcut
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 256, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 256, 3, strides=1)
            inputs = inputs + shortcut
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 256, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 256, 3, strides=1)
            inputs = inputs + shortcut
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 256, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 256, 3, strides=1)
            inputs = inputs + shortcut
            inputs = _conv2d_fixed_padding(inputs, 256, 1, strides=1)
            route83 = inputs
            inputs = tf.concat([route83, route56], axis=1 if data_format == 'NCHW' else 3)
            inputs = _conv2d_fixed_padding(inputs, 512, 1, strides=1)
            route85 = inputs
            inputs = _conv2d_fixed_padding(inputs, 1024, 3, strides=2)
            route86 = inputs
            inputs = _conv2d_fixed_padding(inputs, 512, 1, strides=1)
            route87 = inputs
            inputs = route86
            inputs = _conv2d_fixed_padding(inputs, 512, 1, strides=1)
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 512, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 512, 3, strides=1)
            inputs = inputs + shortcut
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 512, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 512, 3, strides=1)
            inputs = inputs + shortcut
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 512, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 512, 3, strides=1)
            inputs = inputs + shortcut
            shortcut = inputs
            inputs = _conv2d_fixed_padding(inputs, 512, 1, strides=1)
            inputs = _conv2d_fixed_padding(inputs, 512, 3, strides=1)
            inputs = inputs + shortcut
            inputs = _conv2d_fixed_padding(inputs, 512, 1, strides=1)
            route102 = inputs
            inputs = tf.concat([route102, route87], axis=1 if data_format == 'NCHW' else 3)
            inputs = _conv2d_fixed_padding(inputs, 1024, 1, strides=1)

    # Set activation_fn and parameters for conv2d, batch_norm.
    with slim.arg_scope([slim.conv2d, slim.batch_norm, _fixed_padding], data_format=data_format, reuse=reuse):
        with slim.arg_scope([slim.conv2d], normalizer_fn=slim.batch_norm,
                            normalizer_params=batch_norm_params,
                            biases_initializer=None,
                            activation_fn=lambda x: tf.nn.leaky_relu(x, alpha=_LEAKY_RELU)):

            with tf.variable_scope('yolo-v4'):
                inputs = _conv2d_fixed_padding(inputs, 512, 1, strides=1)
                inputs = _conv2d_fixed_padding(inputs, 1024, 3, strides=1)
                inputs = _conv2d_fixed_padding(inputs, 512, 1, strides=1)
                route107 = inputs
                maxpool108 = slim.max_pool2d(inputs, 5, 1, 'SAME')
                inputs = route107
                maxpool110 = slim.max_pool2d(inputs, 9, 1, 'SAME')
                inputs = route107
                maxpool112 = slim.max_pool2d(inputs, 13, 1, 'SAME')
                inputs = tf.concat([maxpool112, maxpool110, maxpool108, route107], axis=1 if data_format == 'NCHW' else 3)
                inputs = _conv2d_fixed_padding(inputs, 512, 1, strides=1)
                inputs = _conv2d_fixed_padding(inputs, 1024, 3, strides=1)
                inputs = _conv2d_fixed_padding(inputs, 512, 1, strides=1)
                route116 = inputs
                inputs = _conv2d_fixed_padding(inputs, 256, 1, strides=1)
                inputs = _upsample(inputs, route85.get_shape().as_list(), data_format)
                route118 = inputs
                inputs = route85
                inputs = _conv2d_fixed_padding(inputs, 256, 1, strides=1)
                route120 = inputs
                inputs = tf.concat([route120, route118], axis=1 if data_format == 'NCHW' else 3)
                inputs = _conv2d_fixed_padding(inputs, 256, 1, strides=1)
                inputs = _conv2d_fixed_padding(inputs, 512, 3, strides=1)
                inputs = _conv2d_fixed_padding(inputs, 256, 1, strides=1)
                inputs = _conv2d_fixed_padding(inputs, 512, 3, strides=1)
                inputs = _conv2d_fixed_padding(inputs, 256, 1, strides=1)
                route126 = inputs
                inputs = _conv2d_fixed_padding(inputs, 128, 1, strides=1)
                inputs = _upsample(inputs, route54.get_shape().as_list(), data_format)
                route128 = inputs
                inputs = route54
                inputs = _conv2d_fixed_padding(inputs, 128, 1, strides=1)
                route130 = inputs
                inputs = tf.concat([route130, route128], axis=1 if data_format == 'NCHW' else 3)
                inputs = _conv2d_fixed_padding(inputs, 128, 1, strides=1)
                inputs = _conv2d_fixed_padding(inputs, 256, 3, strides=1)
                inputs = _conv2d_fixed_padding(inputs, 128, 1, strides=1)
                inputs = _conv2d_fixed_padding(inputs, 256, 3, strides=1)
                inputs = _conv2d_fixed_padding(inputs, 128, 1, strides=1)
                route136 = inputs
                inputs = _conv2d_fixed_padding(inputs, 256, 3, strides=1)
                detect_1 = _detection_layer(inputs, num_classes, _ANCHORS[0:3], img_size, data_format)
                detect_1 = tf.identity(detect_1, name='detect_1')
                inputs = route136
                inputs = _conv2d_fixed_padding(inputs, 256, 3, strides=2)
                route141 = inputs
                inputs = tf.concat([route141, route126], axis=1 if data_format == 'NCHW' else 3)
                inputs = _conv2d_fixed_padding(inputs, 256, 1, strides=1)
                inputs = _conv2d_fixed_padding(inputs, 512, 3, strides=1)
                inputs = _conv2d_fixed_padding(inputs, 256, 1, strides=1)
                inputs = _conv2d_fixed_padding(inputs, 512, 3, strides=1)
                inputs = _conv2d_fixed_padding(inputs, 256, 1, strides=1)
                route147 = inputs
                inputs = _conv2d_fixed_padding(inputs, 512, 3, strides=1)
                detect_2 = _detection_layer(inputs, num_classes, _ANCHORS[3:6], img_size, data_format)
                detect_2 = tf.identity(detect_2, name='detect_2')
                inputs = route147
                inputs = _conv2d_fixed_padding(inputs, 512, 3, strides=2)
                route152 = inputs
                inputs = tf.concat([route152, route116], axis=1 if data_format == 'NCHW' else 3)
                inputs = _conv2d_fixed_padding(inputs, 512, 1, strides=1)
                inputs = _conv2d_fixed_padding(inputs, 1024, 3, strides=1)
                inputs = _conv2d_fixed_padding(inputs, 512, 1, strides=1)
                inputs = _conv2d_fixed_padding(inputs, 1024, 3, strides=1)
                inputs = _conv2d_fixed_padding(inputs, 512, 1, strides=1)
                inputs = _conv2d_fixed_padding(inputs, 1024, 3, strides=1)
                detect_3 = _detection_layer(inputs, num_classes, _ANCHORS[6:9], img_size, data_format)
                detect_3 = tf.identity(detect_3, name='detect_3')
                detections = tf.concat([detect_1, detect_2, detect_3], axis=1)
                detections = tf.identity(detections, name='detections')
                return detections


text:
#create a rectangle latitude/longitude based mesh
#we will use some GIS way to define it
#longitude left and latitude bottom and nrow and ncolumn and resolution is used to define the rectangle
#because it is mesh, it represent the edge instead of center
#we will use gdal api for most operations
import os, sys
import numpy as np
from osgeo import ogr, osr
from pyflowline.classes.hexagon import pyhexagon
from pyflowline.formats.convert_coordinates import convert_gcs_coordinates_to_cell, convert_pcs_coordinates_to_cell
from pyflowline.algorithms.auxiliary.find_index_in_list import check_if_duplicates
from pyflowline.algorithms.auxiliary.gdal_functions import reproject_coordinates_batch

def create_hexagon_mesh(iFlag_rotation_in, \
    dX_left_in, dY_bot_in, \
    dResolution_meter_in, \
        ncolumn_in, \
            nrow_in, \
        sFilename_output_in, \
            sFilename_spatial_reference_in):

    if os.path.exists(sFilename_output_in):
        os.remove(sFilename_output_in)

    if os.path.exists(sFilename_spatial_reference_in):
        pass
    else:
        print('Spatial reference file is missing')
        return

    pDriver_shapefile = ogr.GetDriverByName('Esri Shapefile')
    pDriver_geojson = ogr.GetDriverByName('GeoJSON')
    pDataset_shapefile = pDriver_shapefile.Open(sFilename_spatial_reference_in, 0)
    pLayer_shapefile = pDataset_shapefile.GetLayer(0)
    pSpatial_reference = pLayer_shapefile.GetSpatialRef()
    pDataset = pDriver_geojson.CreateDataSource(sFilename_output_in)
    pSpatial_reference_gcs = osr.SpatialReference()
    pSpatial_reference_gcs.ImportFromEPSG(4326)    # WGS84 lat/lon
    pSpatial_reference_gcs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
    pLayer = pDataset.CreateLayer('cell', pSpatial_reference_gcs, ogr.wkbPolygon)
    # Add one attribute
    pLayer.CreateField(ogr.FieldDefn('id', ogr.OFTInteger64)) #long type for high resolution
    pLayer.CreateField(ogr.FieldDefn('lon', ogr.OFTReal)) #long type for high resolution
    pLayer.CreateField(ogr.FieldDefn('lat', ogr.OFTReal)) #long type for high resolution
    pArea_field = ogr.FieldDefn('area', ogr.OFTReal)
    pArea_field.SetWidth(20)
    pArea_field.SetPrecision(2)
    pLayer.CreateField(pArea_field)
    pLayerDefn = pLayer.GetLayerDefn()
    pFeature = ogr.Feature(pLayerDefn)
    xleft = dX_left_in
    ybottom = dY_bot_in
    dArea = np.power(dResolution_meter_in,2.0)
    #hexagon edge
    dLength_edge = np.sqrt(  2.0 * dArea / (3.0* np.sqrt(3.0))  )
    #geojson
    aHexagon=list()
    #.........
    #(x2,y2)-----(x3,y3)
    #   |           |
    #(x1,y1)-----(x4,y4)
    #...............

    lCellID = 1
    if iFlag_rotation_in ==0:
        dX_shift = 0.5 * dLength_edge * np.sqrt(3.0)
        dY_shift = 0.5 * dLength_edge
        dX_spacing = dLength_edge * np.sqrt(3.0)
        dY_spacing = dLength_edge * 1.5
        for iRow in range(1, nrow_in+1):
            for iColumn in range(1, ncolumn_in+1):
                if iRow % 2 == 1 : #odd
                #define a polygon here
                    x1 = xleft + (iColumn-1) * dX_spacing
                    y1 = ybottom + (iRow -1) * dY_spacing
                else:
                    x1 = xleft + (iColumn-1) * dX_spacing + dX_shift
                    y1 = ybottom + (iRow -1) * dY_spacing

                x2 = x1
                y2 = y1 + dLength_edge

                x3 = x1 + dX_shift
                y3 = y2 + dY_shift

                x4 = x1 + dX_spacing
                y4 = y2

                x5 = x4
                y5 = y1

                x6 = x3
                y6 = y1  -   dY_shift
                aCoords = np.full((7,2), -9999.0, dtype=float)

                x = list()
                x.append(x1)
                x.append(x2)
                x.append(x3)
                x.append(x4)
                x.append(x5)
                x.append(x6)

                y = list()
                y.append(y1)
                y.append(y2)
                y.append(y3)
                y.append(y4)
                y.append(y5)
                y.append(y6)

                x_new , y_new = reproject_coordinates_batch(x, y, pSpatial_reference, \
                    spatial_reference_target = pSpatial_reference_gcs)
                x1=x_new[0]
                x2=x_new[1]
                x3=x_new[2]
                x4=x_new[3]
                x5=x_new[4]
                x6=x_new[5]

                y1=y_new[0]
                y2=y_new[1]
                y3=y_new[2]
                y4=y_new[3]
                y5=y_new[4]
                y6=y_new[5]

                ring = ogr.Geometry(ogr.wkbLinearRing)
                ring.AddPoint(x1, y1)
                ring.AddPoint(x2, y2)
                ring.AddPoint(x3, y3)
                ring.AddPoint(x4, y4)
                ring.AddPoint(x5, y5)
                ring.AddPoint(x6, y6)
                ring.AddPoint(x1, y1)

                pPolygon = ogr.Geometry(ogr.wkbPolygon)
                pPolygon.AddGeometry(ring)
                pFeature.SetGeometry(pPolygon)
                pFeature.SetField("id", lCellID)

                aCoords[0,0] = x1
                aCoords[0,1] = y1
                aCoords[1,0] = x2
                aCoords[1,1] = y2
                aCoords[2,0] = x3
                aCoords[2,1] = y3
                aCoords[3,0] = x4
                aCoords[3,1] = y4
                aCoords[4,0] = x5
                aCoords[4,1] = y5
                aCoords[5,0] = x6
                aCoords[5,1] = y6
                aCoords[6,0] = x1
                aCoords[6,1] = y1

                dummy1= np.array(aCoords)
                dLongitude_center = np.mean(aCoords[0:6,0])
                dLatitude_center = np.mean(aCoords[0:6,1])
                pFeature.SetField("lon", dLongitude_center )
                pFeature.SetField("lat", dLatitude_center )
                pFeature.SetField("area", dArea )
                pLayer.CreateFeature(pFeature)
                pHexagon = convert_gcs_coordinates_to_cell(1, dLongitude_center, dLatitude_center, dummy1)
                pHexagon.lCellID = lCellID
                dArea = pHexagon.calculate_cell_area()
                pHexagon.dArea = dArea
                pHexagon.calculate_edge_length()
                pHexagon.dLongitude_center_degree = dLongitude_center
                pHexagon.dLatitude_center_degree = dLatitude_center

                lCellID_center = lCellID
                #build topoloy
                aNeighbor=list()

                if iColumn > 1:#0
                    lCellID0 = lCellID_center - 1
                    aNeighbor.append(lCellID0)

                if iRow < nrow_in :#1 and 2
                    if iRow %2 ==0:
                        lCellID1 = ncolumn_in * iRow + iColumn
                        aNeighbor.append(lCellID1)

                        if iColumn!=ncolumn_in:
                            lCellID2 = ncolumn_in * iRow + iColumn + 1
                            aNeighbor.append(lCellID2)

                    else:
                        lCellID2 = ncolumn_in * iRow + iColumn
                        aNeighbor.append(lCellID2)

                        if iColumn != 1:
                            lCellID1 = ncolumn_in * iRow + iColumn - 1
                            aNeighbor.append(lCellID1)

                if iColumn < ncolumn_in:#3
                    lCellID3 = lCellID_center + 1
                    aNeighbor.append(lCellID3)

                if iRow > 1 : #4 and 5
                    if iRow %2 ==1:
                        lCellID4 = ncolumn_in * (iRow-2) + iColumn
                        aNeighbor.append(lCellID4)

                        if iColumn !=1:
                            lCellID5 = ncolumn_in * (iRow-2) + iColumn -1
                            aNeighbor.append(lCellID5)

                    else:
                        lCellID5 = ncolumn_in * (iRow-2) + iColumn
                        aNeighbor.append(lCellID5)

                        if iColumn!=ncolumn_in:
                            lCellID4 = ncolumn_in * (iRow-2) + iColumn + 1
                            aNeighbor.append(lCellID4)

                if check_if_duplicates(aNeighbor) == 0:
                    print('error')

                pHexagon.aNeighbor = aNeighbor
                pHexagon.nNeighbor = len(aNeighbor)
                pHexagon.aNeighbor_land= aNeighbor
                pHexagon.nNeighbor_land= pHexagon.nNeighbor

                aHexagon.append(pHexagon)
                lCellID= lCellID + 1
                pass

        pass
    else:
        dX_shift = 0.5 * dLength_edge
        dY_shift = 0.5 * dLength_edge * np.sqrt(3.0)
        dX_spacing = dLength_edge * 1.5
        dY_spacing = dLength_edge * np.sqrt(3.0)
        for iColumn in range(1, ncolumn_in+1):
            for iRow in range(1, nrow_in+1):
                if iColumn % 2 == 0 :
                #define a polygon here
                    x1 = xleft + (iColumn-1) * dX_spacing
                    y1 = ybottom + (iRow-2) * dY_spacing + dY_shift
                else:
                    x1 = xleft + (iColumn-1) * dX_spacing #- dX_shift
                    y1 = ybottom + (iRow-1) * dY_spacing

                x2 = x1 - dX_shift
                y2 = y1 + dY_shift

                x3 = x1
                y3 = y1 + dY_shift * 2.0

                x4 = x1 + dLength_edge
                y4 = y1 + dY_shift * 2.0

                x5 = x4 + dX_shift
                y5 = y1 + dY_shift

                x6 = x1 + dLength_edge
                y6 = y1

                aCoords = np.full((7,2), -9999.0, dtype=float)

                x = list()
                x.append(x1)
                x.append(x2)
                x.append(x3)
                x.append(x4)
                x.append(x5)
                x.append(x6)

                y = list()
                y.append(y1)
                y.append(y2)
                y.append(y3)
                y.append(y4)
                y.append(y5)
                y.append(y6)

                x_new , y_new = reproject_coordinates_batch(x, y, pSpatial_reference)
                x1=x_new[0]
                x2=x_new[1]
                x3=x_new[2]
                x4=x_new[3]
                x5=x_new[4]
                x6=x_new[5]

                y1=y_new[0]
                y2=y_new[1]
                y3=y_new[2]
                y4=y_new[3]
                y5=y_new[4]
                y6=y_new[5]

                ring = ogr.Geometry(ogr.wkbLinearRing)
                ring.AddPoint(x1, y1)
                ring.AddPoint(x2, y2)
                ring.AddPoint(x3, y3)
                ring.AddPoint(x4, y4)
                ring.AddPoint(x5, y5)
                ring.AddPoint(x6, y6)
                ring.AddPoint(x1, y1)
                pPolygon = ogr.Geometry(ogr.wkbPolygon)
                pPolygon.AddGeometry(ring)

                pFeature.SetGeometry(pPolygon)
                pFeature.SetField("id", lCellID)

                aCoords[0,0] = x1
                aCoords[0,1] = y1
                aCoords[1,0] = x2
                aCoords[1,1] = y2
                aCoords[2,0] = x3
                aCoords[2,1] = y3
                aCoords[3,0] = x4
                aCoords[3,1] = y4
                aCoords[4,0] = x5
                aCoords[4,1] = y5
                aCoords[5,0] = x6
                aCoords[5,1] = y6
                aCoords[6,0] = x1
                aCoords[6,1] = y1

                dummy1= np.array(aCoords)
                dLongitude_center = np.mean(aCoords[0:6,0])
                dLatitude_center = np.mean(aCoords[0:6,1])
                pHexagon = convert_gcs_coordinates_to_cell(1, dLongitude_center, dLatitude_center, dummy1)
                pHexagon.lCellID = lCellID
                #build topology
                dArea = pHexagon.calculate_cell_area()
                pHexagon.dArea = dArea
                pHexagon.calculate_edge_length()
                pFeature.SetField("lon", dLongitude_center )
                pFeature.SetField("lat", dLatitude_center )
                pFeature.SetField("area", dArea )
                pLayer.CreateFeature(pFeature)
                lCellID_center = lCellID
                aNeighbor=list()
                if iRow > 1:#0
                    lCellID0 = lCellID_center - 1
                    aNeighbor.append(lCellID0)

                if iColumn> 1:#1 ans 2
                    if iColumn %2 ==0:
                        lCellID1 = nrow_in * (iColumn-2) + iRow
                        aNeighbor.append(lCellID1)
                        if iRow!=nrow_in:
                            lCellID2 = nrow_in * (iColumn-2) + iRow +1
                            aNeighbor.append(lCellID2)
                    else:
                        lCellID2 = nrow_in * (iColumn-2) + iRow
                        aNeighbor.append(lCellID2)
                        if iRow != 1:
                            lCellID1 = nrow_in * (iColumn-2) + iRow -1
                            aNeighbor.append(lCellID1)

                if iRow < nrow_in:#3
                    lCellID3 = lCellID_center + 1
                    aNeighbor.append(lCellID3)

                if iColumn  < ncolumn_in  : #4 and 5
                    if iColumn %2 ==1:
                        lCellID4 = nrow_in * iColumn + iRow
                        aNeighbor.append(lCellID4)
                        if iRow !=1:
                            lCellID5 = nrow_in * iColumn + iRow -1
                            aNeighbor.append(lCellID5)
                    else:
                        lCellID5 = nrow_in * iColumn + iRow
                        aNeighbor.append(lCellID5)
                        if iRow!=nrow_in:
                            lCellID4 = nrow_in * iColumn + iRow  +1
                            aNeighbor.append(lCellID4)

                if check_if_duplicates(aNeighbor) == 0:
                    print('error')

                pHexagon.aNeighbor = aNeighbor
                pHexagon.nNeighbor = len(aNeighbor)
                pHexagon.aNeighbor_land= aNeighbor
                pHexagon.nNeighbor_land= pHexagon.nNeighbor
                aHexagon.append(pHexagon)

                lCellID= lCellID +1

                pass

    pDataset = pLayer = pFeature  = None
    #calculate neighbor distance
    for pHexagon in aHexagon:
        aNeighbor = pHexagon.aNeighbor
        pHexagon.aNeighbor_distance=list()
        for lCellID1 in aNeighbor:
            for pHexagon1 in aHexagon:
                if pHexagon1.lCellID == lCellID1:
                    dDistance = pHexagon.pVertex_center.calculate_distance( pHexagon1.pVertex_center )
                    pHexagon.aNeighbor_distance.append(dDistance)
                    break

    return aHexagon


text:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import astropy.table as t
from astropy.io import fits

import os, sys, glob
from itertools import product as iterprod

from importer import *

# personal
import manga_tools as m

from figures_tools import savefig, gen_gridspec_fig

class PDFvsSNRFig(object):
    def __init__(self, fracs):
        self.fracs = fracs
        self.nfracs = len(fracs)

        # load figure and gridspec object
        self.gs, self.fig = gen_gridspec_fig(
            self.nfracs, spsize=(2.5, 1.))
        self.subplot_inds = list(iterprod(range(self.gs._nrows), range(self.gs._ncols)))

        self.fracs_plot_ix = dict(zip(sorted(fracs), self.subplot_inds))
        self.fig_axes = {f: self.fig.add_subplot(self.gs[ix[0], ix[1]])
                         for f, ix in self.fracs_plot_ix.items()}

        for f_, ax_ in self.fig_axes.items():
            ax_.tick_params(labelsize='x-small')
            ax_.set_xscale('log')
            ax_.set_xlim([.1, 200.])
            ax_.text(x=.2, y=.1, s='f = {:.2f}'.format(f_), size='x-small')

        self.fig.suptitle('SNR vs PDF population', size='x-small')

    @classmethod
    def from_PDFSNR_Result(cls, res):
        if not isinstance(res, PDFSNR_Result):
            raise TypeError('res must be an instance of PDFSNR_Result class')
        fracs = res.frac_dict.keys()
        return cls(fracs)

    def add_object(self, frac_dict, snr, **kwargs):
        for fk, fv in frac_dict.items():
            self.add_to_subplot(key=fk, frac=fv, snr=snr)

    def add_to_subplot(self, key, frac, snr, **kwargs):
        plotting_kwargs = dict(s=.5, edgecolor='None', alpha=.5)
        kw = {**plotting_kwargs, **kwargs}
        self.fig_axes[key].scatter(snr.flatten(), frac.flatten(), **kw)

    def savefig(self):
        savefig(self.fig, fname='snr_vs_goodpdf.png', fdir='.')

class PDFSNR_Result(object):
    def __init__(self, frac_dict, snr):
        self.snr = snr
        self.frac_dict = frac_dict

    @classmethod
    def from_fname(cls, fname):
        return cls(*retrieve_snr_fracs(fname))

    def __getitem__(self, k):
        return self.frac_dict[k]

def retrieve_snr_fracs(fn):
    hdulist = fits.open(fn)
    nfracs = hdulist['GOODFRAC'].data.shape[0]
    fracs = {hdulist['GOODFRAC'].header['FRAC{}'.format(fri)]: \
                 hdulist['GOODFRAC'].data[fri, :, :] for fri in range(nfracs)}
    snrs = hdulist['SNRMED'].data
    hdulist.close()
    return fracs, snrs

if __name__ == '__main__':
    res_fnames = glob.glob('results/*/*_res.fits')

    pdfsnr_results = list(map(PDFSNR_Result.from_fname, res_fnames))

    snrfig = PDFvsSNRFig.from_PDFSNR_Result(pdfsnr_results[0])

    for i, res in enumerate(pdfsnr_results):
        snrfig.add_object(frac_dict=res.frac_dict, snr=res.snr,
                          facecolor='C{}'.format(i % 10))

    snrfig.savefig()

max_forks_repo_name: zpace/pcay

reason: import numpy,import astropy,from astropy


text:
import abc
import logging

import numpy as np
import torch
import torch.nn.functional as F
from torch import nn as nn

import maple.torch.pytorch_util as ptu
from maple.policies.base import ExplorationPolicy
from maple.torch.core import torch_ify, elem_or_tuple_to_numpy
from maple.torch.distributions import (
    Delta, TanhNormal, MultivariateDiagonalNormal, GaussianMixture, GaussianMixtureFull,
)
from maple.torch.networks import Mlp, CNN
from maple.torch.networks.basic import MultiInputSequential
from maple.torch.networks.stochastic.distribution_generator import (
    DistributionGenerator
)
from maple.torch.sac.policies.base import (
    TorchStochasticPolicy,
    PolicyFromDistributionGenerator,
    MakeDeterministic,
)

class PolicyFromQ(TorchStochasticPolicy):
    def __init__(
            self,
            qf,
            policy,
            num_samples=10,
            **kwargs
    ):
        super().__init__()
        self.qf = qf
        self.policy = policy
        self.num_samples = num_samples

    def forward(self, obs):
        with torch.no_grad():
            state = obs.repeat(self.num_samples, 1)
            action = self.policy(state).sample()
            q_values = self.qf(state, action)
            ind = q_values.max(0)[1]
        return Delta(action[ind])

max_stars_repo_name: UT-Austin-RPL/maple

max_issues_repo_name: UT-Austin-RPL/maple

max_forks_repo_name: UT-Austin-RPL/maple


text:
import math
import numpy

def Position(x=0.0,y=0.0,z=0.0):
    return numpy.array([x,y,z])

def Position_match(p1, p2, eps=1.0e-8):
    return \
      abs(p1[0]-p2[0])<eps and \
      abs(p1[1]-p2[1])<eps and \
      abs(p1[2]-p2[2])<eps

def Position_match2(p1, p2, eps=1.0e-8):
    dx = abs(p1[0]-p2[0])
    if (dx>eps and abs(dx-1.0)>eps):
        return False
    dy = abs(p1[1]-p2[1])
    if (dy>eps and abs(dy-1.0)>eps):
        return False
    dz = abs(p1[2]-p2[2])
    if (dz>eps and abs(dz-1.0)>eps):
        return False
    return True

def Position_outer_product(p1, p2):
    x = p1[1]*p2[2] - p2[1]*p1[2]
    y = p1[2]*p2[0] - p2[2]*p1[0]
    z = p1[0]*p2[1] - p2[0]*p1[1]
    return Position(x,y,z)

def Position_inner_product(p1, p2):
    return p1[0]*p2[0] + p1[1]*p2[1] + p1[2]*p2[2]

def Position_distance(p1, p2):
    dx = p1[0] - p2[0]
    dy = p1[1] - p2[1]
    dz = p1[2] - p2[2]
    return math.sqrt(dx*dx+dy*dy+dz*dz)

def Position_length(p):
    dx = p[0]
    dy = p[1]
    dz = p[2]
    return math.sqrt(dx*dx+dy*dy+dz*dz)

def Position_angle(p1, p2):
    len1 = Position_length(p1)
    len2 = Position_length(p2)
    prod = Position_inner_product(p1,p2)
    return math.degrees(math.acos(prod/(len1*len2)))

def Position_reciprocal(unit):
    recp = [Position(), Position(), Position()]
    recp[0] = Position_outer_product(unit[1], unit[2])
    recp[1] = Position_outer_product(unit[2], unit[0])
    recp[2] = Position_outer_product(unit[0], unit[1])

    Vunit = Position_inner_product(recp[0], unit[0])

    recp[0] = recp[0]*(1.0/Vunit) # 2.0*M_PI not multiplied
    recp[1] = recp[1]*(1.0/Vunit) # 2.0*M_PI not multiplied
    recp[2] = recp[2]*(1.0/Vunit) # 2.0*M_PI not multiplied

    return recp

def Position_translate(vecs, p_org):
    x = vecs[0][0]*p_org[0] + vecs[1][0]*p_org[1] + vecs[2][0]*p_org[2]
    y = vecs[0][1]*p_org[0] + vecs[1][1]*p_org[1] + vecs[2][1]*p_org[2]
    z = vecs[0][2]*p_org[0] + vecs[1][2]*p_org[1] + vecs[2][2]*p_org[2]
    return Position(x,y,z)

def Position_fold(p):
    while p[0]< 0.0: p[0]+=1.0
    while p[0]>=1.0: p[0]-=1.0
    while p[1]< 0.0: p[1]+=1.0
    while p[1]>=1.0: p[1]-=1.0
    while p[2]< 0.0: p[2]+=1.0
    while p[2]>=1.0: p[2]-=1.0

max_stars_repo_name: pearcandy/ciflib

max_issues_repo_name: pearcandy/ciflib

max_forks_repo_name: pearcandy/ciflib


text:
import string
import tensorflow_hub as hub
import tensorflow as tf
import numpy as np
import scipy
import model_definitions
from tqdm import tqdm
import pickle
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import f1_score
from allennlp.commands.elmo import ElmoEmbedder

"""
Script consisting of additional experiments using Elmo and Bert word embeddings and retraining new models for emotion
classification. Still in implementation phase.
"""

#TFhub elmo definition
elmo = hub.Module("https://tfhub.dev/google/elmo/2", trainable=False)

#Function to remove punction from input string.
def remove_punc(text):
    exclude = set(string.punctuation)
    text = ''.join(ch for ch in text if ch not in exclude)
    text = ''.join([i for i in text if not i.isdigit()])
    return text

#Transforms a given string composing of a text to ELMO word embeddings. Returns vector of embeddings
def elmo_vectors(x):
  x = x['Text'].tolist()
  embeddings = elmo(x, signature="default", as_dict=True)["elmo"]

  with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    sess.run(tf.tables_initializer())

    # return average of ELMo features
    return sess.run(tf.reduce_mean(embeddings,1))

#Auxiliary function to convert dataframe to list of list format. Pandas method was not working properly
def convert_rows(dataframe):
    return_list = []

    # Iterate over each row
    for index, rows in dataframe.iterrows():
        # Create list for the current row
        my_list = str(rows.Text).split()

        # append the list to the final list
        return_list.append(my_list)
    return return_list

#Function for reading dataset and trasform all texts to its Elmo embedding vectors. We train a Logistic Regression model for simple performance analysis
def process_data(filename, save):
    all_data = model_definitions.read_data(filename)
    all_data['Text'] = all_data['Text'].values.astype('U')
    all_data['Text'] = all_data['Text'].apply(remove_punc)

    #Train and test sets split

    train, test = train_test_split(all_data, test_size=0.20, shuffle=True)

    #Creating batches of 100 due to performance issues
    list_train = [train[i:i + 100] for i in range(0, train.shape[0], 100)]
    list_test = [test[i:i + 100] for i in range(0, test.shape[0], 100)]

    print("Converting text to Elmo")

    elmo_train = [elmo_vectors(x) for x in tqdm(list_train)]
    elmo_test = [elmo_vectors(x) for x in tqdm(list_test)]

    #Saving embeddings
    if save:
        print("Saving Elmo embeddigns")

        elmo_train_new = np.concatenate(elmo_train, axis=0)
        elmo_test_new = np.concatenate(elmo_test, axis=0)

        # save elmo_train_new
        pickle_out = open("elmo_train.pickle", "wb")
        pickle.dump(elmo_train_new, pickle_out, protocol=pickle.HIGHEST_PROTOCOL)
        pickle_out.close()

        # save elmo_test_new
        pickle_out = open("elmo_test.pickle", "wb")
        pickle.dump(elmo_test_new, pickle_out, protocol=pickle.HIGHEST_PROTOCOL)
        pickle_out.close()

    print("Training Model")

    #Logistic Regression model creation and evaluation
    lreg = LogisticRegression()
    lreg.fit(elmo_train_new, train['Emocao'])

    preds_valid = lreg.predict(elmo_test_new)
    print(f1_score(test['Emocao'], preds_valid))

#Test function converting Texts to Elmo embeddings using AllenNLP
def use_allen():
    options_file = "https://s3-us-west-2.amazonaws.com/allennlp/models/elmo/contributed/pt/elmo_pt_options.json"
    weight_file = "https://s3-us-west-2.amazonaws.com/allennlp/models/elmo/contributed/pt/elmo_pt_weights.hdf5"

    eltest = ElmoEmbedder(options_file, weight_file)

    test_list = ['First test']
    sec_test = ['Second test']

    vec = eltest.embed_sentence(test_list)
    vec2 = eltest.embed_sentence(sec_test)

    print(vec)
    print(vec2)
    print(scipy.spatial.distance.cosine(vec,vec2))

def read_embeddings():
    infile = open('')

if __name__ == '__main__':
    pass
    #read_data()
    #use_allen()
    #statistics()

reason: import numpy,import scipy


text:
"""Time based functionality."""

from calendar import isleap
from dataclasses import dataclass, field
from datetime import datetime
from enum import IntEnum
from typing import ClassVar, Type, TypeVar

import numpy as np

from ambient.core import BaseElement

HOUR_IN_SECONDS = 60 * 60
DAY_IN_HOURS = 24
DAY_IN_SECONDS = DAY_IN_HOURS * HOUR_IN_SECONDS

DEFAULT_TIMESTEPS_PER_HOUR = 1
DEFAULT_YEAR = 1900

T = TypeVar("T", bound="TimePeriodBase")  # pylint: disable=invalid-name

def _month_lengths(is_leap_year: bool = False) -> np.ndarray:
    month_lengths = np.array((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31))

    if is_leap_year:
        month_lengths[1] = 29

    return month_lengths

def _month_lookup(month_lengths_array: np.ndarray) -> np.ndarray:
    return np.repeat(np.arange(12), month_lengths_array)

class DayType(IntEnum):
    """Enumerator for day types."""

    NORMAL = 1  #: Treat the days as normal days of the year.

    COOLING_DESIGN = 10  #: Treat the days as cooling design days.
    HEATING_DESIGN = 11  #: Treat the days as heating design days.
    VENTILATION_DESIGN = 12  #: Treat the days as ventilation design days.
    OTHER_DESIGN = 13  #: Treat the days as ventilation design days.

@dataclass
class TimePeriodBase(BaseElement):
    """Class for a simulation time period."""

    day_type: ClassVar[DayType] = DayType.NORMAL

    timesteps_per_hour: int = DEFAULT_TIMESTEPS_PER_HOUR
    days: np.ndarray = field(default_factory=np.array)
    year: int = DEFAULT_YEAR

    @classmethod
    def from_days_and_months(
        cls: Type[T],
        days: np.ndarray,
        months: np.ndarray,
        timesteps_per_hour: int = DEFAULT_TIMESTEPS_PER_HOUR,
        year: int = DEFAULT_YEAR,
    ) -> T:
        """Build a time period from days of the month and the months.

        Args:
            days (array_like): The days of the month.
            months (array_like): The months for each day.
            timesteps_per_hour (int): The number of timesteps per hour.
            year (int): The year for the days.

        Returns:
            TimePeriodBase: The time period with days derived from the input.
        """
        if isinstance(days, int):
            days = np.asarray([days])

        if isinstance(months, int):
            months = np.asarray([months])

        days, months = np.broadcast_arrays(days, months)
        offsets = np.cumsum(
            np.pad(_month_lengths(isleap(year)), (1, 0), constant_values=0)
        )
        days = np.take(offsets, months) + days

        return cls(timesteps_per_hour=timesteps_per_hour, days=days, year=year)

    def __post_init__(self) -> None:
        """Intialise other data."""
        assert self.days is not None

        self._timestep_length = HOUR_IN_SECONDS / self.timesteps_per_hour
        self._timestep_index = 0
        self._day_index = 0
        self._times = (
            np.arange(0, DAY_IN_HOURS * self.timesteps_per_hour)
            / self.timesteps_per_hour
        )
        self._months = np.fromiter(
            (
                datetime.strptime(f"{day + 1} {self.year}", "%j %Y").month - 1
                for day in self.days
            ),
            dtype=np.int,
        )

    @property
    def timestep(self) -> int:
        """Return the current timestep."""
        return self._timestep_index

    @property
    def times_in_hours(self) -> np.ndarray:
        """Return all timesteps for a day in hours."""
        return self._times

    @property
    def time_in_hours(self) -> float:
        """Return the current time of day in hours."""
        return self.times_in_hours[self._timestep_index]

    @property
    def day(self) -> int:
        """Return the current day of the year."""
        assert self.days is not None
        return self.days[self._day_index]

    @property
    def months(self) -> np.ndarray:
        """Return all the months of the year."""
        return self._months

    @property
    def month(self) -> int:
        """Return the current month of the year."""
        return self._months[self._day_index]

    @property
    def is_leap_year(self) -> bool:
        """Return true if the current year is a leap year."""
        return isleap(self.year)

    @property
    def total_timesteps(self) -> int:
        """Return the total number of timesteps from start to end."""
        assert self.days is not None
        return self.days.size * self.times_in_hours.size

@dataclass
class CoolingDesignDay(TimePeriodBase):
    """Cooling design day time period."""

    day_type: ClassVar[DayType] = DayType.COOLING_DESIGN

@dataclass
class HeatingDesignDay(TimePeriodBase):
    """Heating design day time period."""

    day_type: ClassVar[DayType] = DayType.HEATING_DESIGN

max_stars_repo_name: drewyh/ambient

max_issues_repo_name: drewyh/ambient

max_forks_repo_name: drewyh/ambient


text:
from scipy.spatial import distance
from imutils import face_utils
from imutils.video import VideoStream
from threading import Thread
import numpy as np
import playsound
import argparse
import imutils
import time
import dlib
import cv2
import pandas as pd

class DrowsinessDetection(object):
    """
    This class tracks the user's gaze.
    It provides useful information like the position of the eyes
    and pupils and allows to know if the eyes are open or closed
    """
    def __init__(self):
        self.frame = None
        self.eye_left = None
        self.eye_right = None
        self.eye = None
        self.path = None
        #self.calibration = Calibration()

        # _face_detector is used to detect faces
        #self._face_detector = dlib.get_frontal_face_detector()

        # _predictor is used to get facial landmarks of a given face
        #cwd = os.path.abspath(os.path.dirname(__file__))
        #model_path = os.path.abspath(os.path.join(cwd, "trained_models/shape_predictor_68_face_landmarks.dat"))
        #self._predictor = dlib.shape_predictor(model_path)
    def sound_alarm(self, path):
                #play an alarm sound
                playsound.playsound(str(path))

    def eye_aspect_ratio(self, eye):

                A = distance.euclidean(eye[1], eye[5])
                B = distance.euclidean(eye[2], eye[4])
                C = distance.euclidean(eye[0], eye[3])
                ear = (A + B) / (2.0 * C)
                return ear
    def amplitude(self, eye):

                A = distance.euclidean(eye[1], eye[5])
                B = distance.euclidean(eye[2], eye[4])
                amp = (A + B) / 2.0
                return amp

max_stars_repo_name: kawseribn/Driver-Drowsiness-Detection

max_issues_repo_name: kawseribn/Driver-Drowsiness-Detection

max_forks_repo_name: kawseribn/Driver-Drowsiness-Detection

reason: import numpy,from scipy


text:
'''
miscellaneous functions: prob
'''
import os
import datetime
import math

import numpy as np

import torch
import torch.nn.functional as F
#from torch.autograd import Variable
#from torch.distributions import Categorical, Normal

''' stat '''
def logprob_logsumexp(logprob):
    #logprob = torch.cat(logprobs, dim=1)
    '''
    Input:
        logprob: batch_size x num_samples
    Output:
        logprob: batch_size
    '''
    # init
    num_samples = logprob.size(1)

    # eval
    logprob_maxs, _ = torch.max(logprob, dim=1, keepdim=True)
    logprob = logprob - logprob_maxs # w - \hat(w)
    logprob = torch.log(torch.sum(logprob.exp(), dim=1, keepdim=True)) # log sum(exp(w - \hat(w)))
    logprob = -math.log(float(num_samples)) + logprob_maxs + logprob # log(1/num_samples) + w + log sum(exp(w - \hat(w)))
    #logprob = torch.sum(logprob)
    return logprob.squeeze(1)


text:
import numpy as np

# Each row is a training example, each column is a feature  [X1, X2, X3]
X=np.array(
    (
        [0,0,1],
        [0,1,1],
        [1,0,1],
        [1,1,1]
    )
, dtype=float)

y=np.array(
    (
        [0],
        [1],
        [1],
        [0]
    )
, dtype=float)

# Activation function
def sigmoid(t):
    return 1/(1+np.exp(-t))

# Derivative of sigmoid
def sigmoid_derivative(p):
    return p * (1 - p)

# Class definition
class NeuralNetwork:
    def __init__(self, x,y):
        self.input = x
        self.weights1= np.random.rand(self.input.shape[1],4) # considering we have 4 nodes in the hidden layer
        self.weights2 = np.random.rand(4,1)
        self.y = y                                           # the ground truth (y)
        self.output = np. zeros(y.shape)                     # the output of the model (y_hat)

    def feedforward(self):
        self.layer1 = sigmoid(np.dot(self.input, self.weights1))
        self.layer2 = sigmoid(np.dot(self.layer1, self.weights2))
        return self.layer2

    def backprop(self):
        d_weights2 = np.dot(self.layer1.T, 2*(self.y -self.output)*sigmoid_derivative(self.output))
        d_weights1 = np.dot(self.input.T, np.dot(2*(self.y -self.output)*sigmoid_derivative(self.output), self.weights2.T)*sigmoid_derivative(self.layer1))

        self.weights1 += d_weights1
        self.weights2 += d_weights2

    def train(self, X, y):
        self.output = self.feedforward()
        self.backprop()

NN = NeuralNetwork(X,y)
for i in range(1500): # trains the NN 1,000 times
    if i % 100 ==0:
        print ("for iteration # " + str(i) + "\n")
        print ("Input : \n" + str(X))
        print ("Actual Output: \n" + str(y))
        print ("Predicted Output: \n" + str(NN.feedforward()))
        print ("Loss: \n" + str(np.mean(np.square(y - NN.feedforward())))) # mean sum squared loss
        print ("\n")

    NN.train(X, y)

print(NN.weights1)
print(NN.weights2)

max_stars_repo_name: smirghor/python-neural-network

max_issues_repo_name: smirghor/python-neural-network

max_forks_repo_name: smirghor/python-neural-network