Gluon Neural Networks API: type error with initialized weights for gluon.nn.Sequential()












0















I am trying to create a conv neural network which identifies the presence of a sine wave in noisy data using the gluon api for python:



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)


My data is simply 1-d synthetic time series data which I generated and converted from numpy arrays:



#%% 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)


Now the net I defined looks like:



#%% 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))


Then I initialize the parameters and define the trainer:



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})


finally I define the training loop:



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])


When I try to run the code, I get the following error:



MXNetError: [15:49:43] C:cilibmxnet_1533398173145worksrcoperatornnconvolution.cc:281: Check failed: (*in_type)[i] == dtype (0 vs. 1) This layer requires uniform type. Expected 'float64' v.s. given 'float32' at 'weight'



Does this mean the weights being initialized are of the wrong type? I've tried typecasting my data to float32, but that does not change the error.
Any help would be appreciated.










share|improve this question





























    0















    I am trying to create a conv neural network which identifies the presence of a sine wave in noisy data using the gluon api for python:



    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)


    My data is simply 1-d synthetic time series data which I generated and converted from numpy arrays:



    #%% 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)


    Now the net I defined looks like:



    #%% 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))


    Then I initialize the parameters and define the trainer:



    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})


    finally I define the training loop:



    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])


    When I try to run the code, I get the following error:



    MXNetError: [15:49:43] C:cilibmxnet_1533398173145worksrcoperatornnconvolution.cc:281: Check failed: (*in_type)[i] == dtype (0 vs. 1) This layer requires uniform type. Expected 'float64' v.s. given 'float32' at 'weight'



    Does this mean the weights being initialized are of the wrong type? I've tried typecasting my data to float32, but that does not change the error.
    Any help would be appreciated.










    share|improve this question



























      0












      0








      0








      I am trying to create a conv neural network which identifies the presence of a sine wave in noisy data using the gluon api for python:



      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)


      My data is simply 1-d synthetic time series data which I generated and converted from numpy arrays:



      #%% 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)


      Now the net I defined looks like:



      #%% 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))


      Then I initialize the parameters and define the trainer:



      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})


      finally I define the training loop:



      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])


      When I try to run the code, I get the following error:



      MXNetError: [15:49:43] C:cilibmxnet_1533398173145worksrcoperatornnconvolution.cc:281: Check failed: (*in_type)[i] == dtype (0 vs. 1) This layer requires uniform type. Expected 'float64' v.s. given 'float32' at 'weight'



      Does this mean the weights being initialized are of the wrong type? I've tried typecasting my data to float32, but that does not change the error.
      Any help would be appreciated.










      share|improve this question
















      I am trying to create a conv neural network which identifies the presence of a sine wave in noisy data using the gluon api for python:



      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)


      My data is simply 1-d synthetic time series data which I generated and converted from numpy arrays:



      #%% 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)


      Now the net I defined looks like:



      #%% 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))


      Then I initialize the parameters and define the trainer:



      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})


      finally I define the training loop:



      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])


      When I try to run the code, I get the following error:



      MXNetError: [15:49:43] C:cilibmxnet_1533398173145worksrcoperatornnconvolution.cc:281: Check failed: (*in_type)[i] == dtype (0 vs. 1) This layer requires uniform type. Expected 'float64' v.s. given 'float32' at 'weight'



      Does this mean the weights being initialized are of the wrong type? I've tried typecasting my data to float32, but that does not change the error.
      Any help would be appreciated.







      python conv-neural-network






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 20:16









      José Pereda

      25.7k34269




      25.7k34269










      asked Nov 2 '18 at 14:14









      Trent ParkTrent Park

      63




      63
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53120213%2fgluon-neural-networks-api-type-error-with-initialized-weights-for-gluon-nn-sequ%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53120213%2fgluon-neural-networks-api-type-error-with-initialized-weights-for-gluon-nn-sequ%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'