text:
import torch
import numpy as np
import glob
import json
from datetime import datetime
import time
import os

class Dataloader(torch.utils.data.Dataset):

    def __init__(self, __C, _type = 'train'):
        self.__C = __C
        self.answer_dict = ''
        self._type = _type
        self.img_feat_path_list = glob.glob(self.__C.IMG_FEAT_PATH[self._type] + '*.npz')
        self.ques_feat_path = "./dataset/text/extract_bert"

        self.ques_list = json.load(open(self.__C.QUESTION_PATH[self._type], 'r'))["questions"]
        if self._type in ["train", "val"]:
            self.ans_list = json.load(open(self.__C.ANSWER_PATH[self._type], 'r'))["annotations"]

        if self._type == 'train':
            self.data_size = self.ans_list.__len__()
        else:
            self.data_size = self.ques_list.__len__()

        print(datetime.now().strftime("%H:%M:%S"), "\t INFO: DATASET SIZE = ", self.data_size)

        if self.__C.PRELOAD:
            print(datetime.now().strftime("%H:%M:%S"), "\t INFO: Pre-loading Features...")
            time_start = time.time()
            self.iid_to_img_feat = self.img_feat_load(self.img_feat_path_list)
            time_end = time.time()
            print(datetime.now().strftime("%H:%M:%S"), "\t INFO: Finished loading Features in {}s".format(int(time_end - time_start)))
        else:
            self.iid_to_img_feat_path = self.img_feat_path_load(self.img_feat_path_list)

        self.qid_to_ques = self.get_question_id(self.ques_list)
        self.question_features = self.load_ques_feat(os.path.join(self.ques_feat_path, self.BERT_METHOD, ".pt"))
        print(datetime.now().strftime("%H:%M:%S"), "\t INFO: Num qusetions loaded = ", len(self.question_features))

        self.ans_to_ix, self.ix_to_ans = self.ans_stat()
        self.ans_size = self.ans_to_ix.__len__()
        print(datetime.now().strftime("%H:%M:%S"), "\t INFO: Answer vocab size = ", self.ans_size)
        print("Finised!!!")

    def __getitem__(self, idx):

        img_feat_iter = None
        ques_ix_iter = None
        ans_iter = None

        if self.__C.RUN_MODE in ['train']:
            ans = self.ans_list[idx]
            qid = str(ans["question_id"])
            if self.__C.PRELOAD:
                img_feat_x = self.iid_to_img_feat[str(ans['image_id'])]
            else:
                img_feat = np.load(self.iid_to_img_feat_path[str(ans['image_id'])])
                img_feat_x = img_feat['x'].transpose((1, 0))
            img_feat_iter = self.proc_img_feat(img_feat_x)
            ques_ix_iter = self.proc_ques(qid)
            ans_iter = self.proc_ans(ans)

        else:
            ques = self.ques_list[idx]
            if self.__C.PRELOAD:
                img_feat_x = self.iid_to_img_feat[str(ques['image_id'])]
            else:
                img_feat = np.load(self.iid_to_img_feat_path[str(ques['image_id'])])
                img_feat_x = img_feat['x'].transpose((1, 0))
            img_feat_iter = self.proc_img_feat(img_feat_x)

            ques_ix_iter = self.proc_ques(qid)

        """
            return:
                img_feat_iter : 1 x 100 x 1024
                ques_ix_iter  : 1 x 20 x 768 or 1024
                ans_iter      : vocab_ans x 1
        """
        return torch.from_numpy(img_feat_iter), torch.from_numpy(ques_ix_iter), torch.from_numpy(ans_iter)

    def __len__(self):
        return self.data_size

    def load_ques_feat(self, feat_file):
        feat_ques = torch.load(feat_file, map_location=torch.device("cpu"))
        assert len(self.qid_to_ques) == len(feat_ques), "ERROR"
        return feat_ques

    def get_question_id(self, ques_list):
        qid_to_ques = {}
        for ques in ques_list:
            qid = str(ques["question_id"])
            qid_to_ques[qid] = ques
        return qid_to_ques

    def img_feat_load(self, path_list):
        iid_to_feat = {}
        for ix, path in enumerate(path_list):
            iid = str(int(path.split('/')[-1].split('_')[-1].split('.')[0]))
            img_feat = np.load(path)
            img_feat_x = img_feat['x'].transpose((1, 0))
            iid_to_feat[iid] = img_feat_x
            print(datetime.now().strftime("%H:%M:%S"), "\t INFO  Pre Loading : [{} | {}]".format(ix, path_list.__len__()), end="\t\t")

        return iid_to_feat

    def img_feat_path_load(self, path_list):
        iid_to_path = {}
        for path in path_list:
            iid = str(int(path.split('/')[-1].split('_')[-1].split('.')[0]))
            iid_to_path[iid] = path
        return iid_to_path

    def ans_stat(self):
        ans_to_ix, ix_to_ans = json.load(open(self.answer_dict, 'r'))

        return ans_to_ix, ix_to_ans

    def proc_ques(self, qid):

        feat_ques = np.zeros((self.__C.TEXT_FEAT_PAD_SIZE, self.__C.WORD_EMBED_SIZE))
        for i, (tok, vec) in enumerate(self.question_features[qid]["vector"].values()):
            if i >= self.TEXT_FEAT_PAD_SIZE:
                break
            else:
                feat_ques[i] += vec

        ##return matrix MAX_TOKEN x 768 or 1024
        return feat_ques, question_features[qid]["idx_token"]

    def get_score(self, occur):
        if occur == 0:
            return .0
        elif occur == 1:
            return .3
        elif occur == 2:
            return .6
        elif occur == 3:
            return .9
        else:
            return 1.

    def proc_ans(self, ans):
        ans_score = np.zeros(self.ans_to_ix.__len__(), np.float32)
        ans_prob_dict = {}

        for ans_ in ans['answers']:
            ans_proc = self.preprocess_ans(ans_['answers'])
            if ans_proc not in ans_prob_dict:
                ans_prob_dict[ans_proc] = 1
            else:
                ans_prob_dict[ans_proc] += 1

        for ans_ in ans_prob_dict:
            if ans_ in self.ans_to_ix:
                ans_score[self.ans_to_ix[ans_]] = self.get_score(ans_prob_dict[ans_])

        return ans_score

    def preprocess_ans(self, str):
        return ''

    def proc_img_feat(self, img_feat):
        if img_feat.shape[0] > self.__C.IMG_FEAT_PAD_SIZE:
            img_feat = img_feat[:self.__C.IMG_FEAT_PAD_SIZE]

        img_feat = np.pad(
            img_feat,
            ((0, self.__C.IMG_FEAT_PAD_SIZE - img_feat.shape[0]), (0, 0)),
            mode='constant',
            constant_values=0
        )

        return img_feat


