Coder Social home page Coder Social logo

42 AIML

Resources

Python

Learn Python3 not Python2 Why?

Free Online Books

Data Science

Machine Learning

Books

Reinforcement Learning

Books
Course

Deep Learning

Courses
Course Notes
Books

AI

PyTorch

Learning and understanding the concepts are more important than the framework (i.e Pytorch or Tensorflow

Install PyTorch Globally
  1. You should already have python3.6 installed(if you ran the brew setup guide below)
  2. Go to pytorch.org
  3. Select
  • OS -> Mac OSX
  • Package Manager -> pip
  • Python -> 3.6 (you can verify the python version by running python3 --version in terminal)
  • CUDA -> doesn't matter, we aren't going to install GPU drivers yet
  1. Copy what's in the 'Run this command:' text box and paste into terminal
  2. After it's finished you should be done. To verify:
    • pip freeze | grep torch should show you the version installed
    • run
 import torch

 print(torch.__version__)

How to install Python on Mac?

  1. Install brew (PM marvin !brew for instructions)
  2. brew install python3

Install Tips

  • Run and use python3 in shell after you install to test syntax quickly
  • To install packages run pip3 install <package>
    • install better REPL ptpython use that instead of python3 interpreter to test code: pip3 install ptpython then in terminal run ptpython
  • Run Pyton online here

Installing packages into a virtualenv (per project basis)

  1. Install virtualenv package pip3 install virtualenv
    • If you run pip3 freeze, you'll get a list of all pip packages installed and their versions globally.
  2. Go to your project directory cd <my_new_project_path>
  3. Run python3.6 -m virtualenv venv or virtualenv venv. It'll create a new folder to install your dependcies in.
  4. There's also scripts created in the venv folder to activate a virtualenv
  5. Let's activate it: source venv/bin/activate
    • You should notice that your terminal has changed. (venv) should be prepended to the front of the cmd line
  6. Now when you install packages or run pip commands they'll be installed to and ran from the virtualenv
  7. If you do pip freeze, you'll notice it returns an empty list.
    • Try to install a pip package and rerun pip freeze
  8. Now you can install packages normally with pip or install all packages found in a 'requirements.txt' with pip -r requirements.txt
  9. When you run your scripts(e.g. python3.6 do_stuff.py it'll use packages found in env first)
  10. To leave or deactivate the environment simply enter deactivate into the cmdline.

C/Python Differences (Stackoverflow Link)

C Python
Procedural (list of instructions) Multi (Object-oriented + Procedural + Functional)
Static typed (int c = 10;) Dynamic (c = 10)
Compiled (Source -> Target language) Slow to develop, Fast to execute Interpreted (Source -> Intermediate -> Target) Fast to develop, Slow to execute
Whitespace insensitive Whitespace sensitive
Manual memory management Automatic memory management
Not easy to test Very easy to test
Fast Slow

Why use Python?

  1. We can get to what matters faster with lesswork. We can simply go straight to experimentation and testing much more quickly. For most applications, you won't notice the "slowness" of Python.
  2. Most of the deep learning frameworks are written for or support Python.

Ex1 - Running a program

// C - hello_world.c
#include <stdio.h>

int	main(void)
{
	char *s = "Hello, World!";
	
	printf(s);
}
gcc hello_world.c -o hello_world.o
./hello_world.o
# Python - hello_world.py
s = "Hello, World" # Python has no declarations
print(s)
python hello_world.py
  • You can add a def main() to hello_world.py
  • note the commenting is different
    • # for python to end of line
    • // to end of line or /* STUFF */ for multi-line comments

Ex2 - Loops, Whitespace

// C
#include <stdio.h>

void	ft_nprint(const char *s_even, const char *s_odd, int c)
{
	while (c--)
	{
		if (c % 2 == 0 && c % 3 == 0)
		{
			printf(s_even);
			printf(s_odd);
		}
		else if (c % 2 == 0)
			printf(s_even);
		else if  (c % 3 == 0)
			printf(s_odd);
		else
			printf("NEVER");
	}
}
#Python

def	ft_nprint(s_even, s_odd, c):
	while (c):
		if (c % 2 == 0 and c % 3 == 0):
			print(s_even)
			print(s_odd)
		elif (c % 2 == 0):
			print(s_even)
		elif (c % 3 == 0):
			print(s_odd)
		else:
			print("NEVER")
		c -= 1
  • Whitespace matters in Python but you don't need brackets or semi-colons

Ex3 - Return values

// C

int	sum(int a, int b)
{
	return (a + b);
}
# Python

def	sum(a, b):
	return a + b

Ex4 - Variable assignemnt

# Python

def main():
	x = y = 0
	a, b, c = 1, 2, 3 # a = 1, b = 2, c = 3
// C

int main(void)
{
	int x, y;
	int a, b, c;
	
	x = y = 0;
	a = 1;
	b = 2;
	c = 3;
}

Ex5 - Decrement/Increment operator

# Python

i = 10
while (i):
	print(i)
	i -= 1
// C

int i = 10;

while (i--)
	printf(i);

Ex6 - Import/Includes

# Python

from math import sqrt

def main():
	print("Sqrt of 10 is {}", sqrt(10));
// C
#include <stdio.h>
#include <math.h>


int main(void)
{
	printf("Sqrt of 10 is %d", sqrt(10));
}

Ex7 - Iterating over arrays

# Python

nums = [1, 2, 3, 4, 5]

for i in range(0, len(nums)):
	print(nums[i]) # iterate using an index
// C
int size = 5;
int nums[] = [1, 2, 3, 4, 5];

for (i=0; i < size; i++)
	printf(nums[i]);
  • You can't iterate over arrays using pointers in Python
  • There aren't any pointers in Python

Ex8 - Passing arguments by value/reference?

  • Better to Google search this one

AI/ML Club - 42's Projects

42aiml icon 42aiml

General resources for the 42 AI/ML Club

sdc icon sdc

Our club's self driving car project

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.