1 Star 1 Fork 6

张觉非 / 计算图框架

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
nn.py 1.14 KB
一键复制 编辑 原始数据 按行查看 历史
张觉非 提交于 2019-07-09 10:43 . init
from node import *
def neural_network(n, classes=2, hiddens=(12,), activation="ReLU"):
"""
构造一个多层全连接神经网络的计算图。
"""
# x 是一个 n 维向量变量,不初始化,不参与训练
x = Variable((n, 1), init=False, trainable=False)
# 构造全连接层
input_size = n
input_vector = x
for l, h_size in zip(np.arange(len(hiddens)), hiddens):
output = Add(
MatMul(
Variable((h_size, input_size), True),
input_vector
),
Variable((h_size, 1), True)
)
# 隐藏层的输出
if activation == "ReLU":
output = ReLU(output)
elif activation == "Logistic":
output = Logistic(output)
else:
output = output
input_size = h_size
input_vector = output
# 输出层的神经元
logits = Add(
MatMul(
Variable((classes, input_size), True),
input_vector
),
Variable((classes, 1), True)
)
# 返回输入和 logits
return x, logits
Python
1
https://gitee.com/zhangjuefei/computing_graph_demo.git
git@gitee.com:zhangjuefei/computing_graph_demo.git
zhangjuefei
computing_graph_demo
计算图框架
master

搜索帮助