深度学习框架头歌考试2021

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

第1关实现全连接层的反向传播



import numpy as np

class FullyConnected:
    def __init__(self, W, b):
        r'''
        全连接层的初始化。

        Parameter:
        - W: numpy.array, (D_in, D_out)
        - b: numpy.array, (D_out)
        '''
        self.W = W
        self.b = b

        self.x = None
        self.original_x_shape = None

        self.dW = None
        self.db = None

    def forward(self, x):
        r'''
        全连接层的前向传播。

        Parameter:
        - x: numpy.array, (B, d1, d2, ..., dk)

        Return:
        - y: numpy.array, (B, M)
        '''
        self.original_x_shape = x.shape
        x = x.reshape(x.shape[0], -1)
        self.x = x

        out = np.dot(self.x, self.W) + self.b

        return out

    def backward(self, dout):
        r'''
        全连接层的反向传播

        Parameter:
        - dout: numpy.array, (B, M)

        Return:
        - dx: numpy.array, (B, d1, d2, ..., dk) 与self.original_x_shape形状相同

        另外,还需计算以下结果:
        - self.dW: numpy.array, (N, M) 与self.W形状相同
        - self.db: numpy.array, (M,)
        '''
        ########## Begin ##########
        dx = np.dot(dout, self.W.T)
        self.dW = np.dot(self.x.T, dout)
        self.db = np.sum(dout, axis=0)
        return dx
        ########## End ##########

第2关实现常用激活函数的反向传播



import numpy as np

class Sigmoid:
    def __init__(self):
        self.out = None

    def forward(self, x):
        r'''
        Sigmoid激活函数的前向传播。

        Parameter:
        - x: numpy.array, (B, d1, d2, ..., dk)

        Return:
        - y: numpy.array, (B, d1, d2, ..., dk)
        '''
        out = 1. / (1. + np.exp(-x))
        self.out = out
        return out

    def backward(self, dout):
        r'''
        sigmoid的反向传播

        Parameter:
        - dout: numpy.array, (B, d1, d2, ..., dk)

        Return:
        - dx: numpy.array, (B, d1, d2, ..., dk)
        '''
        ########## Begin ##########
        dx = dout * (1.0 - self.out) * self.out
        return dx
        ########## End ##########

class Relu:
    def __init__(self):
        self.mask = None

    def forward(self, x):
        r'''
        ReLU激活函数的前向传播。

        Parameter:
        - x: numpy.array, (B, d1, d2, ..., dk)

        Return:
        - y: numpy.array, (B, d1, d2, ..., dk)
        '''
        self.mask = (x <= 0)
        out = x.copy()
        out[self.mask] = 0
        return out

    def backward(self, dout):
        r'''
        relu的反向传播

        Parameter:
        - dout: numpy.array, (B, d1, d2, ..., dk)

        Return:
        - dx: numpy.array, (B, d1, d2, ..., dk)
        '''
        ########## Begin ##########
        dout[self.mask] = 0
        dx = dout
        return dx
        ########## End ##########

第1关Tensorflow基本运算



import tensorflow as tf
import os

os.environ["TF_CPP_MIN_LOG_LEVEL"]='3'

def simple_func(a,b,c,d):
    '''
    返回值:
    result: 一个标量值
    '''
    # 请在此添加代码 完成本关任务
    # ********** Begin *********#
    a = tf.constant(a)
    b = tf.constant(b)
    c = tf.constant(c)
    d = tf.constant(d)

    result =  tf.multiply(tf.add(a, b),tf.add(c, d)).eval()

    # ********** End **********#
    # 返回result
    return result

a = int(input())
b = int(input())
c = int(input())
d = int(input())

init_op = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init_op)
    print(simple_func(a,b,c,d))

第1关神经元与激活函数



import tensorflow as tf

def neuron(input_value, weight, threshold):
    # 请在此添加代码 完成本关任务
    # ********** Begin *********#
    return tf.sigmoid(tf.reduce_sum(input_value * weight) - threshold).eval()
    # ********** End **********#

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

import tensorflow as tf


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.tanh(tf.reduce_sum(input_value * self.weight) - self.threshold).eval()

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

import tensorflow as tf


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(input_value * self.weight) - self.threshold).eval()
    # ********** End **********#


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

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


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 **********#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

粥粥粥少女的拧发条鸟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值