tensorflow2使用中的一些问题
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了
tensorflow2使用中的一些问题的更多相关文章
- ORA-02020 : 过多的数据库链接在使用中-Windows环境解决步骤
一.现象 编译存储过程时报ORA-02020错误. 错误详细信息:ORA-04052在查找远程对象 xx@yy时出错 ORA-00604 : 递归 SQL 级别 1 出现错误 ORA-02020 : ...
- Subsonic使用中
使用中,遇到各种奇葩问题,依依汇总. 1.引用了Subsonic层后,一运行就开始报错,提示未能找到文件!! //引用后,目标框架可能会被改变,subsonic的默认框架是2.0,请检查框架是否 ...
- mysqldump 备份命令使用中的一些经验总结
mysqldump的一个小坑(自测) 正文:经常使用接触mysql复制功能的朋友应该对mysqldump命令不陌生吧,鄙人最近也在研究学习这一块的内容,经过几天的测试,发现mysqldump使用中容易 ...
- <总结>delphi WebBrowser控件的使用中出现的bug
Delphi WebBrowser控件的使用中出现的bug: 1.WebBrowser.Visible=false:Visible属性不能使WebBrowser控件不可见,暂时用 WebBrowse ...
- SSH框架使用中存在的诡异异常
背景 相信大多数人目前都在使用Spring + Struts2/SpringMVC + Hibernate来构建项目的整体架构,但是在使用中经藏会遇到一些诡异的问题,不知道如果解决,今天我遇到了一个非 ...
- VM出现该虚拟机正在使用中的提示,让获取所有权限解决办法
今天打开虚拟机正要学习,结果说是虚拟机似乎正在使用中,让我重新获取权限.解决办法:打开提示的配置文件的位置,将一个以.lck结尾的文件夹删除或者保存为另外的文件名称,再打开虚拟机就OK了.
- cordova + ionic 使用中碰到的一些问题
cordova + ionic 使用中碰到的一些问题 No Content-Security-Policy meta tag found. Please add one when using ...
- VMware“该虚拟机似乎正在使用中”问题
在用VMware虚拟机的时候,有时会发现打开虚拟机时提示“该虚拟机似乎正在使用中.如果该虚拟机未在使用,请按“获取所有权(T)”按钮获取它的所有权.否则,请按“取消(C)”按钮以防损坏.配置文件: D ...
- 项目使用中Linq使用总结
项目使用中Linq使用总结 本文旨在和网友分享Linq在项目中的实践,曾经我参与过的项目都能看见Linq的影子.(LinqTosql.LinqToString.LinqToXML.LinqToEnti ...
随机推荐
- B. The Number of Products(Codeforces Round #585 (Div. 2))
本题地址: https://codeforces.com/contest/1215/problem/B 本场比赛A题题解:https://www.cnblogs.com/liyexin/p/11535 ...
- HDU 1576:A/B
A/B Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- POJ 2996:Help Me with the Game
Help Me with the Game Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64 ...
- Struts1 的配置文件总结
一.在web.xml中安装Struts 要想使用Struts,我们接触到的第一个配置文件就是web.xml.实际上,Struts的入口点是一个名为ActionServlet的Servlet.在第一次访 ...
- Arduino - -- 串口双向通信
需要用到Arduino UNO的串口双向通信功能,以下源码: int val; void setup() { Serial.begin(9600); // opensserial port, se ...
- c++ STD Gems07
reverse.rotate.permutation #include <iostream> #include <vector> #include <string> ...
- ssh and scp从远程服务器下载文件
scp -r root@172.16.252.32:/home/files /home/files 下载目录 -r root是用户172.16.252.32是ip:/home/files 是你要 ...
- php序列化(serialize)和反序列化(unserialize)函数
用法:serialize().unserialize() 适用情境:serialize()返回字符串,此字符串包含了表示value的字节流,可以存储于任何地方.这有利于存储或传递 PHP 的值,同时不 ...
- 送票啦~ | 京东云邀您参加AI顶级盛会GTC CHINA 2019
本年度不可错过的AI顶级盛会 GTC CHINA2019 即将于12月16–19日在苏州举行 京东云重量级技术专家将携 AI前沿热议话题亮相 京东云相关AI最新动态,也会一并为您带上 小小剧透,快来看 ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring AOP(面向切面编程)
面向切面编程(AOP)和面向对象编程(OOP)类似,也是一种编程模式.Spring AOP 是基于 AOP 编程模式的一个框架,它的使用有效减少了系统间的重复代码,达到了模块间的松耦合目的. AOP ...