text:
# -*- coding: utf-8 -*-
# cs225 assignment8 ex1

import numpy as np

# the implementation will generate a random undirected graph
def MaxSpanningTree(n, weight_range = [1, 20]):
    def __generateMAT(n):
        mat = np.random.randint(weight_range[0], weight_range[1]+1, (n, n))
        for i in range(n):
            for j in range(n):
                if i == j:
                    mat[i, j] = 0
                elif i > j:
                    mat[i, j] = mat[j, i]
        print("Cost Matrix:\n", mat, "\n")
        return mat

    # edge in form of (cost, vertex1, vertex2), where vertex1 < vertex2
    mat, edges, edges_reversed = __generateMAT(n), [], []
    sum = mat.sum()
    for i in range(n):
        for j in range(n):
            if i < j:
                edges.append((mat[i, j], i, j))
                edges_reversed.append((sum-mat[i, j], i, j))
    # Sort the edge list
    edges_reversed = sorted(edges_reversed, key=lambda x: x[0])

    for i in edges:
        print("edge: ", (i[1], i[2]), "weight: ", i[0])

    # We use index from 0 to n - 1
    CC_lst = [[i] for i in range(n)]
    MST_edges = []
    while len(CC_lst) > 1:
        curr_edge = edges_reversed.pop(0)
        for CC in CC_lst:
            if curr_edge[1] in CC:
                CC_1 = CC.copy()
            if curr_edge[2] in CC:
                CC_2 = CC
        # Merge two connected component
        if CC_1 == CC_2:
            continue
        CC_lst.remove(CC_1)
        CC_lst.remove(CC_2)
        CC_1.extend(CC_2)
        CC_lst.append(CC_1)
        MST_edges.append((curr_edge[1], curr_edge[2]))

    # Output the result
    print("\nMaxSpanningTree:\n", MST_edges)

def main():
    MaxSpanningTree(4)

if __name__ == "__main__":
    main()

ext: py

reason: import numpy


text:
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Functions to compute receptive field of a fully-convolutional network.

Please refer to the following g3doc for detailed explanation on how this
computation is performed, and why it is important:
g3doc/photos/vision/features/delf/g3doc/rf_computation.md
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
from tensorflow.contrib.receptive_field.python.util import graph_compute_order
from tensorflow.contrib.receptive_field.python.util import parse_layer_parameters
from tensorflow.python.framework import ops as framework_ops
from tensorflow.python.platform import tf_logging as logging

def _get_rf_size_node_input(stride, kernel_size, rf_size_output):
  """Computes RF size at the input of a given layer.

  Args:
    stride: Stride of given layer (integer).
    kernel_size: Kernel size of given layer (integer).
    rf_size_output: RF size at output of given layer (integer).

  Returns:
    rf_size_input: RF size at input of given layer (integer).
  """
  return stride * rf_size_output + kernel_size - stride

def _get_effective_stride_node_input(stride, effective_stride_output):
  """Computes effective stride at the input of a given layer.

  Args:
    stride: Stride of given layer (integer).
    effective_stride_output: Effective stride at output of given layer
      (integer).

  Returns:
    effective_stride_input: Effective stride at input of given layer
      (integer).
  """
  return stride * effective_stride_output

def _get_effective_padding_node_input(stride, padding,
                                      effective_padding_output):
  """Computes effective padding at the input of a given layer.

  Args:
    stride: Stride of given layer (integer).
    padding: Padding of given layer (integer).
    effective_padding_output: Effective padding at output of given layer
      (integer).

  Returns:
    effective_padding_input: Effective padding at input of given layer
      (integer).
  """
  return stride * effective_padding_output + padding

