数据从int转换为series

时间:2019-03-30 01:21:57

标签: python python-3.x pandas neural-network jupyter-notebook

我创建了一个循环来模拟神经网络的训练,但发现最初分配为Int的权重变成了序列,这很奇怪

样本数据(注意:在同一行中创建了多个样本,以使其最多包含100个观测值)

#         x1    x2  y
data = [ [3.5, 1.5, 1],
         [2.0, 1.0, 0],
         [4.0, 1.5, 1],
         [3.0, 1.0, 0],
         [3.5, 0.5, 1],
         [2.0, 0.5, 0],
         [5.5, 1.0, 1],
         [1.0, 1.0, 0] ]

#[4.5, 1.0, 1]

data = pd.DataFrame(data, columns = ["Length", "Width", "Class"])
data

分配变量:

w1 = np.random.randn()
w2 = np.random.randn()
b = np.random.randn()

print(w1)
print(w2)
print(b)

训练循环:

learning_rate = 0.2
#costs = []

for x in range(50000):
    z = train_data["Length"] * w1 + train_data["Width"] + b
    preds = sigmoid(z)

    target = train_data["Class"]

    cost = np.square(preds - target)


    derivcost_pred = 2 * (preds - target)
    derivpred_sigp = sigmoid_p(z)
    dcost_dz = derivcost_pred * derivpred_sigp

    dz_dw1 = train_data["Length"]
    dz_dw2 = train_data["Width"]
    dz_db = 1

    dcost_dw1 = dcost_dz * dz_dw1
    dcost_dw2 = dcost_dz * dz_dw2
    dcost_db = dcost_dz * dz_db

    w1 = w1 - learning_rate * dcost_dw1
    w2 = w2 - learning_rate * dcost_dw2
    b = b - learning_rate * dcost_db

我的问题是如何获取经过训练的最后一个w1,w2,b值? 另外,如果我要使用系列,我该如何访问最后一个值?

最后,让我知道循环是否有问题

1 个答案:

答案 0 :(得分:1)

对于第一个问题,由于您想要训练的最后一个w1w2b值,我将假定它对应于x=50000-1。如果正确,请在循环末尾添加一行

for x in range(50000):
.
.
.
    if x==50000-1: costs.append([w1, w2, b])

# Print results
w1_trained, w2_trained, b_trained = costs[0][0], costs[0][1], costs[0][2]
print(w1_trained, w2_trained, b_trained)