TypeError: Cannot interpret '4' as a data type

Viewed 43080

I am trying to learn Neural Network. Following is the code. I am getting the error 'TypeError: Cannot interpret '4' as a data type" Can anyone please help me identifying the mistake?

import numpy as np

inputs = [[1, 2 , 3, 2.5],
      [2, 5, 9, 10],
      [5, 1, 2, 7],
      [3, 2, 1, 4],
      [1,1.5, 7, 8]]

class layer_dense:
      def __init__ (self, n_inputs, m_neurons):
        self.weights= np.random.rand(n_inputs, m_neurons)
        self.biases= np.zeros(1, m_neurons)
     def forward (self, inputs):
        self.output= np.dot(inputs, self.weights)+self.biases
    
layer1 = layer_dense(4, 4)
layer2 = layer_dense(5,2)

layer1.forward(inputs)
layer2.forward(layer1.output)
print(layer2.output)
3 Answers

Per function description

numpy.zeros(shape, dtype=float, order='C')

The 2nd parameter should be data type and not a number

The signature for zeros is as follows:

numpy.zeros(shape, dtype=float, order='C')

The shape parameter should be provided as an integer or a tuple of multiple integers. The error you are getting is due to 4 being interpreted as a dtype.

In the other answers, they already mentioned the default method how Numpy handles it. But, I think you wanted to create a 4x4 array.

So, if anyone wants to create a larger array, they should provide the numbers in a tuple. In this format:

print(np.zeros((4,4)))

And other options such as dtype and order are specific to very high class programming where programmers prefer "C-Style" or "Fortan Style" and sometimes mentioning data-type could be an advantage or a necessary depending on the case.

Related