Welcome To Colaboratory - Colaboratory
Welcome To Colaboratory - Colaboratory
Colaboratory, or "Colab" for short, allows you to write and execute Python in your browser, with
Whether you're a student, a data scientist or an AI researcher, Colab can make your work easier. Watch Introduction to Colab to
learn more, or just get started below!
Getting started
The document you are reading is not a static web page, but an interactive environment called a Colab notebook that lets you wr
and execute code.
For example, here is a code cell with a short Python script that computes a value, stores it in a variable, and prints the result:
seconds_in_a_day = 24 * 60 * 60
seconds_in_a_day
86400
To execute the code in the above cell, select it with a click and then either press the play button to the left of the code, or use th
keyboard shortcut "Command/Ctrl+Enter". To edit the code, just click the cell and start editing.
Variables that you de ne in one cell can later be used in other cells:
seconds_in_a_week = 7 * seconds_in_a_day
seconds_in_a_week
Colab notebooks allow you to combine executable code and rich text in a single document, along with images, HTML, LaTeX a
more. When you create your own Colab notebooks, they are stored in your Google Drive account. You can easily share your Cola
notebooks with co-workers or friends, allowing them to comment on your notebooks or even edit them. To learn more, see Over
of Colab. To create a new Colab notebook you can use the File menu above, or use the following link: create a new Colab noteb
Colab notebooks are Jupyter notebooks that are hosted by Colab. To learn more about the Jupyter project, see jupyter.org.
Data science
With Colab you can harness the full power of popular Python libraries to analyze and visualize data. The code cell below uses n
to generate some random data, and uses matplotlib to visualize it. To edit the code, just click the cell and start editing.
import numpy as np
from matplotlib import pyplot as plt
ys = 200 + np.random.randn(100)
x = [x for x in range(len(ys))]
plt.title("Sample Visualization")
plt.show()
You can import your own data into Colab notebooks from your Google Drive account, including from spreadsheets, as well as fr
Github and many other sources. To learn more about importing data, and how Colab can be used for data science, see the links
below under Working with Data.
Machine learning
With Colab you can import an image dataset, train an image classi er on it, and evaluate the model, all in just a few lines of cod
Colab notebooks execute code on Google's cloud servers, meaning you can leverage the power of Google hardware, including G
and TPUs, regardless of the power of your machine. All you need is a browser.
Colab is used extensively in the machine learning community with applications including:
To see sample Colab notebooks that demonstrate machine learning applications, see the machine learning examples below.
More Resources
Working with Notebooks in Colab
Overview of Colaboratory
Guide to Markdown
Importing libraries and installing dependencies
Saving and loading notebooks in GitHub
Interactive forms
Interactive widgets
TensorFlow 2 in Colab
Intro to Pandas
Tensor ow concepts
First steps with TensorFlow
Intro to neural nets
Intro to sparse data and embeddings
Neural Style Transfer: Use deep learning to transfer style between images.
EZ NSynth: Synthesize audio with WaveNet auto-encoders.
Fashion MNIST with Keras and TPUs: Classify fashion-related images with deep learning.
DeepDream: Produce DeepDream images from your own photos.
Convolutional VAE: Create a generative model of handwritten digits.
import numpy as np
from numpy import inf
d = np.array([
[0,5,15,4]
, [5,0,4,8]
, [15,4,0,1]
, [4,8,1,0]
])
iteration = 20
n_ants = 4
n_citys = 4
m= n_ants
n= n_citys
e= .5
alpha=1
beta=2
visibility = 1/d
visibility[visibility == inf] = 0
pheromne = .1*np.ones((m,n))
rute=np.ones((m,n+1))
for i in range(m):
temp_visibility = np.array(visibility)
for j in range(n-1):
combine_feature=np.zeros(5)
cum_prob = np.zeros(5)
cur_loc= int(rute[i,j]-1)
temp_visibility[:,cur_loc] = 0
p_feature = np.power(pheromne[cur_loc,:],beta)
v_feature = np.power(temp_visibility[cur_loc,:],alpha)
p_feature = p_feature[:,np.newaxis]
v_feature = v_feature[:,np.newaxis]
combine_feature = np.multiply(p_feature,v_feature)
total = np.sum(combine_feature)
probs = combine_feature/total
cum_prob = np.cumsum(probs)
r=np.random.random_sample()
city= np.nonzero(cum_prob>r)[0][0]+1
rute[i,j+1] = city
rute_opt = np.array(rute)
dist_cost = np.zeros((m,1))
for i in range(m):
s=0
for j in range(n-1):
s=s+d[int(rute_opt[i,j])-1, int(rute_opt[i,j+1])-1]
dist_cost[i]=s
dist_min_loc=np.argmin(dist_cost)
dist_min_cost=dist_cost[dist_min_loc]
best_route=rute[dist_min_loc,:]
pheromne=(1-e)*pheromne