class ReceptiveField(object):
  """Receptive field of a convolutional neural network.

  Args:
    size: Receptive field size.
    stride: Effective stride.
    padding: Effective padding.
  """

  def __init__(self, size, stride, padding):
    self.size = np.asarray(size)
    self.stride = np.asarray(stride)
    self.padding = np.asarray(padding)

  def compute_input_center_coordinates(self, y, axis=None):
    """Computes the center of the receptive field that generated a feature.

    Args:
      y: An array of feature coordinates with shape `(..., d)`, where `d` is the
        number of dimensions of the coordinates.
      axis: The dimensions for which to compute the input center coordinates.
        If `None` (the default), compute the input center coordinates for all
        dimensions.

    Returns:
      x: Center of the receptive field that generated the features, at the input
        of the network.

    Raises:
      ValueError: If the number of dimensions of the feature coordinates does
        not match the number of elements in `axis`.
    """
    # Use all dimensions.
    if axis is None:
      axis = range(self.size.size)
    # Ensure axis is a list because tuples have different indexing behavior.
    axis = list(axis)
    y = np.asarray(y)
    if y.shape[-1] != len(axis):
      raise ValueError("Dimensionality of the feature coordinates `y` (%d) "
                       "does not match dimensionality of `axis` (%d)" %
                       (y.shape[-1], len(axis)))
    return -self.padding[axis] + y * self.stride[axis] + (
        self.size[axis] - 1) / 2

  def compute_feature_coordinates(self, x, axis=None):
    """Computes the position of a feature given the center of a receptive field.

    Args:
      x: An array of input center coordinates with shape `(..., d)`, where `d`
        is the number of dimensions of the coordinates.
      axis: The dimensions for which to compute the feature coordinates.
        If `None` (the default), compute the feature coordinates for all
        dimensions.

    Returns:
      y: Coordinates of the features.

    Raises:
      ValueError: If the number of dimensions of the input center coordinates
        does not match the number of elements in `axis`.
    """
    # Use all dimensions.
    if axis is None:
      axis = range(self.size.size)
    # Ensure axis is a list because tuples have different indexing behavior.
    axis = list(axis)
    x = np.asarray(x)
    if x.shape[-1] != len(axis):
      raise ValueError("Dimensionality of the input center coordinates `x` "
                       "(%d) does not match dimensionality of `axis` (%d)" %
                       (x.shape[-1], len(axis)))
    return (x + self.padding[axis] +
            (1 - self.size[axis]) / 2) / self.stride[axis]

  def __iter__(self):
    return iter(np.concatenate([self.size, self.stride, self.padding]))

