Friday, August 26, 2016

Machine Learning - Basic Simulator

(Success!  The red dot can seek the blue dot.)
I'll keep this brief.   Below is a simulation I made of a "predator" (red dot) that learns to go towards its "prey" (blue dot).  You can try out the simulation here:  trinket.io/python/136e8d2535 or right here:

Click on the "play" button.  It looks like this: 


To keep a copy of the code, you can copy and paste from the above frame, or download the Python .py file from my GitHub repo: 
github.com/hchiam/machineLearning

The structure of the neural network looks like this:


This simulation is based on code I've made in previous posts:

Machine Learning - Very Basic Code:  (different examples of neural networks)
hchiam.blogspot.ca/2016/08/machine-learning-very-basic-code.html

The Code 7 Project - Problem 3 - Turtle Drawing: 
hchiam.blogspot.ca/2016/07/the-code-7-project-problem-3.html

_________________________

LINKS TO OTHER STUFF: 

Favourites
Programming
Original Art
Games

Chinese Learning Projects:
 - Book
 - HSK 1
 - HSK 5
 - Homonyms Mnemonics

Tuesday, August 23, 2016

Machine Learning - Very Basic Code

Machine Learning!

Sounds complicated?  Well, it can be.  But I've put together some super simple code at this GitHub repo github.com/hchiam/machineLearning which has my .py Python files (plus one from iamtrask) that you can run on your own computer (I like to use Terminal commandline)... or you can run the code yourself right here on this page!

Instructions:
= Run Code
= Edit Code

The following four code examples are four different neural nets, ranging from differing approaches to giving "feedback" to the "neuron connections", to testing out a layered version.  Each example is commented (Python uses the "#" character for comments, which should turn green in these interfaces).


neuralNet1.py:  Version 1.  Example of a very simplified neural network, and using a sensitivity parameter.



