from tensorflow import keras
import tensorflow as tf
import numpy as np
print(tf.__name__,tf.__version__)
print(keras.__name__, keras.__version__)
tensorflow 2.0.0
tensorflow_core.keras 2.2.4-tf
# 定义常量
t = tf.constant([[1., 2., 3.], [4., 5., 6.]])
# 做索引操作
print(t)
# 取从第二例以后的数值
print(t[:, 1:])
# 只把第二列取出来,当做一个tensor
print(t[..., 1]) # 做算子操作
# 加
print(t+10)
# 平方
print(tf.square(t))
# 乘转置
print(t @ tf.transpose(t))
tf.Tensor(
[[1. 2. 3.]
[4. 5. 6.]], shape=(2, 3), dtype=float32)
tf.Tensor(
[[2. 3.]
[5. 6.]], shape=(2, 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(
[[14. 32.]
[32. 77.]], shape=(2, 2), dtype=float32)
# 进行numpy 操作
print(t.numpy())
print(np.square(t))
np_t = np.array([[1., 2., 3.], [4., 5., 6.]])
print(tf.constant(np_t))
[[1. 2. 3.]
[4. 5. 6.]]
[[ 1. 4. 9.]
[16. 25. 36.]]
tf.Tensor(
[[1. 2. 3.]
[4. 5. 6.]], shape=(2, 3), dtype=float64)
# 0维, 一个具体的数
t = tf.constant(3.1415)
print(t)
tf.Tensor(3.1415, shape=(), dtype=float32)
# string
t = tf.constant("tesorflow")
print(t)
print(tf.strings.length(t))
print(tf.strings.unicode_decode(t, "UTF8")) # string array
t = tf.constant(["tensorflow", "pytorch", "咖啡"])
print(tf.strings.length(t, unit="UTF8_CHAR"))
tf.Tensor(b'tesorflow', shape=(), dtype=string)
tf.Tensor(9, shape=(), dtype=int32)
tf.Tensor([116 101 115 111 114 102 108 111 119], shape=(9,), dtype=int32)
tf.Tensor([10 7 2], shape=(3,), dtype=int32)
# ragged tensor
r = tf.ragged.constant([[11, 12], [21, 22, 23], [], [41]])
print(r)
print(r[1:3])
print(r.to_tensor())
# 拼接操作
r2 = tf.ragged.constant([[51, 52], [], [72, 72]])
print(tf.concat([r, r2], axis = 0))
# axis=1 拼接时, 需要维数相同
r3 = tf.ragged.constant([[13,14], [15], [], [42, 43]])
print(tf.concat([r, r3], axis = 1))
<tf.RaggedTensor [[11, 12], [21, 22, 23], [], [41]]>
<tf.RaggedTensor [[21, 22, 23], []]>
tf.Tensor(
[[11 12 0]
[21 22 23]
[ 0 0 0]
[41 0 0]], shape=(4, 3), dtype=int32)
<tf.RaggedTensor [[11, 12], [21, 22, 23], [], [41], [51, 52], [], [72, 72]]>
<tf.RaggedTensor [[11, 12, 13, 14], [21, 22, 23, 15], [], [41, 42, 43]]>
# sparse tensor 大部分位置为0,少部分
s = tf.SparseTensor(indices = [[0, 1], [1, 0], [2, 3]],
values = [1., 2., 3.],
dense_shape = [3, 4])
print(s)
print(tf.sparse.to_dense(s)) # 操作
s2 = s * 2.0
# 没有+法操作 s3 = s + 1 s4 = tf.constant([[10., 20.],
[30., 40.],
[50., 60.],
[70., 80.]])
print(tf.sparse.sparse_dense_matmul(s, s4))
# 如果indices排序不对,使用tf.sparse.reorder进行排序
SparseTensor(indices=tf.Tensor(
[[0 1]
[1 0]
[2 3]], shape=(3, 2), dtype=int64), values=tf.Tensor([1. 2. 3.], shape=(3,), dtype=float32), dense_shape=tf.Tensor([3 4], shape=(2,), dtype=int64))
tf.Tensor(
[[0. 1. 0. 0.]
[2. 0. 0. 0.]
[0. 0. 0. 3.]], shape=(3, 4), dtype=float32)
tf.Tensor(
[[ 30. 40.]
[ 20. 40.]
[210. 240.]], shape=(3, 2), dtype=float32)
# 变量 variables
v = tf.Variable([[1., 2., 3.], [4., 5., 6.]])
print(v)
print(v.value())
print(v.numpy()) # 只能用assign 不能用=
v.assign(2 * v)
print(v.numpy())
v[0, 1].assign(42)
print(v.numpy())
v[1].assign([7., 8., 9.])
print(v.numpy())
<tf.Variable 'Variable:0' shape=(2, 3) dtype=float32, numpy=
array([[1., 2., 3.],
[4., 5., 6.]], dtype=float32)>
tf.Tensor(
[[1. 2. 3.]
[4. 5. 6.]], shape=(2, 3), dtype=float32)
[[1. 2. 3.]
[4. 5. 6.]]
[[ 2. 4. 6.]
[ 8. 10. 12.]]
[[ 2. 42. 6.]
[ 8. 10. 12.]]
[[ 2. 42. 6.]
[ 7. 8. 9.]]

多GPU

mirrored_strategy = tf.distribute.MirroredStrategy()
with mirrored_strategy.scope():
model = xxxnet()
model.compile(optimizer=tf.keras.optimizers.Adam(lr=1e-4), loss='mse', loss_weights=[1])
model.fit( train_generator, epochs=100, steps_per_epoch=5, verbose=1, callbacks = callbacks)
# 需要注意只能用fit,不能用fit_genertor,不过现在fit也支持generator了

最新文章

  1. Java实现时间动态显示方法汇总
  2. Android中 Bitmap和Drawable相互转换的方法
  3. spring相关jar包的含义
  4. SparkSQL读取Hive中的数据
  5. 介绍Unreal Engine 4中的接口(Interface)使用C++和蓝图
  6. Apache 的ab压力测试工具
  7. MXF素材文件交换格式深入研究
  8. zookeeper的安装和启动
  9. Fix java version mismatch in windows---stackoverflow
  10. Android Studio 2.2 External Build
  11. OpenCV 实现哈哈镜效果
  12. SQL优化 MySQL版 -分析explain SQL执行计划与笛卡尔积
  13. 26 , CSS 构造表单
  14. Spring Boot 集成 Spring Security 实现权限认证模块
  15. Android 实现卡片翻转的动画(翻牌动画)
  16. 理解DeepBox算法
  17. IIS预编译提升加载速度
  18. php 通过 strtr 方法来替换文本中指定的内容
  19. jmeter之数据库相关
  20. Altium Ddesigner 栅格 含义

热门文章

  1. 第一部分 JavaScript语言核心(三)
  2. 在Mac上如何运行jar文件
  3. MySLQ排序后标记排行
  4. 浅谈ASCII 、ISO8859-1、GB2312、GBK、Unicode、UTF-8 的区别。
  5. 科学 multi port
  6. 实验吧web-难-认真一点!(布尔盲注,py脚本)
  7. 吴裕雄--天生自然TensorFlow2教程:合并与分割
  8. 单变量线性回归(Linear Regression with One Variable)与代价函数
  9. c 转二进制
  10. Tensorflow——用openpose进行人体骨骼检测