TF2:model.predict 在 Colab 中返回字典但在本地测试中不返回
TF2: model.predict returning dictionary in Colab but not in local test
我想获得模型中某些层的激活值。我创建了一个格式为
的词典
layer_act_op = {layer.name: layer.output for layer in model.layers}
然后创建了一个激活模型
act_model = tf.keras.models.Model(inputs=model.input, outputs=layer_act_op)
现在,当我 运行
activation = act_model.predict(data)
- 在 Colab 中(使用 Google Compute Engine 后端),
activation
是一个字典,其键对应于 layer_act_op
中的键
和激活值
- 在我的本地 PC 中,当我在一个函数中 运行 this 时,
activation
是一个长度等于 layer_act_op
中条目数的列表
在这两种情况下,tf.eagerly_running()
return True
。
我不明白为什么会出现这种不同的行为。我怎样才能在本地 PC 上使用 model.predict
并获得带有激活的字典?
TF版本:2.2.0rc4
感谢您提出有趣的问题。我已经在 colab 和本地 PC (MacOS 10.15) 上对其进行了测试。并找出 tf2.1(稳定)和 tf2.2(不稳定)之间的输出不同。也许您可以在本地 PC 上检查您的 tf 版本,卸载 tf2.1,然后使用 pip3 install tf-nightly
.
安装 nightly 版本
我用下面的脚本测试:
import tensorflow as tf
from tensorflow.keras import Sequential, layers
import numpy as np
print(tf.__version__)
model = Sequential()
model.add(layers.Input(shape=(224, 224, 3)))
model.add(layers.Conv2D(filters=64, kernel_size=5, activation="relu", name='conv1'))
model.add(layers.ReLU(name='relu'))
layer_act_op = {layer.name: layer.output for layer in model.layers}
act_model = tf.keras.models.Model(inputs=model.input, outputs=layer_act_op)
data = np.arange(224*224*3*2).reshape(2, 224, 224, 3)
_ = act_model.predict(data)
print(type(_))
print(_.keys())
输出:
2.2.0-dev20200429
2020-05-04 09:37:04.953746: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with Intel(R) M
KL-DNN to use the following CPU instructions in performance-critical operations: AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2020-05-04 09:37:04.977887: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fd8c0626f20 initialized for platform Ho
st (this does not guarantee that XLA will be used). Devices:
2020-05-04 09:37:04.977916: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
<class 'dict'>
dict_keys(['conv1', 'relu'])
我想获得模型中某些层的激活值。我创建了一个格式为
的词典layer_act_op = {layer.name: layer.output for layer in model.layers}
然后创建了一个激活模型
act_model = tf.keras.models.Model(inputs=model.input, outputs=layer_act_op)
现在,当我 运行
activation = act_model.predict(data)
- 在 Colab 中(使用 Google Compute Engine 后端),
activation
是一个字典,其键对应于layer_act_op
中的键 和激活值 - 在我的本地 PC 中,当我在一个函数中 运行 this 时,
activation
是一个长度等于layer_act_op
中条目数的列表
在这两种情况下,tf.eagerly_running()
return True
。
我不明白为什么会出现这种不同的行为。我怎样才能在本地 PC 上使用 model.predict
并获得带有激活的字典?
TF版本:2.2.0rc4
感谢您提出有趣的问题。我已经在 colab 和本地 PC (MacOS 10.15) 上对其进行了测试。并找出 tf2.1(稳定)和 tf2.2(不稳定)之间的输出不同。也许您可以在本地 PC 上检查您的 tf 版本,卸载 tf2.1,然后使用 pip3 install tf-nightly
.
我用下面的脚本测试:
import tensorflow as tf
from tensorflow.keras import Sequential, layers
import numpy as np
print(tf.__version__)
model = Sequential()
model.add(layers.Input(shape=(224, 224, 3)))
model.add(layers.Conv2D(filters=64, kernel_size=5, activation="relu", name='conv1'))
model.add(layers.ReLU(name='relu'))
layer_act_op = {layer.name: layer.output for layer in model.layers}
act_model = tf.keras.models.Model(inputs=model.input, outputs=layer_act_op)
data = np.arange(224*224*3*2).reshape(2, 224, 224, 3)
_ = act_model.predict(data)
print(type(_))
print(_.keys())
输出:
2.2.0-dev20200429
2020-05-04 09:37:04.953746: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with Intel(R) M
KL-DNN to use the following CPU instructions in performance-critical operations: AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2020-05-04 09:37:04.977887: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fd8c0626f20 initialized for platform Ho
st (this does not guarantee that XLA will be used). Devices:
2020-05-04 09:37:04.977916: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
<class 'dict'>
dict_keys(['conv1', 'relu'])