簡體   English   中英

將 Keras 順序 l 中的深度 model 轉換為火炬功能中的 model

[英]convert a deep model in Keras sequential l to a model in torch functional

我是手電筒新手,不知道如何將 keras 中的順序 model 轉換為手電筒中的功能。 這是我想要協調的代碼。 它是 keras 中的三層 DNN,火車形狀為 (n 76) 所以 in_feats=76

paramDict = {
    'epoch': 200,
    'batchSize': 32,
    'dropOut': 0.2,
    'loss': 'binary_crossentropy',
    'metrics': ['accuracy'],
    'activation1': 'relu',
    'activation2': 'sigmoid',
    'monitor': 'val_accuracy',
    'save_best_only': True,
    'mode': 'max'
}
class_weight = {0: 1.0, 1: 4.0}

hl = [128, 256, 512, 1024, 1024, 1024,1024, 1024, 1024, 1024];

optimizerDict = {
    'adam': Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999),
    }

numHidden=3
numberOfClasses=2
    model = Sequential()
    model.add(Dense(hl[0], activation = paramDict['activation1'], input_shape =(in_feats,)))
    model.add(Dropout(paramDict['dropOut']))
    for i in range(1, numHidden):
        if i < len(hl):
            model.add(Dense(hl[i], activation = paramDict['activation1']))
            model.add(Dropout(paramDict['dropOut']))
        else:
            model.add(Dense(1024, activation = paramDict['activation1']))
            model.add(Dropout(paramDict['dropOut']))
    
    model.add(Dense(numberOfClasses, activation=paramDict['activation2']))

網絡在 PyTorch 中定義如下

import torch
import torch.nn as nn

numHidden=3
numberOfClasses=2
n = 76
hl = [128, 256, 512, 1024, 1024, 1024,1024, 1024, 1024, 1024]

class NeuralNetwork(nn.Module):
    def __init__(self):
        super(NeuralNetwork, self).__init__()        
        self.layer1 = nn.Linear(n, hl[0], nn.Dropout(p=0.2)) # nodes in each layers, dropout
        self.layer2 = nn.Linear(hl[0], hl[1], nn.Dropout(p=0.2))
        self.layer3 = nn.Linear(hl[1], hl[2], nn.Dropout(p=0.2))
        self.layer4 = nn.Linear(hl[2], numberOfClasses, nn.Dropout(p=0.2))

        self.activation1 = nn.ReLU() # activation function
        self.activation2 = nn.Sigmoid()
        self.dropout = nn.Dropout(0.2)

    def forward(self, x):
        x = self.activation1(self.dropout(self.layer1(x)))
        x = self.activation1(self.dropout(self.layer2(x)))
        x = self.activation1(self.dropout(self.layer3(x)))
        x = self.activation2(self.dropout(self.layer4(x)))
        return x

model = NeuralNetwork()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM