东方耀AI技术分享

标题: 08、TF2.0之实战tf.constant和tf.strings与ragged tensor [打印本页]

作者: 东方耀    时间: 2019-11-4 11:17
标题: 08、TF2.0之实战tf.constant和tf.strings与ragged tensor
08、TF2.0之实战tf.constant和tf.strings与ragged tensor


  1. # -*- coding: utf-8 -*-
  2. __author__ = u'东方耀 微信:dfy_88888'
  3. __date__ = '2019/10/30 20:54'
  4. __product__ = 'PyCharm'
  5. __filename__ = 'tf_base_api'

  6. import numpy as np
  7. import matplotlib as mpl
  8. import matplotlib.pyplot as plt
  9. import sklearn
  10. import pandas as pd
  11. import os
  12. import sys
  13. import time
  14. import tensorflow as tf
  15. from tensorflow import keras

  16. print(sys.version_info)
  17. for module in mpl, np, pd, sklearn, tf, keras:
  18.     print(module.__name__, module.__version__)

  19. # 实战tf.constant和tf.strings与ragged tensor

  20. t = tf.constant(value=np.arange(1, 7).reshape(2, 3), dtype=tf.float32)
  21. print(t)
  22. # index
  23. print(t[:, 1:])
  24. print(t[:, 1])
  25. print(t[..., 1])

  26. # op
  27. print(t+10)
  28. print(tf.square(t))
  29. print(tf.exp(t))
  30. print(tf.pow(t, 2))
  31. # t @ tf.transpose(t)  t.dot(t.T)
  32. print(t @ tf.transpose(t))
  33. print(tf.matmul(t, tf.transpose(t)))

  34. # tf直接转numpy对象
  35. print(t.numpy())
  36. print(np.square(t))

  37. # scalars 0维tensor 标量
  38. t0 = tf.constant(value=3.14)
  39. print(t0.numpy())
  40. print(type(t0))
  41. print(t0.shape)

  42. # strings
  43. str1 = tf.constant(value='dfy_88888')
  44. print(str1)
  45. print(type(str1))
  46. print(tf.strings.length(str1))
  47. print(tf.strings.length(str1, unit='BYTE'))
  48. print(tf.strings.length(str1, unit='UTF8_CHAR'))
  49. print(tf.strings.unicode_decode(str1, 'UTF8'))


  50. # strings array
  51. str2 = tf.constant(value=['caffe', 'cafe', 'dfy', '咖啡'])
  52. print(tf.strings.length(str2, unit='BYTE'))
  53. print(tf.strings.length(str2, unit='UTF8_CHAR'))
  54. # tf.RaggedTensor 不规则的数据 2.0新加的
  55. print(tf.strings.unicode_decode(str2, 'UTF8'))

  56. r1 = tf.ragged.constant([[1, 2], [3], [4, 5, 6]])
  57. # RaggedTensor
  58. print(r1)
  59. print(r1.shape)
  60. # Tensor
  61. print(r1[1])
  62. print(r1[1:3])

  63. # ops on ragged tensor:concat 拼接
  64. r2 = tf.ragged.constant([[51, 52], [], [71], [99, 80, 100]])
  65. r3 = tf.concat([r1, r2], axis=0)
  66. print(r3)
  67. print(r3.shape)

  68. # r4 = tf.concat([r1, r2], axis=1)
  69. r = tf.ragged.constant([[], [0], [100]])
  70. r4 = tf.concat([r1, r], axis=1)
  71. print(r4)
  72. print(r4.shape)

  73. # ragged_tensor 转换为 普通tensor 用0补齐
  74. print(r4.to_tensor())
复制代码