def compute_receptive_field_from_graph_def(graph_def,
                                           input_node,
                                           output_node,
                                           stop_propagation=None,
                                           input_resolution=None):
  """Computes receptive field (RF) parameters from a Graph or GraphDef object.

  The algorithm stops the calculation of the receptive field whenever it
  encounters an operation in the list `stop_propagation`. Stopping the
  calculation early can be useful to calculate the receptive field of a
  subgraph such as a single branch of the
  [inception network](https://arxiv.org/abs/1512.00567).

  Args:
    graph_def: Graph or GraphDef object.
    input_node: Name of the input node or Tensor object from graph.
    output_node: Name of the output node or Tensor object from graph.
    stop_propagation: List of operations or scope names for which to stop the
      propagation of the receptive field.
    input_resolution: 2D list. If the input resolution to the model is fixed and
      known, this may be set. This is helpful for cases where the RF parameters
      vary depending on the input resolution (this happens since SAME padding in
      tensorflow depends on input resolution in general). If this is None, it is
      assumed that the input resolution is unknown, so some RF parameters may be
      unknown (depending on the model architecture).

  Returns:
    rf_size_x: Receptive field size of network in the horizontal direction, with
      respect to specified input and output.
    rf_size_y: Receptive field size of network in the vertical direction, with
      respect to specified input and output.
    effective_stride_x: Effective stride of network in the horizontal direction,
      with respect to specified input and output.
    effective_stride_y: Effective stride of network in the vertical direction,
      with respect to specified input and output.
    effective_padding_x: Effective padding of network in the horizontal
      direction, with respect to specified input and output.
    effective_padding_y: Effective padding of network in the vertical
      direction, with respect to specified input and output.

  Raises:
    ValueError: If network is not aligned or if either input or output nodes
      cannot be found. For network criterion alignment, see
      photos/vision/features/delf/g3doc/rf_computation.md
  """
  # Convert a graph to graph_def if necessary.
  if isinstance(graph_def, framework_ops.Graph):
    graph_def = graph_def.as_graph_def()

  # Convert tensors to names.
  if isinstance(input_node, framework_ops.Tensor):
    input_node = input_node.op.name
  if isinstance(output_node, framework_ops.Tensor):
    output_node = output_node.op.name

  stop_propagation = stop_propagation or []

  # Computes order of computation for a given graph.
  node_info, name_to_node = graph_compute_order.get_compute_order(
      graph_def=graph_def,
      input_node_name=input_node,
      input_node_size=input_resolution)

  # Sort in reverse topological order.
  ordered_node_info = sorted(node_info.items(), key=lambda x: -x[1].order)

  # Dictionaries to keep track of receptive field, effective stride and
  # effective padding of different nodes.
  rf_sizes_x = {}
  rf_sizes_y = {}
  effective_strides_x = {}
  effective_strides_y = {}
  effective_paddings_x = {}
  effective_paddings_y = {}

  # Initialize dicts for output_node.
  rf_sizes_x[output_node] = 1
  rf_sizes_y[output_node] = 1
  effective_strides_x[output_node] = 1
  effective_strides_y[output_node] = 1
  effective_paddings_x[output_node] = 0
  effective_paddings_y[output_node] = 0

  # Flag to denote if we found output node yet. If we have not, we skip nodes
  # until the output node is found.
  found_output_node = False

  # Flag to denote if padding is undefined. This happens when SAME padding mode
  # is used in conjunction with stride and kernel sizes which make it such that
  # the padding to be applied would depend on the input size. In this case,
  # alignment checks are skipped, and the effective padding is None.
  undefined_padding = False

  for _, (o, node, _, _) in ordered_node_info:
    if node:
      logging.vlog(3, "%10d %-100s %-20s" % (o, node.name[:90], node.op))
    else:
      continue

    # When we find input node, we can stop.
    if node.name == input_node:
      break

    # Loop until we find the output node. All nodes before finding the output
    # one are irrelevant, so they can be skipped.
    if not found_output_node:
      if node.name == output_node:
        found_output_node = True

    if found_output_node:
      if node.name not in rf_sizes_x:
        assert node.name not in rf_sizes_y, ("Node %s is in rf_sizes_y, but "
                                             "not in rf_sizes_x" % node.name)
        # In this case, node is not relevant since it's not part of the
        # computation we're interested in.
        logging.vlog(3, "Irrelevant node %s, skipping it...", node.name)
        continue

      # Get params for this layer.
      (kernel_size_x, kernel_size_y, stride_x, stride_y, padding_x,
       padding_y, _, _) = parse_layer_parameters.get_layer_params(
           node, name_to_node, node_info[node.name].input_size)
      logging.vlog(3, "kernel_size_x = %s, kernel_size_y = %s, "
                   "stride_x = %s, stride_y = %s, "
                   "padding_x = %s, padding_y = %s, input size = %s" %
                   (kernel_size_x, kernel_size_y, stride_x, stride_y, padding_x,
                    padding_y, node_info[node.name].input_size))
      if padding_x is None or padding_y is None:
        undefined_padding = True

      # Get parameters at input of this layer which may or may not be propagated
      # to the input layers.
      rf_size_input_x = _get_rf_size_node_input(stride_x, kernel_size_x,
                                                rf_sizes_x[node.name])
      rf_size_input_y = _get_rf_size_node_input(stride_y, kernel_size_y,
                                                rf_sizes_y[node.name])
      effective_stride_input_x = _get_effective_stride_node_input(
          stride_x, effective_strides_x[node.name])
      effective_stride_input_y = _get_effective_stride_node_input(
          stride_y, effective_strides_y[node.name])
      if not undefined_padding:
        effective_padding_input_x = _get_effective_padding_node_input(
            stride_x, padding_x, effective_paddings_x[node.name])
        effective_padding_input_y = _get_effective_padding_node_input(
            stride_y, padding_y, effective_paddings_y[node.name])
      else:
        effective_padding_input_x = None
        effective_padding_input_y = None
      logging.vlog(
          4, "rf_size_input_x = %s, rf_size_input_y = %s, "
          "effective_stride_input_x = %s, effective_stride_input_y = %s, "
          "effective_padding_input_x = %s, effective_padding_input_y = %s" %
          (rf_size_input_x, rf_size_input_y, effective_stride_input_x,
           effective_stride_input_y, effective_padding_input_x,
           effective_padding_input_y))

      # Loop over this node's inputs and potentially propagate information down.
      for inp_name in node.input:
        # Stop the propagation of the receptive field.
        if any(inp_name.startswith(stop) for stop in stop_propagation):
          logging.vlog(3, "Skipping explicitly ignored node %s.", inp_name)
          continue

        logging.vlog(4, "inp_name = %s", inp_name)
        if inp_name.startswith("^"):
          # The character "^" denotes a control dependency, so this input node
          # can be safely ignored.
          continue

        inp_node = name_to_node[inp_name]
        logging.vlog(4, "inp_node = \n%s", inp_node)
        if inp_name in rf_sizes_x:
          assert inp_name in rf_sizes_y, ("Node %s is in rf_sizes_x, but "
                                          "not in rf_sizes_y" % inp_name)
          logging.vlog(
              4, "rf_sizes_x[inp_name] = %s,"
              " rf_sizes_y[inp_name] = %s, "
              "effective_strides_x[inp_name] = %s,"
              " effective_strides_y[inp_name] = %s, "
              "effective_paddings_x[inp_name] = %s,"
              " effective_paddings_y[inp_name] = %s" %
              (rf_sizes_x[inp_name], rf_sizes_y[inp_name],
               effective_strides_x[inp_name], effective_strides_y[inp_name],
               effective_paddings_x[inp_name], effective_paddings_y[inp_name]))
          # This node was already discovered through a previous path, so we need
          # to make sure that graph is aligned. This alignment check is skipped
          # if the padding is not defined, since in this case alignment cannot
          # be checked.
          if not undefined_padding:
            if effective_strides_x[inp_name] != effective_stride_input_x:
              raise ValueError(
                  "Graph is not aligned since effective stride from different "
                  "paths is different in horizontal direction")
            if effective_strides_y[inp_name] != effective_stride_input_y:
              raise ValueError(
                  "Graph is not aligned since effective stride from different "
                  "paths is different in vertical direction")
            if (rf_sizes_x[inp_name] - 1
               ) / 2 - effective_paddings_x[inp_name] != (
                   rf_size_input_x - 1) / 2 - effective_padding_input_x:
              raise ValueError(
                  "Graph is not aligned since center shift from different "
                  "paths is different in horizontal direction")
            if (rf_sizes_y[inp_name] - 1
               ) / 2 - effective_paddings_y[inp_name] != (
                   rf_size_input_y - 1) / 2 - effective_padding_input_y:
              raise ValueError(
                  "Graph is not aligned since center shift from different "
                  "paths is different in vertical direction")
          # Keep track of path with largest RF, for both directions.
          if rf_sizes_x[inp_name] < rf_size_input_x:
            rf_sizes_x[inp_name] = rf_size_input_x
            effective_strides_x[inp_name] = effective_stride_input_x
            effective_paddings_x[inp_name] = effective_padding_input_x
          if rf_sizes_y[inp_name] < rf_size_input_y:
            rf_sizes_y[inp_name] = rf_size_input_y
            effective_strides_y[inp_name] = effective_stride_input_y
            effective_paddings_y[inp_name] = effective_padding_input_y
        else:
          assert inp_name not in rf_sizes_y, ("Node %s is in rf_sizes_y, but "
                                              "not in rf_sizes_x" % inp_name)
          # In this case, it is the first time we encounter this node. So we
          # propagate the RF parameters.
          rf_sizes_x[inp_name] = rf_size_input_x
          rf_sizes_y[inp_name] = rf_size_input_y
          effective_strides_x[inp_name] = effective_stride_input_x
          effective_strides_y[inp_name] = effective_stride_input_y
          effective_paddings_x[inp_name] = effective_padding_input_x
          effective_paddings_y[inp_name] = effective_padding_input_y

  if not found_output_node:
    raise ValueError("Output node was not found")
  if input_node not in rf_sizes_x:
    raise ValueError("Input node was not found")
  return ReceptiveField(
      (rf_sizes_x[input_node], rf_sizes_y[input_node]),
      (effective_strides_x[input_node], effective_strides_y[input_node]),
      (effective_paddings_x[input_node], effective_paddings_y[input_node]))

