You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tf01-dataset_basic_api.ipynb 23 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 9,
  6. "metadata": {},
  7. "outputs": [
  8. {
  9. "name": "stdout",
  10. "output_type": "stream",
  11. "text": [
  12. "2.2.0\n",
  13. "sys.version_info(major=3, minor=6, micro=9, releaselevel='final', serial=0)\n",
  14. "matplotlib 3.3.4\n",
  15. "numpy 1.19.5\n",
  16. "pandas 1.1.5\n",
  17. "sklearn 0.24.2\n",
  18. "tensorflow 2.2.0\n",
  19. "tensorflow.keras 2.3.0-tf\n"
  20. ]
  21. }
  22. ],
  23. "source": [
  24. "import matplotlib as mpl\n",
  25. "import matplotlib.pyplot as plt\n",
  26. "%matplotlib inline\n",
  27. "import numpy as np\n",
  28. "import sklearn\n",
  29. "import pandas as pd\n",
  30. "import os\n",
  31. "import sys\n",
  32. "import time\n",
  33. "import tensorflow as tf\n",
  34. "\n",
  35. "from tensorflow import keras\n",
  36. "\n",
  37. "print(tf.__version__)\n",
  38. "print(sys.version_info)\n",
  39. "for module in mpl, np, pd, sklearn, tf, keras:\n",
  40. " print(module.__name__, module.__version__)"
  41. ]
  42. },
  43. {
  44. "cell_type": "markdown",
  45. "metadata": {},
  46. "source": [
  47. "# from_tensor_slices ,repeat,batch,interleave"
  48. ]
  49. },
  50. {
  51. "cell_type": "code",
  52. "execution_count": 10,
  53. "metadata": {},
  54. "outputs": [
  55. {
  56. "name": "stdout",
  57. "output_type": "stream",
  58. "text": [
  59. "<TensorSliceDataset shapes: (), types: tf.int64>\n"
  60. ]
  61. }
  62. ],
  63. "source": [
  64. "#内存中构建dataset,初始化dataset,返回的像一个迭代器\n",
  65. "dataset = tf.data.Dataset.from_tensor_slices(np.arange(1,6).repeat(6))\n",
  66. "\n",
  67. "print(dataset)"
  68. ]
  69. },
  70. {
  71. "cell_type": "code",
  72. "execution_count": 11,
  73. "metadata": {},
  74. "outputs": [
  75. {
  76. "name": "stdout",
  77. "output_type": "stream",
  78. "text": [
  79. "tf.Tensor(1, shape=(), dtype=int64)\n",
  80. "tf.Tensor(1, shape=(), dtype=int64)\n",
  81. "tf.Tensor(1, shape=(), dtype=int64)\n",
  82. "tf.Tensor(1, shape=(), dtype=int64)\n",
  83. "tf.Tensor(1, shape=(), dtype=int64)\n",
  84. "tf.Tensor(1, shape=(), dtype=int64)\n",
  85. "tf.Tensor(2, shape=(), dtype=int64)\n",
  86. "tf.Tensor(2, shape=(), dtype=int64)\n",
  87. "tf.Tensor(2, shape=(), dtype=int64)\n",
  88. "tf.Tensor(2, shape=(), dtype=int64)\n",
  89. "tf.Tensor(2, shape=(), dtype=int64)\n",
  90. "tf.Tensor(2, shape=(), dtype=int64)\n",
  91. "tf.Tensor(3, shape=(), dtype=int64)\n",
  92. "tf.Tensor(3, shape=(), dtype=int64)\n",
  93. "tf.Tensor(3, shape=(), dtype=int64)\n",
  94. "tf.Tensor(3, shape=(), dtype=int64)\n",
  95. "tf.Tensor(3, shape=(), dtype=int64)\n",
  96. "tf.Tensor(3, shape=(), dtype=int64)\n",
  97. "tf.Tensor(4, shape=(), dtype=int64)\n",
  98. "tf.Tensor(4, shape=(), dtype=int64)\n",
  99. "tf.Tensor(4, shape=(), dtype=int64)\n",
  100. "tf.Tensor(4, shape=(), dtype=int64)\n",
  101. "tf.Tensor(4, shape=(), dtype=int64)\n",
  102. "tf.Tensor(4, shape=(), dtype=int64)\n",
  103. "tf.Tensor(5, shape=(), dtype=int64)\n",
  104. "tf.Tensor(5, shape=(), dtype=int64)\n",
  105. "tf.Tensor(5, shape=(), dtype=int64)\n",
  106. "tf.Tensor(5, shape=(), dtype=int64)\n",
  107. "tf.Tensor(5, shape=(), dtype=int64)\n",
  108. "tf.Tensor(5, shape=(), dtype=int64)\n"
  109. ]
  110. }
  111. ],
  112. "source": [
  113. "#可以做遍历,每一个都是tensor\n",
  114. "for item in dataset:\n",
  115. " print(item)"
  116. ]
  117. },
  118. {
  119. "cell_type": "code",
  120. "execution_count": 12,
  121. "metadata": {},
  122. "outputs": [
  123. {
  124. "name": "stdout",
  125. "output_type": "stream",
  126. "text": [
  127. "tf.Tensor(1, shape=(), dtype=int64)\n",
  128. "tf.Tensor(1, shape=(), dtype=int64)\n",
  129. "tf.Tensor(1, shape=(), dtype=int64)\n",
  130. "tf.Tensor(1, shape=(), dtype=int64)\n",
  131. "tf.Tensor(1, shape=(), dtype=int64)\n",
  132. "tf.Tensor(1, shape=(), dtype=int64)\n",
  133. "tf.Tensor(2, shape=(), dtype=int64)\n",
  134. "tf.Tensor(2, shape=(), dtype=int64)\n",
  135. "tf.Tensor(2, shape=(), dtype=int64)\n",
  136. "tf.Tensor(2, shape=(), dtype=int64)\n",
  137. "tf.Tensor(2, shape=(), dtype=int64)\n",
  138. "tf.Tensor(2, shape=(), dtype=int64)\n",
  139. "tf.Tensor(3, shape=(), dtype=int64)\n",
  140. "tf.Tensor(3, shape=(), dtype=int64)\n",
  141. "tf.Tensor(3, shape=(), dtype=int64)\n",
  142. "tf.Tensor(3, shape=(), dtype=int64)\n",
  143. "tf.Tensor(3, shape=(), dtype=int64)\n",
  144. "tf.Tensor(3, shape=(), dtype=int64)\n",
  145. "tf.Tensor(4, shape=(), dtype=int64)\n",
  146. "tf.Tensor(4, shape=(), dtype=int64)\n",
  147. "tf.Tensor(4, shape=(), dtype=int64)\n",
  148. "tf.Tensor(4, shape=(), dtype=int64)\n",
  149. "tf.Tensor(4, shape=(), dtype=int64)\n",
  150. "tf.Tensor(4, shape=(), dtype=int64)\n",
  151. "tf.Tensor(5, shape=(), dtype=int64)\n",
  152. "tf.Tensor(5, shape=(), dtype=int64)\n",
  153. "tf.Tensor(5, shape=(), dtype=int64)\n",
  154. "tf.Tensor(5, shape=(), dtype=int64)\n",
  155. "tf.Tensor(5, shape=(), dtype=int64)\n",
  156. "tf.Tensor(5, shape=(), dtype=int64)\n",
  157. "tf.Tensor(1, shape=(), dtype=int64)\n",
  158. "tf.Tensor(1, shape=(), dtype=int64)\n",
  159. "tf.Tensor(1, shape=(), dtype=int64)\n",
  160. "tf.Tensor(1, shape=(), dtype=int64)\n",
  161. "tf.Tensor(1, shape=(), dtype=int64)\n",
  162. "tf.Tensor(1, shape=(), dtype=int64)\n",
  163. "tf.Tensor(2, shape=(), dtype=int64)\n",
  164. "tf.Tensor(2, shape=(), dtype=int64)\n",
  165. "tf.Tensor(2, shape=(), dtype=int64)\n",
  166. "tf.Tensor(2, shape=(), dtype=int64)\n",
  167. "tf.Tensor(2, shape=(), dtype=int64)\n",
  168. "tf.Tensor(2, shape=(), dtype=int64)\n",
  169. "tf.Tensor(3, shape=(), dtype=int64)\n",
  170. "tf.Tensor(3, shape=(), dtype=int64)\n",
  171. "tf.Tensor(3, shape=(), dtype=int64)\n",
  172. "tf.Tensor(3, shape=(), dtype=int64)\n",
  173. "tf.Tensor(3, shape=(), dtype=int64)\n",
  174. "tf.Tensor(3, shape=(), dtype=int64)\n",
  175. "tf.Tensor(4, shape=(), dtype=int64)\n",
  176. "tf.Tensor(4, shape=(), dtype=int64)\n",
  177. "tf.Tensor(4, shape=(), dtype=int64)\n",
  178. "tf.Tensor(4, shape=(), dtype=int64)\n",
  179. "tf.Tensor(4, shape=(), dtype=int64)\n",
  180. "tf.Tensor(4, shape=(), dtype=int64)\n",
  181. "tf.Tensor(5, shape=(), dtype=int64)\n",
  182. "tf.Tensor(5, shape=(), dtype=int64)\n",
  183. "tf.Tensor(5, shape=(), dtype=int64)\n",
  184. "tf.Tensor(5, shape=(), dtype=int64)\n",
  185. "tf.Tensor(5, shape=(), dtype=int64)\n",
  186. "tf.Tensor(5, shape=(), dtype=int64)\n",
  187. "tf.Tensor(1, shape=(), dtype=int64)\n",
  188. "tf.Tensor(1, shape=(), dtype=int64)\n",
  189. "tf.Tensor(1, shape=(), dtype=int64)\n",
  190. "tf.Tensor(1, shape=(), dtype=int64)\n",
  191. "tf.Tensor(1, shape=(), dtype=int64)\n",
  192. "tf.Tensor(1, shape=(), dtype=int64)\n",
  193. "tf.Tensor(2, shape=(), dtype=int64)\n",
  194. "tf.Tensor(2, shape=(), dtype=int64)\n",
  195. "tf.Tensor(2, shape=(), dtype=int64)\n",
  196. "tf.Tensor(2, shape=(), dtype=int64)\n",
  197. "tf.Tensor(2, shape=(), dtype=int64)\n",
  198. "tf.Tensor(2, shape=(), dtype=int64)\n",
  199. "tf.Tensor(3, shape=(), dtype=int64)\n",
  200. "tf.Tensor(3, shape=(), dtype=int64)\n",
  201. "tf.Tensor(3, shape=(), dtype=int64)\n",
  202. "tf.Tensor(3, shape=(), dtype=int64)\n",
  203. "tf.Tensor(3, shape=(), dtype=int64)\n",
  204. "tf.Tensor(3, shape=(), dtype=int64)\n",
  205. "tf.Tensor(4, shape=(), dtype=int64)\n",
  206. "tf.Tensor(4, shape=(), dtype=int64)\n",
  207. "tf.Tensor(4, shape=(), dtype=int64)\n",
  208. "tf.Tensor(4, shape=(), dtype=int64)\n",
  209. "tf.Tensor(4, shape=(), dtype=int64)\n",
  210. "tf.Tensor(4, shape=(), dtype=int64)\n",
  211. "tf.Tensor(5, shape=(), dtype=int64)\n",
  212. "tf.Tensor(5, shape=(), dtype=int64)\n",
  213. "tf.Tensor(5, shape=(), dtype=int64)\n",
  214. "tf.Tensor(5, shape=(), dtype=int64)\n",
  215. "tf.Tensor(5, shape=(), dtype=int64)\n",
  216. "tf.Tensor(5, shape=(), dtype=int64)\n"
  217. ]
  218. }
  219. ],
  220. "source": [
  221. "dataset1 = dataset.repeat(3) #为了epoch服务的\n",
  222. "for item in dataset1:\n",
  223. " print(item)"
  224. ]
  225. },
  226. {
  227. "cell_type": "code",
  228. "execution_count": 13,
  229. "metadata": {},
  230. "outputs": [
  231. {
  232. "name": "stdout",
  233. "output_type": "stream",
  234. "text": [
  235. "<BatchDataset shapes: (None,), types: tf.int64>\n",
  236. "tf.Tensor([1 1 1 1 1 1 2], shape=(7,), dtype=int64)\n",
  237. "tf.Tensor([2 2 2 2 2 3 3], shape=(7,), dtype=int64)\n",
  238. "tf.Tensor([3 3 3 3 4 4 4], shape=(7,), dtype=int64)\n",
  239. "tf.Tensor([4 4 4 5 5 5 5], shape=(7,), dtype=int64)\n",
  240. "tf.Tensor([5 5 1 1 1 1 1], shape=(7,), dtype=int64)\n",
  241. "tf.Tensor([1 2 2 2 2 2 2], shape=(7,), dtype=int64)\n",
  242. "tf.Tensor([3 3 3 3 3 3 4], shape=(7,), dtype=int64)\n",
  243. "tf.Tensor([4 4 4 4 4 5 5], shape=(7,), dtype=int64)\n",
  244. "tf.Tensor([5 5 5 5 1 1 1], shape=(7,), dtype=int64)\n",
  245. "tf.Tensor([1 1 1 2 2 2 2], shape=(7,), dtype=int64)\n",
  246. "tf.Tensor([2 2 3 3 3 3 3], shape=(7,), dtype=int64)\n",
  247. "tf.Tensor([3 4 4 4 4 4 4], shape=(7,), dtype=int64)\n",
  248. "tf.Tensor([5 5 5 5 5 5], shape=(6,), dtype=int64)\n"
  249. ]
  250. },
  251. {
  252. "data": {
  253. "text/plain": [
  254. "13"
  255. ]
  256. },
  257. "execution_count": 13,
  258. "metadata": {},
  259. "output_type": "execute_result"
  260. }
  261. ],
  262. "source": [
  263. "# 1. repeat epoch 遍历一次数据集,就称为1次epoch\n",
  264. "# 2. get batch 取一部分数据集\n",
  265. "\n",
  266. "dataset = dataset.repeat(3).batch(7)\n",
  267. "print(dataset)\n",
  268. "i=0\n",
  269. "for item in dataset:\n",
  270. " i=i+1\n",
  271. " print(item)\n",
  272. "i"
  273. ]
  274. },
  275. {
  276. "cell_type": "code",
  277. "execution_count": 14,
  278. "metadata": {},
  279. "outputs": [
  280. {
  281. "name": "stdout",
  282. "output_type": "stream",
  283. "text": [
  284. "tf.Tensor([1 1 1 1 1 1 2], shape=(7,), dtype=int64)\n",
  285. "tf.Tensor([2 2 2 2 2 3 3], shape=(7,), dtype=int64)\n",
  286. "tf.Tensor([3 3 3 3 4 4 4], shape=(7,), dtype=int64)\n",
  287. "tf.Tensor([4 4 4 5 5 5 5], shape=(7,), dtype=int64)\n",
  288. "tf.Tensor([5 5 1 1 1 1 1], shape=(7,), dtype=int64)\n",
  289. "tf.Tensor([1 2 2 2 2 2 2], shape=(7,), dtype=int64)\n",
  290. "tf.Tensor([3 3 3 3 3 3 4], shape=(7,), dtype=int64)\n",
  291. "tf.Tensor([4 4 4 4 4 5 5], shape=(7,), dtype=int64)\n",
  292. "tf.Tensor([5 5 5 5 1 1 1], shape=(7,), dtype=int64)\n",
  293. "tf.Tensor([1 1 1 2 2 2 2], shape=(7,), dtype=int64)\n",
  294. "tf.Tensor([2 2 3 3 3 3 3], shape=(7,), dtype=int64)\n",
  295. "tf.Tensor([3 4 4 4 4 4 4], shape=(7,), dtype=int64)\n",
  296. "tf.Tensor([5 5 5 5 5 5], shape=(6,), dtype=int64)\n"
  297. ]
  298. }
  299. ],
  300. "source": [
  301. "for i in dataset:\n",
  302. " print(i)"
  303. ]
  304. },
  305. {
  306. "cell_type": "code",
  307. "execution_count": 21,
  308. "metadata": {
  309. "scrolled": false
  310. },
  311. "outputs": [
  312. {
  313. "name": "stdout",
  314. "output_type": "stream",
  315. "text": [
  316. "<InterleaveDataset shapes: (), types: tf.int64>\n",
  317. "--------------------------------------------------\n",
  318. "tf.Tensor(1, shape=(), dtype=int64)\n",
  319. "tf.Tensor(1, shape=(), dtype=int64)\n",
  320. "tf.Tensor(1, shape=(), dtype=int64)\n",
  321. "tf.Tensor(1, shape=(), dtype=int64)\n",
  322. "tf.Tensor(1, shape=(), dtype=int64)\n",
  323. "tf.Tensor(1, shape=(), dtype=int64)\n",
  324. "tf.Tensor(2, shape=(), dtype=int64)\n",
  325. "tf.Tensor(2, shape=(), dtype=int64)\n",
  326. "tf.Tensor(2, shape=(), dtype=int64)\n",
  327. "tf.Tensor(2, shape=(), dtype=int64)\n",
  328. "tf.Tensor(2, shape=(), dtype=int64)\n",
  329. "tf.Tensor(3, shape=(), dtype=int64)\n",
  330. "tf.Tensor(3, shape=(), dtype=int64)\n",
  331. "tf.Tensor(3, shape=(), dtype=int64)\n",
  332. "tf.Tensor(3, shape=(), dtype=int64)\n",
  333. "tf.Tensor(3, shape=(), dtype=int64)\n",
  334. "tf.Tensor(4, shape=(), dtype=int64)\n",
  335. "tf.Tensor(4, shape=(), dtype=int64)\n",
  336. "tf.Tensor(4, shape=(), dtype=int64)\n",
  337. "tf.Tensor(4, shape=(), dtype=int64)\n",
  338. "tf.Tensor(4, shape=(), dtype=int64)\n",
  339. "tf.Tensor(5, shape=(), dtype=int64)\n",
  340. "tf.Tensor(5, shape=(), dtype=int64)\n",
  341. "tf.Tensor(5, shape=(), dtype=int64)\n",
  342. "tf.Tensor(5, shape=(), dtype=int64)\n",
  343. "tf.Tensor(5, shape=(), dtype=int64)\n",
  344. "tf.Tensor(1, shape=(), dtype=int64)\n",
  345. "tf.Tensor(1, shape=(), dtype=int64)\n",
  346. "tf.Tensor(1, shape=(), dtype=int64)\n",
  347. "tf.Tensor(1, shape=(), dtype=int64)\n",
  348. "tf.Tensor(2, shape=(), dtype=int64)\n",
  349. "tf.Tensor(3, shape=(), dtype=int64)\n",
  350. "tf.Tensor(4, shape=(), dtype=int64)\n",
  351. "tf.Tensor(5, shape=(), dtype=int64)\n",
  352. "tf.Tensor(1, shape=(), dtype=int64)\n",
  353. "tf.Tensor(1, shape=(), dtype=int64)\n",
  354. "tf.Tensor(2, shape=(), dtype=int64)\n",
  355. "tf.Tensor(2, shape=(), dtype=int64)\n",
  356. "tf.Tensor(2, shape=(), dtype=int64)\n",
  357. "tf.Tensor(2, shape=(), dtype=int64)\n",
  358. "tf.Tensor(2, shape=(), dtype=int64)\n",
  359. "tf.Tensor(3, shape=(), dtype=int64)\n",
  360. "tf.Tensor(3, shape=(), dtype=int64)\n",
  361. "tf.Tensor(3, shape=(), dtype=int64)\n",
  362. "tf.Tensor(3, shape=(), dtype=int64)\n",
  363. "tf.Tensor(3, shape=(), dtype=int64)\n",
  364. "tf.Tensor(3, shape=(), dtype=int64)\n",
  365. "tf.Tensor(4, shape=(), dtype=int64)\n",
  366. "tf.Tensor(4, shape=(), dtype=int64)\n",
  367. "tf.Tensor(4, shape=(), dtype=int64)\n",
  368. "tf.Tensor(4, shape=(), dtype=int64)\n",
  369. "tf.Tensor(4, shape=(), dtype=int64)\n",
  370. "tf.Tensor(5, shape=(), dtype=int64)\n",
  371. "tf.Tensor(5, shape=(), dtype=int64)\n",
  372. "tf.Tensor(5, shape=(), dtype=int64)\n",
  373. "tf.Tensor(5, shape=(), dtype=int64)\n",
  374. "tf.Tensor(5, shape=(), dtype=int64)\n",
  375. "tf.Tensor(1, shape=(), dtype=int64)\n",
  376. "tf.Tensor(1, shape=(), dtype=int64)\n",
  377. "tf.Tensor(1, shape=(), dtype=int64)\n",
  378. "tf.Tensor(1, shape=(), dtype=int64)\n",
  379. "tf.Tensor(1, shape=(), dtype=int64)\n",
  380. "tf.Tensor(2, shape=(), dtype=int64)\n",
  381. "tf.Tensor(2, shape=(), dtype=int64)\n",
  382. "tf.Tensor(2, shape=(), dtype=int64)\n",
  383. "tf.Tensor(2, shape=(), dtype=int64)\n",
  384. "tf.Tensor(4, shape=(), dtype=int64)\n",
  385. "tf.Tensor(5, shape=(), dtype=int64)\n",
  386. "tf.Tensor(1, shape=(), dtype=int64)\n",
  387. "tf.Tensor(2, shape=(), dtype=int64)\n",
  388. "tf.Tensor(2, shape=(), dtype=int64)\n",
  389. "tf.Tensor(2, shape=(), dtype=int64)\n",
  390. "tf.Tensor(3, shape=(), dtype=int64)\n",
  391. "tf.Tensor(3, shape=(), dtype=int64)\n",
  392. "tf.Tensor(3, shape=(), dtype=int64)\n",
  393. "tf.Tensor(3, shape=(), dtype=int64)\n",
  394. "tf.Tensor(3, shape=(), dtype=int64)\n",
  395. "tf.Tensor(4, shape=(), dtype=int64)\n",
  396. "tf.Tensor(4, shape=(), dtype=int64)\n",
  397. "tf.Tensor(4, shape=(), dtype=int64)\n",
  398. "tf.Tensor(4, shape=(), dtype=int64)\n",
  399. "tf.Tensor(4, shape=(), dtype=int64)\n",
  400. "tf.Tensor(5, shape=(), dtype=int64)\n",
  401. "tf.Tensor(5, shape=(), dtype=int64)\n",
  402. "tf.Tensor(5, shape=(), dtype=int64)\n",
  403. "tf.Tensor(5, shape=(), dtype=int64)\n",
  404. "tf.Tensor(5, shape=(), dtype=int64)\n",
  405. "tf.Tensor(5, shape=(), dtype=int64)\n",
  406. "tf.Tensor(3, shape=(), dtype=int64)\n",
  407. "tf.Tensor(4, shape=(), dtype=int64)\n"
  408. ]
  409. },
  410. {
  411. "data": {
  412. "text/plain": [
  413. "90"
  414. ]
  415. },
  416. "execution_count": 21,
  417. "metadata": {},
  418. "output_type": "execute_result"
  419. }
  420. ],
  421. "source": [
  422. "# interleave: 作用 https://zhuanlan.zhihu.com/p/97876668\n",
  423. "# case: 文件dataset -> 具体数据集\n",
  424. "\n",
  425. "dataset2 = dataset.interleave(\n",
  426. " lambda v: tf.data.Dataset.from_tensor_slices(v), # map_fn,第一参数是回调函数\n",
  427. " cycle_length = 5, # cycle_length,每一个cycle提取的个数\n",
  428. " block_length = 6, # block_length\n",
  429. ")\n",
  430. "print(dataset2)\n",
  431. "print('-'*50)\n",
  432. "i=0\n",
  433. "for item in dataset2:\n",
  434. " i=i+1\n",
  435. " print(item)\n",
  436. "i"
  437. ]
  438. },
  439. {
  440. "cell_type": "code",
  441. "execution_count": 16,
  442. "metadata": {},
  443. "outputs": [],
  444. "source": [
  445. "# 下面的例子理解interleave更加简单"
  446. ]
  447. },
  448. {
  449. "cell_type": "code",
  450. "execution_count": 17,
  451. "metadata": {},
  452. "outputs": [
  453. {
  454. "name": "stdout",
  455. "output_type": "stream",
  456. "text": [
  457. "tf.Tensor(1, shape=(), dtype=int64)\n",
  458. "tf.Tensor(2, shape=(), dtype=int64)\n",
  459. "tf.Tensor(3, shape=(), dtype=int64)\n",
  460. "tf.Tensor(4, shape=(), dtype=int64)\n",
  461. "tf.Tensor(5, shape=(), dtype=int64)\n",
  462. "--------------------------------------------------\n",
  463. "tf.Tensor(1, shape=(), dtype=int64)\n",
  464. "tf.Tensor(1, shape=(), dtype=int64)\n",
  465. "tf.Tensor(1, shape=(), dtype=int64)\n",
  466. "tf.Tensor(1, shape=(), dtype=int64)\n",
  467. "tf.Tensor(2, shape=(), dtype=int64)\n",
  468. "tf.Tensor(2, shape=(), dtype=int64)\n",
  469. "tf.Tensor(2, shape=(), dtype=int64)\n",
  470. "tf.Tensor(2, shape=(), dtype=int64)\n",
  471. "tf.Tensor(3, shape=(), dtype=int64)\n",
  472. "tf.Tensor(3, shape=(), dtype=int64)\n",
  473. "tf.Tensor(3, shape=(), dtype=int64)\n",
  474. "tf.Tensor(3, shape=(), dtype=int64)\n",
  475. "tf.Tensor(1, shape=(), dtype=int64)\n",
  476. "tf.Tensor(1, shape=(), dtype=int64)\n",
  477. "tf.Tensor(2, shape=(), dtype=int64)\n",
  478. "tf.Tensor(2, shape=(), dtype=int64)\n",
  479. "tf.Tensor(3, shape=(), dtype=int64)\n",
  480. "tf.Tensor(3, shape=(), dtype=int64)\n",
  481. "tf.Tensor(4, shape=(), dtype=int64)\n",
  482. "tf.Tensor(4, shape=(), dtype=int64)\n",
  483. "tf.Tensor(4, shape=(), dtype=int64)\n",
  484. "tf.Tensor(4, shape=(), dtype=int64)\n",
  485. "tf.Tensor(5, shape=(), dtype=int64)\n",
  486. "tf.Tensor(5, shape=(), dtype=int64)\n",
  487. "tf.Tensor(5, shape=(), dtype=int64)\n",
  488. "tf.Tensor(5, shape=(), dtype=int64)\n",
  489. "tf.Tensor(4, shape=(), dtype=int64)\n",
  490. "tf.Tensor(4, shape=(), dtype=int64)\n",
  491. "tf.Tensor(5, shape=(), dtype=int64)\n",
  492. "tf.Tensor(5, shape=(), dtype=int64)\n"
  493. ]
  494. }
  495. ],
  496. "source": [
  497. "a = tf.data.Dataset.range(1,6)\n",
  498. "for i in a:\n",
  499. " print(i)\n",
  500. "# b=a.repeat(6)\n",
  501. "# for i in b:\n",
  502. "# print(i)\n",
  503. "print('-'*50)\n",
  504. "#cycle_length是使用了几个block以后,就要重复的 参数\n",
  505. "a1=a.interleave(lambda x: tf.data.Dataset.from_tensors(x).repeat(6),\n",
  506. " cycle_length=3, block_length=4)\n",
  507. "for i in a1:\n",
  508. " print(i)"
  509. ]
  510. },
  511. {
  512. "cell_type": "markdown",
  513. "metadata": {},
  514. "source": [
  515. "# 针对各种格式数据变为dataset"
  516. ]
  517. },
  518. {
  519. "cell_type": "code",
  520. "execution_count": 18,
  521. "metadata": {},
  522. "outputs": [
  523. {
  524. "name": "stdout",
  525. "output_type": "stream",
  526. "text": [
  527. "<TensorSliceDataset shapes: ((2,), ()), types: (tf.int64, tf.string)>\n",
  528. "tf.Tensor([1 2], shape=(2,), dtype=int64) tf.Tensor(b'cat', shape=(), dtype=string)\n",
  529. "tf.Tensor([3 4], shape=(2,), dtype=int64) tf.Tensor(b'dog', shape=(), dtype=string)\n",
  530. "tf.Tensor([5 6], shape=(2,), dtype=int64) tf.Tensor(b'fox', shape=(), dtype=string)\n"
  531. ]
  532. }
  533. ],
  534. "source": [
  535. "x = np.array([[1, 2], [3, 4], [5, 6]])\n",
  536. "y = np.array(['cat', 'dog', 'fox'])\n",
  537. "#输入的参数是元祖的情况下\n",
  538. "dataset3 = tf.data.Dataset.from_tensor_slices((x, y))\n",
  539. "print(dataset3)\n",
  540. "\n",
  541. "for item_x, item_y in dataset3:\n",
  542. " print(item_x, item_y)"
  543. ]
  544. },
  545. {
  546. "cell_type": "code",
  547. "execution_count": 19,
  548. "metadata": {},
  549. "outputs": [
  550. {
  551. "name": "stdout",
  552. "output_type": "stream",
  553. "text": [
  554. "<TensorSliceDataset shapes: {feature1: (2,), label: ()}, types: {feature1: tf.int64, label: tf.string}>\n",
  555. "tf.Tensor([1 2], shape=(2,), dtype=int64) tf.Tensor(b'cat', shape=(), dtype=string)\n",
  556. "tf.Tensor([3 4], shape=(2,), dtype=int64) tf.Tensor(b'dog', shape=(), dtype=string)\n",
  557. "tf.Tensor([5 6], shape=(2,), dtype=int64) tf.Tensor(b'fox', shape=(), dtype=string)\n"
  558. ]
  559. }
  560. ],
  561. "source": [
  562. "#输入的参数是字典的情况下\n",
  563. "dataset4 = tf.data.Dataset.from_tensor_slices({\"feature1\": x,\n",
  564. " \"label\": y})\n",
  565. "print(dataset4)\n",
  566. "for item in dataset4:\n",
  567. " print(item[\"feature1\"], item[\"label\"])"
  568. ]
  569. },
  570. {
  571. "cell_type": "code",
  572. "execution_count": 20,
  573. "metadata": {},
  574. "outputs": [
  575. {
  576. "name": "stdout",
  577. "output_type": "stream",
  578. "text": [
  579. "<RangeDataset shapes: (), types: tf.int64>\n",
  580. "<InterleaveDataset shapes: (), types: tf.int64>\n",
  581. "tf.Tensor(1, shape=(), dtype=int64)\n",
  582. "tf.Tensor(1, shape=(), dtype=int64)\n",
  583. "tf.Tensor(1, shape=(), dtype=int64)\n",
  584. "tf.Tensor(1, shape=(), dtype=int64)\n",
  585. "tf.Tensor(2, shape=(), dtype=int64)\n",
  586. "tf.Tensor(2, shape=(), dtype=int64)\n",
  587. "tf.Tensor(2, shape=(), dtype=int64)\n",
  588. "tf.Tensor(2, shape=(), dtype=int64)\n",
  589. "tf.Tensor(1, shape=(), dtype=int64)\n",
  590. "tf.Tensor(1, shape=(), dtype=int64)\n",
  591. "tf.Tensor(2, shape=(), dtype=int64)\n",
  592. "tf.Tensor(2, shape=(), dtype=int64)\n",
  593. "tf.Tensor(3, shape=(), dtype=int64)\n",
  594. "tf.Tensor(3, shape=(), dtype=int64)\n",
  595. "tf.Tensor(3, shape=(), dtype=int64)\n",
  596. "tf.Tensor(3, shape=(), dtype=int64)\n",
  597. "tf.Tensor(4, shape=(), dtype=int64)\n",
  598. "tf.Tensor(4, shape=(), dtype=int64)\n",
  599. "tf.Tensor(4, shape=(), dtype=int64)\n",
  600. "tf.Tensor(4, shape=(), dtype=int64)\n",
  601. "tf.Tensor(3, shape=(), dtype=int64)\n",
  602. "tf.Tensor(3, shape=(), dtype=int64)\n",
  603. "tf.Tensor(4, shape=(), dtype=int64)\n",
  604. "tf.Tensor(4, shape=(), dtype=int64)\n",
  605. "tf.Tensor(5, shape=(), dtype=int64)\n",
  606. "tf.Tensor(5, shape=(), dtype=int64)\n",
  607. "tf.Tensor(5, shape=(), dtype=int64)\n",
  608. "tf.Tensor(5, shape=(), dtype=int64)\n",
  609. "tf.Tensor(5, shape=(), dtype=int64)\n",
  610. "tf.Tensor(5, shape=(), dtype=int64)\n"
  611. ]
  612. }
  613. ],
  614. "source": [
  615. "a = tf.data.Dataset.range(1, 6) # ==> [ 1, 2, 3, 4, 5 ]\n",
  616. "print(a)\n",
  617. "# NOTE: New lines indicate \"block\" boundaries.\n",
  618. "dataset2=a.interleave(lambda x: tf.data.Dataset.from_tensors(x).repeat(6),\n",
  619. " cycle_length=2, block_length=4) # ==> [1, 1, 1, 1,\n",
  620. " # 2, 2, 2, 2,\n",
  621. " # 1, 1,\n",
  622. " # 2, 2,\n",
  623. " # 3, 3, 3, 3,\n",
  624. " # 4, 4, 4, 4,\n",
  625. " # 3, 3,\n",
  626. " # 4, 4,\n",
  627. " # 5, 5, 5, 5,\n",
  628. " # 5, 5]\n",
  629. "print(dataset2)\n",
  630. "for item in dataset2:\n",
  631. " print(item)"
  632. ]
  633. },
  634. {
  635. "cell_type": "code",
  636. "execution_count": null,
  637. "metadata": {},
  638. "outputs": [],
  639. "source": []
  640. }
  641. ],
  642. "metadata": {
  643. "kernelspec": {
  644. "display_name": "Python 3",
  645. "language": "python",
  646. "name": "python3"
  647. },
  648. "language_info": {
  649. "codemirror_mode": {
  650. "name": "ipython",
  651. "version": 3
  652. },
  653. "file_extension": ".py",
  654. "mimetype": "text/x-python",
  655. "name": "python",
  656. "nbconvert_exporter": "python",
  657. "pygments_lexer": "ipython3",
  658. "version": "3.6.9"
  659. }
  660. },
  661. "nbformat": 4,
  662. "nbformat_minor": 2
  663. }

随着人工智能和大数据的发展,任一方面对自动化工具有着一定的需求,在当下疫情防控期间,使用mindspore来实现yolo模型来进行目标检测及语义分割,对视频或图片都可以进行口罩佩戴检测和行人社交距离检测,来对公共场所的疫情防控来实行自动化管理。