0

再会,

我正在开发一个用于无线信号检测的深度学习模型。下面是计算模型精度和误码率 (BER) 的函数片段:

from chainer.datasets import TupleDataset
import numpy as np
from chainer import cuda
from chainer import function

def get_idp_acc(model, dataset_tuple, comp_ratio, profile = None, batchsize = 128, gpu = -1):
    chainer.config.train = True
    xp = np if gpu < 0 else cuda.cupy
    x, indices, x_zf, HtH, Hty = dataset_tuple._datasets[0], dataset_tuple._datasets[1], dataset_tuple._datasets[2], dataset_tuple._datasets[3], dataset_tuple._datasets[4]

    accs = 0
    BERs = 0
    model.train = False

    for j in range(0, len(x), batchsize):
        x_batch = xp.array(x[j:j + batchsize])
        indices_batch = xp.array(indices[j:j + batchsize])
        x_zf_batch = xp.array(x_zf[j:j + batchsize])
        HtH_batch = xp.array(HtH[j:j + batchsize])
        Hty_batch = xp.array(Hty[j:j + batchsize])
        if profile == None:
            acc_data = model(x_batch, indices_batch, x_zf_batch, HtH_batch, Hty_batch, comp_ratio = comp_ratio,
                            ret_param = 'acc')
        else:
            acc_data = model(x_batch, indices_batch, x_zf_batch, HtH_batch, Hty_batch, comp_ratio = comp_ratio,
                            ret_param = 'acc', profile = profile)

        acc_data.to_cpu()
        acc = acc_data.data
        BER = 1.0 - acc
        accs += acc * len(x_batch)
        BERs += BER * len(x_batch)
    return (accs / len(x)) * 100.

运行代码时,尽管已导入所有必需的链接器模块,但仍出现以下错误。在这个问题上我真的需要你的帮助,因为我被困了近两个月,在我的项目中没有取得任何进展。

Traceback (most recent call last):
  File "/Users/mac/Documents/idp_detnet/examples/run_mlp.py", line 14, in <module>
    mlp.run(args)
  File "/Users/mac/Documents/idp_detnet/examples/mlp.py", line 39, in run
    acc_dict[name], BER_dict[name] = util.sweep_idp(model, test, comp_ratios, args)
  File "/Users/mac/Documents/idp_detnet/examples/util.py", line 107, in sweep_idp
    batchsize=args.batchsize, profile=profile))
  File "/Users/mac/Documents/idp_detnet/examples/util.py", line 83, in get_idp_acc
    acc_data.to_cpu()
AttributeError: 'numpy.float32' object has no attribute 'to_cpu'  

以下是提供模型定义代码的附加信息:

K = 10
num_layers = 3*K

def lin_soft_sign(x, t):
    '''Linear soft sign activation function from the original paper Eq. (11)'''
    y =  -1 + F.relu(x + t)/ F.absolute(t) - F.relu(- t)/ F.absolute(t)
    return y

def accuracy(x, y):
    '''Computes the fraction of elements for which x and y are equal'''
    return np.mean(np.equal(x, y)).astype(np.float32)


class MLP(chainer.Chain):
    def __init__(self, K, coeff_generator, profiles = None, z_dims = 8*K, v_dims = 2*K):
        super(MLP, self).__init__()
        if profiles == None:
            profiles = [(0, 10)]
        self.coeff_generator = coeff_generator
        self.z_dims = z_dims
        self.v_dims = v_dims
        self.K = K
        self.profiles = profiles
        self.profile = 0
        with self.init_scope():
            self.p0_l1 = IncompleteLinear(None, self.z_dims)
            self.p1_l1 = IncompleteLinear(None, self.z_dims)
            self.p2_l1 = IncompleteLinear(None, self.z_dims)
            self.p0_lv = IncompleteLinear(None, self.v_dims)
            self.p1_lv = IncompleteLinear(None, self.v_dims)
            self.p2_lv = IncompleteLinear(None, self.v_dims)
            self.p0_l3 = IncompleteLinear(None, self.K)
            self.p1_l3 = IncompleteLinear(None, self.K)
            self.p2_l3 = IncompleteLinear(None, self.K)

    def __call__(self, x, indices, x_zf, HtH, Hty, ret_param = 'loss', profile = None, comp_ratio = None):
        if profile == None:
            profile = self.profile

        # Form Zero-forcing detection
        err_rel = F.sum((x - x_zf)**2, axis = 1) 

        params = layer_profile(self.coeff_generator,
                               *self.profiles[profile], self.z_dims,
                               self.v_dims, comp_ratio)

        def detnet_layer(x_d, x_logit, v, z_dims, v_dims):

            HtH_x = np.matmul(HtH, np.expand_dims(x_d.data, axis = 2).astype(np.float32))
            HtH_x = F.squeeze(HtH_x, axis = -1)

            #x_concat = np.concatenate([Hty, x, HtH_x, v], axis=1)
            x_concat = F.concat([Hty, x_d, HtH_x, v], axis = 1)

            if profile == 0:
                z = F.relu(self.p0_l1(x_concat))
                v += self.p0_lv(z, *params)
                x_logit += self.p0_l3(z, *params)
                x = lin_soft_sign(x_logit, F.broadcast_to(np.ones(1).astype(np.float32), x_logit.shape))

            elif profile == 1:
                z = F.relu(self.p1_l1(x_concat))
                v += self.p1_lv(z, *params)
                x_logit += self.p1_l3(z, *params)
                x = lin_soft_sign(x_logit, F.broadcast_to(np.ones(1).astype(np.float32), x_logit.shape))
            elif profile == 2:
                z = F.relu(self.p2_l1(x_concat))
                v += self.p2_lv(z, *params)
                x_logit += self.p2_l3(z, *params)
                x = lin_soft_sign(x_logit, F.broadcast_to(np.ones(1).astype(np.float32), x_logit.shape))

            return x, x_logit, v

        x_k = np.zeros((Hty.shape[0], self.K), dtype = np.float32)
        x_k_logit = np.zeros((Hty.shape[0], self.K), dtype = np.float32)
        v = np.zeros((Hty.shape[0], self.v_dims), dtype = np.float32)
        loss = 0

        mod = sg.Modulator('BPSK', K)

        for k in range(1, num_layers + 1):

            x_k, x_k_logit, v = detnet_layer(x_k, x_k_logit, v, self.z_dims, self.v_dims)
            err = F.sum((x - x_k)**2, 1) 
            loss += (np.log(k)).astype(np.float32) * F.mean(err/err_rel)       

        report = {'loss': loss, 'acc': accuracy(mod.demodulate(x_k.data), indices)}

        reporter.report(report, self)
        return report[ret_param]

    def report_params(self):
        return ['validation/main/acc']

    def param_names(self):
        if len(self.profiles) > 1:
            return 'IDPDETNET_{}_{}_{}_p{}'.format(self.z_dims, self.v_dims, self.coeff_generator.__name__, len(self.profiles))
        return 'IDPDETNET_{}_{}_{}'.format(self.z_dims, self.v_dims, self.coeff_generator.__name__) 

