发生异常: TypeErrorempty() received an invalid combination of arguments - got (tuple, dtype=NoneType, d

请问这个报错怎么解决啊 ?在做BPNN+GA时候报错

发生异常: TypeError

empty() received an invalid combination of arguments - got (tuple, dtype=NoneType, device=NoneType), but expected one of: * (tuple of ints size, *, tuple of names names, torch.memory_format memory_format, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad) * (tuple of ints size, *, torch.memory_format memory_format, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)

File "py", line 40, in __init__ self.hidden = nn.Linear(n_feature, n_hidden) Filepy", line 54, in evaluate net = BPNetModel(n_feature=1, n_hidden=hidden_size, n_output=1) File  line 97, in <module> algorithms.eaMuCommaLambda(population, toolbox, mu=5, lambda_=10, cxpb=0.7, mutpb=0.3, ngen=5) TypeError: empty() received an invalid combination of arguments - got (tuple, dtype=NoneType, device=NoneType), but expected one of: * (tuple of ints size, *, tuple of names names, torch.memory_format memory_format, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad) * (tuple of ints size, *, torch.memory_format memory_format, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)

import numpy as np

import pandas as pd

import torch

import torch.nn as nn

import torch.nn.functional as F

import torch.optim as optim

from sklearn import preprocessing

from sklearn.model_selection import train_test_split

from sklearn.metrics import mean_squared_error

from deap import base, creator, tools, algorithms

import matplotlib.pyplot as plt


 

# Load and preprocess data

path = 'data2.csv'

data = pd.read_csv(path)

X = data.loc[:, 'gap']

y = data.loc[:, 'power']

X = X.values.reshape(-1, 1)

y = y.values.reshape(-1, 1)

min_max_scaler = preprocessing.MinMaxScaler()

X_scaled = min_max_scaler.fit_transform(X)

y_scaled = min_max_scaler.fit_transform(y)

x_train, x_test, y_train, y_test = train_test_split(

    X_scaled, y_scaled, test_size=0.2, random_state=22, shuffle=False

)

x_train = torch.FloatTensor(np.array(x_train))

y_train = torch.FloatTensor(np.array(y_train))

x_test = torch.FloatTensor(np.array(x_test))

y_test = torch.FloatTensor(np.array(y_test))

# print(x_train.shape)

# print(y_train.shape)

# Define the BP neural network

class BPNetModel(nn.Module):

    def __init__(self, n_feature, n_hidden, n_output):

        super(BPNetModel, self).__init__()

        self.hidden = nn.Linear(n_feature, n_hidden)

        self.output = nn.Linear(n_hidden, n_output)

    def forward(self, x):

        x = F.relu(self.hidden(x))

        y_pred = self.output(x)

        return y_pred

# Define the evaluation function

def evaluate(ind):

    lr, hidden_size = ind[0], ind[1]

    epochs =2000

   

    net = BPNetModel(n_feature=1, n_hidden=hidden_size, n_output=1)

    optimizer = optim.Adam(net.parameters(), lr=lr)

    loss_fun = nn.MSELoss()

    loss_steps = np.zeros(epochs)

    for epoch in range(epochs):

        y_pred = net(x_train)

        loss = loss_fun(y_pred, y_train)

        optimizer.zero_grad()

        loss.backward()

        optimizer.step()

        loss_steps[epoch] = loss.item()

    with torch.no_grad():

        y_pred = net(x_test)

        y_result = y_pred.numpy().flatten()

    mse = mean_squared_error(y_test, y_result)

    return mse,

# Define the genetic algorithm parameters

creator.create("FitnessMin", base.Fitness, weights=(-1.0,))

creator.create("Individual", list, fitness=creator.FitnessMin)

toolbox = base.Toolbox()

toolbox.register("attr_float", np.random.uniform, 0.01, 0.3)

toolbox.register("attr_int", np.random.randint, 1, 10)  # Hidden Layer Size

toolbox.register(

    "individual", tools.initCycle, creator.Individual, (toolbox.attr_float, toolbox.attr_int), n=1

)

toolbox.register("population", tools.initRepeat, list, toolbox.individual)

toolbox.register("mate", tools.cxBlend, alpha=0.5)

toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.2)

toolbox.register("select", tools.selTournament, tournsize=3)

toolbox.register("evaluate", evaluate)

# Run the genetic algorithm

population = toolbox.population(n=10)

algorithms.eaMuCommaLambda(population, toolbox, mu=5, lambda_=10, cxpb=0.7, mutpb=0.3, ngen=5)

# Get the best individual

best_ind = tools.selBest(population, k=1)[0]

best_lr, best_hidden_size =float(best_ind[0]), int(best_ind[1])

print(f"Best Learning Rate: {best_lr}")

print(f"Best Hidden Layer Size: {best_hidden_size}")

# Train the best model

best_net = BPNetModel(n_feature=1, n_hidden=best_hidden_size, n_output=1)

best_optimizer = optim.Adam(best_net.parameters(), lr=best_lr)

best_loss_fun = nn.MSELoss()

epochs = 2000

best_loss_steps = np.zeros(epochs)

for epoch in range(epochs):

    y_pred = best_net(x_train)

    loss = best_loss_fun(y_pred, y_train)

    best_optimizer.zero_grad()

    loss.backward()

    best_optimizer.step()

    best_loss_steps[epoch] = loss.item()

# Make predictions using the best model

with torch.no_grad():

    y_pred_best = best_net(x_test)

    y_result_best = y_pred_best.numpy().flatten()

# Output the results

print("Predicted y values using the best model:")

print(y_result_best)

# Plot the loss for the best learning rate and hidden layer size

plt.figure()

plt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值