In [17]:
import torch
from torch.autograd import Variable
import torch.nn as nn
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd

torch.manual_seed(119)


Out[17]:
<torch._C.Generator at 0x104a591f0>

In [18]:
n = 100
data = torch.ones(n,2)

In [19]:
x0 = torch.normal(2*data, 1)
y0 = torch.zeros(n, 1)

In [20]:
x1 = torch.normal(-2*data, 1)
y1= torch.ones(n, 1)

In [21]:
x = torch.cat((x0, x1)).type(torch.FloatTensor)
y = torch.cat((y0, y1)).type(torch.FloatTensor)

In [22]:
# plt.scatter(x.data[:,0], x.data[:,1], s=100, lw=0, cmap="RdYlGn")
plt.scatter(x.data[:,0], x.data[:,1], c=y.squeeze().numpy(), s=100, lw=0, cmap='RdYlGn')
plt.show()



In [23]:
# ex = pd.read_csv("https://stats.idre.ucla.edu/stat/data/binary.csv")

In [24]:
linear = nn.Linear(2, 1, bias=True)
sigmoid = nn.Sigmoid()
model = nn.Sequential(linear, sigmoid)

In [25]:
optimizer = torch.optim.SGD(model.parameters(), lr=0.05)
cost_function = nn.BCELoss()

In [26]:
a = 0
for step in range(100):

    hypothesis = model(x)
    cost = cost_function(hypothesis, y)
    
    optimizer.zero_grad()
    cost.backward()
    optimizer.step()
    
    if step % 10 ==0 :
        plt.cla()
        plt.plot(hypothesis.data.squeeze().numpy(), ".")
        print(step, cost.data.numpy())
        
# plt.scatter(x.data[:,0], x.data[:,1], c=y.squeeze().numpy(), s=100, lw=0, cmap='RdYlGn')
# plt.show()


0 1.4314239
10 0.3827346
20 0.20591196
30 0.14425442
40 0.11294719
50 0.093878955
60 0.08096852
70 0.07160241
80 0.06446975
90 0.058839116

In [27]:
# plt.plot(linear(x).data.squeeze().numpy(), ".")
plt.plot(model(x).data.squeeze().numpy(), ".")


Out[27]:
[<matplotlib.lines.Line2D at 0x10cbc1cc0>]

In [28]:
# plt.scatter(x.data[:,0], x.data[:,1], s=100, lw=0, cmap="RdYlGn")
plt.scatter(x.data[:,0], x.data[:,1], c=y.squeeze().numpy(), s=100, lw=0, cmap='RdYlGn')
plt.show()



In [ ]:
def cross(h,y):
    return

In [ ]:
# y*torch.log(1/(1+np.e**(-model(x))))

In [10]:
cost = cost_function(hypothesis, y)

In [11]:
cost


Out[11]:
tensor(1.00000e-02 *
       5.4688)

In [20]:
model(x)