import os
import sys
sys.path.insert(0, os.path.abspath(
    os.path.join(os.path.dirname(__file__), '..')))

import numpy as np

import visualize as vz
import idp.coeffs_generator as cg
from net import MLP
import util
K = 10
N = 4
v_dims = 2*K
z_dims = 8*K
SNR_dB_tmin = -4
SNR_dB_tmax = 24

SNR_dB_test = np.linspace(SNR_dB_tmin, SNR_dB_tmax, 8)
num_snr_test = len(SNR_dB_test)

def run(args):
    train, test = util.get_dataset(args.modeltype)
    names = ['all-one (standard)', 'linear']
    colors = [vz.colors.all_one_lg, vz.colors.linear_lg]
    models = [
                MLP.MLP(K, cg.uniform, z_dims = 8*K, v_dims = 2*K),
                MLP.MLP(K, cg.linear, z_dims = 8*K, v_dims = 2*K)
                ]

    comp_ratios = np.linspace(0.1, 1.0, 20)
    acc_dict = {}
    BER_dict = {}
    ratios_dict = {}

    for i in range(num_snr_test):
        for name, model in zip(names, models):
            util.load_or_train_model(model, train, test, args)
            acc_dict[name], BER_dict[name] = util.sweep_idp(model, test, comp_ratios, args)
            ratios_dict[name] = [100. * cr for cr in comp_ratios]

    filename = "IDPDETNET1_{}".format(args.modeltype)
    vz.plot(ratios_dict, acc_dict, names, filename, colors = colors,
            folder = args.figure_path, ext=args.ext,
            title = 'IDPDETNET (BPSK)',
            xlabel = 'IDP (%)',
            ylabel = 'Test Accuracy (%)', ylim = (0, 100))

    filename = "IDPDETNET2_{}".format(args.modeltype)
    vz.plot(ratios_dict, BER_dict, names, filename, colors = colors,
            folder=args.figure_path, ext=args.ext,
            title='IDPDETNET (BPSK)',
            xlabel='IDP (%)',
            ylabel='BER (bits/sec)')

    filename = "IDPDETNET3_{}".format(args.modeltype)
    vz.plot(num_snr_test,   BER_dict, names, filename, colors = colors,
        folder = args.figure_path, ext = args.ext,
        title = 'IDPDETNET (BPSK)',
        xlabel = 'SNR (dB)',
        ylabel = ' BER (bits/sec)')

if __name__ == '__main__':
    args = util.default_parser('IDPDETNET Example').parse_args()
    run(args)

嗨,德井圣哉。谢谢你的好意。以下是基于上述代码的模型定义:

model = MLP.MLP(K, cg.uniform, z_dims = 8*K, v_dims = 2*K)

或者

model = MLP.MLP(K, cg.linear, z_dims = 8*K, v_dims = 2*K)

嗨@BloodyD。感谢您的出色贡献。该模型开始训练,但后来返回以下错误:

1           nan         nan                   0.50108              5.85448
Traceback (most recent call last):
  File "run_mlp.py", line 14, in <module>
    mlp.run(args)
  File "/Users/mac/Documents/idp_detnet/examples/mlp.py", line 38, in run
    util.load_or_train_model(model, train, test, args)
  File "/Users/mac/Documents/idp_detnet/examples/util.py", line 204, in load_or_train_model
    train_model(model, train, test, args)
  File "/Users/mac/Documents/idp_detnet/examples/util.py", line 184, in train_model
    return eval(fp.read().replace('\n', ''))
  File "<string>", line 1, in <module>
NameError: name 'NaN' is not defined

错误发生在下面这段代码的最后一行:

name = model.param_names()
    save_model(model, os.path.join(args.model_path, name))
    chainer.config.train = False
    with open(os.path.join(args.out, 'log'), 'r') as fp:
        return eval(fp.read().replace('\n', '')) 
4

0 回答 0