neuralNet2.py:  Version 2.  Example of a very simplified neural network, with weighting based on "responsibilities" of different inputs.  (This one seems really fast but may be unstable or naive because it's basically using learning sensitivity = 1.)



neuralNet3.py:  Version 3.  Example of a very simplified neural network that combines version 1 and version 2, combining sensitivity parameter and "responsibilities" of different inputs.
 


neuralNet4_Layered.py:  Version 4.  Example of a neural network that kinda combines version 2 and version 3:  uses learning error sensitivity, but now also uses a hidden layer. It also has my own "transformed" (i.e. translated) version of the sigmoid function that goes from -1 to 1.


Again, note that I created these code examples to help myself wrap my head around the very basic basics of machine learning, specifically neural networks.  Hopefully they help you too, through experimenting and reading the comments.  You can Google the rest.


LINKS TO INTERESTING READS/FINDS for Machine Learning:
  • Neural network playground:  here.
  • Python neural network in 11 lines of code:  here.
  • Python genetic algorithm in 15 lines of code:  here.
  • Ruby code for "suggested posts" via machine learning:  here.
  • Python Recurrent Neural Network for generating Shakespeare and more:  here.
  • A longer read but great code examples running in-browserhere.
_________________________

LINKS TO OTHER STUFF: 

Favourites
Programming
Original Art
Games

Chinese Learning Projects:
 - Book
 - HSK 1
 - HSK 5
 - Homonyms Mnemonics

Sunday, August 7, 2016

The Code 7 Project - Problem 5 - Pascal Triangle

(Build Schema.  Run SQL.  Pascal triangle row values in tables below.)
The Code 7 Project - Problem 5 - Pascal Triangle

As usual, you can see my code and run it online without installing anything specialhttp://sqlfiddle.com/#!9/87f0d/3 

For problem 5 from this website, my basic goal was to use SQL to create and access values from a math construct called Pascal's triangleYou can choose a row number (i.e. which row in the triangle to look at), and then you receive the values in that row, output in the form of a table.  My code is programmed using the MySQL dialect of SQL, based on the available online compiler that I used.

If you go to that sqlfiddle link for the code, it should automatically run and print out a few tables (the "val" columns are the values that correspond to their respective rows in Pascal's triangle).  I used the online “sqlfiddle” website to create the MySQL table and client because it seemed easier to use than the corresponding TutorialsPoint website.

I found this YouTube video really helpful in figuring out how to get the fiddler website to work (change the “‘;” in the blue button to “//“):  youtube.com/watch?v=QtiCEAeSOYc   

More info on the "Code 7 Project":  hchiam.blogspot.ca/2016/06/code-7-project-github-blog.html

_________________________

A separate side-project:  "Cognate Language" in Python code.  

Description at the GitHub readme file: 
github.com/hchiam/cognateLanguage/blob/master/README.md


And most up-to-date versions of files: 
github.com/hchiam/cognateLanguage 


_________________________

LINKS TO OTHER STUFF: 

Favourites
Programming
Original Art
Games

Chinese Learning Projects:
 - Book
 - HSK 1
 - HSK 5
 - Homonyms Mnemonics

Basic Terminal Commands for GitHub Branch and Merge, in one place:

You can easily Google for websites describing details of how "git branch" and "git merge" work, but I thought it'd be useful to have their basic commands in one page for reference.  If you want fuller descriptions, goto Google(By the way, "commandline" = "Terminal" on Macs.)

First navigate folders using cd and check what files are there using ls before typing the following commands.  You'll be typing each command one at a time (the comments in blue that start with "#" are just for short explanations--they don't perform anything in Terminal).   

In the commands below, "nameOfBranch" is the name of the branch (a copy of files "branching" temporarily from the master copy).  This would be the name of the branch that you want to create, edit, remotely "save", merge with the original master copy, and finally get rid of:

# create branch & use its files
git branch nameOfBranch
git checkout nameOfBranch

# make changes to its files on your computer…

# transfer files to remote repository, with a message describing the “save”
git add --all
git commit -m “message”
git push origin nameOfBranch

# merge changes into master (by first moving to master branch copy)
git checkout master
git merge nameOfBranch

git push origin master

# get rid of old branch
git branch -D nameOfBranch

I've backed up these instructions (if you prefer a file) at: 
github.com/hchiam/code7/blob/master/gitBranchMerge.txt

More examples of Terminal/Command-Line code for GitHub:  (ctrl+f "essential commands")
http://hchiam.blogspot.ca/2016/06/the-code-7-project-problem-1.html


_________________________

LINKS TO OTHER STUFF: 

Favourites
Programming
Original Art
Games

Chinese Learning Projects:
 - Book
 - HSK 1
 - HSK 5
 - Homonyms Mnemonics

Sunday, July 31, 2016

The Code 7 Project - Problem 4 - Puzzle Game

The Code 7 Project - Problem 4 - Puzzle Game


Description of the "Code 7 Project": 
hchiam.blogspot.ca/2016/06/code-7-project-github-blog.html
Practicing general programming skills by building 7 simple programs over 7 different weekends (when my other commitments allow it).

Description of Problem 4:

blog.smartbear.com/programming/7-silly-programming-challenges-to-do-for-fun

Game Solution So Far for Problem 4:

scratch.mit.edu/projects/117437175

GitHub Repo Backup of Game Solution for Problem 4:
github.com/hchiam/code7


You can play the game right here, without having to go the any of those websites or downloading anything:  Click on the green flag to begin and start clicking the puzzle pieces to move them around.
I might update this game later.





_________________________

LINKS TO OTHER STUFF: 

Favourites
Programming
Original Art
Games

Chinese Learning Projects:
 - Book
 - HSK 1
 - HSK 5
 - Homonyms Mnemonics

Sunday, July 3, 2016

The Code 7 Project - Problem 3 - Turtle Drawing

(Code. Canvas. Command-line.)

The Code 7 Project - Problem 3 - Turtle Drawing

Drawing with code.  This was more of a fun exercise than it was "serious" problem-solvingBasically, for problem 3, I tested out the drawing capabilities of the Turtle extension of PythonI made it draw a certain species of fractal pattern (something like the patterns in tree branching or lung branching).  The Python code gives simple interactivity with Terminal to let you choose the number of fractal "levels" in the output drawing.  My solution just took a couple of functions to modularize things, as well as a little recursion (see example code for "tail recursion" under Problem 2).

Links to more info:

[EDIT, July 4th, 2016:  You can copy and paste this code into the interface at https://trinket.io/python
so you can test drawing with the Turtle extension without having to install Python (the tutorialspoint website isn't able to create drawings with Turtle).]

[EDIT EDIT, July 5th, 2016:  Oooor you can run the code right here:]

 

_________________________

LINKS TO OTHER STUFF: 

Favourites
Programming
Original Art
Games

Chinese Learning Projects:
 - Book
 - HSK 1
 - HSK 5
 - Homonyms Mnemonics

Sunday, June 26, 2016

The Code 7 Project - Problem 2 - Fibonacci

(Click image to test code in Java online.  No install necessary.)

The Code 7 Project - Problem 2 - Fibonacci

  • Given a number "n", find the "n"-th number in the Fibonacci sequence (0, 1, 1, 2, 3, 5, 8, 13, 21, ...).  
  • Full problem description is here.  
  • Description of the Code 7 Project is here

For this problem (and sub-problems), I've used tutorialspoint.com's online interface to compile and execute the code quickly.

There were 5 sub-problems:
2.a) Just do it.
2.b) No loop.  Only constants, no variables.
2.c) No local variables.
2.d) Tail recursion (the function calls itself on the last line in said function).
2.e) No recursion. 
 


Code for all of these can be found under my GitHub repo:  
https://github.com/hchiam/code7

Below are individual links to each sub-problem's solution code, and instructions to view/run the code in your web browser:  


2.a) Just do it.

http://goo.gl/sxNER4
Click on "problem2a.java" (not .class) to see the code. 
Click on "Compile" to make sure the code is set up. 
Click on "Execute" to run the code.  

2.b) No loop.  Only constants, no variables.

http://goo.gl/HS4xhp
Click on "problem2b.java" (not .class) to see the code. 
Click on "Compile" to make sure the code is set up. 
Click on "Execute" to run the code.  

2.c) No local variables.

http://goo.gl/diziRi
Click on "problem2c.java" (not .class) to see the code. 
Click on "Compile" to make sure the code is set up.  
Click on "Execute" to run the code.   

2.d) Tail recursion (the function calls itself on the last line in said function).

http://goo.gl/6ixNZ8
[EDIT:  the above link's code isn't true tail recursion, but it does use recursion.  Go to this link for the example of true tail recursion:  http://goo.gl/ytnRHF]
Click on "problem2d.java" (not .class) to see the code. 
Click on "Compile" to make sure the code is set up.  
Click on "Execute" to run the code.   

2.e) No recursion.

(Same as 2.b).)



_________________________

LINKS TO OTHER STUFF: 

Favourites
Programming
Original Art
Games

Chinese Learning Projects:
 - Book
 - HSK 1
 - HSK 5
 - Homonyms Mnemonics