Beispiel #1
0
def main():
    data_path = args.data_path

    train_df = pd.read_csv(os.path.join(data_path, 'train.csv'))
    test_df = pd.read_csv(os.path.join(data_path, 'test.csv'))

    train_df['type'] = 1
    test_df['type'] = 0

    df = pd.concat([train_df, test_df], ignore_index=True)

    df, field_dims = preprocessing(df)

    train_df = df[df['type'] == 1]
    test_df = df[df['type'] == 0]

    x_train = train_df[[col for col in train_df.columns if col not in ['Survived', 'type']]].values
    y_train = train_df[['Survived']].values.ravel()

    x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.2, random_state=1024)

    x_test = test_df.drop(['Survived', 'type'], axis=1).values

    print("x_train.shape =", x_train.shape)
    print("x_val.shape =", x_val.shape)
    print("x_test.shape =", x_test.shape)
    print("y_train.shape =", y_train.shape)
    print("y_val.shape =", y_val.shape)

    X_train_tensor = torch.tensor(x_train).long()
    y_train_tensor = torch.tensor(y_train).float()
    X_val_tensor = torch.tensor(x_val).long()
    y_val_tensor = torch.tensor(y_val).float()
    X_test_tensor = torch.tensor(x_test).long()

    dl_train = DataLoader(TensorDataset(X_train_tensor, y_train_tensor), shuffle=True, batch_size=8)
    dl_val = DataLoader(TensorDataset(X_val_tensor, y_val_tensor), shuffle=True, batch_size=8)
    dl_test = DataLoader(TensorDataset(X_test_tensor))

    device = torch.device(args.device)
    # fm = FactorizationMachineModel(field_dims=field_dims, embed_dim=16)
    fm = DeepFMModel(field_dims=field_dims, embed_dim=16)
    summary(fm, input_shape=(15,), input_dtype=torch.long)
    criterion = nn.BCELoss()
    optimizer = torch.optim.Adam(params=fm.parameters(), lr=0.001, weight_decay=1e-6)

    writer = SummaryWriter('../data/tensorboard/')
    for e in range(args.epochs):
        train(fm, dl_train, device, criterion, optimizer)
        train_auc_score = test(fm, dl_train, device)
        val_auc_score = test(fm, dl_val, device)
        writer.add_scalars('AUC', {'Train': train_auc_score,
                                   'Val': val_auc_score}, e)
        print(f'\nepoch {e}\t train auc: {round(train_auc_score, 4)}\tvalidation auc: {round(val_auc_score, 4)}')

    writer.add_graph(fm, input_to_model=torch.rand(1, 15).long())
    writer.flush()
    writer.close()
