火炬大小不匹配m1 m2

时间:2020-04-23 04:18:32

标签: python neural-network pytorch

我想使用Pytorch构建二进制分类器。但是,我收到了错误消息“大小不匹配,m1:[13900 x 2],m2:[4 x 1]

以下我正在尝试运行的代码。

iris=np.load(path_iris)
X_train=iris['train_X']
Y_train=iris['train_Y']
X_test=iris['test_X']
Y_test=iris['test_Y']

print(f'X train size: {X_train.shape}')
print(f'X test size: {Y_train.shape}')
print(f'y train size: {X_test.shape}')
print(f'y test size: {Y_test.shape}')

X火车尺寸:(120,4) X测试大小:(120) y火车尺寸:(30,4) y测试大小:(30,)

import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.optim as optim

class network():

    def __init__(self, X, Y, n_hidden=1, learning_rate=1e-2, device='cpu'):
        self.device = device
        self.X = X
        self.Y = Y.reshape(-1,1)
        self.Y_t = torch.FloatTensor(self.Y).to(device=self.device)
        self.n_input_dim = X.shape[1]
        self.n_output = 1
        self.learning_rate = learning_rate
        self.n_hidden = n_hidden

        # Build network
        self.net = nn.Sequential(
            nn.Linear(self.n_input_dim, self.n_hidden), 
            nn.ELU(),
            nn.Linear(self.n_hidden, self.n_hidden),
            nn.ELU(),
            nn.Linear(self.n_hidden, self.n_output),
            nn.Sigmoid())

        self.loss_func = nn.BCELoss()
        self.optimizer = torch.optim.SGD(self.net.parameters(), 
                                          lr=self.learning_rate)

    def predict(self, X):
        # Function to generate predictions based on data
        X_t = torch.FloatTensor(X).to(device=self.device)
        return self.net(X_t)

    def calculate_loss(self, y_hat):
        return self.loss_func(y_hat, self.Y_t)

    def update_network(self, y_hat):
        self.optimizer.zero_grad()
        loss = self.calculate_loss(y_hat)
        loss.backward()
        self.optimizer.step()
        self.training_loss.append(loss.item())

    def calculate_accuracy(self, y_hat_class, Y):
        return np.sum(Y.reshape(-1,1)==y_hat_class) / len(Y)

    def train(self, n_iters=1000):
        self.training_loss = []
        self.training_accuracy = []
        for i in range(n_iters):
            y_hat = self.predict(X_train)
            self.update_network(y_hat)
            y_hat_class = np.where(y_hat<0.5, 0, 1)
            accuracy = self.calculate_accuracy(y_hat_class, self.Y)
            self.training_accuracy.append(accuracy)

    def plot_training_results(self):
        fig, ax = plt.subplots(2, 1, figsize=(12,8))
        ax[0].plot(self.training_loss)
        ax[0].set_ylabel('Loss')
        ax[0].set_title('Training Loss')

        ax[1].plot(self.training_accuracy)
        ax[1].set_ylabel('Classification Accuracy')
        ax[1].set_title('Training Accuracy')

        plt.tight_layout()
        plt.show()

    def plot_testing_results(self, X_test, Y_test):
        # Pass test data
        y_hat_test = self.predict(X_test)
        y_hat_test_class = np.where(y_hat_test<0.5, 0, 1)
        print("Test Accuracy {:.2f}%".format(
            self.calculate_accuracy(y_hat_test_class, Y_test) * 100))

        # Plot the decision boundary
        # Determine grid range in x and y directions
        x_min, x_max = X_train[:, 0].min()-0.1, X_train[:, 0].max()+0.1
        y_min, y_max = X_train[:, 1].min()-0.1, X_train[:, 1].max()+0.1

        # Set grid spacing parameter
        spacing = min(x_max - x_min, y_max - y_min) / 100

        # Create grid
        XX, YY = np.meshgrid(np.arange(x_min, x_max, spacing),
                       np.arange(y_min, y_max, spacing))

        # Concatenate data to match input
        data = np.hstack((XX.ravel().reshape(-1,1), 
                          YY.ravel().reshape(-1,1)))

        # Pass data to predict method
        db_prob = self.predict(data)

        clf = np.where(db_prob<0.5,0,1)

        Z = clf.reshape(XX.shape)

        plt.figure(figsize=(12,8))
        plt.contourf(XX, YY, Z, cmap=plt.cm.Accent, alpha=0.5)
        plt.scatter(X_test[:,0], X_test[:,1], c=Y_test, 
                    cmap=plt.cm.Accent)
        plt.show()

net = network(X_train, Y_train)
print(net.net)
net.train()

n_hidden_nodes = range(1,10)

for n in n_hidden_nodes:
    print("Number of hidden nodes: {:d}".format(n))
    net = network(X_train, Y_train, n_hidden=n)
    net.train()
    net.plot_testing_results(X_test, Y_test)

如何修改代码以消除错误?非常感谢。

0 个答案:

没有答案