|
08、TF2.0之实战tf.constant和tf.strings与ragged tensor
- # -*- coding: utf-8 -*-
- __author__ = u'东方耀 微信:dfy_88888'
- __date__ = '2019/10/30 20:54'
- __product__ = 'PyCharm'
- __filename__ = 'tf_base_api'
- import numpy as np
- import matplotlib as mpl
- import matplotlib.pyplot as plt
- import sklearn
- import pandas as pd
- import os
- import sys
- import time
- import tensorflow as tf
- from tensorflow import keras
- print(sys.version_info)
- for module in mpl, np, pd, sklearn, tf, keras:
- print(module.__name__, module.__version__)
- # 实战tf.constant和tf.strings与ragged tensor
- t = tf.constant(value=np.arange(1, 7).reshape(2, 3), dtype=tf.float32)
- print(t)
- # index
- print(t[:, 1:])
- print(t[:, 1])
- print(t[..., 1])
- # op
- print(t+10)
- print(tf.square(t))
- print(tf.exp(t))
- print(tf.pow(t, 2))
- # t @ tf.transpose(t) t.dot(t.T)
- print(t @ tf.transpose(t))
- print(tf.matmul(t, tf.transpose(t)))
- # tf直接转numpy对象
- print(t.numpy())
- print(np.square(t))
- # scalars 0维tensor 标量
- t0 = tf.constant(value=3.14)
- print(t0.numpy())
- print(type(t0))
- print(t0.shape)
- # strings
- str1 = tf.constant(value='dfy_88888')
- print(str1)
- print(type(str1))
- print(tf.strings.length(str1))
- print(tf.strings.length(str1, unit='BYTE'))
- print(tf.strings.length(str1, unit='UTF8_CHAR'))
- print(tf.strings.unicode_decode(str1, 'UTF8'))
- # strings array
- str2 = tf.constant(value=['caffe', 'cafe', 'dfy', '咖啡'])
- print(tf.strings.length(str2, unit='BYTE'))
- print(tf.strings.length(str2, unit='UTF8_CHAR'))
- # tf.RaggedTensor 不规则的数据 2.0新加的
- print(tf.strings.unicode_decode(str2, 'UTF8'))
- r1 = tf.ragged.constant([[1, 2], [3], [4, 5, 6]])
- # RaggedTensor
- print(r1)
- print(r1.shape)
- # Tensor
- print(r1[1])
- print(r1[1:3])
- # ops on ragged tensor:concat 拼接
- r2 = tf.ragged.constant([[51, 52], [], [71], [99, 80, 100]])
- r3 = tf.concat([r1, r2], axis=0)
- print(r3)
- print(r3.shape)
- # r4 = tf.concat([r1, r2], axis=1)
- r = tf.ragged.constant([[], [0], [100]])
- r4 = tf.concat([r1, r], axis=1)
- print(r4)
- print(r4.shape)
- # ragged_tensor 转换为 普通tensor 用0补齐
- print(r4.to_tensor())
复制代码
东方老师AI官网:http://www.ai111.vip
有任何问题可联系东方老师微信:dfy_88888
【微信二维码图片】
- sys.version_info(major=3, minor=6, micro=8, releaselevel='final', serial=0)
- matplotlib 3.1.1
- numpy 1.17.3
- pandas 0.25.2
- sklearn 0.21.3
- tensorflow 2.0.0
- tensorflow_core.keras 2.2.4-tf
- 2019-11-04 11:14:50.026692: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
- 2019-11-04 11:14:50.199491: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
- name: GeForce GTX 750 major: 5 minor: 0 memoryClockRate(GHz): 1.0845
- pciBusID: 0000:01:00.0
- 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.
- 2019-11-04 11:14:50.203068: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
- 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
- 2019-11-04 11:14:50.207506: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
- name: GeForce GTX 750 major: 5 minor: 0 memoryClockRate(GHz): 1.0845
- pciBusID: 0000:01:00.0
- 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.
- 2019-11-04 11:14:50.211351: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
- tf.Tensor(
- [[1. 2. 3.]
- [4. 5. 6.]], shape=(2, 3), dtype=float32)
- 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:
- 2019-11-04 11:14:51.901409: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165] 0
- 2019-11-04 11:14:51.901622: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0: N
- 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)
- tf.Tensor(
- [[2. 3.]
- [5. 6.]], shape=(2, 2), dtype=float32)
- tf.Tensor([2. 5.], shape=(2,), dtype=float32)
- tf.Tensor([2. 5.], shape=(2,), dtype=float32)
- tf.Tensor(
- [[11. 12. 13.]
- [14. 15. 16.]], shape=(2, 3), dtype=float32)
- tf.Tensor(
- [[ 1. 4. 9.]
- [16. 25. 36.]], shape=(2, 3), dtype=float32)
- tf.Tensor(
- [[ 2.7182817 7.389056 20.085537 ]
- [ 54.59815 148.41316 403.4288 ]], shape=(2, 3), dtype=float32)
- tf.Tensor(
- [[ 1. 4. 9. ]
- [16. 24.999998 36. ]], shape=(2, 3), dtype=float32)
- 2019-11-04 11:14:51.978749: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_100.dll
- tf.Tensor(
- [[14. 32.]
- [32. 77.]], shape=(2, 2), dtype=float32)
- tf.Tensor(
- [[14. 32.]
- [32. 77.]], shape=(2, 2), dtype=float32)
- [[1. 2. 3.]
- [4. 5. 6.]]
- [[ 1. 4. 9.]
- [16. 25. 36.]]
- 3.14
- <class 'tensorflow.python.framework.ops.EagerTensor'>
- ()
- tf.Tensor(b'dfy_88888', shape=(), dtype=string)
- <class 'tensorflow.python.framework.ops.EagerTensor'>
- tf.Tensor(9, shape=(), dtype=int32)
- tf.Tensor(9, shape=(), dtype=int32)
- tf.Tensor(9, shape=(), dtype=int32)
- tf.Tensor([100 102 121 95 56 56 56 56 56], shape=(9,), dtype=int32)
- tf.Tensor([5 4 3 6], shape=(4,), dtype=int32)
- tf.Tensor([5 4 3 2], shape=(4,), dtype=int32)
- <tf.RaggedTensor [[99, 97, 102, 102, 101], [99, 97, 102, 101], [100, 102, 121], [21654, 21857]]>
- <tf.RaggedTensor [[1, 2], [3], [4, 5, 6]]>
- (3, None)
- tf.Tensor([3], shape=(1,), dtype=int32)
- <tf.RaggedTensor [[3], [4, 5, 6]]>
- <tf.RaggedTensor [[1, 2], [3], [4, 5, 6], [51, 52], [], [71], [99, 80, 100]]>
- (7, None)
- <tf.RaggedTensor [[1, 2], [3, 0], [4, 5, 6, 100]]>
- (3, None)
- tf.Tensor(
- [[ 1 2 0 0]
- [ 3 0 0 0]
- [ 4 5 6 100]], shape=(3, 4), dtype=int32)
- Process finished with exit code 0
复制代码
|
|