max_stars_repo_name: tianyapiaozi/tensorflow

max_issues_repo_name: shrikunjsarda/tensorflow

max_forks_repo_name: shrikunjsarda/tensorflow


text:
## Implementation of the single shooting scheme of optimal control problems

## Create NLP
import casadi as C
import numpy as np
import pylab

N   = 30       # Control discretization
T   = 3.0      # End time
h   = T/N      # Discretization step
nx  = 1
nu  = 1

Q = 1
R = 1

# Declare variables (use scalar graph)
u  = C.SX.sym('u',nu)    # control
x  = C.SX.sym('x',nx)    # states

# YOUR CODE HERE: System dynamics
# xdot = ...;

f = C.Function('f', [x,u],[xdot])

# RK4
U   = C.MX.sym('U')
X0  = C.MX.sym('X0',nx)
X   = X0

# YOUR CODE HERE: implement the RK4 integrator
RK4 = C.Function('RK4',[X0,U],[X])

# Formulate NLP (use matrix graph)
nv = N;
u = C.SX.sym('u',nv)

# Objective function
J=0;

# Get an expression for the cost and state at end
x_0 = 0.05;
X = x_0;
for i in range(N):
    # YOUR CODE HERE: build the cost of the NLP
    # 1 - get x_next using RK4
    # 2 - J = J + ...

# Terminal constraints: x_0(T)=x_1(T)=0
g = X

# Allocate an NLP solver
nlp = {'x': u, 'f': J, 'g': g}

# Create IPOPT solver object
options = {"ipopt": {"hessian_approximation":"limited-memory"}}
solver = C.nlpsol('solver', 'ipopt', nlp, options)

## Solve the NLP
arg = {}
# YOUR CODE HERE: upper and lower bounds on x and g(x)
# arg.x0  =  0.;        # solution guess
# arg.lbx =  ...;       # lower bound on x
# arg.ubx =  ...;       # upper bound on x
# arg.lbg =  ...;       # lower bound on g
# arg.ubg =  ...;       # upper bound on g

# Solve the problem
res = solver(arg)

f_opt       = res["f"].full()
u_opt       = res["x"].full()

# Compute state trajectory integrating the dynamics
x_opt       = np.zeros((nx,N+1))
x_opt[:,0]    = x_0

for i in range(1,N):
    out         = RK4([x_opt[:,i-1],u_opt[i-1]])
    x_opt[:,i]    = out[0].full()

## Plot results
pylab.figure()
pylab.subplot(2,1,1)
pylab.plot(x_opt.T)
pylab.xlabel('time - t')
pylab.ylabel('state - x')
pylab.grid(True)
pylab.subplot(2,1,2)
pylab.plot(u_opt,'r')
pylab.xlabel('time - t')
pylab.ylabel('input - u')
pylab.grid(True)
pylab.show()


text:
import unittest
import torch
import numpy as np
from . import transformer_numpy
from . import transformer_torch

class TestTransformer(unittest.TestCase):

    def assertArrayAlmostEqual(self, a, b, max_delta=1e-5):
        if isinstance(a, torch.Tensor):
            a = a.detach().numpy()
        if isinstance(b, torch.Tensor):
            b = b.detach().numpy()
        self.assertEqual(a.shape, b.shape)
        self.assertEqual(list(a.shape), list(b.shape))
        delta = np.sqrt(np.sum((a - b) ** 2))
        self.assertLessEqual(delta, max_delta)

    def test_einsum_1(self):
        a = torch.rand(3,4,5,6)
        b = torch.rand(3,7,5,6)
        c = transformer_torch.einsum("BTNH,BFNH->BNFT", a, b)
        d =  np.einsum("BTNH,BFNH->BNFT", a, b)
        self.assertArrayAlmostEqual(c, d)

    def test_einsum_2(self):
        a = torch.rand(3,4,5,6)
        b = torch.rand(5,6,7)
        c = transformer_torch.einsum('abcd,cde->abe', a, b)
        d =  np.einsum('abcd,cde->abe', a, b)
        self.assertArrayAlmostEqual(c, d)

    def test_get_position_encoding(self):
        a = transformer_torch.get_position_encoding(10, 512)
        b = transformer_numpy.get_position_encoding(10, 512)
        self.assertArrayAlmostEqual(a, b)

    def test_encode(self):
        if False:
            return
        arr = np.load('examples/brokenegg.npz')
        inputs = np.array([[6086, 33, 631, 769, 489, 3, 2]], dtype=np.long)
        targets = np.array([[1, 123, 37, 166, 120, 16147, 712, 123, 37, 166, 18, 7, 489, 3, 2]], dtype=np.long)

        transformer_numpy.set_variables(arr)
        outputs_numpy = transformer_numpy.body(inputs, targets)

        transformer_torch.set_variables(arr)
        outputs_torch = transformer_torch.body(torch.tensor(inputs), torch.tensor(targets)[:, :-1])

        self.assertArrayAlmostEqual(outputs_numpy, outputs_torch, max_delta=1e-2)

