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 ...
随机推荐
- NiFi之Processor配置
Processor(处理器)之配置 选择一个Processor,比如ExecuteSQL,从它的名字可以看出该处理器的功能就是去执行一个sql(当然是执行的该sql必须要有返回值的),下面就以Exec ...
- python刷LeetCode:28. 实现 strStr()
难度等级:简单 题目描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 ( ...
- hadoop的文件操作整理java
package dada; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; im ...
- UVA - 11181 Probability|Given (条件概率)
题意:有n个人,已知每个人买东西的概率,求在已知r个人买了东西的条件下每个人买东西的概率. 分析:二进制枚举个数为r的子集,按定义求即可. #include<cstdio> #includ ...
- UVA - 294 Divisors (约数)(数论)
题意:输入两个整数L,U(1<=L<=U<=109,U-L<=10000),统计区间[L,U]的整数中哪一个的正约数最多.如果有多个,输出最小值. 分析: 1.求一个数的约数, ...
- No enclosing instance of type test is accessible. Must qualify the allocation with an enclosing inst
今日遇到一个报错如下: No enclosing instance of type test is accessible. Must qualify the allocation with an en ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习:字符串
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Maven - Eclipse例子
版权所有,未经授权,禁止转载 章节 Maven – 简介 Maven – 工作原理 Maven – Repository(存储库) Maven – pom.xml 文件 Maven – 依赖管理 Ma ...
- impdp导入.dmp到oracle
1.创建表空间 create tablespace CCGRP_PRO --表空间名 datafile 'D:\oracleData\test.dbf' --物理文件 表空间数据文件存放路径size ...
- promise 核心 几个小问题
1.如何改变pending的壮体 抛出异常.pending变为rejected // throw new Error('fail') 内部抛出异常也这样 reason为抛出的error resol ...