我尝试实现参数初始化,得到错误信息:
import numpy as np
def initialize_with_zeros(dim):
w = np.zeros(dim, 1)
b = 0
return w, b
dim = 2
initialize_with_zeros(dim)
这是错误:
TypeError Traceback (most recent call last) in () 5 6 dim = 2 ----> 7 initialize_with_zeros(dim)
in initialize_with_zeros(dim) 1 def initialize_with_zeros(dim): ----> 2 w = np.zeros(dim, 1) 3 b = 0 4 return w, b 5
TypeError: data type not understood
请您参考如下方法:
np.zeros仅将形状作为元组或单个整数(在一维数组的情况下)。如果你只需要一个一维数组,传递一个参数。如果您需要二维数组,请作为元组 (dim,1) 传递。因此,根据您的需要,要么使用
w = np.zeros(dim)
这会给你一个一维的零数组
或使用
w = np.zeros((dim, 1))
这将为您提供一个具有 dim 行数和 1 列数的二维数组。
numpy.zeros(shape, dtype=float, order='C')
Parameters:
shape : int or tuple of ints Shape of the new array, e.g., (2, 3) or 2.
