TensorFlow入门--构建神经网络

第1关:神经元与激活函数

本关任务:编写一个能模拟神经元工作过程的函数。

# -*- coding: utf-8 -*-
import tensorflow as tf

# 模拟一个 M-P 神经元的工作原理
# input_value 是输入值, 类型为一维的tf.constant
# weight 是这个神经元的权重, 类型为一维的tf.constant
# threshold 是这个神经元的阈值, 类型为零维的tf.constant
# 返回值是一个浮点数
def neuron(input_value, weight, threshold):
    # 请在此添加代码 完成本关任务
    # ********** Begin *********#
    res=input_value*weight
    ans=tf.reduce_sum(res)-threshold
    return tf.sigmoid(ans).eval()
    # ********** End **********#
    

第2关:神经元与激活函数 - tanh方法

本关任务:编写一个能模拟神经元的类。

# -*- coding: utf-8 -*-
import tensorflow as tf

# 模拟一个 M-P 神经元
class neuron(object):

    # 构造函数
    # weight为本神经元的权重,类型为一维的tf.constant
    # threshold 是这个神经元的阈值, 类型为零维的tf.constant
    def __init__(self, weight, threshold):
    # 请在此添加代码 完成本关任务
    # ********** Begin *********#
        self.weight=weight
        self.threshold=threshold
    # ********** End **********#

    # 计算函数
    # input_value 是输入值, 类型为一维的tf.constant
    # 返回值是一个浮点数
    def computes(self, input_value):
    # 请在此添加代码 完成本关任务
    # ********** Begin *********#
        A=input_value*self.weight
        B=tf.reduce_sum(A)-self.threshold
        return tf.tanh(B).eval()
    # ********** End **********#
    


第3关:构建简单的单隐层前馈神经网络

本关任务:编写一个模拟单隐层前馈神经网络的小程序。

# -*- coding: utf-8 -*-
import tensorflow as tf

# 模拟一个 M-P 神经元
class neuron(object):

    # 构造函数
    # weight为本神经元的权重,类型为一维的tf.constant
    # threshold 是这个神经元的阈值, 类型为零维的tf.constant
    def __init__(self, weight, threshold):
    # 请在此添加代码 完成本关任务
    # ********** Begin *********#
        self.weight=weight
        self.threshold=threshold
    # ********** End **********#

    # 计算函数
    # input_value 是输入值, 类型为一维的tf.constant
    # 返回值是一个浮点数
    def computes(self, input_value):
    # 请在此添加代码 完成本关任务
    # ********** Begin *********#
        return tf.nn.relu(tf.reduce_sum(self.weight*input_value)-self.threshold).eval()
    # ********** End **********#

# 模拟神经网络中的一层
class Dense(object):
    
    # 构造函数
    # weights 为本层中每个神经元的权重,元素类型为一维的tf.constant,weights的类型是python的列表
    # thresholds 为本层中每个神经元的权重,元素类型为零维的tf.constant,thresholds的类型是python的列表
    def __init__(self, weights, thresholds):
    # 请在此添加代码 完成本关任务
    # ********** Begin *********#
        size=len(weights)
        self.neurons=[]
        for i in range(size):
            self.neurons.append(neuron(weights[i],thresholds[i]))
    # ********** End **********#

    # 计算函数
    # input_value 是输入值, 类型为一维的tf.constant
    # 返回值应为一个 1 维, 长度为n的Tensor, n是本层中神经元的数量
    def computes(self, input_value):
    # 请在此添加代码 完成本关任务
    # ********** Begin *********#
        res=[]
        size=len(self.neurons)
        for i in range(size):
            res.append(self.neurons[i].computes(input_value))
        return tf.constant(res)

    # ********** End **********#

# 模拟一个简单的神经网络
# input_value是这个神经网络的输入,类型为一维的tf.constant
# wegihtsOfMiddle 是这个神经网络中间层每个神经元的权重, 元素类型为一维的tf.constant,wegihtsOfMiddle的类型是python的列表
# thresholdsOfMiddle 是这个神经网络中间层每个神经元的阈值, 元素类型为零维的tf.constant,thresholdsOfMiddle的类型是python的列表
# wegihtsOfOut 是这个神经网络输出层每个神经元的权重, 元素类型为一维的tf.constant,wegihtsOfOut 的类型是python的列表
# thresholdsOfOut 是这个神经网络输出层每个神经元的阈值, 元素类型为零维的tf.constant,thresholdsOfOut 的类型是python的列表
# 返回值是一个一维浮点数组 (注意不是Tensor),数组的长度为输出层神经元的数量
def NetWork(input_value, wegihtsOfMiddle, thresholdsOfMiddle, weightsOfOut, thresholdsOfOut):
    # 请在此添加代码 完成本关任务
    # ********** Begin *********#
    mid=Dense(wegihtsOfMiddle,thresholdsOfMiddle)
    out=Dense(weightsOfOut,thresholdsOfOut)
    return out.computes(mid.computes(input_value)).eval()
    # ********** End **********#

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

粥粥粥少女的拧发条鸟

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值