我正在尝试创建一个conv神经网络,使用python的gluon api来识别噪声数据中正弦波的存在:
from __future__ import print_function
import mxnet as mx
import numpy as np
import random
import matplotlib.pyplot as plt
from mxnet import nd, autograd, gluon
ctx = mx.cpu()
mx.random.seed(1)
我的数据只是我从numpy数组生成和转换的一维合成时间序列数据:
#%% GENERATE SYNTHETIC DATA
mean = 0
std = 1
sample_rate = 512
sample_size = 500
t = np.linspace(0,1,sample_rate)
amplitudes = np.linspace(0.1,1,sample_size,dtype = float)
frequencies= np.linspace(100,150,sample_size,dtype = float)
clean = np.zeros(shape=(sample_size,sample_rate))
noisy = np.zeros_like(clean)
noData = np.zeros_like(clean)
for j in range(0,sample_size):
clean[j,:] = amplitudes[j]*np.sin(frequencies[j]*t)
noise = np.random.normal(mean, std, size=sample_rate)
noisy[j,:] = clean[j,:] + noise
noData[j,:] = noise
#%%
batch_size = 32
num_inputs = 3*sample_size
num_outputs = 2
#input pattern data
P = np.concatenate((clean,noisy,noData),axis=0)
#input target data 1-sine wave in noise, 0- just noise
O = np.matlib.repmat(np.array([1]),2*sample_size,1)
Z = np.matlib.repmat(np.array([0]),sample_size,1)
T = np.concatenate((O,Z),axis=0)
#indices to shuffle up the data
indices = random.sample(range(0,num_inputs),num_inputs)
#split into training and test data
ptrain = P[indices[0:1200],:]
ttrain = T[indices[0:1200]]
ptest = P[indices[1200:-1],:]
ttest = T[indices[1200:-1]]
#reshape data since conv1d takes 3d tensor as input
ptrain = np.reshape(ptrain,(1200,1,512))
ptest = np.reshape(ptest,(299,1,512))
#put data into correct format for gluon
train_data = gluon.data.DataLoader(gluon.data.ArrayDataset(ptrain,ttrain),
batch_size=batch_size, shuffle=False)
test_data = gluon.data.DataLoader(gluon.data.ArrayDataset(ptest,ttest),
batch_size=batch_size, shuffle=False)
现在我定义的网络如下:
#%% CREATE FUNCTION WHICH DEFINES THE NETWORK
num_fc = 64
net = gluon.nn.Sequential()
with net.name_scope():
net.add(gluon.nn.Conv1D(channels=10, kernel_size=5, activation='relu'))
net.add(gluon.nn.MaxPool1D(pool_size=2, strides=2))
net.add(gluon.nn.Conv1D(channels=20, kernel_size=5, activation='relu'))
net.add(gluon.nn.MaxPool1D(pool_size=2, strides=2))
#The Flatten layer collapses all axis, except the first one, into one axis.
net.add(gluon.nn.Flatten())
net.add(gluon.nn.Dense(num_fc, activation="relu"))
net.add(gluon.nn.Dense(num_outputs))
然后我初始化参数并定义训练器:
net.collect_params().initialize(mx.init.Constant(0.01), ctx=ctx)
softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss()
trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': .001})
最后,我定义了训练循环:
epochs = 1
smoothing_constant = .01
for e in range(epochs):
for i, (data, label) in enumerate(train_data):
data = data.as_in_context(ctx)
label = label.as_in_context(ctx)
with autograd.record():
#---------------------------------------------------
output = net(data) #HERE'S WHERE THE ERROR OCCURS!!!
#---------------------------------------------------
loss = softmax_cross_entropy(output, label)
loss.backward()
trainer.step(data.shape[0])
当我尝试运行代码时,出现以下错误:
MXNetError:[15:49:43] C:\ ci \ libmxnet_1533398173145 \ work \ src \ operator \ nn \ convolution.cc:281:检查失败:(* in_type)[i] == dtype(0 vs. 1)该层需要统一的类型。预期为'float64'vs.赋予“ float32”为“重量”
这是否意味着要初始化的权重类型错误?我尝试将数据类型转换为float32,但这不会更改错误。 任何帮助将不胜感激。