#!/usr/bin/env python # coding: utf-8 # In[97]: import matplotlib.pyplot as plt # In[106]: from collections import Counter c = Counter((w for l in open("train.txt") for w in l.strip().split() if w != "")) # In[124]: #lp = l.strip().split() ngrams = Counter((w for l in open("train.txt") for lp in [l.strip().split()] for w in zip(lp, lp[1:], lp[2:]) if w != "")) # In[4]: get_ipython().run_line_magic('matplotlib', 'inline') # In[5]: import numpy as np ls = np.array([n for _, n in c.most_common(1000)]) words = [w for w, _ in c.most_common(1000)] # In[111]: hist = [0]*100000 for w, k in c.most_common(): #print k hist[k] += 1 # In[125]: nhist = [0]*100000 for w, k in ngrams.most_common(): #print k nhist[k] += 1 # In[114]: hist[:10] # In[134]: #plt.plot(ls[:200]) sns.set_context("talk", rc={"lines.linewidth": 2.5}) sns.set_style("darkgrid") f, ax = plt.subplots(figsize=(15, 7)) #plt.plot(figsize=(10,10)) g = sns.barplot(y="total", x="abbrev", data={"total": nhist[1:20], "abbrev": range(1,20)}, label="Total") #g.get_axes()[0].set_yscale('log') g.set( yscale="log") #_ = ax.set_yticklabels([ w if i % 10 ==0 else "" for i, w in enumerate(words[:200]) ]) plt.savefig("nhist.png") # In[121]: #plt.plot(ls[:200]) sns.set_context("talk", rc={"lines.linewidth": 2.5}) sns.set_style("darkgrid") f, ax = plt.subplots(figsize=(15, 7)) #plt.plot(figsize=(10,10)) sns.barplot(y="total", x="abbrev", data={"total": hist[6:50], "abbrev": range(6,50)}, label="Total") #_ = ax.set_yticklabels([ w if i % 10 ==0 else "" for i, w in enumerate(words[:200]) ]) plt.savefig("hist.png") # In[78]: #plt.plot(ls[:200]) sns.set_context("talk", rc={"lines.linewidth": 2.5}) sns.set_style("darkgrid") f, ax = plt.subplots(figsize=(10, 7)) #plt.plot(figsize=(10,10)) sns.barplot(x="total", y="abbrev", data={"total": ls[:200], "abbrev": words[:200]}, label="Total") _ = ax.set_yticklabels([ w if i % 10 ==0 else "" for i, w in enumerate(words[:200]) ]) plt.savefig("zipf.png") # In[12]: import seaborn as sns # In[19]: y # In[94]: import math x = np.linspace(-5, 5, 1000) y = x * x * x plt.plot(x, y) #plt.gcf().subplots_adjust(top=0.75) plt.ylim(-5.2, 5.2) plt.xlim(-5, 5) plt.savefig("cube.png") # In[96]: y2 = np.gradient(y) plt.plot(x, 100*y2) #sns.regplot(y="y", x="x", data={"x":x, "y":y}, scatter=False, order=5)plt.ylim(-0.2, 1.2) plt.xlim(-5, 5) plt.ylim(-0.2, 5.2) #plt.xlim(-5, 5) plt.savefig("cubegrad.png") # In[85]: x = np.linspace(-5, 5, 1000) y = np.tanh(x) plt.plot(x, y) #plt.gcf().subplots_adjust(top=0.75) plt.ylim(-1.2, 1.2) plt.xlim(-5, 5) #sns.regplot(y="y", x="x", data={"x":x, "y":y}, scatter=False, order=5) plt.savefig("tanh.png") # In[87]: y2 = np.gradient(y) plt.plot(x, 100*y2) #sns.regplot(y="y", x="x", data={"x":x, "y":y}, scatter=False, order=5)plt.ylim(-0.2, 1.2) plt.xlim(-5, 5) plt.ylim(-0.2, 1.2) #plt.xlim(-5, 5) plt.savefig("tanhgrad.png") # In[82]: x = np.linspace(-5, 5, 1000) y = np.maximum(-1, x) y = np.minimum(1, y) plt.plot(x, y) #plt.gcf().subplots_adjust(top=0.75) plt.ylim(-1.2, 1.2) plt.xlim(-5, 5) #sns.regplot(y="y", x="x", data={"x":x, "y":y}, scatter=False, order=5) plt.savefig("hardtanh.png") # In[83]: y2 = np.gradient(y) plt.plot(x, 100*y2) #sns.regplot(y="y", x="x", data={"x":x, "y":y}, scatter=False, order=5) plt.ylim(-0.2, 1.2) plt.xlim(-5, 5) plt.savefig("hardtanhgrad.png") # In[79]: x = np.linspace(-5, 5, 1000) y = 1./(1 + np.exp(-x)) plt.plot(x, y) #plt.gcf().subplots_adjust(top=0.75) plt.ylim(-0.2, 1.2) plt.xlim(-5, 5) #sns.regplot(y="y", x="x", data={"x":x, "y":y}, scatter=False, order=5) plt.savefig("sigmoid.png") # In[80]: y2 = np.gradient(y) plt.plot(x,100*y2) plt.ylim(-0.2, 1.2) plt.xlim(-5, 5) plt.savefig("sigmoidgrad.png") # In[7]: import IPython IPy # In[75]: x = np.linspace(-5, 5, 1000) y = np.array([max(a, 0) for a in list(x)]) plt.plot(x, y) #sns.regplot(y="y", x="x", data={"x":x, "y":y}, scatter=False, order=5) plt.ylim(-0.2, 5) plt.xlim(-5, 5) plt.savefig("Relu.png") # In[78]: y2 = np.gradient(y) plt.plot(x, y2*100) #sns.regplot(y="y", x="x", data={"x":x, "y":y}, scatter=False, order=5)plt.ylim(-0.2, 1.2) plt.ylim(-0.2, 1.2) plt.xlim(-5, 5) plt.savefig("Relugrad.png") # In[71]: x = np.linspace(-5, 5, 1000) y = x > 0 plt.plot(x, y) #sns.regplot(y="y", x="x", data={"x":x, "y":y}, scatter=False, order=5) plt.ylim(-0.2, 1.2) plt.xlim(-5, 5) plt.savefig("01.png") # In[73]: y2 = np.gradient(y) plt.plot(x, y2*100) #sns.regplot(y="y", x="x", data={"x":x, "y":y}, scatter=False, order=5) plt.ylim(-0.2, 1.2) plt.xlim(-5, 5) plt.savefig("01grad.png")