当我从 keras 转换为 pytorch 时,我得到 AttributeError: ‘Network’ object has no attribute ‘network’
pytorch 271
原文标题 :when I Convert from keras to pytorch I get AttributeError: ‘Network’ object has no attribute ‘network’
def build_model(dropout_rate=0.5):
model = Sequential()
model.add(Lambda(lambda x: x/127.5-1.0, input_shape=p.INPUT_SHAPE)) #normalize the data
model.add(Conv2D(24, (5,5), strides=(2, 2), activation='elu'))
model.add(Conv2D(36, (5,5), strides=(2, 2), activation='elu'))
model.add(Conv2D(48, (5,5), strides=(2, 2), activation='elu'))
model.add(Conv2D(64, (3,3), activation='elu'))
model.add(Conv2D(64, (3,3), activation='elu'))
model.add(Dropout(dropout_rate))
model.add(Flatten())
model.add(Dense(100, activation='elu'))
model.add(Dense(50, activation='elu'))
model.add(Dense(10, activation='elu'))
model.add(Dense(1))
model.summary() # prints out the model description
return model
model = build_model()
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
self.layer1 = torch.nn.Sequential(
torch.nn.Conv2d(3, 24, kernel_size=5, stride=2, padding=1),
torch.nn.ELU())
self.layer2 = torch.nn.Sequential(
torch.nn.Conv2d(1, 36, kernel_size=5, stride=2, padding=1),
torch.nn.ELU())
self.layer3 = torch.nn.Sequential(
torch.nn.Conv2d(1, 48, kernel_size=5, stride=2, padding=1),
torch.nn.ELU())
self.layer4 = torch.nn.Sequential(
torch.nn.Conv2d(1, 64, kernel_size=3, stride=2, padding=1),
torch.nn.ELU(),
torch.nn.Conv2d(1, 64, kernel_size=3, stride=2, padding=1),
torch.nn.ELU(),
torch.nn.Dropout(p=DROPOUT_PROB))
self.fc1 = torch.nn.Sequential(
torch.nn.Linear(in_features=100, out_features=50),
torch.nn.Dropout(p=DROPOUT_PROB))
self.fc2 = torch.nn.Sequential(
torch.nn.Linear(in_features=50, out_features=10),
torch.nn.Dropout(p=DROPOUT_PROB))
self.fc3 = torch.nn.Sequential(
torch.nn.Linear(in_features=10, out_features=OUTPUT_SIZE))
def forward(self, x):
y = self.network(x)
y = self.layer2(y)
y = self.layer3(y)
y = self.layer4(y)
y = out.view(out.size(0), -1) # Flatten them for FC
y = self.fc1(y)
y = self.fc2(y)
y = self.fc3(y)
return y
def save_to_path(self, path):
torch.save(self.state_dict(), path)
def load_from_path(self, path):
self.load_state_dict(torch.load(path))
model = Network()