运行环境
python环境
keras
No module named 'keras'
问题描述:
from tensorflow import keras
keras.__version__
Traceback (most recent call last):
......
ModuleNotFoundError: No module named 'keras'
解决方案:
虽然Tensorflow包中能找到keras的接口定义,但仍需要额外安装keras包
conda install keras
vscode中tf.keras没有智能提示
问题描述:

解决方案1:
import tensorflow.keras as keras 并用 keras 代替 tf.keras

把这几行代码添加到 tensorflow/__init__.py 文件末尾
# Explicitly import lazy-loaded modules to support autocompletion.
# pylint: disable=g-import-not-at-top
if _typing.TYPE_CHECKING:
from tensorflow_estimator.python.estimator.api._v2 import estimator as estimator
from keras.api._v2 import keras
from keras.api._v2.keras import losses
from keras.api._v2.keras import metrics
from keras.api._v2.keras import optimizers
from keras.api._v2.keras import initializers
# pylint: enable=g-import-not-at-top

编程提示
python编程
API
tf.linalg.band_part
tf.linalg.band_part(
input, # 输入Tensor
num_lower, # 主对角线往下扩展的宽度,为负数时表示取下三角矩阵
num_upper, # 主对角线往上扩展的宽度,为负数时表示取上三角矩阵
name=None
)
功能:相当于对矩阵加一个与主对角线平行的斜条状掩码,在取三角矩阵或限制搜索边界时很有用。
import tensorflow as tf
a=tf.linspace(1,16,16)
a=tf.reshape(a,[4,4])
b=tf.linalg.band_part(a,0,0)
c=tf.linalg.band_part(a,0,-1)
d=tf.linalg.band_part(a,-1,0)
e=tf.linalg.band_part(a,1,2)
print("原始矩阵")
print(a)
print("取主对角线")
print(b)
print("取上三角矩阵")
print(c)
print("取下三角矩阵")
print(d)
print("主对角线向两侧扩展,扩展宽度由num_lower和num_upper指定")
print(e)
执行结果
原始矩阵
tf.Tensor(
[[ 1. 2. 3. 4.]
[ 5. 6. 7. 8.]
[ 9. 10. 11. 12.]
[13. 14. 15. 16.]], shape=(4, 4), dtype=float64)
取主对角线
tf.Tensor(
[[ 1. 0. 0. 0.]
[ 0. 6. 0. 0.]
[ 0. 0. 11. 0.]
[ 0. 0. 0. 16.]], shape=(4, 4), dtype=float64)
取上三角矩阵
tf.Tensor(
[[ 1. 2. 3. 4.]
[ 0. 6. 7. 8.]
[ 0. 0. 11. 12.]
[ 0. 0. 0. 16.]], shape=(4, 4), dtype=float64)
取下三角矩阵
tf.Tensor(
[[ 1. 0. 0. 0.]
[ 5. 6. 0. 0.]
[ 9. 10. 11. 0.]
[13. 14. 15. 16.]], shape=(4, 4), dtype=float64)
主对角线向两侧扩展,扩展宽度由num_lower和num_upper指定
tf.Tensor(
[[ 1. 2. 3. 0.]
[ 5. 6. 7. 8.]
[ 0. 10. 11. 12.]
[ 0. 0. 15. 16.]], shape=(4, 4), dtype=float64)
tf.while_loop
tf.while_loop(
cond, # 返回boolean值的函数,返回False时循环结束
body, # 循环体函数
loop_vars, # cond和body中会用到的变量
shape_invariants=None, # loop_vars中各变量的shape,当有变量的shape不固定时(如NLP任务的句子长度),必须显示定义出来
parallel_iterations=10, # while_loop并不是严格的循环,它允许多次迭代并行执行,此参数限制并发量
back_prop=True, # 已废弃,建议用tf.stop_gradient
swap_memory=False, # 允许将推理过程中产生的tensors(会参与back propagation)从GPU转存到CPU以节省显存,在长序列和大batch训练中很有必要
maximum_iterations=None, # 循环体最大迭代次数,迭代次数达到maximum_iterations后,不管cond返回什么循环都会结束
name=None
)
示例代码
@tf.function
def forward_decoder(self, encoder_output):
batch = tf.shape(encoder_output)[0]
chunk_idx = tf.constant(0)
decoder_output = tf.zeros([batch, 0, self.feat_dim], dtype=tf.float32)
def cond(chunk_idx, decoder_output):
# 循环条件
def body(chunk_idx, decoder_output):
# 循环体
chunk_idx, decoder_output = tf.while_loop(cond, body,
[chunk_idx, decoder_output],
shape_invariants=[tf.TensorShape([]), tf.TensorShape([batch, None, self.feat_dim])])
报错信息
TypeError: Dimension value must be integer or None or have an __index__ method, got value '<tf.Tensor 'fast_speech2/strided_slice_1:0' shape=() dtype=int32>' with type '<class 'tensorflow.python.framework.ops.Tensor'>'
这个报错是说Dimension不是一个整数、None,或者有__index__()方法的对象。google一下多数结果都是说设shape的时候可能用了个float,但我反复查了并没有这种情况。奇怪的是我也没用到strided_slice操作。最后发现是因为设置shape_invariants时用了batch这个中间变量,虽然它是int32型,但在静态图构图时它其实是tf.shape这个op。所以不加@tf.function就不报错,加上了就会报错。把shape_invariants中的batch修改为None就好了。
shape_invariants=[tf.TensorShape([]), tf.TensorShape([None, None, self.feat_dim])])