Simulation.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <template>
  2. <div>
  3. <v-row>
  4. <v-col cols="4">
  5. <div class="svgBox text-center" />
  6. <div class="svgBox1 text-center" />
  7. </v-col>
  8. <v-col cols="4">
  9. <editor />
  10. </v-col>
  11. <v-col cols="4">
  12. <div class="svgBox2 text-center" />
  13. </v-col>
  14. </v-row>
  15. </div>
  16. </template>
  17. <script>
  18. /* eslint-disable @typescript-eslint/no-unused-vars */
  19. /* eslint-disable no-unused-vars */
  20. /* eslint-disable no-console */
  21. /* eslint-disable camelcase */
  22. import * as d3 from 'd3'
  23. import Editor from './Editor'
  24. export default {
  25. name: 'Simulation',
  26. components: { Editor },
  27. data() {
  28. return {
  29. dataSquareLength: 40,
  30. calculationSquareLength: 40,
  31. wAndBSquareLength: 70,
  32. svgHeight: 1200,
  33. svgWidth: 70,
  34. data1: [1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1],
  35. dataSetW: [],
  36. dataSetB: []
  37. }
  38. },
  39. mounted() {
  40. },
  41. methods: {
  42. // 绘制训练数据
  43. initTrainingData() {
  44. const svgTrain = d3
  45. .select('.svgBox')
  46. .append('svg')
  47. .attr('id', 'svgTrain')
  48. .attr('width', 210)
  49. .attr('height', 180)
  50. .attr('x', 0)
  51. .attr('y', 0) // 添加svg画布
  52. const g = svgTrain.append('g') // 添加g元素来组合对象
  53. let x = 0
  54. let y = 0
  55. const squareLength = this.dataSquareLength
  56. for (let i = 0; i < 12; i++) {
  57. g.append('rect')
  58. .attr('width', squareLength)
  59. .attr('height', squareLength)
  60. .attr('x', x)
  61. .attr('y', y)
  62. .attr('fill', '#FFFFFF')
  63. .attr('stroke', '#000000')
  64. .attr('stroke-width', 3)
  65. g.append('text')
  66. .text(this.data1[i])
  67. .attr('fill', 'black')
  68. .attr('x', x + squareLength / 2)
  69. .attr('y', y + squareLength / 2)
  70. .attr('text-anchor', 'middle')
  71. .style('font-size', '20px')
  72. .attr('dy', 8)
  73. x += squareLength
  74. if (x > 2 * squareLength) {
  75. x = 0
  76. y += squareLength
  77. }
  78. }
  79. },
  80. // 绘制需计算的加权输入、输出等数据
  81. initCalculation() {
  82. const svgCalculation = d3
  83. .select('.svgBox1')
  84. .append('svg')
  85. .attr('id', 'svgCalculation')
  86. .attr('width', 210)
  87. .attr('height', 500)
  88. .attr('x', 0)
  89. .attr('y', 0) // 添加svg画布
  90. const g = svgCalculation.append('g') // 添加g元素来组合对象
  91. let x = 0
  92. let y = 0
  93. const squareLength = this.calculationSquareLength
  94. const data1 = ['隐藏层z', '隐藏层a', '输出层a', '输出层z']
  95. // 隐藏层绘制
  96. for (let i = 0; i < 2; i++) {
  97. const rect = g
  98. .append('rect')
  99. .attr('width', squareLength)
  100. .attr('height', squareLength * 3)
  101. .attr('x', x)
  102. .attr('y', y)
  103. .attr('fill', '#FFFFFF')
  104. .attr('stroke', '#000000')
  105. .attr('stroke-width', 3)
  106. const text = g
  107. .append('text')
  108. .text(data1[i])
  109. .attr('fill', 'black')
  110. .attr('x', x + squareLength / 2)
  111. .attr('y', y + (squareLength * 3) / 2)
  112. .attr('text-anchor', 'middle')
  113. .style('font-size', '10px')
  114. .attr('dy', 8)
  115. let y1 = y
  116. for (let j = 0; j < 3; j++) {
  117. const rect = g
  118. .append('rect')
  119. .attr('width', squareLength)
  120. .attr('height', squareLength)
  121. .attr('x', x + squareLength)
  122. .attr('y', y1)
  123. .attr('fill', '#FFFFFF')
  124. .attr('stroke', '#000000')
  125. .attr('stroke-width', 3)
  126. const text = g
  127. .append('text')
  128. .text(j + 1)
  129. .attr('fill', 'black')
  130. .attr('x', x + squareLength + squareLength / 2)
  131. .attr('y', y1 + squareLength / 2)
  132. .attr('text-anchor', 'middle')
  133. .style('font-size', '10px')
  134. .attr('dy', 8)
  135. y1 += squareLength
  136. }
  137. y += squareLength * 3
  138. }
  139. // 输出层绘制
  140. for (let i = 2; i < 4; i++) {
  141. const rect = g
  142. .append('rect')
  143. .attr('width', squareLength)
  144. .attr('height', squareLength * 2)
  145. .attr('x', x)
  146. .attr('y', y)
  147. .attr('fill', '#FFFFFF')
  148. .attr('stroke', '#000000')
  149. .attr('stroke-width', 3)
  150. const text = g
  151. .append('text')
  152. .text(data1[i])
  153. .attr('fill', 'black')
  154. .attr('x', x + squareLength / 2)
  155. .attr('y', y + squareLength)
  156. .attr('text-anchor', 'middle')
  157. .style('font-size', '10px')
  158. .attr('dy', 8)
  159. let y1 = y
  160. for (let j = 0; j < 2; j++) {
  161. const rect = g
  162. .append('rect')
  163. .attr('width', squareLength)
  164. .attr('height', squareLength)
  165. .attr('x', x + squareLength)
  166. .attr('y', y1)
  167. .attr('fill', '#FFFFFF')
  168. .attr('stroke', '#000000')
  169. .attr('stroke-width', 3)
  170. const text = g
  171. .append('text')
  172. .text(j + 1)
  173. .attr('fill', 'black')
  174. .attr('x', x + squareLength + squareLength / 2)
  175. .attr('y', y1 + squareLength / 2)
  176. .attr('text-anchor', 'middle')
  177. .style('font-size', '10px')
  178. .attr('dy', 8)
  179. y1 += squareLength
  180. }
  181. y += squareLength * 2
  182. }
  183. const g1 = svgCalculation.append('g').attr('id', 'gVariables') // 添加g元素来组合对象
  184. x += squareLength * 2
  185. y = 0
  186. for (let i = 0; i < 10; i++) {
  187. const rect = g1
  188. .append('rect')
  189. .attr('width', squareLength)
  190. .attr('height', squareLength)
  191. .attr('x', x)
  192. .attr('y', y)
  193. .attr('fill', '#FFFFFF')
  194. .attr('stroke', '#000000')
  195. .attr('stroke-width', 3)
  196. const text = g1
  197. .append('text')
  198. .text('')
  199. .attr('fill', 'black')
  200. .attr('x', x + squareLength / 2)
  201. .attr('y', y + squareLength / 2)
  202. .attr('text-anchor', 'middle')
  203. .style('font-size', '10px')
  204. .attr('dy', 8)
  205. y += squareLength
  206. }
  207. },
  208. // 绘制权重和偏置
  209. initWAndB() {
  210. const svgTitle = d3
  211. .select('.svgBox2')
  212. .append('svg')
  213. .attr('id', 'svgTitle')
  214. .attr('width', this.svgWidth)
  215. .attr('height', this.svgHeight)
  216. .attr('x', 0)
  217. .attr('y', 0)
  218. const svgw = d3
  219. .select('.svgBox2')
  220. .append('svg')
  221. .attr('id', 'svgw')
  222. .attr('width', 210)
  223. .attr('height', this.svgHeight) // 添加svg画布
  224. const svgb = d3
  225. .select('.svgBox2')
  226. .append('svg')
  227. .attr('id', 'svgb')
  228. .attr('width', this.svgWidth)
  229. .attr('height', this.svgHeight)
  230. .attr('x', 280)
  231. .attr('y', 0)
  232. const g = svgw.append('g') // 添加g元素来组合对象
  233. let x = 0
  234. let y = 70
  235. const squareLength = this.wAndBSquareLength
  236. // 绘制权重
  237. const rect = g
  238. .append('rect')
  239. .attr('width', 210)
  240. .attr('height', 70)
  241. .attr('x', 0)
  242. .attr('y', 0)
  243. .attr('fill', '#FFFFFF')
  244. .attr('stroke', '#000000')
  245. .attr('stroke-width', 3)
  246. const text = g
  247. .append('text')
  248. .text('w')
  249. .attr('fill', 'black')
  250. .attr('x', 105)
  251. .attr('y', 35)
  252. .attr('text-anchor', 'middle')
  253. .style('font-size', '20px')
  254. .attr('dy', 8)
  255. for (let i = 0; i < 42; i++) {
  256. const rect = g
  257. .append('rect')
  258. .attr('width', 70)
  259. .attr('height', 70)
  260. .attr('x', x)
  261. .attr('y', y)
  262. .attr('fill', '#FFFFFF')
  263. .attr('stroke', '#000000')
  264. .attr('stroke-width', 3)
  265. const text = g
  266. .append('text')
  267. .text('')
  268. .attr('fill', 'black')
  269. .attr('x', x + 35)
  270. .attr('y', y + 35)
  271. .attr('text-anchor', 'middle')
  272. .style('font-size', '20px')
  273. .attr('dy', 8)
  274. x += 70
  275. if (x > 140) {
  276. x = 0
  277. y += 70
  278. }
  279. }
  280. // 绘制偏置
  281. const g1 = svgb.append('g') // 添加g元素来组合对象
  282. const rect1 = g1
  283. .append('rect')
  284. .attr('width', 70)
  285. .attr('height', 70)
  286. .attr('x', 0)
  287. .attr('y', 0)
  288. .attr('fill', '#FFFFFF')
  289. .attr('stroke', '#000000')
  290. .attr('stroke-width', 3)
  291. const text1 = g1
  292. .append('text')
  293. .text('b')
  294. .attr('fill', 'black')
  295. .attr('x', 35)
  296. .attr('y', 35)
  297. .attr('text-anchor', 'middle')
  298. .style('font-size', '20px')
  299. .attr('dy', 8)
  300. x = 0
  301. y = 70
  302. for (let i = 0; i < 14; i++) {
  303. const rect = g1
  304. .append('rect')
  305. .attr('width', 70)
  306. .attr('height', 70)
  307. .attr('x', x)
  308. .attr('y', y)
  309. .attr('fill', '#FFFFFF')
  310. .attr('stroke', '#000000')
  311. .attr('stroke-width', 3)
  312. const text = g1
  313. .append('text')
  314. .text('')
  315. .attr('fill', 'black')
  316. .attr('x', x + 35)
  317. .attr('y', y + 35)
  318. .attr('text-anchor', 'middle')
  319. .style('font-size', '20px')
  320. .attr('dy', 8)
  321. y += 70
  322. }
  323. const g2 = svgTitle.append('g')
  324. x = 0
  325. y = 70
  326. for (let i = 0; i < 3; i++) {
  327. const rect1 = g2
  328. .append('rect')
  329. .attr('width', 70)
  330. .attr('height', 280)
  331. .attr('x', x)
  332. .attr('y', y)
  333. .attr('fill', '#FFFFFF')
  334. .attr('stroke', '#000000')
  335. .attr('stroke-width', 3)
  336. const text1 = g2
  337. .append('text')
  338. .text('隐藏层单元' + (i + 1))
  339. .attr('fill', 'black')
  340. .attr('x', x + 35)
  341. .attr('y', y + 140)
  342. .attr('text-anchor', 'middle')
  343. .style('font-size', '10px')
  344. .attr('dy', 8)
  345. y += 280
  346. }
  347. for (let i = 0; i < 2; i++) {
  348. const rect1 = g2
  349. .append('rect')
  350. .attr('width', 70)
  351. .attr('height', 70)
  352. .attr('x', x)
  353. .attr('y', y)
  354. .attr('fill', '#FFFFFF')
  355. .attr('stroke', '#000000')
  356. .attr('stroke-width', 3)
  357. const text1 = g2
  358. .append('text')
  359. .text('输出层单元' + (i + 1))
  360. .attr('fill', 'black')
  361. .attr('x', x + 35)
  362. .attr('y', y + 35)
  363. .attr('text-anchor', 'middle')
  364. .style('font-size', '10px')
  365. .attr('dy', 8)
  366. y += 70
  367. }
  368. },
  369. // 设置权重和偏置初始值
  370. setWAndB() {
  371. const svgw = d3.select('#svgw')
  372. const svgb = d3.select('#svgb')
  373. const dataSetW = []
  374. const dataSetB = []
  375. for (let i = 0; i < 56; i++) {
  376. const a = this.getNumberInNormalDistribution(0, 1)
  377. console.log(a)
  378. dataSetW.push(a)
  379. this.dataSetW.push(a)
  380. }
  381. for (let i = 0; i < 5; i++) {
  382. const a = this.getNumberInNormalDistribution(0, 1)
  383. console.log(a)
  384. dataSetB.push(a)
  385. this.dataSetB.push(a)
  386. }
  387. svgw.selectAll('text').text(function (d, i) {
  388. if (i === 0) {
  389. return 'w'
  390. }
  391. return dataSetW[i]
  392. })
  393. dataSetB.reverse()
  394. svgb.selectAll('text').text(function (d, i) {
  395. if (i === 0) {
  396. return 'b'
  397. } else if (i !== 1 && i !== 5 && i !== 9 && i !== 13 && i !== 14) {
  398. return ''
  399. }
  400. return dataSetB.pop()
  401. })
  402. },
  403. getNumberInNormalDistribution(mean, stdDev) {
  404. return (mean + this.uniform2NormalDistribution() * stdDev).toFixed(3)
  405. },
  406. uniform2NormalDistribution() {
  407. let sum = 0.0
  408. for (let i = 0; i < 12; i++) {
  409. sum = sum + Math.random()
  410. }
  411. return sum - 6.0
  412. },
  413. calculateVariables() {
  414. const g = d3.select('#gVariables')
  415. console.log(this.dataSetB)
  416. let z1 = 0
  417. let z2 = 0
  418. let z3 = 0
  419. let a1 = 0
  420. let a2 = 0
  421. let a3 = 0 // 隐藏层变量
  422. let z3_1 = 0
  423. let z3_2 = 0
  424. let a3_1 = 0
  425. let a3_2 = 0 // 输出层变量
  426. for (let i = 0; i < 12; i++) {
  427. z1 = z1 + Number(this.dataSetW[i]) * Number(this.data1[i])
  428. }
  429. z1 = (z1 + Number(this.dataSetB[0])).toFixed(3)
  430. a1 = this.calculateA(z1)
  431. for (let i = 0; i < 12; i++) {
  432. z2 = z2 + Number(this.dataSetW[i + 12]) * Number(this.data1[i])
  433. }
  434. z2 = (z2 + Number(this.dataSetB[1])).toFixed(3)
  435. a2 = this.calculateA(z2)
  436. for (let i = 0; i < 12; i++) {
  437. z3 = z3 + Number(this.dataSetW[i + 24]) * Number(this.data1[i])
  438. }
  439. z3 = (z3 + Number(this.dataSetB[2])).toFixed(3)
  440. a3 = this.calculateA(z3)
  441. z3_1 = (
  442. a1 * Number(this.dataSetW[36]) +
  443. a2 * Number(this.dataSetW[37]) +
  444. a3 * Number(this.dataSetW[38]) +
  445. Number(this.dataSetB[3])
  446. ).toFixed(3)
  447. z3_2 = (
  448. a1 * Number(this.dataSetW[39]) +
  449. a2 * Number(this.dataSetW[40]) +
  450. a3 * Number(this.dataSetW[41]) +
  451. Number(this.dataSetB[4])
  452. ).toFixed(3)
  453. a3_1 = this.calculateA(z3_1)
  454. a3_2 = this.calculateA(z3_2)
  455. const data = [z1, z2, z3, a1, a2, a3, z3_1, z3_2, a3_1, a3_2]
  456. g.selectAll('text').text(function (d, i) {
  457. return data[i]
  458. })
  459. },
  460. calculateA(z) {
  461. return (1 / (1 + Math.exp(-z))).toFixed(3)
  462. }
  463. }
  464. }
  465. </script>
  466. <style scoped></style>