import matplotlib.pyplot as plt
from collections import Counter
c = Counter((w for l in open("train.txt") for w in l.strip().split() if w != "<unk>"))
#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 != "<unk>"))
%matplotlib inline
import numpy as np
ls = np.array([n for _, n in c.most_common(1000)])
words = [w for w, _ in c.most_common(1000)]
hist = [0]*100000
for w, k in c.most_common():
#print k
hist[k] += 1
nhist = [0]*100000
for w, k in ngrams.most_common():
#print k
nhist[k] += 1
hist[:10]
[0, 30, 14, 25, 72, 276, 1014, 744, 593, 512]
#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")
#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")
#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")
/usr/local/lib/python3.4/dist-packages/matplotlib/__init__.py:892: UserWarning: axes.color_cycle is deprecated and replaced with axes.prop_cycle; please use the latter. warnings.warn(self.msg_depr % (key, alt_key))
import seaborn as sns
y
array([-1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -1. , -0.99999997, -0.99999856, -0.99991793, -0.99534489, -0.76580379, 0.76580379, 0.99534489, 0.99991793, 0.99999856, 0.99999997, 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ])
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")
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")
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")
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")
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")
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")
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")
y2 = np.gradient(y)
plt.plot(x,100*y2)
plt.ylim(-0.2, 1.2)
plt.xlim(-5, 5)
plt.savefig("sigmoidgrad.png")
import IPython
IPy
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")
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")
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")
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")