东方老师AI官网:http://www.ai111.vip
有任何问题可联系东方老师微信:dfy_88888
【微信二维码图片】
  1. sys.version_info(major=3, minor=6, micro=8, releaselevel='final', serial=0)
  2. matplotlib 3.1.1
  3. numpy 1.17.3
  4. pandas 0.25.2
  5. sklearn 0.21.3
  6. tensorflow 2.0.0
  7. tensorflow_core.keras 2.2.4-tf
  8. 2019-11-04 11:14:50.026692: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
  9. 2019-11-04 11:14:50.199491: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
  10. name: GeForce GTX 750 major: 5 minor: 0 memoryClockRate(GHz): 1.0845
  11. pciBusID: 0000:01:00.0
  12. 2019-11-04 11:14:50.199987: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
  13. 2019-11-04 11:14:50.203068: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
  14. 2019-11-04 11:14:50.203699: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
  15. 2019-11-04 11:14:50.207506: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
  16. name: GeForce GTX 750 major: 5 minor: 0 memoryClockRate(GHz): 1.0845
  17. pciBusID: 0000:01:00.0
  18. 2019-11-04 11:14:50.207999: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
  19. 2019-11-04 11:14:50.211351: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
  20. tf.Tensor(
  21. [[1. 2. 3.]
  22. [4. 5. 6.]], shape=(2, 3), dtype=float32)
  23. 2019-11-04 11:14:51.901068: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect StreamExecutor with strength 1 edge matrix:
  24. 2019-11-04 11:14:51.901409: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165]      0
  25. 2019-11-04 11:14:51.901622: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0:   N
  26. 2019-11-04 11:14:51.905666: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 508 MB memory) -> physical GPU (device: 0, name: GeForce GTX 750, pci bus id: 0000:01:00.0, compute capability: 5.0)
  27. tf.Tensor(
  28. [[2. 3.]
  29. [5. 6.]], shape=(2, 2), dtype=float32)
  30. tf.Tensor([2. 5.], shape=(2,), dtype=float32)
  31. tf.Tensor([2. 5.], shape=(2,), dtype=float32)
  32. tf.Tensor(
  33. [[11. 12. 13.]
  34. [14. 15. 16.]], shape=(2, 3), dtype=float32)
  35. tf.Tensor(
  36. [[ 1.  4.  9.]
  37. [16. 25. 36.]], shape=(2, 3), dtype=float32)
  38. tf.Tensor(
  39. [[  2.7182817   7.389056   20.085537 ]
  40. [ 54.59815   148.41316   403.4288   ]], shape=(2, 3), dtype=float32)
  41. tf.Tensor(
  42. [[ 1.        4.        9.      ]
  43. [16.       24.999998 36.      ]], shape=(2, 3), dtype=float32)
  44. 2019-11-04 11:14:51.978749: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_100.dll
  45. tf.Tensor(
  46. [[14. 32.]
  47. [32. 77.]], shape=(2, 2), dtype=float32)
  48. tf.Tensor(
  49. [[14. 32.]
  50. [32. 77.]], shape=(2, 2), dtype=float32)
  51. [[1. 2. 3.]
  52. [4. 5. 6.]]
  53. [[ 1.  4.  9.]
  54. [16. 25. 36.]]
  55. 3.14
  56. <class 'tensorflow.python.framework.ops.EagerTensor'>
  57. ()
  58. tf.Tensor(b'dfy_88888', shape=(), dtype=string)
  59. <class 'tensorflow.python.framework.ops.EagerTensor'>
  60. tf.Tensor(9, shape=(), dtype=int32)
  61. tf.Tensor(9, shape=(), dtype=int32)
  62. tf.Tensor(9, shape=(), dtype=int32)
  63. tf.Tensor([100 102 121  95  56  56  56  56  56], shape=(9,), dtype=int32)
  64. tf.Tensor([5 4 3 6], shape=(4,), dtype=int32)
  65. tf.Tensor([5 4 3 2], shape=(4,), dtype=int32)
  66. <tf.RaggedTensor [[99, 97, 102, 102, 101], [99, 97, 102, 101], [100, 102, 121], [21654, 21857]]>
  67. <tf.RaggedTensor [[1, 2], [3], [4, 5, 6]]>
  68. (3, None)
  69. tf.Tensor([3], shape=(1,), dtype=int32)
  70. <tf.RaggedTensor [[3], [4, 5, 6]]>
  71. <tf.RaggedTensor [[1, 2], [3], [4, 5, 6], [51, 52], [], [71], [99, 80, 100]]>
  72. (7, None)
  73. <tf.RaggedTensor [[1, 2], [3, 0], [4, 5, 6, 100]]>
  74. (3, None)
  75. tf.Tensor(
  76. [[  1   2   0   0]
  77. [  3   0   0   0]
  78. [  4   5   6 100]], shape=(3, 4), dtype=int32)

  79. Process finished with exit code 0
复制代码










欢迎光临 东方耀AI技术分享 (http://www.ai111.vip/) Powered by Discuz! X3.4