其他分享
首页 > 其他分享> > “判断性别”Demo需求分析和初步设计(中)

“判断性别”Demo需求分析和初步设计(中)

作者:互联网

大家好~我开设了“深度学习基础班”的线上课程,带领同学从0开始学习全连接和卷积神经网络,进行数学推导,并且实现可以运行的Demo程序

线上课程资料:
本节课录像回放
加QQ群,获得ppt等资料,与群主交流讨论:106047770

本系列文章为线上课程的复盘,每上完一节课就会同步发布对应的文章

本文为第二节课:“判断性别”Demo需求分析和初步设计(中)的复盘文章

本课程系列文章可进入索引查看:
深度学习基础课系列文章索引

目录

回顾相关课程内容

主问题:什么是神经网络

主问题:什么是前向传播

任务:用代码实现神经网络

let forward = (state: state, sampleData: sampleData): float => {
  sampleData.height *. state.weight1 +.
  sampleData.weight *. state.weight2 +.
  state.bias->_activateFunc
}

let inference = (state: state, sampleData: sampleData): gender => {
  forward(state, sampleData)->_convert
}
type state = {
  weight13: float,
  weight14: float,
  weight23: float,
  weight24: float,
  weight35: float,
  weight45: float,
  bias3: float,
  bias4: float,
  bias5: float,
}

type sampleData = {
  weight: float,
  height: float,
}

type gender =
  | Male
  | Female
  | InValid

let createState = (): state => {
  weight13: Js.Math.random(),
  weight14: Js.Math.random(),
  weight23: Js.Math.random(),
  weight24: Js.Math.random(),
  weight35: Js.Math.random(),
  weight45: Js.Math.random(),
  bias3: Js.Math.random(),
  bias4: Js.Math.random(),
  bias5: Js.Math.random(),
}

// not implement
let train = (state: state, allSampleData: array<sampleData>): state => {
  state
}

let _activateFunc = x => x

let _convert = x =>
  switch x {
  | 0. => Male
  | 1. => Female
  | _ => InValid
  }

let forward = (state: state, sampleData: sampleData): float => {
  let y3 = Neural_forward_answer.forward(
    (
      {
        weight1: state.weight13,
        weight2: state.weight23,
        bias: state.bias3,
      }: Neural_forward_answer.state
    ),
    sampleData->Obj.magic,
  )

  let y4 = Neural_forward_answer.forward(
    (
      {
        weight1: state.weight14,
        weight2: state.weight24,
        bias: state.bias4,
      }: Neural_forward_answer.state
    ),
    sampleData->Obj.magic,
  )

  Neural_forward_answer.forward(
    (
      {
        weight1: state.weight35,
        weight2: state.weight45,
        bias: state.bias5,
      }: Neural_forward_answer.state
    ),
    (
      {
        weight: y3,
        height: y4,
      }: Neural_forward_answer.sampleData
    ),
  )
}

let inference = (state: state, sampleData: sampleData): gender => {
  Js.log(forward(state, sampleData))

  forward(state, sampleData)->_convert
}

let state = createState()

let allSampleData = [
  {
    weight: 50.,
    height: 150.,
  },
  {
    weight: 51.,
    height: 149.,
  },
  {
    weight: 60.,
    height: 172.,
  },
  {
    weight: 90.,
    height: 188.,
  },
]

let state = state->train(allSampleData)

allSampleData->Js.Array.forEach(sampleData => {
  inference(state, sampleData)->Js.log
}, _)

标签:Demo,float,sampleData,Js,state,let,forward,性别,初步设计
来源: https://www.cnblogs.com/chaogex/p/16618506.html