Beispiel #2
0
        torch.tensor(x_valid).float(),
        torch.tensor(y_valid).float()),
                          shuffle=False,
                          batch_size=batch_size)
    # 测试数据管道
    for features, labels in dl_train:
        print(features, labels)
        break
    for features, labels in dl_valid:
        print(features, labels)
        break

    net = Model(n_feature=n_feature, n_hidden=n_hidden, n_output=n_output)
    print(net)

    summary(net, input_shape=(n_feature, ))

    # 训练模型
    # 1、定义损失函数、优化函数、评估指标
    from sklearn.metrics import accuracy_score

    loss_func = nn.BCELoss()
    optimizer = torch.optim.Adam(params=net.parameters(), lr=lr)
    metric_func = lambda y_pred, y_true: accuracy_score(
        y_true.data.numpy(),
        y_pred.data.numpy() > 0.5)
    metric_name = "accuracy"
    # 2、训练模型

    dfhistory = pd.DataFrame(columns=[
        "epoch", "loss", metric_name, "val_loss", "val_" + metric_name
    net.add_module("linear1", nn.Linear(15, 20))
    net.add_module("relu1", nn.ReLU())
    net.add_module("linear2", nn.Linear(20, 15))
    net.add_module("relu2", nn.ReLU())
    net.add_module("linear3", nn.Linear(15, 1))
    net.add_module("sigmoid", nn.Sigmoid())
    return net

net = create_net()
print(net)


#%% 两种方法summary数据
# 1
from torchkeras import summary
summary(net, input_shape=(15,))
# 2
# from torchsummary import summary
# summary(net, input_size=(15,))


#%% 训练模型
from sklearn.metrics import accuracy_score

loss_func = nn.BCELoss()
optimizer = torch.optim.Adam(params=net.parameters(), lr=0.01)
metric_func = lambda y_pred, y_true: accuracy_score(y_true.data.numpy(), y_pred.data.numpy() > 0.5)
metric_name = "accuracy"

#%% 脚本循环
import datetime
Beispiel #4
0
# @File   : test_net.py

import torch
from torch import nn
from torchkeras import summary
from thop import profile


class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        y = self.relu(x)
        return y


if __name__ == '__main__':
    model = Net()
    print(model)
    print(summary(model, input_shape=(3, 20, 20)))
    print('number of params:', sum(param.numel() for param in model.parameters()))
    inputs = torch.randn(8, 3, 20, 20)
    flops, params = profile(model, (inputs,))
    print('flops:', flops, 'params:', params)
                              traindata=True,
                              shuffle=True)
    val_dloader = load_data(data_base_dir + 'cnews.val.txt',
                            traindata=False,
                            shuffle=False)

    print('*' * 27, '%d 个 step:' % len(train_dloader))  # 1000 个step/batch
    sample_batch = next(iter(train_dloader))
    print('*' * 27, 'sample_batch:', len(sample_batch), sample_batch[0].size(),
          sample_batch[0].dtype, sample_batch[1].size(),
          sample_batch[1].dtype)  # 4   [b, doc_maxlen] int64

    model = DPCNN(num_classes, net_depth=net_depth, max_seqlen=doc_maxlen)

    torchkeras.summary(model,
                       input_shape=(doc_maxlen, ),
                       input_dtype=torch.int64)

    model = model.to(device)
    if ngpu > 1:
        model = torch.nn.DataParallel(model, device_ids=list(
            range(ngpu)))  # 设置并行执行  device_ids=[0,1,2,3]

    model.eval()
    sample_out = model(sample_batch[0])
    print('*' * 10, 'sample_out:', sample_out.shape)  # [b, 10]

    optimizer = torch.optim.AdamW(model.parameters(), lr=LR, weight_decay=1e-4)
    scheduler_1r = torch.optim.lr_scheduler.LambdaLR(
        optimizer, lr_lambda=lambda epoch: 0.1 if epoch > EPOCHS * 0.8 else 1)
    train_model(
Beispiel #6
0
        x = self.dropout(x)
        x = self.adaptive_pool(x)
        x = self.flatten(x)
        x = self.linear1(x)
        x = self.relu(x)
        x = self.linear2(x)
        y = self.sigmoid(x)
        return y


net = Net()
print(net)

import torchkeras

print(torchkeras.summary(net, input_shape=(3, 28, 28)))

import pandas as pd
from sklearn.metrics import roc_auc_score

model = net
model.optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
model.loss_func = nn.BCELoss()
model.metric_func = lambda y_pred, y_true: roc_auc_score(
    y_true.data.numpy(), y_pred.data.numpy())
model_metric_name = "auc"


def train_step(model, features, labels):
    labels_tensor = trans_labels(labels, model.out_n)
    model.train()
# 使用nn.ModuleList作为模型容器:
# 注意:下面中的ModuleList不能使用Python中的列表代替:
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.layers = nn.ModuleList([
            nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3),
            nn.MaxPool2d(kernel_size=2, stride=2),
            nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5),
            nn.MaxPool2d(kernel_size=2, stride=2),
            nn.Dropout2d(p=0.1),
            nn.AdaptiveMaxPool2d((1, 1)),
            nn.Flatten(),
            nn.Linear(64, 32),
            nn.ReLU(),
            nn.Linear(32, 1),
            nn.Sigmoid()
        ])

    def forward(self, x):
        for layer in self.layers:
            # print(layer)  # layer是layers中的各行
            x = layer(x)
        return x


net = Net()
print(net)

summary(net, input_shape=(3, 32, 32))
        x = self.adaptive_pool(x)
        x = self.flatten(x)
        x = self.linear1(x)
        x = self.relu(x)
        x = self.linear2(x)
        y = self.sigmoid(x)
        return y


net = Net()
# print(net)

#%%
import torchkeras

torchkeras.summary(net, input_shape=(3, 32, 32))

#%% 训练模型,函数形式循环
import pandas as pd
from sklearn.metrics import roc_auc_score

model = net
model.optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
model.loss_func = torch.nn.BCELoss()
model.metric_func = lambda y_pred, y_true: roc_auc_score(
    y_true.data.numpy(), y_pred.data.numpy())
model.metric_name = "auc"


def train_step(model, features, labels):
    """
Beispiel #9
0
    def __init__(self):
        super(Resnet18,self).__init__()
        self.resnet18 = resnet18
        self.linear1 = nn.Linear(1000,128)
        self.relu = nn.ReLU()
        self.linear2 = nn.Linear(128,1)
        self.sigmoid = nn.Sigmoid()
        
    def forward(self,x):
        x = self.resnet18(x)
        x = self.linear1(x)
        x = self.relu(x)
        x = self.linear2(x)
        y = self.sigmoid(x)
        return y
'''
net = Resnet18()
summary(net,input_shape= (3,30,30))
print(net)
while 1:
    pass
'''

class LetNet(nn.Module):
    def __init__(self):
        super(LetNet,self).__init__()
        self.conv1 = nn.Conv2d(in_channels=3,out_channels=32,kernel_size = 3)
        self.pool1 = nn.MaxPool2d(kernel_size = 2,stride = 2)
        self.conv2 = nn.Conv2d(in_channels=32,out_channels=64,kernel_size = 5)
        self.pool2 = nn.MaxPool2d(kernel_size = 2,stride = 2)
        self.dropout = nn.Dropout2d(p = 0.1)