【问题标题】:Neural Network (operands could not be broadcast together with shapes (1,713) (713,18) )神经网络(操作数不能与形状一起广播 (1,713) (713,18) )
【发布时间】:2020-02-03 02:27:33
【问题描述】:

我目前正在 Coursera 上学习 Deeplearning.ai 的深度学习专业,并且我正在执行第一个需要使用逻辑回归思维实现神经网络的任务。问题在于,任务是将神经网络实现为 非结构化数据(图像) 的逻辑回归函数。我已经成功完成了作业,得到了所有预期的输出。但是,我现在尝试将编码神经网络用于STRUCTURE DATA,但遇到广播错误。部分代码如下:

数据集代码

path_train = r'C:\Users\Ahmed Ismail Khalid\Desktop\Research Paper\Research Paper Feature Sets\Balanced Feature Sets\Balanced Train combined scores.csv'
path_test = r'C:\Users\Ahmed Ismail Khalid\Desktop\Research Paper\Research Paper Feature Sets\Balanced Feature Sets\Balanced Test combined scores.csv'

df_train = pd.read_csv(path_train)
#df_train = df_train.to_numpy()

df_test = pd.read_csv(path_test)
#df_test = df_test.to_numpy()

x_train = df_train.iloc[:,1:19]
x_train = x_train.to_numpy()
x_train = x_train.T

y_train = df_train.iloc[:,19]
y_train = y_train.to_numpy()
y_train = y_train.reshape(y_train.shape[0],1)
y_train = y_train.T

x_test = df_test.iloc[:,1:19]
x_test = x_test.to_numpy()
x_test = x_test.T

y_test = df_test.iloc[:,19]
y_test = y_test.to_numpy()
y_test = y_test.reshape(y_test.shape[0],1)
y_test = y_test.T

print ("Number of training examples: m_train = " + str(m_train))
print ("Number of testing examples: m_test = " + str(m_test))
print ("train_set_x shape: " + str(x_train.shape))
print ("train_set_y shape: " + str(y_train.shape))
print ("test_set_x shape: " + str(x_test.shape))
print ("test_set_y shape: " + str(y_test.shape))

数据集代码的输出

Number of training examples: df_train = 713
Number of testing examples: df_test = 237
x_train shape: (18, 713)
y_train shape: (1, 713)
x_test shape: (18, 237)
y_test shape: (1, 237)

传播功能代码

def propagate(w,b,X,Y) :

    m = X.shape[1]

    A = sigmoid((w.T * X) + b)

    cost = (- 1 / m) * np.sum(np.dot(Y,np.log(A)) + np.dot((1 - Y), np.log(1 - A)))

    dw = (1 / m) * np.dot((X,(A - Y)).T)
    db = (1 / m) * np.sum(A - Y)

    assert(dw.shape == w.shape)
    assert(db.dtype == float)
    cost = np.squeeze(cost)
    assert(cost.shape == ())

    grads = {"dw": dw,
             "db": db}

    return grads, cost

优化和建模功能

**def optimize**(w,b,X,Y,num_iterations,learning_rate,print_cost) :

costs = []

for i in range(num_iterations) :

    # Cost and gradient calculation
    grads, cost = propagate(w,b,X,Y)

    # Retrieve derivatives from gradients
    dw = grads['dw']
    db = grads['db']

    # Update w and b
    w = w - learning_rate * dw
    b = b - learning_rate * db

    if i % 100 == 0:
        costs.append(cost)

    # Print the cost every 100 training iterations
    if print_cost and i % 100 == 0:
        print ("Cost after iteration %i: %f" %(i, cost))

    params = {"w": w,
          "b": b}

    grads = {"dw": dw,
         "db": db}

    return params, grads, costs

**def model**(X_train, Y_train, X_test, Y_test, num_iterations = 2000, learning_rate = 0.5, print_cost = False) :

# initialize parameters with zero
w, b = initialize_with_zeros(X_train.shape[0])

# Gradient descent (≈ 1 line of code)
parameters, grads, costs = optimize(w,b,X_train,Y_train,num_iterations,learning_rate,print_cost)

# Retrieve parameters w and b from dictionary "parameters"
w = parameters["w"]
b = parameters["b"]

# Predict train/test set examples (≈ 2 lines of code)
Y_prediction_train = predict(w,b,X_train)
Y_prediction_test = predict(w,b,X_test)

 # Print train/test Errors
print("train accuracy: {} %".format(100 - np.mean(abs(Y_prediction_train - Y_train)) * 100))
print("test accuracy: {} %".format(100 - np.mean(abs(Y_prediction_test - Y_test)) * 100))


d = {"costs": costs,
     "Y_prediction_test": Y_prediction_test, 
     "Y_prediction_train" : Y_prediction_train, 
     "w" : w, 
     "b" : b,
     "learning_rate" : learning_rate,
     "num_iterations": num_iterations}

return d

模型函数输出

Cost after iteration 0: 0.693147
train accuracy: -0.1402524544179613 %
test accuracy: 0.4219409282700326 %

当我运行代码时,我在A = sigmoid((w.T * X) + b) 得到ValueError: operands could not be broadcast together with shapes (1,713) (713,18)。我对神经网络和 numpy 的使用非常陌生,所以我无法弄清楚问题所在。任何和所有的帮助将不胜感激。包含整个代码的整个.ipynb文件可以是downloaded from here

谢谢

【问题讨论】:

    标签: python numpy neural-network logistic-regression broadcast


    【解决方案1】:

    * 运算符是元素乘法,并且您的数组具有不兼容的形状。您需要矩阵乘法,您可以使用 np.matmul()@ 运算符:

    A = sigmoid(w.T @ X + b)
    

    很多机器学习,尤其是神经网络,都是关于保持事物的形状笔直。检查wXY 的形状——它们应该分别是:(features, 1)(features, m)(1, m),其中features 对你来说是18,m 是713 .

    您还应该能够确保A 的形状与Y 匹配。

    【讨论】:

    • 现在我在cost = (- 1 / m) * np.sum(np.dot(Y,np.log(A)) + np.dot((1 - Y), np.log(1 - A)))收到错误ValueError: shapes (1,713) and (1,18) not aligned: 713 (dim 1) != 1 (dim 0)
    • 我发现了这个问题(我认为)并修改了我的答案。
    • 所以我修复了,确保形状是直的。但是,现在我在dw = (1 / m) * np.dot((X,(A - Y)).T) 得到AttributeError: 'tuple' object has no attribute 'T'。我检查了所有内容,使用的变量都不是元组。 X, A, w, m 和 Y 都是 np.ndarrays
    • @SteviG 注意该行中的括号。 .T 附加到 (X, (A - Y)) 这是一个元组。它应该在(A - Y)
    • 好的,没问题。我会自己探索一下,如果它仍然存在,将打开一个新线程。不想在这里发布我遇到的每一个问题。再次感谢您的帮助。如果其他人也遇到同样的问题,我已赞成并接受您的回答以帮助其他人
    猜你喜欢
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多