示例#1
0
 def save(self, file):
     Sequential.save(self, file)
     tf.train.Saver().save(self.session, file + '_model.ckpt')
示例#2
0
labels = torch.Tensor([mapping[label] for label in labels]).type(torch.long)
X_train, X_test, y_train, y_test = train_test_split(X,
                                                    labels,
                                                    test_size=0.1,
                                                    shuffle=False)

N = len(X_train)
layers = fully_connected([length * width, 256, 256, 256, num_unique_labels])
model = Sequential(*layers)
op = torch.optim.Adam(model.parameters(), lr=.001)
loss_fn = nn.CrossEntropyLoss(reduction='sum')
for epoch_num in range(epochs):
    epoch_loss = 0
    training = batch_training_generator(X_train,
                                        y_train,
                                        batch_size,
                                        shuffle=True)
    for x_set, y_set in training:
        op.zero_grad()
        output = model(x_set)
        loss = loss_fn(output, y_set)
        loss /= N
        epoch_loss += float(loss)
        loss.backward()
        op.step()
    print(epoch_num, epoch_loss, accuracy(model, X_train, y_train))
model.append(nn.Softmax(dim=1))
print(accuracy(model, X_test, y_test))
if save_name is not None:
    model.save(save_name)