max_stars_repo_name: brokenegg/transformer

max_issues_repo_name: brokenegg/transformer

max_forks_repo_name: brokenegg/transformer


text:
# coding=utf-8
"""Visualize one frame, multifuture ground truth and prediction."""

import argparse
import cv2
import os
import pickle
import numpy as np
from glob import glob
from tqdm import tqdm

parser = argparse.ArgumentParser()
parser.add_argument("gt_path")
parser.add_argument("prediction_file")
parser.add_argument("multivideo_path")
parser.add_argument("vis_path")  # traj_id
parser.add_argument("--show_obs", action="store_true")
parser.add_argument("--plot_points", action="store_true")
parser.add_argument("--use_heatmap", action="store_true")
parser.add_argument("--show_less_gt", action="store_true")
parser.add_argument("--drop_frame", type=int, default=1)
parser.add_argument("--job", type=int, default=1, help="total job")
parser.add_argument("--curJob", type=int, default=1,
                    help="split the moment for this annotator.")

def get_valid_idx(trajs, args):
  new_traj_list = []
  for traj in trajs:
    traj = np.array(traj)
    traj_indexed = np.zeros_like(traj)
    for i, (x,y) in enumerate(traj):
      x = round(x) - 1
      y = round(y) - 1
      if x < 0:
        x = 0
      if y < 0:
        y=0
      if x >= args.imgw:
        x = args.imgw - 1
      if y >= args.imgh:
        y = args.imgh - 1
      traj_indexed[i] = x, y
    new_traj_list.append(traj_indexed)
  return new_traj_list

# traj is a list of xy tuple
def plot_traj(img, traj, color):
  """Plot a trajectory on image."""
  traj = np.array(traj, dtype="float32")
  points = zip(traj[:-1], traj[1:])

  for p1, p2 in points:
    img = cv2.line(img, tuple(p1), tuple(p2), color=color, thickness=2)

  return img

if __name__ == "__main__":
  args = parser.parse_args()
  args.imgh, args.imgw = 1080, 1920

  with open(args.prediction_file, "rb") as f:
    prediction = pickle.load(f)

  if not os.path.exists(args.vis_path):
    os.makedirs(args.vis_path)

  count = 0
  for traj_id in tqdm(prediction):
    count += 1
    if (count % args.job) != (args.curJob - 1):
      continue
    gt_file = os.path.join(args.gt_path, "%s.p" % traj_id)
    with open(gt_file, "rb") as f:
      gt = pickle.load(f)

    video_file = os.path.join(args.multivideo_path, "%s.mp4" % traj_id)

    target_path = os.path.join(args.vis_path, "%s" % traj_id)
    if not os.path.exists(target_path):
      os.makedirs(target_path)

    vcap = cv2.VideoCapture(video_file)
    if not vcap.isOpened():
      raise Exception("Cannot open %s" % video_file)
    frame_count = vcap.get(cv2.CAP_PROP_FRAME_COUNT)
    cur_frame = 0
    printed_frame_count = 0
    while cur_frame < frame_count:
      _, frame_data = vcap.read()
      if cur_frame % args.drop_frame != 0:
        cur_frame += 1
        continue

      # get length first
      max_gt_pred_length = 0
      for future_id in gt:
        points = gt[future_id]["x_agent_traj"]  # (frameIdx, pid, x, y)
        points = [one[2:] for one in points]
        max_gt_pred_length = max([len(points), max_gt_pred_length])

      # 1. plot all the outputs

      if args.use_heatmap:
        new_layer = np.zeros((args.imgh, args.imgw), dtype="float")
        num_between_line = 40
        # convert all the point into valid index
        trajs_indexed = get_valid_idx(prediction[traj_id], args)
        for traj_indexed in trajs_indexed:
          for (x1, y1), (x2, y2) in zip(traj_indexed[:-1], traj_indexed[1:]):
            # all x,y between
            xs = np.linspace(x1, x2, num=num_between_line, endpoint=True)
            ys = np.linspace(y1, y2, num=num_between_line, endpoint=True)
            points = zip(xs, ys)
            for x, y in points:
              x = int(x)
              y = int(y)
              new_layer[y, x] = 1.0

        # gaussian interpolate
        from scipy.ndimage import gaussian_filter

        f_new_layer = gaussian_filter(new_layer, sigma=10)

        f_new_layer = np.uint8(f_new_layer*255)

        ret, mask = cv2.threshold(f_new_layer, 1, 255, cv2.THRESH_BINARY)

        #print new_layer

        heatmap_img = cv2.applyColorMap(f_new_layer, cv2.COLORMAP_AUTUMN)

        heatmap_img_masked = cv2.bitwise_and(heatmap_img,heatmap_img, mask=mask)

        frame_data = cv2.addWeighted(frame_data, 1.0, heatmap_img_masked, 1.0, 0)

      # 2. plot all the ground truth first
      for future_id in gt:
        points = gt[future_id]["x_agent_traj"]  # (frameIdx, pid, x, y)
        gt_len = len(points)
        if args.show_less_gt:
          gt_len = max_gt_pred_length/2
        points = [one[2:] for one in points[:gt_len]]
        frame_data = plot_traj(frame_data, points, (0, 255, 0))

        if args.show_obs:
          frame_data = plot_traj(frame_data,
                                 [one[2:] for one in gt[future_id]["obs_traj"]],
                                 (0, 255, 255))

      # plot the predicted trajectory
      for pred_out in prediction[traj_id]:
        if args.plot_points:
          for x, y in pred_out[:max_gt_pred_length]:
            frame_data = cv2.circle(frame_data, (int(x), int(y)), radius=5,
                                    color=(255, 0, 0), thickness=1)
        if not args.use_heatmap:
          frame_data = plot_traj(frame_data, pred_out[:max_gt_pred_length],
                                 (0, 0, 255))

      target_file = os.path.join(target_path, "%08d.jpg" % printed_frame_count)
      cv2.imwrite(target_file, frame_data)
      printed_frame_count += 1

      cur_frame += 1

