TypeError: Cannot interpret '4' as a data type
Asked Answered
G

4

15

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)
Geophyte answered 12/1, 2021 at 8:35 Comment(0)
J
17

Per function description

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

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

Jongjongleur answered 12/1, 2021 at 8:41 Comment(0)
B
3

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.

Bernice answered 2/7, 2022 at 8:40 Comment(0)
E
2

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.

Edile answered 12/1, 2021 at 8:40 Comment(0)
A
0

This line "self.biases= np.zeros(1, m_neurons)" you are missing some parenthesis the line should be "self.biases= np.zeros((1, m_neurons))"

Airspace answered 10/10, 2023 at 19:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.