Out[20]:
tensor([[ 0.0475],
        [ 0.0004],
        [ 0.0005],
        [ 0.0357],
        [ 0.0266],
        [ 0.0230],
        [ 0.0003],
        [ 0.0042],
        [ 0.0000],
        [ 0.0012],
        [ 0.0004],
        [ 0.0016],
        [ 0.0004],
        [ 0.0008],
        [ 0.2515],
        [ 0.0001],
        [ 0.0321],
        [ 0.0006],
        [ 0.0014],
        [ 0.0144],
        [ 0.0000],
        [ 0.0003],
        [ 0.0042],
        [ 0.0017],
        [ 0.0003],
        [ 0.0014],
        [ 0.0009],
        [ 0.0006],
        [ 0.0046],
        [ 0.0012],
        [ 0.0005],
        [ 0.0002],
        [ 0.0001],
        [ 0.0024],
        [ 0.0000],
        [ 0.0014],
        [ 0.0005],
        [ 0.0006],
        [ 0.0090],
        [ 0.0027],
        [ 0.0000],
        [ 0.0003],
        [ 0.0002],
        [ 0.0034],
        [ 0.0001],
        [ 0.0094],
        [ 0.0040],
        [ 0.0025],
        [ 0.0119],
        [ 0.0003],
        [ 0.0009],
        [ 0.0018],
        [ 0.0008],
        [ 0.0000],
        [ 0.0012],
        [ 0.0005],
        [ 0.0087],
        [ 0.0295],
        [ 0.0005],
        [ 0.0000],
        [ 0.3120],
        [ 0.0174],
        [ 0.0035],
        [ 0.0002],
        [ 0.0049],
        [ 0.0022],
        [ 0.0013],
        [ 0.0000],
        [ 0.0228],
        [ 0.0010],
        [ 0.2596],
        [ 0.0035],
        [ 0.0310],
        [ 0.0002],
        [ 0.0016],
        [ 0.0178],
        [ 0.0030],
        [ 0.0002],
        [ 0.0130],
        [ 0.0023],
        [ 0.0002],
        [ 0.0005],
        [ 0.0003],
        [ 0.0001],
        [ 0.0043],
        [ 0.0073],
        [ 0.0011],
        [ 0.1044],
        [ 0.0002],
        [ 0.0678],
        [ 0.0343],
        [ 0.0315],
        [ 0.0002],
        [ 0.0053],
        [ 0.0297],
        [ 0.0209],
        [ 0.0215],
        [ 0.1014],
        [ 0.0001],
        [ 0.0004],
        [ 0.9656],
        [ 0.9915],
        [ 0.9796],
        [ 0.9999],
        [ 0.9701],
        [ 0.9944],
        [ 0.9974],
        [ 0.9996],
        [ 0.9953],
        [ 0.9945],
        [ 0.9983],
        [ 0.9620],
        [ 0.9978],
        [ 0.9955],
        [ 0.9996],
        [ 0.9997],
        [ 0.9971],
        [ 0.9999],
        [ 0.9999],
        [ 0.9680],
        [ 0.9990],
        [ 0.9993],
        [ 0.9959],
        [ 0.9702],
        [ 0.9995],
        [ 0.9955],
        [ 0.9972],
        [ 0.9977],
        [ 0.9689],
        [ 0.9998],
        [ 0.9997],
        [ 0.9733],
        [ 0.9914],
        [ 0.9998],
        [ 0.9984],
        [ 0.9997],
        [ 0.9997],
        [ 0.9964],
        [ 0.9906],
        [ 0.9996],
        [ 0.9982],
        [ 0.9982],
        [ 0.9984],
        [ 0.9997],
        [ 0.9936],
        [ 0.9985],
        [ 0.9962],
        [ 0.9681],
        [ 0.9688],
        [ 0.9999],
        [ 0.9996],
        [ 0.9592],
        [ 0.9925],
        [ 0.9964],
        [ 0.9752],
        [ 0.9954],
        [ 0.9985],
        [ 0.9928],
        [ 0.9724],
        [ 0.9824],
        [ 0.9952],
        [ 0.9963],
        [ 0.9922],
        [ 0.9779],
        [ 0.9981],
        [ 0.9546],
        [ 0.9930],
        [ 0.9992],
        [ 0.9471],
        [ 0.9925],
        [ 0.9997],
        [ 0.9891],
        [ 0.9978],
        [ 0.9966],
        [ 0.9892],
        [ 0.9827],
        [ 0.9911],
        [ 0.9976],
        [ 0.9999],
        [ 0.9965],
        [ 0.9999],
        [ 0.9997],
        [ 0.9966],
        [ 0.9997],
        [ 0.9970],
        [ 0.9968],
        [ 0.9965],
        [ 0.9981],
        [ 0.9756],
        [ 0.9994],
        [ 1.0000],
        [ 0.9996],
        [ 0.9994],
        [ 0.9958],
        [ 0.9401],
        [ 0.9964],
        [ 0.9994],
        [ 0.9898],
        [ 0.9882],
        [ 0.9862]])

In [ ]:


In [ ]:


In [ ]:
xmin, xmax = -5, 5
n_samples = 100
np.random.seed(0)
X = np.random.normal(size=n_samples)
yy = (X > 0).astype(np.float)
X[X > 0] *= 4
X += .3 * np.random.normal(size=n_samples)
X = X[:, np.newaxis]

In [ ]:
a = np.random.randn(100,1)

In [ ]:
plt.plot(a, ".")

In [ ]:
a = np.random.randn(100,1)
a.mean()

In [ ]:
a[a>0] = 1
a[a<=0] = 0

In [ ]:


In [ ]:


In [ ]:


In [ ]:
# x1 = np.random.randn(100)
# y1 = np.random.randn(100) + 3
# x2 = np.random.randn(100) + 3
# y2 = np.random.randn(100)
# plt.plot(x1, y1, "+", x2, y2, "x")
# plt.axis('equal')
# plt.show()

In [ ]:
# plt.plot(sigmoid(linear(x)).data.squeeze().numpy(), ".")

In [ ]:
plt.plot(linear(x).data.squeeze().numpy(),)
plt.show()

In [ ]:
plt.plot((np.e**model(x).data.squeeze()/(1+np.e**model(x).data.squeeze())).numpy())

In [ ]:


In [ ]:


In [ ]:


In [ ]: