题解 | 单神经元

单神经元

https://www.nowcoder.com/practice/8903b7c94c6f4f4f963b7d05e1e397c7

import math
import numpy as np
def softmax(x):
    return 1/(1+math.exp(-1*x))


def single_neuron_model(features, labels, weights, bias):
    probabilities = []
    diff = 0
    for i in range(len(features)):
        z = 0
        for j in range(len(features[i])):
            z += features[i][j] * weights[j]
        z = softmax(z + bias)
        z = round(z,4)
        probabilities.append(z)
        diff += (z - labels[i])**2
    mse = diff / len(features)
    mse = round(mse, 4)
    return probabilities, mse


if __name__ == "__main__":
    features = np.array(eval(input()))
    labels = np.array(eval(input()))
    weights = np.array(eval(input()))
    bias = float(input())
    print(single_neuron_model(features, labels, weights, bias))

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务