Friday, April 22, 2016

N gram

#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;
const string START_SYMBOL = "#@#";
const string END_SYMBOL = "@!#@";
class Ngram {
 public:
  Ngram(vector data, int n) {
    int size = data.size();
    for(int j = 0;j    {
        string curr_string = data[j];
        vector words = split_string_by_space(curr_string)
        for (int i = 0; i < words.size() - n + 1; i++)
            ngrams[concat(words[i], i, i+n)]++;
    }
  }
 
 

  double LogProb(const string& sentence) {
    // IMPLEMENT ME
    double prob = 1.0;
    vector words = split_string_by_space(sentence);
    int n = words.size();
    for(int i = 0;i    {
        string curr_str;
        if(i==0) curr_str = START_SYMBOL + words[i];
        else if(i==n-1) curr_str = words[i] + END_SYMBOL;
        else curr_str = words[i];
       
        if(i>=1)
            string pre_str = words[i] + " " + words[i-1];
        else
            string pre_str = words[i] + END_SYMBOL;
        prob = prob * ngrams[curr_str]./ngrams[pre_str];
    }
    return prob;
  }
 
  vector split_string_by_space(string curr_string)
  {
      vector words;
      int k = 0;
      int i = 0;
      while(k      {
          if(curr_string[k]!=' ') k++;
          else
          {
             words.push_back(curr_string.substr(i,k-i));
             i = k;
             k++;
          }
      }
  }
 
  string concat(string words, int start, int end) {
     
        string sb;
        for (int i = start; i < end; i++)
        {
            if(i==start) sb = words[i];
            else sb = sb+ " " + words[i];
        }
        if(start == 0) sb= START_SYMBOL + sb;
        if(end == words.size()-1) sb = sb + END_SYMBOL;
       
        return sb;
    }


  unordered_mapngrams;
};


#ifndef __main__
#define __main__
int main() {
  vector data = {
    "I am Sam",
    "Sam I am",
    "Sam likes green eggs and ham",
    "I like green eggs and pizza",
    "I do not like green eggs and ham",
    "I do not eat green eggs and ham",
  };

  Ngram ngram(data, 3);
  // should print the log (natural log) probability of the sentence
  cout << ngram.LogProb("I like green eggs and ham") << endl;
  return 0;
}
#endif

Wednesday, March 30, 2016

Torch

What is Torch?

Torch is a scientific computing framework with wide support for machine learning algorithms that puts GPUs first. It is easy to use and efficient, thanks to an easy and fast scripting language, LuaJIT, and an underlying C/CUDA implementation.

A summary of core features: 

  • a powerful N-dimensional array
  • lots of routines for indexing, slicing, transposing, ...
  • amazing interface to C, via LuaJIT
  • linear algebra routines
  • neural network, and energy-based models
  • numeric optimization routines
  • Fast and efficient GPU support
  • Embeddable, with ports to iOS, Android and FPGA backends

Why Torch?

The goal of Torch is to have maximum flexibility and speed in building your scientific algorithms while making the process extremely simple. Torch comes with a large ecosystem of community-driven packages in machine learning, computer vision, signal processing, parallel processing, image, video, audio and networking among others, and builds on top of the Lua community.

At the heart of Torch are the popular neural network and optimization libraries which are simple to use, while having maximum flexibility in implementing complex neural network topologies. You can build arbitrary graphs of neural networks, and parallelize them over CPUs and GPUs in an efficient manner.

Using Torch

Start with our Getting Started guide to download and try Torch yourself. Torch is open-source, so you can also start with the code on the GitHub repo.

Torch is constantly evolving: it is already used within Facebook, Google, Twitter, NYU, IDIAP, Purdue and several other companies and research labs.

Monday, March 28, 2016

Debug the code in Ubuntu


You can use an IDE(http://en.wikipedia.org/wiki/Integrated_development_environment) which provides code management, highlighting, debugging facilities. You may try any of these.
or you may choose to use gdb(https://www.gnu.org/software/gdb/) directly from the command line.

Friday, March 25, 2016

How to Install Docker on Ubuntu 14.04 LTS


How to Install Docker on Ubuntu 14.04 LTS

Docker 101: The Basics

Introduction

Docker is a container-based software framework for automating deployment of applications. “Containers” are encapsulated, lightweight, and portable application modules.

Pre-Flight Check

  • These instructions are intended for installing Docker.
  • I’ll be working from a Liquid Web Core Managed Ubuntu 14.04 LTS server, and I’ll be logged in as root.

Step 1: Installation of Docker

First, you’ll follow a simple best practice: ensuring the list of available packages is up to date before installing anything new.
apt-get update
Let’s install Docker by installing the docker-io package:
apt-get -y install docker.io
Link and fix paths with the following two commands:
ln -sf /usr/bin/docker.io /usr/local/bin/docker
sed -i '$acomplete -F _docker docker' /etc/bash_completion.d/docker.io
Finally, and optionally, let’s configure Docker to start when the server boots:
update-rc.d docker.io defaults

Step 2: Download a Docker Container

Let’s begin using Docker! Download the fedora Docker image:
docker pull ubuntu

Step 3: Run a Docker Container

Now, to setup a basic ubuntu container with a bash shell, we just run one command. docker run will run a command in a new container, -i attaches stdin and stdout, -t allocates a tty, and we’re using the standard ubuntu container.
docker run -i -t ubuntu /bin/bash
That’s it! You’re now using a bash shell inside of a ubuntu docker container.
To disconnect, or detach, from the shell without exiting use the escape sequence Ctrl-p + Ctrl-q.
There are many community containers already available, which can be found through a search. In the command below I am searching for the keyword debian:
docker search debian
Be Sociable, Share!

Thursday, March 24, 2016

Best Time to Buy and Sell Stock with Cooldown

Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:
  • You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
  • After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)


分析

因为当前日期买卖股票会受到之前日期买卖股票行为的影响,首先考虑到用DP解决。
这道题比较麻烦的是有个cooldown的限制,其实本质也就是买与卖之间的限制。对于某一天,股票有三种状态: buy, sell, cooldown, sell与cooldown我们可以合并成一种状态,因为手里最终都没股票,最终需要的结果是sell,即手里股票卖了获得最大利润。所以我们可以用两个DP数组分别记录当前持股跟未持股的状态。然后根据题目中的限制条件,理清两个DP数组的表达式。
对于当天最终未持股的状态,最终最大利润有两种可能,一是今天没动作跟昨天未持股状态一样,二是昨天持股了,今天卖了。所以我们只要取这两者之间最大值即可,表达式如下:

sellDp[i] = Math.max(sellDp[i - 1], buyDp[i - 1] + prices[i]);
对于当天最终持股的状态,最终最大利润有两种可能,一是今天没动作跟昨天持股状态一样,二是前天还没持股,今天买了股票,这里是因为cooldown的原因,所以今天买股要追溯到前天的状态。我们只要取这两者之间最大值即可,表达式如下:

buyDp[i] = Math.max(buyDp[i - 1], sellDp[i - 2] - prices[i]);
最终我们要求的结果是
sellDp[n - 1] 表示最后一天结束时手里没股票时的累积最大利润


public class Solution {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0) {
            return 0;
        }
       
        // 表示当天最终未持股的情况下,当天结束后的累计最大利润
        int[] sellDp = new int[prices.length];
        // 表示当天最终持股的情况下,当天结束后的累计最大利润
        int[] buyDp = new int[prices.length];
       
        // 考虑初始情况
        buyDp[0] = -prices[0];
        sellDp[0] = 0;
        for (int i = 1; i < prices.length; i++) {
            sellDp[i] = Math.max(sellDp[i - 1], buyDp[i - 1] + prices[i]);
            if (i >= 2) {
                buyDp[i] = Math.max(buyDp[i - 1], sellDp[i - 2] - prices[i]);
            } else {
                buyDp[i] = Math.max(buyDp[i - 1], -prices[i]);
            }
        }
        return sellDp[prices.length - 1];
    }
}

Wednesday, March 23, 2016

opencv_annotation code

/*****************************************************************************************************

USAGE:

./opencv_annotation -images -annotations






Created by: Puttemans Steven

*****************************************************************************************************/



#include

#include

#include

#include

#include

#include

#include

using namespace std;

using namespace cv;




// Function prototypes


void on_mouse(int, int, int, int, void*);

string int2string(int);

bool get_annotations(Mat, stringstream*);




// Public parameters


Mat image;

int roi_x0 = 0, roi_y0 = 0, roi_x1 = 0, roi_y1 = 0, num_of_rec = 0;

bool start_draw = false;




// Window name for visualisation purposes


const string window_name="OpenCV Based Annotation Tool";




// FUNCTION : Mouse response for selecting objects in images

// If left button is clicked, start drawing a rectangle as long as mouse moves

// Stop drawing once a new left click is detected by the on_mouse function


void on_mouse(int event, int x, int y, int , void * )



{

// Action when left button is clicked

if(event == EVENT_LBUTTONDOWN)



{

if(!start_draw)



{

roi_x0 = x;

roi_y0 = y;

start_draw = true;

} else {

roi_x1 = x;

roi_y1 = y;

start_draw = false;



}

}

// Action when mouse is moving

if((event == EVENT_MOUSEMOVE) && start_draw)



{

// Redraw bounding box for annotation

Mat current_view;



image.copyTo(current_view);

rectangle(current_view, Point(roi_x0,roi_y0), Point(x,y), Scalar(0,0,255));



imshow(window_name, current_view);

}

}

// FUNCTION : snippet to convert an integer value to a string using a clean function

// instead of creating a stringstream each time inside the main code


string int2string(int num)



{

stringstream temp_stream;

temp_stream << num;

return temp_stream.str();



}

// FUNCTION : given an image containing positive object instances, add all the object

// annotations to a known stringstream


bool get_annotations(Mat input_image, stringstream* output_stream)



{

// Make it possible to exit the annotation

bool stop = false;

// Reset the num_of_rec element at each iteration

// Make sure the global image is set to the current image



num_of_rec = 0;

image = input_image;

// Init window interface and couple mouse actions

namedWindow(window_name, WINDOW_AUTOSIZE);



setMouseCallback(window_name, on_mouse);

imshow(window_name, image);

stringstream temp_stream;

int key_pressed = 0;

do



{

// Keys for processing

// You need to select one for confirming a selection and one to continue to the next image

// Based on the universal ASCII code of the keystroke: http://www.asciitable.com/

// c = 99 add rectangle to current image

// n = 110 save added rectangles and show next image

// = 27 exit program



key_pressed = 0xFF & waitKey(0);

switch( key_pressed )



{

case 27:



destroyWindow(window_name);

stop = true;

case 99:

// Add a rectangle to the list



num_of_rec++;

// Draw initiated from top left corner

if(roi_x0



{

temp_stream << " " << int2string(roi_x0) << " " << int2string(roi_y0) << " " << int2string(roi_x1-roi_x0) << " " << int2string(roi_y1-roi_y0);



}

// Draw initiated from bottom right corner

if(roi_x0>roi_x1 && roi_y0>roi_y1)



{

temp_stream << " " << int2string(roi_x1) << " " << int2string(roi_y1) << " " << int2string(roi_x0-roi_x1) << " " << int2string(roi_y0-roi_y1);



}

// Draw initiated from top right corner

if(roi_x0>roi_x1 && roi_y0



{

temp_stream << " " << int2string(roi_x1) << " " << int2string(roi_y0) << " " << int2string(roi_x0-roi_x1) << " " << int2string(roi_y1-roi_y0);



}

// Draw initiated from bottom left corner

if(roi_x0roi_y1)



{

temp_stream << " " << int2string(roi_x0) << " " << int2string(roi_y1) << " " << int2string(roi_x1-roi_x0) << " " << int2string(roi_y0-roi_y1);



}

rectangle(input_image, Point(roi_x0,roi_y0), Point(roi_x1,roi_y1), Scalar(0,255,0), 1);

break;



}

// Check if escape has been pressed

if(stop)



{

return false;



}

}

// Continue as long as the next image key has not been pressed

while(key_pressed != 110);

// If there are annotations AND the next image key is pressed

// Write the image annotations to the file

if(num_of_rec>0 && key_pressed==110)



{

*output_stream << " " << num_of_rec << temp_stream.str() << endl;



}

// Close down the window



destroyWindow(window_name);

return true;



}

int main( int argc, const char** argv )



{

// If no arguments are given, then supply some information on how this tool works

if( argc == 1 ){

cout << "Usage: " << argv[0] << endl;

cout << " -images [example - /data/testimages/]" << endl;

cout << " -video [example - /data/video.mp4/]" << endl;

cout << " -annotations [example - /data/annotations.txt]" << endl;

return -1;



}

// Read in the input arguments

string image_folder;

string annotations;

string video_file;

bool image_annotation = false;

bool video_annotation = false;

for(int i = 1; i < argc; ++i )



{

if( !strcmp( argv[i], "-images" ) )



{

image_folder = argv[++i];

image_annotation = true;



}

else if( !strcmp( argv[i], "-annotations" ) )



{

annotations = argv[++i];



}

else if( !strcmp( argv[i], "-videos" ) )



{

video_file = argv[++i];

video_annotation = true;



}

}

// Create the outputfilestream

ofstream output(annotations.c_str());

if(image_annotation)



{

// Return the image filenames inside the image folder

vector<String> filenames;

String folder(image_folder);



glob(folder, filenames);

// Loop through each image stored in the images folder

// Create and temporarily store the annotations

// At the end write everything to the annotations file

for (size_t i = 0; i < filenames.size(); i++){

// Read in an image

Mat current_image = imread(filenames[i]);

// Perform annotations & generate corresponding output

stringstream output_stream;



get_annotations(current_image, &output_stream);

// Store the annotations, write to the output file

if (output_stream.str() != ""){



output << filenames[i] << output_stream.str();

}

}

}

if(video_annotation)



{

VideoCapture cap(video_file);

if(!cap.isOpened())



{

cout << "could not open the video file"<

return -1;



}

int frame_no = 0;





while(1)



{

// Read in an image

Mat current_image;

bool bSuccess = cap.read(current_image);

if(!bSuccess)



{

cout << "cannot read the frames from the video file" <

break;



}

// Perform annotations & generate corresponding output

stringstream output_stream;

bool success = get_annotations(current_image, &output_stream);

if(!success)

break;

// Store the annotations, write to the output file

if (output_stream.str() != ""){

output << "frame_number = " << frame_no++ << ":" << output_stream.str();



}

}

}

return 0;



}