Untitled

 avatar
unknown
python
4 years ago
1.6 kB
9
Indexable
    for train, val in CV.split(X_par, y_par):
        X_train = torch.Tensor(X_par[train, :])
        y_train = torch.Tensor(y_par[train])
        X_test = torch.Tensor(X_par[val, :])
        y_test = torch.Tensor(y_par[val])
        besth = 0
        bestMSE = np.infty


        for h in h_range:
            # Train the net on training data
            model = lambda: torch.nn.Sequential(
                torch.nn.Linear(M, h),  # M features to n_hidden_units
                torch.nn.Tanh(),  # 1st transfer function,
                torch.nn.Linear(h, 1),  # n_hidden_units to 1 output neuron
                # no final tranfer function, i.e. "linear output"
            )
            net, final_loss, learning_curve = train_neural_net(model,
                                                               loss_fn,
                                                               X=X_train,
                                                               y=y_train,
                                                               n_replicates=n_replicates,
                                                               max_iter=max_iter)

            print('\n\tBest loss: {}\n'.format(final_loss))

            # Determine estimated class labels for test set
            y_test_est = net(X_test)

            # Determine errors and errors
            se = (y_test_est.float() - y_test.float()) ** 2  # squared error
            setest =  sum(se).type(torch.float)
            mse = (sum(se).type(torch.float) / len(y_test)).data.numpy()  # mean
            if mse < bestMSE:
                bestMSE = mse
                besth = h
Editor is loading...