import torch
import pandas as pd
import numpy as np
import matplotlib as plt
from torch import nn
data = pd.read_csv("dataset/credit-a.csv", header=None)
print(data.info())
data.head()
print("data.head():\t", data.head())
X = data.iloc[:, :-1]
Y = data.iloc[:, -1]
print("Y.unique:\t", np.unique(Y))
b = X.values
X = torch.from_numpy(X.values).type(torch.float32)
print("X.shape:\t", X.shape)
print("X.size():\t", X.size())
c = Y.values.reshape(-1, 1)
Y = torch.from_numpy(Y.values).type(torch.float32)
print("Y.values")
model = nn.Sequential(
nn.Linear(15, 1),
nn.Sigmoid()
)
print("model", model)
loss_fn = nn.BCELoss()
opt = torch.optim.Adam(model.parameters(), lr=0.0001)
"""
mimi-batch训练,
"""
batches = 16
no_of_batch = 653//16
epoches = 1000
for epoch in range(epoches):
for i in range(no_of_batch):
start = i*batches
end = start+batches
x = X[start: end]
y = Y[start: end]
y_pred = model(x)
loss = loss_fn(y_pred, y)
opt.zero_grad()
loss.backward()
opt.step()
model.state_dict()
print("model.state_dict():\t", model.state_dict())
predict = ((model(x).data.numpy() > 0.5).astype("int") == Y.numpy()).mean()
print("predict:\t", predict)
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 653 entries, 0 to 652
Data columns (total 16 columns):
--- ------ -------------- -----
0 0 653 non-null int64
1 1 653 non-null float64
2 2 653 non-null float64
3 3 653 non-null int64
4 4 653 non-null int64
5 5 653 non-null int64
6 6 653 non-null int64
7 7 653 non-null float64
8 8 653 non-null int64
9 9 653 non-null int64
10 10 653 non-null int64
11 11 653 non-null int64
12 12 653 non-null int64
13 13 653 non-null int64
14 14 653 non-null float64
15 15 653 non-null int64
dtypes: float64(4), int64(12)
memory usage: 81.8 KB
None
data.head(): 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 0 30.83 0.000 0 0 9 0 1.25 0 0 1 1 0 202 0.0 -1
1 1 58.67 4.460 0 0 8 1 3.04 0 0 6 1 0 43 560.0 -1
2 1 24.50 0.500 0 0 8 1 1.50 0 1 0 1 0 280 824.0 -1
3 0 27.83 1.540 0 0 9 0 3.75 0 0 5 0 0 100 3.0 -1
4 0 20.17 5.625 0 0 9 0 1.71 0 1 0 1 2 120 0.0 -1
Y.unique: [-1 1]
X.shape: torch.Size([653, 15])
X.size(): torch.Size([653, 15])
Y.values
model Sequential(
(0): Linear(in_features=15, out_features=1, bias=True)
(1): Sigmoid()
)
E:\Professional Software\Anconda\envs\pytracking\lib\site-packages\torch\nn\modules\loss.py:498: UserWarning: Using a target size (torch.Size([16])) that is different to the input size (torch.Size([16, 1])) is deprecated. Please ensure they have the same size.
return F.binary_cross_entropy(input, target, weight=self.weight, reduction=self.reduction)
model.state_dict(): OrderedDict([('0.weight', tensor([[ 0.1030, -0.0765, -0.2319, 0.5685, 0.5918, -0.0065, 0.5697, -0.9154,
2.0562, 0.7506, -1.4317, 0.5080, 0.0578, 0.0038, -0.0282]])), ('0.bias', tensor([0.1462]))])
predict: 0.20501531393568148