reason: import numpy,from scipy


text:
"""
Created on: April 2019
Sasha Prokosheva

Download Enron data mailbox for person = "skilling-j"
"""
import pandas as pd
pd.set_option('display.max_colwidth', 500000)
pd.set_option('precision', 0)
import numpy as np
import re

# To save dictionary to csv
import unicodecsv as csv_unicode
import os
import csv

# To read several files
import glob

# Date
import datetime

##############################################################################################
# Parameters
##############################################################################################
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)

path_data = dname + '/data/'
person = "skilling-j"
fields = ["Message-ID: ", "\nDate: ", "\nFrom: ", "\nTo: ", "\nSubject: ", "\nCc: ", "\nMime-Version: ", "\nContent-Type: ", "\nContent-Transfer-Encoding: ", "\nBcc: ", "\nX-From: ", "\nX-To: ", "\nX-cc: ", "\nX-bcc: ", "\nX-Folder: ", "\nX-Origin: ", "\nX-FileName: "]
csv_columns =  ["Message-ID", "Date", "From", "To", "Subject", "Cc", "Mime-Version", "Content-Type", "Content-Transfer-Encoding", "Bcc", "X-From", "X-To", "X-cc", "X-bcc", "X-Folder", "X-Origin", "X-FileName", "body"]

##############################################################################################
# Functions
##############################################################################################
def find_between(s, start, end):
    return (s.split(start))[1].split(end)[0]

