【Pytorch学习笔记】从零开始搭建神经网络(3/3)

上一篇进行到类之间的调用

上一篇:程序内部调用函数和类的问题

这次说清楚调用其他模块的方式

1、一共两个模块(两个.py文件)

2、由 from_zero_to_one.py 调用 activateFunc.py

from_zero_to_one.py

# -*- coding: utf-8 -*-
# @Time       : 2022/3/1 15:19
# @Author     : nemo
# @Software   : PyCharm

import numpy as np

from activateFunc import sigmoid


class Neuron:
    def __init__(self, weight, bias):
        self.weight = weight
        self.bias = bias

    def feedforward(self, input):
        result = np.dot(self.weight, input) + self.bias

        return sigmoid(result)


class OurNeuronNetwork:
    def __init__(self):
        weight = np.array([0, 1])
        bias = 0

        self.h1 = Neuron(weight, bias)
        self.h2 = Neuron(weight, bias)
        self.o1 = Neuron(weight, bias)

    def feedforward(self, x):
        out_h1 = self.h1.feedforward(x)
        out_h2 = self.h2.feedforward(x)
        out_o1 = self.o1.feedforward(np.array([out_h1, out_h2]))
        return out_o1


def main():
    network = OurNeuronNetwork()
    input_num = np.array([2, 3])
    print(network.feedforward(input_num))


if __name__ == '__main__':
    main()

activateFunc.py

# -*- coding: utf-8 -*-
# @Time       : 2022/3/4 9:05
# @Author     : nemo
# @Software   : PyCharm


import numpy as np


def sigmoid(x):
    return 1 / (1 + np.exp(-x))

3、在from_zero_to_one.py中运行

4、注意:(1)引用格式 (2)前提:两个文件处于同一级目录

下一节,上手Pokemon图像分类程序,开始全局调试

你可能感兴趣的:(Python/Pycharm,深度学习/PyTorch,pytorch,神经网络,深度学习)