###############################################################################################
# Parse emails
###############################################################################################
os.chdir(path_data + 'maildir/ + person)
folders = [item for item in glob.glob(path_data + 'maildir/' + person + "/*")]

list = []

for folder in folders:
    emails = [item for item in glob.glob(folder + "/*")]
    for email in emails:
        with open(email, 'r') as eml:
            text = eml.read()
            fields_upd = [x for x in fields if text.find(x) != -1]
            dict = {}
            for i in range(len(fields_upd)):
                if i == 0:
                    dict[fields_upd[i][:-2]] = find_between(text, fields_upd[i], fields_upd[i+1])
                elif (i > 0) and (i < len(fields_upd) - 1):
                    f_name = fields_upd[i].strip(' ').strip('\X-').strip('\n').strip(':')
                    dict[f_name] = find_between(text, fields_upd[i], fields_upd[i+1])
                else:
                    f_name = fields_upd[i].strip(' ').strip('\X-').strip('\n').strip(':')
                    dict[f_name] = find_between(text, fields_upd[i], '\n\n')
            dict['body'] = text.split('\n\n', 1)[1]
            list.append(dict)

###############################################################################################
# Save DF to csv
###############################################################################################
df = pd.DataFrame(list)
df.to_csv(path_data + person + '.csv', encoding = 'utf-8', index=False)

max_stars_repo_name: sashapr/mailbox-analysis

max_issues_repo_name: sashapr/mailbox-analysis

max_forks_repo_name: sashapr/mailbox-analysis


text:
import numpy as np
import math
import time
import pandas as pd
import sys

train_path  = sys.argv[1]+'toy_dataset_train.csv'
test_path   = sys.argv[1]+'toy_dataset_test.csv'
output_path = sys.argv[2]

#test_labels_path = sys.argv[1]+'toy_dataset_test_labels.csv'

class NeuralNet:
    def __init__(self,seed):
        seed = seed
        np.random.seed(seed)

    class Layer:
        def __init__(self, in_size, out_size):
            self.in_size = in_size                          # dim(x_l) = m
            self.out_size = out_size                        # dim(y_l) = n
            # Xavier Initializtion of weights for a layer   # dim(w_l) = (m+1)*n
            self.w = np.float32(np.random.normal(0,1,size=(in_size+1, out_size)) * np.sqrt(2/(in_size + out_size + 1)))
            self.w = np.float64(self.w)

        def forward(self, input):                                       # forward pass
            input = np.insert(input,0,np.ones(input.shape[0]),axis=1)   # append 1 to input
            self.input = input

            return np.dot(input,self.w)

        def backward(self, out_error, learning_rate):       # backward pass
            w_temp = np.delete(self.w,0,0)                  # remove first row (bias) from w
            in_error = np.dot(out_error, w_temp.T)
            w_error = np.dot(self.input.T, out_error)
            self.w -= learning_rate * w_error               # constant lr strategy

            return in_error

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

    class ActivationLayer:
        def __init__(self, act_fun, act_prime):
            self.act_fun = act_fun                          #activation function
            self.act_prime = act_prime                      #derivative of activation function

        def forward(self, input):
            self.input = input
            #print(self.act_fun(input))
            return self.act_fun(input)                      #activation of linear input

        def backward(self, out_error,lr):
            return np.multiply(out_error, self.act_prime(self.input))   #derivative w.r.t. activation function

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

    class SoftmaxLayer:
        def __init__(self, in_size):
            self.in_size = in_size

        def forward(self, input):
            self.input = input
            v = np.amax(input,axis=1,keepdims=True)
            #print(input-v)
            tmp = np.exp(input-v)
            tmp = tmp / np.sum(tmp,axis=1,keepdims=True)
            self.output = tmp
            return self.output

        def backward(self, out_error,lr):

            return out_error

########################################################################################################
###################################################################################################
# Some activation functions and their derivatives
def sigmoid(x):
    return 1 / (1 + np.exp(-x))

def sigmoid_prime(x):
    z = sigmoid(x)
    return z*(1-z)

def tanh(x):
    return np.tanh(x)

def tanh_prime(x):
    return 1 - np.tanh(x)**2

def relu(x):
    return np.maximum(x, 0)

def relu_prime(x):
    return np.array(x >= 0).astype('int')

###################################################################################################
#Some Error functions and their derivatives
def mse(pred, y):
    return np.mean(np.power(pred - y, 2))

def mse_prime(pred, y):
    return 2 * (pred - y) / pred.shape[0]

def cee(pred,y):
    #pred = np.clip(pred,a_min=10**(-15),a_max=10**15)
    v = np.log(np.sum(pred*y,axis=1,keepdims=True))
    return abs(np.sum(v)/y.shape[0])

def cee_prime(pred,y):
    return (pred-y)/y.shape[0]

###################################################################################################
# mapping from int to loss functions/activation functions
loss_fun_dict = {
    0:cee,
    1:mse
}
loss_fun_prime_dict = {
    0:cee_prime,
    1:mse_prime
}

act_fun_dict = {
    0:sigmoid,
    1:tanh,
    2:relu
}
act_fun_prime_dict = {
    0:sigmoid_prime,
    1:tanh_prime,
    2:relu_prime
}

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

def accuracy(x,y):
    x = np.argmax(x,axis=1)
    #y = np.argmax(y,axis=1)
    c=0
    for i in range(x.shape[0]):
        if x[i]==y[i]:
            c+=1

    return c/x.shape[0]

def pred_model(net,X_test,Y_test=None):
    output = X_test
    for layer in net:                                       #forward pass
        output = layer.forward(output)

    error = cee(output,Y_test)                             #error calculation
    acc = accuracy(output,Y_test)
    print(error,acc)
    return np.argmax(output,axis=1)

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

def train_model(net,X_train,Y_train,epochs,batchsize,lr,lr_strat,loss_fun,details=False,interval=1):
    batchnum = X_train.shape[0]//batchsize
    lr_ = lr

    for i in range(epochs):
        err=0
        if lr_strat==1:
            lr_ = lr/np.sqrt(epochs+1)

        for n in range(batchnum):
            mini_x_train = X_train[n*batchsize:(n+1)*batchsize]
            mini_y_train = Y_train[n*batchsize:(n+1)*batchsize]

            output = mini_x_train
            for layer in net:                                                           #forward pass
                output = layer.forward(output)

            err += loss_fun_dict[loss_fun](output,mini_y_train)                         #error calculation

            out_error = loss_fun_prime_dict[loss_fun](output,mini_y_train)              #derivative of error
            for layer in reversed(net):                                                 #backward pass
                out_error = layer.backward(out_error,lr_)

        if details:
            output = X_train
            for layer in net:                                                           #forward pass
                output = layer.forward(output)

            error = loss_fun_dict[loss_fun](output,Y_train)                             #error calculation

            if (i+1)%interval==0:
                print(i+1,error,accuracy(output,Y_train))

    return

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

df = pd.read_csv(train_path,header=None)
test_df = pd.read_csv(test_path,header=None)

Y_df = df[df.columns[0]].to_numpy()                 #training output data
Y_train = pd.get_dummies(Y_df).to_numpy()           #OHE of training output
df.drop(df.columns[0],inplace=True,axis=1)
test_df.drop(test_df.columns[0],inplace=True,axis=1)

X_train = df.to_numpy()                             #training input data
X_train = X_train/255                               #scaling the data to fit b/w 0 and 1

X_test = test_df.to_numpy()
X_test = X_test/255

# labels_df = pd.read_csv(test_labels_path,header=None)
# Y_test = pd.get_dummies(labels_df[labels_df.columns[0]].to_numpy()).to_numpy()

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

file = open(sys.argv[3], 'r')
params = file.readlines()
file.close()

epochs      = int(params[0][:-1])
batchsize   = int(params[1][:-1])
layers      = list(map(int,params[2][1:-2].split(',')))
lr_strategy = int(params[3][:-1])
lr          = float(params[4][:-1])
act_fun     = int(params[5][:-1])
loss_fun    = int(params[6][:-1])
seed        = int(params[7])

#print(epochs,batchsize,layers,lr_strategy,lr,act_fun,loss_fun,seed)

nn = NeuralNet(seed=seed)
network = []

network.append(nn.Layer(X_train.shape[1],layers[0]))
network.append(nn.ActivationLayer(act_fun_dict[act_fun],act_fun_prime_dict[act_fun]))

for i in range(len(layers)-1):
    network.append(nn.Layer(layers[i],layers[i+1]))
    network.append(nn.ActivationLayer(act_fun_dict[act_fun],act_fun_prime_dict[act_fun]))

if loss_fun==0:
    network[len(network)-1] = nn.SoftmaxLayer(layers[len(layers)-1])

#print(len(network))

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

train_model(network,X_train,Y_train,epochs,batchsize,lr,lr_strategy,loss_fun)   #training the model

for i in range(len(layers)):                                                    #saving weights
    w = network[2*i].w
    np.save('{}w_{}'.format(output_path,i+1),w)

output = X_test                                                                 #final prediction
for layer in network:
    output = layer.forward(output)

Y_out = np.argmax(output,axis=1)
np.save('{}predictions'.format(output_path),Y_out)                              #saving predictions

#print(accuracy(Y_test,Y_out))

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